1
|
|
|
<?php namespace Arcanedev\LaravelMessenger\Traits; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelMessenger\Contracts\Discussion as DiscussionContract; |
4
|
|
|
use Arcanedev\LaravelMessenger\Contracts\Participant as ParticipantContract; |
5
|
|
|
use Arcanedev\LaravelMessenger\Models; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait Messagable |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\LaravelMessenger\Traits |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @property int id |
14
|
|
|
* @property \Illuminate\Database\Eloquent\Collection discussions |
15
|
|
|
* @property \Illuminate\Database\Eloquent\Collection participants |
16
|
|
|
* @property \Illuminate\Database\Eloquent\Collection messages |
17
|
|
|
*/ |
18
|
|
|
trait Messagable |
19
|
|
|
{ |
20
|
|
|
/* ------------------------------------------------------------------------------------------------ |
21
|
|
|
| Traits |
22
|
|
|
| ------------------------------------------------------------------------------------------------ |
23
|
|
|
*/ |
24
|
|
|
use ConfigHelper; |
25
|
|
|
|
26
|
|
|
/* ------------------------------------------------------------------------------------------------ |
27
|
|
|
| Relationships |
28
|
|
|
| ------------------------------------------------------------------------------------------------ |
29
|
|
|
*/ |
30
|
|
|
/** |
31
|
|
|
* Thread relationship. |
32
|
|
|
* |
33
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\belongsToMany |
34
|
|
|
*/ |
35
|
|
|
public function discussions() |
36
|
|
|
{ |
37
|
|
|
return $this->belongsToMany( |
|
|
|
|
38
|
|
|
$this->getModelFromConfig('discussions', Models\Discussion::class), |
39
|
|
|
$this->getTableFromConfig('participants', 'participants'), |
40
|
|
|
'user_id', |
41
|
|
|
'discussion_id' |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Participants relationship. |
47
|
|
|
* |
48
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
49
|
|
|
*/ |
50
|
|
|
public function participants() |
51
|
|
|
{ |
52
|
|
|
return $this->hasMany( |
|
|
|
|
53
|
|
|
$this->getModelFromConfig('participants', Models\Participant::class) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Message relationship. |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
61
|
|
|
*/ |
62
|
|
|
public function messages() |
63
|
|
|
{ |
64
|
|
|
return $this->hasMany( |
|
|
|
|
65
|
|
|
$this->getModelFromConfig('messages', Models\Message::class) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/* ------------------------------------------------------------------------------------------------ |
70
|
|
|
| Main Functions |
71
|
|
|
| ------------------------------------------------------------------------------------------------ |
72
|
|
|
*/ |
73
|
|
|
/** |
74
|
|
|
* Returns the new messages count for user. |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public function newMessagesCount() |
79
|
|
|
{ |
80
|
|
|
return count($this->discussionsWithNewMessages()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns all discussions IDs with new messages. |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function discussionsWithNewMessages() |
89
|
|
|
{ |
90
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $participants */ |
91
|
|
|
$participants = app(ParticipantContract::class) |
92
|
|
|
->where('user_id', $this->id) |
93
|
|
|
->get() |
94
|
|
|
->pluck('last_read', 'discussion_id'); |
95
|
|
|
|
96
|
|
|
if ($participants->isEmpty()) return []; |
97
|
|
|
|
98
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $discussions */ |
99
|
|
|
$discussions = app(DiscussionContract::class) |
100
|
|
|
->whereIn('id', $participants->keys()->toArray()) |
101
|
|
|
->get(); |
102
|
|
|
|
103
|
|
|
return $discussions->filter(function (Models\Discussion $discussion) use ($participants) { |
104
|
|
|
return $discussion->updated_at > $participants->get($discussion->id); |
105
|
|
|
})->pluck('id')->toArray(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.