@@ -96,6 +96,7 @@ public static function getAllLatest()
96
96
* Returns an array of user ids that are associated with the thread.
97
97
*
98
98
* @param null $userId
99
+ *
99
100
* @return array
100
101
*/
101
102
public function participantsUserIds ($ userId = null )
@@ -114,13 +115,15 @@ public function participantsUserIds($userId = null)
114
115
*
115
116
* @param $query
116
117
* @param $userId
118
+ *
117
119
* @return mixed
118
120
*/
119
121
public function scopeForUser ($ query , $ userId )
120
122
{
121
123
$ participantsTable = Models::table ('participants ' );
122
124
$ threadsTable = Models::table ('threads ' );
123
125
126
+
124
127
return $ query ->join ($ participantsTable , $ this ->getQualifiedKeyName (), '= ' , $ participantsTable . '.thread_id ' )
125
128
->where ($ participantsTable . '.user_id ' , $ userId )
126
129
->where ($ participantsTable . '.deleted_at ' , null )
@@ -132,6 +135,7 @@ public function scopeForUser($query, $userId)
132
135
*
133
136
* @param $query
134
137
* @param $userId
138
+ *
135
139
* @return mixed
136
140
*/
137
141
public function scopeForUserWithNewMessages ($ query , $ userId )
@@ -154,22 +158,22 @@ public function scopeForUserWithNewMessages($query, $userId)
154
158
*
155
159
* @param $query
156
160
* @param $participants
161
+ *
157
162
* @return mixed
158
163
*/
159
164
public function scopeBetween ($ query , array $ participants )
160
165
{
161
166
$ query ->whereHas ('participants ' , function ($ query ) use ($ participants ) {
162
167
$ query ->whereIn ('user_id ' , $ participants )
163
168
->groupBy ('thread_id ' )
164
- ->havingRaw ('COUNT(thread_id)= ' . count ($ participants ));
169
+ ->havingRaw ('COUNT(thread_id)= ' . count ($ participants ));
165
170
});
166
171
}
167
172
168
173
/**
169
174
* Adds users to this thread.
170
175
*
171
176
* @param array $participants list of all participants
172
- * @return void
173
177
*/
174
178
public function addParticipants (array $ participants )
175
179
{
@@ -186,13 +190,13 @@ public function addParticipants(array $participants)
186
190
/**
187
191
* Mark a thread as read for a user.
188
192
*
189
- * @param integer $userId
193
+ * @param int $userId
190
194
*/
191
195
public function markAsRead ($ userId )
192
196
{
193
197
try {
194
198
$ participant = $ this ->getParticipantFromUser ($ userId );
195
- $ participant ->last_read = new Carbon ;
199
+ $ participant ->last_read = new Carbon () ;
196
200
$ participant ->save ();
197
201
} catch (ModelNotFoundException $ e ) {
198
202
// do nothing
@@ -202,7 +206,8 @@ public function markAsRead($userId)
202
206
/**
203
207
* See if the current thread is unread by the user.
204
208
*
205
- * @param integer $userId
209
+ * @param int $userId
210
+ *
206
211
* @return bool
207
212
*/
208
213
public function isUnread ($ userId )
@@ -223,7 +228,9 @@ public function isUnread($userId)
223
228
* Finds the participant record from a user id.
224
229
*
225
230
* @param $userId
231
+ *
226
232
* @return mixed
233
+ *
227
234
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
228
235
*/
229
236
public function getParticipantFromUser ($ userId )
@@ -245,8 +252,9 @@ public function activateAllParticipants()
245
252
/**
246
253
* Generates a string of participant information.
247
254
*
248
- * @param null $userId
255
+ * @param null $userId
249
256
* @param array $columns
257
+ *
250
258
* @return string
251
259
*/
252
260
public function participantsString ($ userId = null , $ columns = ['name ' ])
@@ -262,10 +270,10 @@ public function participantsString($userId = null, $columns = ['name'])
262
270
->select ($ this ->getConnection ()->raw ($ selectString ));
263
271
264
272
if ($ userId !== null ) {
265
- $ participantNames ->where ($ usersTable . '.id ' , '!= ' , $ userId );
273
+ $ participantNames ->where ($ usersTable. '.id ' , '!= ' , $ userId );
266
274
}
267
275
268
- $ userNames = $ participantNames ->lists ($ usersTable . '.name ' );
276
+ $ userNames = $ participantNames ->lists ($ usersTable. '.name ' );
269
277
270
278
return implode (', ' , $ userNames );
271
279
}
@@ -274,6 +282,7 @@ public function participantsString($userId = null, $columns = ['name'])
274
282
* Checks to see if a user is a current participant of the thread.
275
283
*
276
284
* @param $userId
285
+ *
277
286
* @return bool
278
287
*/
279
288
public function hasParticipant ($ userId )
@@ -290,6 +299,7 @@ public function hasParticipant($userId)
290
299
* Generates a select string used in participantsString().
291
300
*
292
301
* @param $columns
302
+ *
293
303
* @return string
294
304
*/
295
305
protected function createSelectString ($ columns )
@@ -301,18 +311,105 @@ protected function createSelectString($columns)
301
311
switch ($ dbDriver ) {
302
312
case 'pgsql ' :
303
313
case 'sqlite ' :
304
- $ columnString = implode (" || ' ' || " . $ tablePrefix . $ usersTable . '. ' , $ columns );
305
- $ selectString = '( ' . $ tablePrefix . $ usersTable . '. ' . $ columnString . ') as name ' ;
314
+ $ columnString = implode (" || ' ' || " . $ tablePrefix. $ usersTable. '. ' , $ columns );
315
+ $ selectString = '( ' . $ tablePrefix. $ usersTable. '. ' . $ columnString. ') as name ' ;
306
316
break ;
307
317
case 'sqlsrv ' :
308
- $ columnString = implode (" + ' ' + " . $ tablePrefix . $ usersTable . '. ' , $ columns );
309
- $ selectString = '( ' . $ tablePrefix . $ usersTable . '. ' . $ columnString . ') as name ' ;
318
+ $ columnString = implode (" + ' ' + " . $ tablePrefix. $ usersTable. '. ' , $ columns );
319
+ $ selectString = '( ' . $ tablePrefix. $ usersTable. '. ' . $ columnString. ') as name ' ;
310
320
break ;
311
321
default :
312
- $ columnString = implode (", ' ', " . $ tablePrefix . $ usersTable . '. ' , $ columns );
313
- $ selectString = 'concat( ' . $ tablePrefix . $ usersTable . '. ' . $ columnString . ') as name ' ;
322
+ $ columnString = implode (", ' ', " . $ tablePrefix. $ usersTable. '. ' , $ columns );
323
+ $ selectString = 'concat( ' . $ tablePrefix. $ usersTable. '. ' . $ columnString. ') as name ' ;
314
324
}
315
325
316
326
return $ selectString ;
317
327
}
328
+ /**
329
+ * Returns array of unread messages in thread for given user.
330
+ *
331
+ * @param $user_id
332
+ *
333
+ * @return array
334
+ */
335
+ public function userUnreadMessages ($ user_id )
336
+ {
337
+ $ messages = $ this ->messages ()->get ();
338
+ $ participant = $ this ->getParticipantFromUser ($ user_id );
339
+ if (!$ participant ) {
340
+ return [];
341
+ }
342
+ $ unread = array ();
343
+ $ i = count ($ messages ) - 1 ;
344
+ while ($ i ) {
345
+ if ($ messages [$ i ]->updated_at ->gt ($ participant ->last_read )) {
346
+ array_push ($ unread , $ messages [$ i ]);
347
+ } else {
348
+ break ;
349
+ }
350
+ --$ i ;
351
+ }
352
+
353
+ return $ unread ;
354
+ }
355
+
356
+ /**
357
+ * Returns count of unread messages in thread for given user.
358
+ *
359
+ * @param $user_id
360
+ *
361
+ * @return int
362
+ */
363
+ public function userUnreadMessagesCount ($ user_id )
364
+ {
365
+ $ messages = $ this ->messages ()->get ();
366
+ $ participant = $ this ->getParticipantFromUser ($ user_id );
367
+ if (!$ participant ) {
368
+ return 0 ;
369
+ }
370
+ $ count = 0 ;
371
+ $ i = count ($ messages ) - 1 ;
372
+ while ($ i ) {
373
+ if ($ messages [$ i ]->updated_at ->gt ($ participant ->last_read )) {
374
+ ++$ count ;
375
+ } else {
376
+ break ;
377
+ }
378
+ --$ i ;
379
+ }
380
+
381
+ return $ count ;
382
+ }
383
+
384
+ /**
385
+ * Returns the "participant" table name to use in manual queries.
386
+ *
387
+ * @return string
388
+ */
389
+ private function getParticipantTable ()
390
+ {
391
+ if ($ this ->participantTable !== null ) {
392
+ return $ this ->participantTable ;
393
+ }
394
+
395
+ $ participantModel = Config::get ('messenger.participant_model ' );
396
+
397
+ return $ this ->participantTable = (new $ participantModel ())->getTable ();
398
+ }
399
+
400
+ /**
401
+ * Returns the "users" table name to use in manual queries.
402
+ *
403
+ * @return string
404
+ */
405
+ private function getUsersTable ()
406
+ {
407
+ if ($ this ->usersTable !== null ) {
408
+ return $ this ->usersTable ;
409
+ }
410
+
411
+ $ userModel = Config::get ('messenger.user_model ' );
412
+
413
+ return $ this ->usersTable = (new $ userModel ())->getTable ();
414
+ }
318
415
}
0 commit comments