| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Http\Controllers\Controller; |
| 6 |
use FluentCommunity\App\Models\Feed; |
| 7 |
use FluentCommunity\App\Models\Notification; |
| 8 |
use FluentCommunity\App\Models\NotificationSubscriber; |
| 9 |
use FluentCommunity\App\Services\ProfileHelper; |
| 10 |
use FluentCommunity\Framework\Http\Request\Request; |
| 11 |
|
| 12 |
class NotificationsController extends Controller |
| 13 |
{ |
| 14 |
public function getNotifications(Request $request) |
| 15 |
{ |
| 16 |
$user = $this->getUser(true); |
| 17 |
|
| 18 |
$notifications = Notification::whereHas('subscribers', function ($query) use ($user) { |
| 19 |
return $query->where('user_id', $user->ID); |
| 20 |
}) |
| 21 |
->with([ |
| 22 |
'xprofile' => function ($q) { |
| 23 |
return $q->select(ProfileHelper::getXProfilePublicFields()); |
| 24 |
}, |
| 25 |
'subscriber' => function ($q) { |
| 26 |
return $q->where('user_id', get_current_user_id()); |
| 27 |
} |
| 28 |
]) |
| 29 |
->byStatus($request->get('status'), $user->ID) |
| 30 |
->byType($request->get('notification_type', 'all')) |
| 31 |
->orderBy('updated_at', 'DESC') |
| 32 |
->paginate(); |
| 33 |
|
| 34 |
$data = [ |
| 35 |
'notifications' => $notifications, |
| 36 |
'unread_count' => Notification::byStatus('unread', $user->ID)->count() |
| 37 |
]; |
| 38 |
return apply_filters('fluent_community/notifications_api_response', $data, $request->all()); |
| 39 |
} |
| 40 |
|
| 41 |
public function getUnreadNotifications(Request $request) |
| 42 |
{ |
| 43 |
$user = $this->getUser(true); |
| 44 |
|
| 45 |
$unreadNotifications = Notification::byStatus('unread', $user->ID) |
| 46 |
->with(['xprofile' => function ($q) { |
| 47 |
return $q->select(ProfileHelper::getXProfilePublicFields()); |
| 48 |
}]) |
| 49 |
->orderBy('updated_at', 'DESC') |
| 50 |
->byType($request->get('notification_type', 'all')) |
| 51 |
->limit(50) |
| 52 |
->get(); |
| 53 |
|
| 54 |
$data = [ |
| 55 |
'notifications' => $unreadNotifications, |
| 56 |
'unread_count' => Notification::byStatus('unread', get_current_user_id())->count() |
| 57 |
]; |
| 58 |
|
| 59 |
return apply_filters('fluent_community/unread_notifications_api_response', $data, $request->all()); |
| 60 |
} |
| 61 |
|
| 62 |
public function markAllRead(Request $request) |
| 63 |
{ |
| 64 |
NotificationSubscriber::where('is_read', 0) |
| 65 |
->where('user_id', get_current_user_id()) |
| 66 |
->update(['is_read' => 1]); |
| 67 |
|
| 68 |
return [ |
| 69 |
'message' => __('All notifications have been marked as read.', 'fluent-community') |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
public function markAsRead(Request $request, $notification_id) |
| 74 |
{ |
| 75 |
$notification = Notification::findOrFail($notification_id); |
| 76 |
|
| 77 |
NotificationSubscriber::whereHas('notification', function ($query) use ($notification) { |
| 78 |
$query->where('id', $notification->id); |
| 79 |
if ($notification->feed_id) { |
| 80 |
$query->orWhere('feed_id', $notification->feed_id); |
| 81 |
} |
| 82 |
}) |
| 83 |
->where('user_id', get_current_user_id()) |
| 84 |
->update(['is_read' => 1]); |
| 85 |
|
| 86 |
return [ |
| 87 |
'unread_count' => Notification::byStatus('unread', get_current_user_id())->count() |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
public function markAsReadByFeedId(Request $request, $feedId) |
| 92 |
{ |
| 93 |
$feed = Feed::withoutGlobalScopes()->findOrFail($feedId); |
| 94 |
|
| 95 |
NotificationSubscriber::whereHas('notification', function ($query) use ($feed) { |
| 96 |
return $query->where('feed_id', $feed->id); |
| 97 |
}) |
| 98 |
->where('user_id', get_current_user_id()) |
| 99 |
->update(['is_read' => 1]); |
| 100 |
|
| 101 |
$userModel = $this->getUser(); |
| 102 |
|
| 103 |
return [ |
| 104 |
'unread_notification_count' => $userModel->getUnreadNotificationCount(), |
| 105 |
'unread_feed_ids' => $userModel->getUnreadNotificationFeedIds(), |
| 106 |
]; |
| 107 |
} |
| 108 |
} |
| 109 |
|