1 | <?php namespace Arcanedev\LaravelMessenger\Models; |
||
33 | class Discussion extends Model implements DiscussionContract |
||
34 | { |
||
35 | /* ------------------------------------------------------------------------------------------------ |
||
36 | | Traits |
||
37 | | ------------------------------------------------------------------------------------------------ |
||
38 | */ |
||
39 | use SoftDeletes; |
||
40 | |||
41 | /* ------------------------------------------------------------------------------------------------ |
||
42 | | Properties |
||
43 | | ------------------------------------------------------------------------------------------------ |
||
44 | */ |
||
45 | /** |
||
46 | * The attributes that can be set with Mass Assignment. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $fillable = ['subject']; |
||
51 | |||
52 | /** |
||
53 | * The attributes that should be mutated to dates. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $dates = ['deleted_at']; |
||
58 | |||
59 | /* ------------------------------------------------------------------------------------------------ |
||
60 | | Constructor |
||
61 | | ------------------------------------------------------------------------------------------------ |
||
62 | */ |
||
63 | /** |
||
64 | * Create a new Eloquent model instance. |
||
65 | * |
||
66 | * @param array $attributes |
||
67 | */ |
||
68 | public function __construct(array $attributes = []) |
||
76 | |||
77 | /* ------------------------------------------------------------------------------------------------ |
||
78 | | Relationships |
||
79 | | ------------------------------------------------------------------------------------------------ |
||
80 | */ |
||
81 | /** |
||
82 | * Participants relationship. |
||
83 | * |
||
84 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
85 | */ |
||
86 | public function participants() |
||
92 | |||
93 | /** |
||
94 | * Messages relationship. |
||
95 | * |
||
96 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
97 | */ |
||
98 | public function messages() |
||
104 | |||
105 | /** |
||
106 | * Get the user that created the first message. |
||
107 | * |
||
108 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
109 | */ |
||
110 | public function creator() |
||
114 | |||
115 | /* ------------------------------------------------------------------------------------------------ |
||
116 | | Scopes |
||
117 | | ------------------------------------------------------------------------------------------------ |
||
118 | */ |
||
119 | /** |
||
120 | * Scope discussions that the user is associated with. |
||
121 | * |
||
122 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
123 | * @param int $userId |
||
124 | * |
||
125 | * @return \Illuminate\Database\Eloquent\Builder |
||
126 | */ |
||
127 | public function scopeForUser(Builder $query, $userId) |
||
138 | |||
139 | /** |
||
140 | * Scope discussions with new messages that the user is associated with. |
||
141 | * |
||
142 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
143 | * @param int $userId |
||
144 | * |
||
145 | * @return \Illuminate\Database\Eloquent\Builder |
||
146 | */ |
||
147 | public function scopeForUserWithNewMessages(Builder $query, $userId) |
||
161 | |||
162 | /** |
||
163 | * Scope discussions between given user ids. |
||
164 | * |
||
165 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
166 | * @param array $userIds |
||
167 | * |
||
168 | * @return \Illuminate\Database\Eloquent\Builder |
||
169 | */ |
||
170 | public function scopeBetween(Builder $query, array $userIds) |
||
180 | |||
181 | /** |
||
182 | * Get the participants table name. |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | protected function getParticipantsTable() |
||
190 | |||
191 | /** |
||
192 | * Scope the query by the subject. |
||
193 | * |
||
194 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
195 | * @param string $subject |
||
196 | * @param bool $strict |
||
197 | * |
||
198 | * @return \Illuminate\Database\Eloquent\Builder |
||
199 | */ |
||
200 | public function scopeSubject(Builder $query, $subject, $strict = false) |
||
206 | |||
207 | /* ------------------------------------------------------------------------------------------------ |
||
208 | | Getters & Setters |
||
209 | | ------------------------------------------------------------------------------------------------ |
||
210 | */ |
||
211 | /** |
||
212 | * Get the latest_message attribute. |
||
213 | * |
||
214 | * @return \Arcanedev\LaravelMessenger\Models\Message |
||
215 | */ |
||
216 | public function getLatestMessageAttribute() |
||
220 | |||
221 | /* ------------------------------------------------------------------------------------------------ |
||
222 | | Main Functions |
||
223 | | ------------------------------------------------------------------------------------------------ |
||
224 | */ |
||
225 | /** |
||
226 | * Returns all of the latest discussions by `updated_at` date. |
||
227 | * |
||
228 | * @return \Illuminate\Database\Eloquent\Collection |
||
229 | */ |
||
230 | public static function getLatest() |
||
234 | |||
235 | /** |
||
236 | * Returns all discussions by subject. |
||
237 | * |
||
238 | * @param string $subject |
||
239 | * @param bool $strict |
||
240 | * |
||
241 | * @return \Illuminate\Database\Eloquent\Collection |
||
242 | */ |
||
243 | public static function getBySubject($subject, $strict = false) |
||
247 | |||
248 | /** |
||
249 | * Returns an array of user ids that are associated with the discussion. |
||
250 | * |
||
251 | * @param int|null $userId |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | public function participantsUserIds($userId = null) |
||
268 | |||
269 | /** |
||
270 | * Add a user to discussion as a participant. |
||
271 | * |
||
272 | * @param int $userId |
||
273 | * |
||
274 | * @return \Arcanedev\LaravelMessenger\Models\Participant |
||
275 | */ |
||
276 | public function addParticipant($userId) |
||
286 | |||
287 | /** |
||
288 | * Add users to discussion as participants. |
||
289 | * |
||
290 | * @param array $userIds |
||
291 | * |
||
292 | * @return \Illuminate\Database\Eloquent\Collection |
||
293 | */ |
||
294 | public function addParticipants(array $userIds) |
||
302 | |||
303 | /** |
||
304 | * Remove a participant from discussion. |
||
305 | * |
||
306 | * @param int $userId |
||
307 | * @param bool $reload |
||
308 | * |
||
309 | * @return int |
||
310 | */ |
||
311 | public function removeParticipant($userId, $reload = true) |
||
322 | |||
323 | /** |
||
324 | * Remove participants from discussion. |
||
325 | * |
||
326 | * @param array $userIds |
||
327 | * @param bool $reload |
||
328 | * |
||
329 | * @return int |
||
330 | */ |
||
331 | public function removeParticipants(array $userIds, $reload = true) |
||
342 | |||
343 | /** |
||
344 | * Mark a discussion as read for a user. |
||
345 | * |
||
346 | * @param int $userId |
||
347 | * |
||
348 | * @return bool|int |
||
349 | */ |
||
350 | public function markAsRead($userId) |
||
360 | |||
361 | /** |
||
362 | * See if the current thread is unread by the user. |
||
363 | * |
||
364 | * @param int $userId |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | public function isUnread($userId) |
||
374 | |||
375 | /** |
||
376 | * Finds the participant record from a user id. |
||
377 | * |
||
378 | * @param int $userId |
||
379 | * |
||
380 | * @return \Arcanedev\LaravelMessenger\Models\Participant |
||
381 | */ |
||
382 | public function getParticipantByUserId($userId) |
||
388 | |||
389 | /** |
||
390 | * Get the trashed participants. |
||
391 | * |
||
392 | * @return \Illuminate\Database\Eloquent\Collection |
||
393 | */ |
||
394 | public function getTrashedParticipants() |
||
400 | |||
401 | /** |
||
402 | * Restores all participants within a discussion. |
||
403 | * |
||
404 | * @param bool $reload |
||
405 | * |
||
406 | * @return int |
||
407 | */ |
||
408 | public function restoreAllParticipants($reload = true) |
||
419 | |||
420 | /** |
||
421 | * Generates a participant information as a string. |
||
422 | * |
||
423 | * @param int|null $ignoredUserId |
||
424 | * @param \Closure|null $callback |
||
425 | * @param string $glue |
||
426 | * |
||
427 | * @return string |
||
428 | */ |
||
429 | public function participantsString($ignoredUserId = null, $callback = null, $glue = ', ') |
||
445 | |||
446 | /** |
||
447 | * Checks to see if a user is a current participant of the discussion. |
||
448 | * |
||
449 | * @param int $userId |
||
450 | * |
||
451 | * @return bool |
||
452 | */ |
||
453 | public function hasParticipant($userId) |
||
459 | |||
460 | /** |
||
461 | * Returns array of unread messages in discussion for given user. |
||
462 | * |
||
463 | * @param int $userId |
||
464 | * |
||
465 | * @return \Illuminate\Support\Collection |
||
466 | */ |
||
467 | public function userUnreadMessages($userId) |
||
479 | |||
480 | /** |
||
481 | * Returns count of unread messages in thread for given user. |
||
482 | * |
||
483 | * @param int $userId |
||
484 | * |
||
485 | * @return int |
||
486 | */ |
||
487 | public function userUnreadMessagesCount($userId) |
||
491 | } |
||
492 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: