-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathChatChannelController.php
More file actions
139 lines (119 loc) · 4.93 KB
/
Copy pathChatChannelController.php
File metadata and controls
139 lines (119 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Fleetbase\Http\Controllers\Internal\v1;
use Fleetbase\Http\Controllers\FleetbaseController;
use Fleetbase\Http\Resources\User as UserResource;
use Fleetbase\Models\ChatChannel;
use Fleetbase\Models\ChatParticipant;
use Fleetbase\Models\User;
use Illuminate\Http\Request;
class ChatChannelController extends FleetbaseController
{
/**
* The resource to query.
*
* @var string
*/
public $resource = 'chat_channel';
/**
* Creates a chat channel and optional initial participants.
*
* @return \Fleetbase\Http\Resources\ChatChannel|\Illuminate\Http\Response
*/
public function createRecord(Request $request)
{
try {
$name = $request->input('chatChannel.name');
$meta = $request->input('chatChannel.meta', []);
$participants = $request->array('chatChannel.participants');
$chatChannel = ChatChannel::create([
'company_uuid' => session('company'),
'created_by_uuid' => session('user'),
'name' => $name,
'meta' => $meta,
]);
foreach ($participants as $userId) {
$user = User::where('uuid', $userId)->orWhere('public_id', $userId)->first();
if (!$user || $user->uuid === session('user')) {
continue;
}
ChatParticipant::firstOrCreate([
'company_uuid' => session('company'),
'user_uuid' => $user->uuid,
'chat_channel_uuid' => $chatChannel->uuid,
]);
}
$chatChannel->refresh();
$chatChannel->load(['participants.user', 'lastMessage']);
$this->resource::wrap($this->resourceSingularlName);
return new $this->resource($chatChannel);
} catch (\Exception $e) {
return response()->error(app()->hasDebugModeEnabled() ? $e->getMessage() : 'Unable to create chat channel.');
}
}
/**
* Query users available for a new or existing chat channel.
*
* @return \Fleetbase\Http\Resources\UserCollection
*/
public function getAvailableParticipants(Request $request)
{
$query = $request->input('query');
$chatChannelId = $request->input('channel');
$chatChannel = $chatChannelId ? ChatChannel::where('uuid', $chatChannelId)->orWhere('public_id', $chatChannelId)->first() : null;
$users = User::whereHas('companyUsers', function ($query) {
$query->where('company_uuid', session('company'));
})
->where('uuid', '!=', session('user'))
->when($query, function ($builder) use ($query) {
$builder->search($query);
});
if ($chatChannel) {
$participantUserUuids = $chatChannel->participants()->pluck('user_uuid');
$users->whereNotIn('uuid', $participantUserUuids);
}
return UserResource::collection($users->limit(25)->get());
}
/**
* Retrieves the unread message count for a specific chat channel.
*
* This method fetches a chat channel by its UUID and calculates the unread messages
* for the authenticated user. It returns a JSON response with the count or an error
* message if the chat channel is not found.
*
* @param string $channelId the UUID of the chat channel
* @param Request $request the incoming request instance, containing the user information
*
* @return \Illuminate\Http\JsonResponse
*/
public function getUnreadCountForChannel(string $channelId, Request $request)
{
$chatChannel = ChatChannel::where('uuid', $channelId)->first();
if (!$chatChannel) {
return response()->json(['error' => 'Chat channel not found.'], 404);
}
$unreadCount = $chatChannel->getUnreadMessageCountForUser($request->user());
return response()->json(['unreadCount' => $unreadCount]);
}
/**
* Retrieves the total unread message count across all chat channels for the current user.
*
* This method aggregates the unread messages for all channels in which the current user is a participant.
* It returns the total unread count as a JSON response.
*
* @param Request $request the incoming request instance
*
* @return \Illuminate\Http\JsonResponse
*/
public function getUnreadCount(Request $request)
{
$unreadCount = 0;
$userUuid = $request->user()->uuid;
$chatChannels = ChatChannel::whereHas('participants', function ($query) use ($userUuid) {
$query->where('user_uuid', $userUuid);
})->get();
foreach ($chatChannels as $chatChannel) {
$unreadCount += $chatChannel->getUnreadMessageCountForUser($request->user());
}
return response()->json(['unreadCount' => $unreadCount]);
}
}