|
3 | 3 | namespace App\Models; |
4 | 4 |
|
5 | 5 | use App\Exceptions\CouldNotMarkReplyAsSolution; |
6 | | -use App\Helpers\HasAuthor; |
7 | 6 | use App\Helpers\HasSlug; |
8 | 7 | use App\Helpers\HasTags; |
9 | | -use App\Helpers\HasTimestamps; |
| 8 | +use Spatie\Feed\Feedable; |
| 9 | +use Spatie\Feed\FeedItem; |
| 10 | +use App\Helpers\HasAuthor; |
10 | 11 | use App\Helpers\ModelHelpers; |
11 | | -use App\Helpers\ProvidesSubscriptions; |
| 12 | +use App\Helpers\HasTimestamps; |
| 13 | +use Illuminate\Support\Carbon; |
12 | 14 | use App\Helpers\ReceivesReplies; |
| 15 | +use App\Helpers\ProvidesSubscriptions; |
13 | 16 | use DB; |
14 | 17 | use Exception; |
15 | 18 | use Illuminate\Contracts\Pagination\Paginator; |
|
19 | 22 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
20 | 23 | use Illuminate\Support\Str; |
21 | 24 |
|
22 | | -final class Thread extends Model implements ReplyAble, SubscriptionAble |
| 25 | +final class Thread extends Model implements ReplyAble, SubscriptionAble, Feedable |
23 | 26 | { |
24 | 27 | use HasAuthor, HasSlug, HasTimestamps, ModelHelpers, ProvidesSubscriptions, ReceivesReplies, HasTags; |
25 | 28 |
|
26 | 29 | const TABLE = 'threads'; |
27 | 30 |
|
| 31 | + const FEED_PAGE_SIZE = 20; |
| 32 | + |
28 | 33 | /** |
29 | 34 | * {@inheritdoc} |
30 | 35 | */ |
@@ -109,6 +114,19 @@ public function delete() |
109 | 114 | parent::delete(); |
110 | 115 | } |
111 | 116 |
|
| 117 | + public function toFeedItem() |
| 118 | + { |
| 119 | + $updatedAt = Carbon::parse($this->latest_creation); |
| 120 | + |
| 121 | + return FeedItem::create() |
| 122 | + ->id($this->id) |
| 123 | + ->title($this->subject) |
| 124 | + ->summary($this->body) |
| 125 | + ->updated($updatedAt) |
| 126 | + ->link(route('thread', $this->slug)) |
| 127 | + ->author($this->author()->name); |
| 128 | + } |
| 129 | + |
112 | 130 | /** |
113 | 131 | * @return \App\Models\Thread[] |
114 | 132 | */ |
@@ -173,4 +191,15 @@ public static function resolutionTime() |
173 | 191 | return false; |
174 | 192 | } |
175 | 193 | } |
| 194 | + |
| 195 | + /** |
| 196 | + * retrieve 20 feed items with use of page parameter. |
| 197 | + */ |
| 198 | + public static function getFeedItems() |
| 199 | + { |
| 200 | + $page = intval(request('page', 1)); |
| 201 | + $query = static::feedQuery(); |
| 202 | + |
| 203 | + return $query->skip(($page - 1) * static::FEED_PAGE_SIZE)->take(static::FEED_PAGE_SIZE)->get(); |
| 204 | + } |
176 | 205 | } |
0 commit comments