| 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 |
$notifcations = 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 |
return [ |
| 35 |
'notifications' => $notifcations |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
public function getUnreadNotifications(Request $request) |
| 40 |
{ |
| 41 |
$user = $this->getUser(true); |
| 42 |
|
| 43 |
$unreadNotifications = Notification::byStatus('unread', $user->ID) |
| 44 |
->with(['xprofile' => function ($q) { |
| 45 |
return $q->select(ProfileHelper::getXProfilePublicFields()); |
| 46 |
}]) |
| 47 |
->orderBy('updated_at', 'DESC') |
| 48 |
->byType($request->get('notification_type', 'all')) |
| 49 |
->limit(50) |
| 50 |
->get(); |
| 51 |
|
| 52 |
return [ |
| 53 |
'notifications' => $unreadNotifications, |
| 54 |
'unread_count' => Notification::byStatus('unread', get_current_user_id())->count() |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
public function markAllRead(Request $request) |
| 59 |
{ |
| 60 |
NotificationSubscriber::where('is_read', 0) |
| 61 |
->where('user_id', get_current_user_id()) |
| 62 |
->update(['is_read' => 1]); |
| 63 |
|
| 64 |
return [ |
| 65 |
'message' => __('All notifications have been marked as read.', 'fluent-community') |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
public function markAsRead(Request $request, $notification_id) |
| 70 |
{ |
| 71 |
$notification = Notification::find($notification_id); |
| 72 |
|
| 73 |
NotificationSubscriber::whereHas('notification', function ($query) use ($notification) { |
| 74 |
return $query->where('id', $notification->id) |
| 75 |
->orWhere('feed_id', $notification->feed_id); |
| 76 |
}) |
| 77 |
->where('user_id', get_current_user_id()) |
| 78 |
->update(['is_read' => 1]); |
| 79 |
|
| 80 |
return [ |
| 81 |
'unread_count' => Notification::byStatus('unread', get_current_user_id())->count() |
| 82 |
]; |
| 83 |
|
| 84 |
return $this->getUnreadNotifications($request); |
| 85 |
} |
| 86 |
|
| 87 |
public function markAsReadByFeedId(Request $request, $feedId) |
| 88 |
{ |
| 89 |
$feed = Feed::findOrfail($feedId); |
| 90 |
|
| 91 |
NotificationSubscriber::whereHas('notification', function ($query) use ($feed) { |
| 92 |
return $query->where('feed_id', $feed->id); |
| 93 |
}) |
| 94 |
->where('user_id', get_current_user_id()) |
| 95 |
->update(['is_read' => 1]); |
| 96 |
|
| 97 |
$userModel = $this->getUser(); |
| 98 |
|
| 99 |
return [ |
| 100 |
'unread_notification_count' => $userModel->getUnreadNotificationCount(), |
| 101 |
'unread_feed_ids' => $userModel->getUnreadNotificationFeedIds(), |
| 102 |
]; |
| 103 |
} |
| 104 |
} |
| 105 |
|