66use App \Contracts \ReplyInterface ;
77use App \Contracts \SubscribeInterface ;
88use App \Exceptions \CouldNotMarkReplyAsSolution ;
9+ use App \Filters \Thread \ThreadFilters ;
910use App \Traits \HasAuthor ;
1011use App \Traits \HasReplies ;
1112use App \Traits \HasSlug ;
1617use CyrildeWit \EloquentViewable \Contracts \Viewable ;
1718use CyrildeWit \EloquentViewable \InteractsWithViews ;
1819use Exception ;
19- use Illuminate \Contracts \Pagination \Paginator ;
2020use Illuminate \Database \Eloquent \Builder ;
21- use Illuminate \Database \Eloquent \Collection ;
2221use Illuminate \Database \Eloquent \Factories \HasFactory ;
2322use Illuminate \Database \Eloquent \Model ;
2423use Illuminate \Database \Eloquent \Relations \BelongsTo ;
@@ -171,26 +170,37 @@ public function unmarkSolution()
171170 $ this ->save ();
172171 }
173172
174- public function scopeResolved (Builder $ query ): Builder
173+ public function scopeForChannel (Builder $ query, Channel $ channel ): Builder
175174 {
176- return $ query ->whereNotNull ('solution_reply_id ' );
175+ return $ query ->whereHas ('channels ' , function ($ query ) use ($ channel ) {
176+ if ($ channel ->hasItems ()) {
177+ $ query ->whereIn ('channels.id ' , array_merge ([$ channel ->id ], $ channel ->items ->modelKeys ()));
178+ } else {
179+ $ query ->where ('channels.slug ' , $ channel ->slug ());
180+ }
181+ });
177182 }
178183
179- public function scopeUnresolved (Builder $ query ): Builder
184+ public function scopeRecent (Builder $ query ): Builder
180185 {
181- return $ query ->whereNull ( ' solution_reply_id ' );
186+ return $ query ->feedQuery ()-> orderByDesc ( ' last_posted_at ' );
182187 }
183188
184- public function scopeForChannel (Builder $ query, string $ channel ): Builder
189+ public function scopeResolved (Builder $ query ): Builder
185190 {
186- return $ query ->whereHas ('channels ' , function ($ query ) use ($ channel ) {
187- $ query ->where ('channels.slug ' , $ channel );
188- });
191+ return $ query ->feedQuery ()
192+ ->whereNotNull ('solution_reply_id ' );
189193 }
190194
191- public function scopeRecent (Builder $ query ): Builder
195+ public function scopeUnresolved (Builder $ query ): Builder
196+ {
197+ return $ query ->feedQuery ()
198+ ->whereNull ('solution_reply_id ' );
199+ }
200+
201+ public function scopeFilter (Builder $ builder , $ request , array $ filters = []): Builder
192202 {
193- return self :: feedQuery ()-> orderByDesc ( ' created_at ' );
203+ return ( new ThreadFilters ( $ request ))-> add ( $ filters )-> filter ( $ builder );
194204 }
195205
196206 public function delete ()
@@ -214,37 +224,12 @@ public function toFeedItem(): FeedItem
214224 ->authorName ($ this ->user ->name );
215225 }
216226
217- public static function feed (int $ limit = 20 ): Collection
218- {
219- return static ::feedQuery ()->limit ($ limit )->get ();
220- }
221-
222- public static function feedPaginated (int $ perPage = 20 ): Paginator
223- {
224- return static ::feedQuery ()->paginate ($ perPage );
225- }
226-
227- public static function feedByChannelPaginated (Channel $ channel , int $ perPage = 20 ): Paginator
228- {
229- return static ::feedByChannelQuery ($ channel )
230- ->paginate ($ perPage );
231- }
232-
233- public static function feedByChannelQuery (Channel $ channel ): Builder
234- {
235- return static ::feedQuery ()
236- ->join ('channel_thread ' , function ($ join ) {
237- $ join ->on ('threads.id ' , 'channel_thread.thread_id ' );
238- })
239- ->where ('channel_thread.channel_id ' , $ channel ->id );
240- }
241-
242227 /**
243228 * This will order the threads by creation date and latest reply.
244229 */
245- public static function feedQuery ( ): Builder
230+ public function scopeFeedQuery ( Builder $ query ): Builder
246231 {
247- return static :: with ([
232+ return $ query -> with ([
248233 'solutionReply ' ,
249234 'replies ' ,
250235 'reactions ' ,
@@ -283,7 +268,7 @@ public static function resolutionTime()
283268
284269 public static function getFeedItems (): SupportCollection
285270 {
286- return static ::feedQuery ()
271+ return static ::with ([ ' reactions ' ])-> feedQuery ()
287272 ->paginate (static ::FEED_PAGE_SIZE )
288273 ->getCollection ();
289274 }
0 commit comments