|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Http\Controllers\Forum; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Events\CommentDeleted; |
|
6
|
|
|
use Coyote\Events\CommentSaved; |
|
7
|
|
|
use Coyote\Events\PostSaved; |
|
8
|
|
|
use Coyote\Events\TopicWasSaved; |
|
9
|
|
|
use Coyote\Http\Requests\Forum\PostCommentRequest; |
|
10
|
|
|
use Coyote\Http\Resources\PostCommentResource; |
|
11
|
|
|
use Coyote\Http\Resources\PostResource; |
|
12
|
|
|
use Coyote\Post; |
|
13
|
|
|
use Coyote\Repositories\Contracts\TopicRepositoryInterface; |
|
14
|
|
|
use Coyote\Http\Controllers\Controller; |
|
15
|
|
|
use Coyote\Services\Forum\Tracker; |
|
16
|
|
|
use Coyote\Services\Stream\Activities\Create as Stream_Create; |
|
17
|
|
|
use Coyote\Services\Stream\Activities\Update as Stream_Update; |
|
18
|
|
|
use Coyote\Services\Stream\Activities\Delete as Stream_Delete; |
|
19
|
|
|
use Coyote\Services\Stream\Activities\Move as Stream_Move; |
|
20
|
|
|
use Coyote\Services\Stream\Objects\Comment as Stream_Comment; |
|
21
|
|
|
use Coyote\Services\Stream\Objects\Topic as Stream_Topic; |
|
22
|
|
|
use Coyote\Stream; |
|
23
|
|
|
use Illuminate\Contracts\Notifications\Dispatcher; |
|
24
|
|
|
|
|
25
|
|
|
class CommentController extends Controller |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @param PostCommentRequest $request |
|
29
|
|
|
* @param Dispatcher $dispatcher |
|
30
|
|
|
* @param Post\Comment|null $comment |
|
31
|
|
|
* @return PostCommentResource |
|
32
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function save(PostCommentRequest $request, Dispatcher $dispatcher, ?Post\Comment $comment) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
if (!$comment->exists) { |
|
37
|
|
|
$comment->user()->associate($this->auth); |
|
|
|
|
|
|
38
|
|
|
$comment->post_id = $request->input('post_id'); |
|
39
|
|
|
} else { |
|
40
|
|
|
$this->authorize('update', [$comment, $comment->post->forum]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Maybe user does not have an access to this category? |
|
44
|
|
|
$this->authorize('access', [$comment->post->forum]); |
|
45
|
|
|
// Only moderators can post comment if topic (or forum) was locked |
|
46
|
|
|
$this->authorize('write', [$comment]); |
|
47
|
|
|
|
|
48
|
|
|
$comment->fill($request->only(['text'])); |
|
49
|
|
|
|
|
50
|
|
|
$this->transaction(function () use ($comment) { |
|
51
|
|
|
$comment->save(); |
|
52
|
|
|
|
|
53
|
|
|
stream($comment->wasRecentlyCreated ? Stream_Create::class : Stream_Update::class, ...$this->target($comment)); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
if ($comment->wasRecentlyCreated) { |
|
56
|
|
|
// subscribe post. notify about all future comments to this post |
|
57
|
|
|
$comment->post->subscribe($this->userId, true); |
|
58
|
|
|
} |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$comment->setRelation('forum', $comment->post->forum); |
|
62
|
|
|
|
|
63
|
|
|
broadcast(new CommentSaved($comment))->toOthers(); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
PostCommentResource::withoutWrapping(); |
|
66
|
|
|
|
|
67
|
|
|
return (new PostCommentResource($comment))->additional(['is_subscribed' => $comment->post->subscribers()->forUser($this->userId)->exists()]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param Post\Comment $comment |
|
72
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function delete(Post\Comment $comment) |
|
75
|
|
|
{ |
|
76
|
|
|
abort_if(!$comment->post, 404); |
|
77
|
|
|
|
|
78
|
|
|
$this->authorize('delete', [$comment, $comment->post->forum]); |
|
79
|
|
|
|
|
80
|
|
|
$this->transaction(function () use ($comment) { |
|
81
|
|
|
$comment->delete(); |
|
82
|
|
|
|
|
83
|
|
|
stream(Stream_Delete::class, ...$this->target($comment)); |
|
|
|
|
|
|
84
|
|
|
}); |
|
85
|
|
|
|
|
86
|
|
|
event(new CommentDeleted($comment)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Post $post |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function show(Post $post) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->authorize('access', [$post->forum]); |
|
97
|
|
|
|
|
98
|
|
|
PostCommentResource::withoutWrapping(); |
|
99
|
|
|
|
|
100
|
|
|
$post->load('comments.user'); |
|
101
|
|
|
|
|
102
|
|
|
$post->comments->each(function (Post\Comment $comment) use ($post) { |
|
103
|
|
|
$comment->setRelation('forum', $post->forum); |
|
104
|
|
|
}); |
|
105
|
|
|
|
|
106
|
|
|
return PostCommentResource::collection($post->comments)->keyBy('id'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function migrate(Post\Comment $comment, TopicRepositoryInterface $repository) |
|
110
|
|
|
{ |
|
111
|
|
|
$topic = $comment->post->topic; |
|
112
|
|
|
|
|
113
|
|
|
// Maybe user does not have an access to this category? |
|
114
|
|
|
$this->authorize('access', [$comment->post->forum]); |
|
115
|
|
|
// Only moderators can post comment if topic (or forum) was locked |
|
116
|
|
|
$this->authorize('write', [$comment]); |
|
117
|
|
|
$this->authorize('delete', [$comment, $comment->post->forum]); |
|
118
|
|
|
|
|
119
|
|
|
/** @var Post $post */ |
|
120
|
|
|
$post = $this->transaction(function () use ($topic, $comment, $repository) { |
|
121
|
|
|
$stream = Stream::where('object->objectType', 'comment')->where('object->id', $comment->id)->first(); |
|
122
|
|
|
|
|
123
|
|
|
$post = $topic->posts()->forceCreate( |
|
124
|
|
|
array_merge( |
|
125
|
|
|
['forum_id' => $topic->forum_id, 'topic_id' => $topic->id, 'ip' => $stream->ip, 'browser' => $stream->browser], |
|
|
|
|
|
|
126
|
|
|
$comment->only(['created_at', 'text', 'user_id']) |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
$comment->delete(); |
|
131
|
|
|
$repository->adjustReadDate($topic->id, $comment->created_at->subSecond()); |
|
132
|
|
|
|
|
133
|
|
|
stream(Stream_Move::class, ...$this->target($comment)); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
return $post; |
|
136
|
|
|
}); |
|
137
|
|
|
|
|
138
|
|
|
$post->load('assets'); |
|
139
|
|
|
$tracker = Tracker::make($topic); |
|
140
|
|
|
|
|
141
|
|
|
// fire the event. it can be used to index a content and/or add page path to "pages" table |
|
142
|
|
|
event(new TopicWasSaved($topic)); |
|
143
|
|
|
// add post to elasticsearch |
|
144
|
|
|
broadcast(new PostSaved($post))->toOthers(); |
|
145
|
|
|
|
|
146
|
|
|
PostResource::withoutWrapping(); |
|
147
|
|
|
|
|
148
|
|
|
return (new PostResource($post))->setTracker($tracker)->resolve($this->request); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
private function target(Post\Comment $comment): array |
|
152
|
|
|
{ |
|
153
|
|
|
$target = (new Stream_Topic())->map($comment->post->topic); |
|
154
|
|
|
|
|
155
|
|
|
// it is IMPORTANT to parse text first, and then put information to activity stream. |
|
156
|
|
|
// so that we will save plan text (without markdown) |
|
157
|
|
|
$object = (new Stream_Comment())->map($comment->post, $comment, $comment->post->topic); |
|
158
|
|
|
|
|
159
|
|
|
return [$object, $target]; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.