-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathTopicController.php
171 lines (144 loc) · 6.36 KB
/
TopicController.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Coyote\Http\Controllers\Forum;
use Coyote\Domain\Seo;
use Coyote\Domain\Seo\Schema\DiscussionForumPosting;
use Coyote\Forum;
use Coyote\Forum\Reason;
use Coyote\Http\Factories\CacheFactory;
use Coyote\Http\Resources\FlagResource;
use Coyote\Http\Resources\PollResource;
use Coyote\Http\Resources\PostCollection;
use Coyote\Http\Resources\TopicResource;
use Coyote\Repositories\Criteria\Post\WithSubscribers;
use Coyote\Repositories\Criteria\Post\WithTrashedInfo;
use Coyote\Repositories\Criteria\WithTrashed;
use Coyote\Services\Flags;
use Coyote\Services\Forum\Tracker;
use Coyote\Services\Forum\TreeBuilder\Builder;
use Coyote\Services\Forum\TreeBuilder\JsonDecorator;
use Coyote\Services\Forum\TreeBuilder\ListDecorator;
use Coyote\Services\Parser\Extensions\Emoji;
use Coyote\Topic;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\View\View;
class TopicController extends BaseController
{
use CacheFactory;
public function index(Request $request, Forum $forum, Topic $topic): Collection|View|array
{
$this->breadcrumb->push($topic->title, route('forum.topic', [$forum->slug, $topic->id, $topic->slug]),
leafWithLink:true);
// get the topic (and forum) mark time value from middleware
$markTime = $request->attributes->get('mark_time');
/** @var Gate $gate */
$gate = app(Gate::class);
$page = (int)$request->get('page');
$perPage = $this->postsPerPage($request);
// user with forum-update ability WILL see every post
// NOTE: criteria MUST BE pushed before calling getPage() method!
if ($gate->allows('delete', $forum)) {
$this->post->pushCriteria(new WithTrashed());
$this->post->pushCriteria(new WithTrashedInfo());
$topic->replies = $topic->replies_real; // user is able to see real number of posts in this topic
}
// user wants to show certain post. we need to calculate page number based on post id.
if ($request->filled('p')) {
$page = $this->post->getPage(min(2147483647, (int)$request->get('p')), $topic->id, $perPage);
}
// show posts of last page if page parameter is higher than pages count
$lastPage = max((int)ceil(($topic->replies + 1) / $perPage), 1);
if ($page > $lastPage) {
$page = $lastPage;
}
$this->post->pushCriteria(new WithSubscribers($this->userId));
$paginate = $this->post->lengthAwarePagination($topic, $page, $perPage);
$this->pushForumCriteria(true);
// create forum list for current user (according to user's privileges)
$treeBuilder = new Builder($this->forum->list());
$treeDecorator = new ListDecorator($treeBuilder);
$userForums = $treeDecorator->build();
// important: load topic owner so we can highlight user's login
if ($page === 1) {
$topic->setRelation('firstPost', $paginate->first());
} else {
$topic->load('firstPost');
}
$tracker = Tracker::make($topic);
if ($gate->allows('delete', $forum) || $gate->allows('move', $forum)) {
$reasons = Reason::query()->pluck('name', 'id')->toArray();
$this->forum->resetCriteria();
$this->pushForumCriteria(false);
// forum list only for moderators
$treeBuilder->setForums($this->forum->list());
$allForums = (new JsonDecorator($treeBuilder))->build();
} else {
$allForums = [];
$reasons = null;
}
$resource = (new PostCollection($paginate))
->setRelations($topic, $forum)
->setTracker($tracker);
$dateTime = $paginate->last()->created_at;
TopicResource::wrap('data');
$posts = $resource->toResponse($this->request)->getData(true);
TopicResource::withoutWrapping();
if ($markTime < $dateTime) {
$tracker->asRead($dateTime);
}
if ($request->wantsJson()) {
return $posts;
}
$topic->load('tags');
$post = array_first($posts['data']);
return $this
->view('forum.topic', [
'threadStartUrl' => route('forum.topic', [$forum->slug, $topic->id, $topic->slug]),
'posts' => $posts,
'forum' => $forum,
'paginate' => $paginate,
'reasons' => $reasons,
'model' => $topic, // we need eloquent model in twig to show information about locked/moved topic
'topic' => (new TopicResource($tracker))->toResponse($request)->getData(true),
'poll' => $topic->poll ? (new PollResource($topic->poll))->resolve($request) : null,
'is_writeable' => $gate->allows('write', $forum) && $gate->allows('write', $topic),
'all_forums' => $allForums,
'emojis' => Emoji::all(),
'user_forums' => $userForums,
'description' => excerpt($post['text'], 100),
'flags' => $this->flags($forum),
'schema_topic' => $this->discussionForumPosting($topic, $post['html']),
]);
}
private function discussionForumPosting(Topic $topic, string $html): Seo\Schema
{
$user = $topic->firstPost->user;
return new Seo\Schema(new DiscussionForumPosting(
route('forum.topic', [$topic->forum, $topic, $topic->slug]),
$topic->title,
\reduced_whitespace(\plain($html)),
$user?->name ?? $topic->firstPost->user_name,
$user ? route('profile', ['user_trashed' => $user->id]) : null,
$topic->replies,
$topic->score,
$topic->views,
$topic->created_at,
));
}
private function flags(Forum $forum): array
{
/** @var Flags $flags */
$flags = resolve(Flags::class);
$resourceFlags = $flags
->fromModels([Topic::class])
->permission('delete', [$forum])
->get();
return FlagResource::collection($resourceFlags)->toArray($this->request);
}
public function mark(Topic $topic): void
{
$tracker = Tracker::make($topic);
$tracker->asRead($topic->last_post_created_at);
}
}