forked from musonza/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.php
More file actions
185 lines (158 loc) · 5.01 KB
/
Message.php
File metadata and controls
185 lines (158 loc) · 5.01 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Musonza\Chat\Models;
use Illuminate\Database\Eloquent\Model;
use Musonza\Chat\BaseModel;
use Musonza\Chat\Chat;
use Musonza\Chat\ConfigurationManager;
use Musonza\Chat\Eventing\AllParticipantsDeletedMessage;
use Musonza\Chat\Eventing\EventGenerator;
use Musonza\Chat\Eventing\MessageWasSent;
class Message extends BaseModel
{
use EventGenerator;
protected $fillable = [
'body',
'participation_id',
'type',
];
protected $table = ConfigurationManager::MESSAGES_TABLE;
/**
* All of the relationships to be touched.
*
* @var array
*/
protected $touches = ['conversation'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'flagged' => 'boolean',
];
protected $appends = ['sender'];
public function participation()
{
return $this->belongsTo(Participation::class, 'participation_id');
}
public function getSenderAttribute()
{
$participantModel = $this->participation->messageable;
if (method_exists($participantModel, 'getParticipantDetails')) {
return $participantModel->getParticipantDetails();
}
$fields = Chat::senderFieldsWhitelist();
return $fields ? $this->participation->messageable->only($fields) : $this->participation->messageable;
}
public function unreadCount(Model $participant)
{
return MessageNotification::where('messageable_id', $participant->getKey())
->where('is_seen', 0)
->where('messageable_type', $participant->getMorphClass())
->count();
}
public function conversation()
{
return $this->belongsTo(Conversation::class, 'conversation_id');
}
/**
* Adds a message to a conversation.
*
* @param Conversation $conversation
* @param string $body
* @param Participation $participant
* @param string $type
*
* @return Model
*/
public function send(Conversation $conversation, string $body, Participation $participant, string $type = 'text'): Model
{
$message = $conversation->messages()->create([
'body' => $body,
'participation_id' => $participant->getKey(),
'type' => $type,
]);
if (Chat::broadcasts()) {
broadcast(new MessageWasSent($message))->toOthers();
} else {
event(new MessageWasSent($message));
}
$this->createNotifications($message);
return $message;
}
/**
* Creates an entry in the message_notification table for each participant
* This will be used to determine if a message is read or deleted.
*
* @param Message $message
*/
protected function createNotifications($message)
{
MessageNotification::make($message, $message->conversation);
}
/**
* Deletes a message for the participant.
*
* @param Model $participant
*
* @return void
*/
public function trash(Model $participant): void
{
MessageNotification::where('messageable_id', $participant->getKey())
->where('messageable_type', $participant->getMorphClass())
->where('message_id', $this->getKey())
->delete();
if ($this->unDeletedCount() === 0) {
event(new AllParticipantsDeletedMessage($this));
}
}
public function unDeletedCount()
{
return MessageNotification::where('message_id', $this->getKey())
->count();
}
/**
* Return user notification for specific message.
*
* @param Model $participant
*
* @return MessageNotification
*/
public function getNotification(Model $participant): MessageNotification
{
return MessageNotification::where('messageable_id', $participant->getKey())
->where('messageable_type', $participant->getMorphClass())
->where('message_id', $this->id)
->select([
'*',
'updated_at as read_at',
])
->first();
}
/**
* Marks message as read.
*
* @param $participant
*/
public function markRead($participant): void
{
$this->getNotification($participant)->markAsRead();
}
public function flagged(Model $participant): bool
{
return (bool) MessageNotification::where('messageable_id', $participant->getKey())
->where('message_id', $this->id)
->where('messageable_type', $participant->getMorphClass())
->where('flagged', 1)
->first();
}
public function toggleFlag(Model $participant): self
{
MessageNotification::where('messageable_id', $participant->getKey())
->where('message_id', $this->id)
->where('messageable_type', $participant->getMorphClass())
->update(['flagged' => $this->flagged($participant) ? false : true]);
return $this;
}
}