forked from musonza/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageNotification.php
More file actions
66 lines (55 loc) · 1.98 KB
/
MessageNotification.php
File metadata and controls
66 lines (55 loc) · 1.98 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
<?php
namespace Musonza\Chat\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Musonza\Chat\BaseModel;
use Musonza\Chat\ConfigurationManager;
class MessageNotification extends BaseModel
{
use SoftDeletes;
protected $table = ConfigurationManager::MESSAGE_NOTIFICATIONS_TABLE;
protected $fillable = ['messageable_id', 'messageable_type', 'message_id', 'conversation_id'];
protected $dates = ['deleted_at'];
/**
* Creates a new notification.
*
* @param Message $message
* @param Conversation $conversation
*/
public static function make(Message $message, Conversation $conversation)
{
self::createCustomNotifications($message, $conversation);
}
public function unReadNotifications(Model $participant)
{
return self::where([
['messageable_id', '=', $participant->getKey()],
['messageable_type', '=', $participant->getMorphClass()],
['is_seen', '=', 0],
])->get();
}
public static function createCustomNotifications($message, $conversation)
{
$notification = [];
foreach ($conversation->participants as $participation) {
$is_sender = ($message->participation_id == $participation->id) ? 1 : 0;
$notification[] = [
'messageable_id' => $participation->messageable_id,
'messageable_type' => $participation->messageable_type,
'message_id' => $message->id,
'participation_id' => $participation->id,
'conversation_id' => $conversation->id,
'is_seen' => $is_sender,
'is_sender' => $is_sender,
'created_at' => $message->created_at,
];
}
self::insert($notification);
}
public function markAsRead()
{
$this->is_seen = 1;
$this->update(['is_seen' => 1]);
$this->save();
}
}