1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license https://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
14
|
|
|
|
15
|
|
|
use vistart\Models\models\BaseMongoNotificationModel; |
16
|
|
|
use vistart\Models\models\BaseNotificationModel; |
17
|
|
|
use vistart\Models\models\BaseUserModel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Description of NotificationLog |
21
|
|
|
* |
22
|
|
|
* @author vistart <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
trait NotificationReadTrait |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* @param BaseUserModel $user |
30
|
|
|
* @param BaseMongoNotificationModel|BaseNotificationModel $notification |
31
|
|
|
* @return boolean False if notification was marked as read or it didn't exist. |
32
|
|
|
*/ |
33
|
3 |
|
public static function read($user, $notification) |
34
|
|
|
{ |
35
|
3 |
|
if (empty($notification) || !$notification->findByIdentity($user)->guid($notification->guid)->one()) { |
36
|
1 |
|
return false; |
37
|
|
|
} |
38
|
3 |
|
$log = static::findByIdentity($user)->content($notification->guid)->one(); |
39
|
3 |
|
if (!$log) { |
40
|
3 |
|
$log = $user->create(static::className(), ['content' => $notification->guid]); |
41
|
3 |
|
} |
42
|
3 |
|
return $log->save(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @param BaseUserModel $user |
48
|
|
|
* @param BaseMongoNotificationModel|BaseNotificationModel $notification |
49
|
|
|
* @return boolean True if notification was marked as unread or it didn't exist. |
50
|
|
|
*/ |
51
|
3 |
|
public static function unread($user, $notification) |
52
|
|
|
{ |
53
|
1 |
|
if (empty($notification) || !$notification->findByIdentity($user)->guid($notification->guid)->one()) { |
54
|
1 |
|
return true; |
55
|
|
|
} |
56
|
1 |
|
$log = static::findByIdentity($user)->content($notification->guid)->one(); |
57
|
1 |
|
if ($log) { |
58
|
1 |
|
return $log->delete() == 1; |
59
|
|
|
} |
60
|
3 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if $notification is read by $user. |
65
|
|
|
* @param BaseUserModel $user |
66
|
|
|
* @param BaseMongoNotificationModel|BaseNotificationModel $notification |
67
|
|
|
* @return boolean |
68
|
|
|
*/ |
69
|
|
|
public static function isRead($user, $notification) |
70
|
|
|
{ |
71
|
|
|
if (empty($notification) || !$notification->findByIdentity($user)->guid($notification->guid)->one()) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
return ((int) static::findByIdentity($user)->content($notification->guid)->count()) > 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Vacuum all the invalid notification read records. |
79
|
|
|
* @param BaseUserModel $user |
80
|
|
|
* @param string $notificationClass |
81
|
|
|
* @return integer The sum of notification read records vacuumed. |
82
|
|
|
*/ |
83
|
2 |
|
public static function vacuum($user, $notificationClass) |
84
|
|
|
{ |
85
|
2 |
|
$logs = static::findByIdentity($user)->all(); |
86
|
2 |
|
$count = 0; |
87
|
2 |
|
foreach ($logs as $log) { |
88
|
2 |
|
if (is_string($notificationClass) && ((int) ($notificationClass::findByIdentity($user)->guid($log->content)->count())) > 0) { |
89
|
2 |
|
continue; |
90
|
|
|
} |
91
|
|
|
$count += $log->delete(); |
92
|
2 |
|
} |
93
|
2 |
|
return $count; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|