2
2
3
3
namespace Andaletech \Inbox \Models ;
4
4
5
+ use DateTime ;
5
6
use Andaletech \Inbox \Libs \Utils ;
6
7
use Illuminate \Database \Eloquent \Model ;
8
+ use Illuminate \Database \Eloquent \Builder ;
7
9
use Andaletech \Inbox \Contracts \Models \IParticipant ;
8
10
9
11
/**
10
- * @property Carbon\Carbon $seen_at
12
+ * @property Carbon\Carbon $read_at
11
13
* @property Carbon\Carbon $trashed_at
12
14
* @property array $tags
13
15
* @property array $extra
@@ -88,6 +90,41 @@ public function participant()
88
90
89
91
#endregion relationships
90
92
93
+ #region query scope
94
+
95
+ public function scopeForParticipant (Builder $ query , $ type , $ id )
96
+ {
97
+ return $ query ->where ($ query ->qualifyColumn ('participant_type ' ), $ type )
98
+ ->where (
99
+ $ query ->qualifyColumn ('participant_id ' ),
100
+ $ id
101
+ );
102
+ }
103
+
104
+ public function scopeForMessageNanoId (Builder $ query , $ messageNanoId )
105
+ {
106
+ return $ query ->whereHas ('message ' , function ($ subQ ) use ($ messageNanoId ) {
107
+ return $ subQ ->where ('nano_id ' , $ messageNanoId );
108
+ });
109
+ }
110
+
111
+ public function scopeForThreadId (Builder $ query , $ threadId )
112
+ {
113
+ return $ query ->where ('thread_id ' , $ threadId );
114
+ }
115
+
116
+ public function scopeWasRead (Builder $ query )
117
+ {
118
+ return $ query ->whereNotNull ('read_at ' );
119
+ }
120
+
121
+ public function scopeNotRead (Builder $ query )
122
+ {
123
+ return $ query ->whereNull ('read_at ' );
124
+ }
125
+
126
+ #endregion query scope
127
+
91
128
#region override parent methods.
92
129
93
130
public function toArray ()
@@ -105,5 +142,18 @@ public function toArray()
105
142
106
143
#region class specific methods
107
144
145
+ public function markAsRead (?DateTime $ dateTime = null , $ save = true )
146
+ {
147
+ if ($ this ->read_at ) {
148
+ return $ this ->read_at ;
149
+ }
150
+ $ this ->read_at = ($ dateTime ?? (new DateTime ()))->format ('Y-m-d H:i:s ' );
151
+ if ($ save ) {
152
+ $ this ->save ();
153
+ }
154
+
155
+ return $ this ->read_at ;;
156
+ }
157
+
108
158
#endregion class specific methods
109
159
}
0 commit comments