|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Http\Controllers\Forum; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Forum\Reason; |
|
6
|
|
|
use Coyote\Events\TopicWasDeleted; |
|
7
|
|
|
use Coyote\Events\PostWasDeleted; |
|
8
|
|
|
use Coyote\Http\Factories\FlagFactory; |
|
9
|
|
|
use Coyote\Services\Stream\Activities\Delete as Stream_Delete; |
|
10
|
|
|
use Coyote\Services\Stream\Objects\Topic as Stream_Topic; |
|
11
|
|
|
use Coyote\Services\Stream\Objects\Post as Stream_Post; |
|
12
|
|
|
use Coyote\Services\Stream\Objects\Forum as Stream_Forum; |
|
13
|
|
|
use Coyote\Services\UrlBuilder\UrlBuilder; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
|
|
16
|
|
|
class DeleteController extends BaseController |
|
17
|
|
|
{ |
|
18
|
|
|
use FlagFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Delete post or whole thread |
|
22
|
|
|
* |
|
23
|
|
|
* @param \Coyote\Post $post |
|
24
|
|
|
* @param Request $request |
|
25
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
26
|
|
|
*/ |
|
27
|
|
|
public function index($post, Request $request) |
|
28
|
|
|
{ |
|
29
|
|
|
// it must be like that. only if reason has been chosen, we need to validate it. |
|
30
|
|
|
if ($request->get('reason')) { |
|
31
|
|
|
$this->validate($request, ['reason' => 'int|exists:forum_reasons,id']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Step 1. Get post category |
|
35
|
|
|
$forum = &$post->forum; |
|
36
|
|
|
|
|
37
|
|
|
// Step 2. Does user really have permission to delete this post? |
|
38
|
|
|
$this->authorize('delete', [$post, $forum]); |
|
39
|
|
|
|
|
40
|
|
|
// Step 3. Maybe user does not have an access to this category? |
|
41
|
|
|
$forum->userCanAccess($this->userId) || abort(401, 'Unauthorized'); |
|
42
|
|
|
$topic = &$post->topic; |
|
43
|
|
|
|
|
44
|
|
|
// Step 4. Only moderators can delete this post if topic (or forum) was locked |
|
45
|
|
|
if ($this->getGateFactory()->denies('delete', $forum)) { |
|
46
|
|
|
if ($topic->is_locked || $forum->is_locked || $post->id < $topic->last_post_id || $post->deleted_at) { |
|
47
|
|
|
abort(401, 'Unauthorized'); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$url = $this->transaction(function () use ($post, $topic, $forum, $request) { |
|
52
|
|
|
$url = UrlBuilder::topic($topic); |
|
53
|
|
|
|
|
54
|
|
|
$notification = [ |
|
55
|
|
|
'sender_id' => $this->userId, |
|
56
|
|
|
'sender_name' => $this->auth->name, |
|
57
|
|
|
'subject' => str_limit($topic->subject, 84) |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
$reason = null; |
|
61
|
|
|
|
|
62
|
|
|
if ($request->get('reason')) { |
|
63
|
|
|
$reason = Reason::find($request->get('reason')); |
|
64
|
|
|
|
|
65
|
|
|
$notification = array_merge($notification, [ |
|
66
|
|
|
'excerpt' => $reason->name, |
|
67
|
|
|
'reasonName' => $reason->name, |
|
68
|
|
|
'reasonText' => $reason->description |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// if this is the first post in topic... we must delete whole thread |
|
73
|
|
|
if ($post->id === $topic->first_post_id) { |
|
74
|
|
|
$redirect = redirect()->route('forum.category', [$forum->slug]); |
|
75
|
|
|
|
|
76
|
|
|
$subscribersId = $topic->subscribers()->pluck('user_id'); |
|
77
|
|
|
if ($post->user_id !== null) { |
|
|
|
|
|
|
78
|
|
|
$subscribersId[] = $post->user_id; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$topic->delete(); |
|
82
|
|
|
// delete topic's flag |
|
83
|
|
|
$this->getFlagFactory()->deleteBy('topic_id', $topic->id, $this->userId); |
|
84
|
|
|
|
|
85
|
|
|
if ($subscribersId) { |
|
86
|
|
|
app('notification.topic.delete') |
|
87
|
|
|
->with($notification) |
|
88
|
|
|
->setUsersId($subscribersId->toArray()) |
|
89
|
|
|
->notify(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// fire the event. it can be used to delete row from "pages" table or from search index |
|
93
|
|
|
event(new TopicWasDeleted($topic)); |
|
94
|
|
|
|
|
95
|
|
|
$object = (new Stream_Topic())->map($topic); |
|
96
|
|
|
$target = (new Stream_Forum())->map($forum); |
|
97
|
|
|
} else { |
|
98
|
|
|
$subscribersId = $post->subscribers()->pluck('user_id'); |
|
99
|
|
|
|
|
100
|
|
|
if ($post->user_id !== null) { |
|
|
|
|
|
|
101
|
|
|
$subscribersId[] = $post->user_id; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$post->delete(); |
|
105
|
|
|
// delete post's flags |
|
106
|
|
|
$this->getFlagFactory()->deleteBy('post_id', $post->id, $this->userId); |
|
107
|
|
|
|
|
108
|
|
|
if ($subscribersId) { |
|
109
|
|
|
app('notification.post.delete') |
|
110
|
|
|
->with($notification) |
|
111
|
|
|
->setUrl($url) |
|
112
|
|
|
->setUsersId($subscribersId->toArray()) |
|
113
|
|
|
->notify(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$url .= '?p=' . $post->id . '#id' . $post->id; |
|
117
|
|
|
|
|
118
|
|
|
$redirect = back(); |
|
119
|
|
|
// fire the event. delete from search index |
|
120
|
|
|
event(new PostWasDeleted($post)); |
|
121
|
|
|
|
|
122
|
|
|
$object = (new Stream_Post(['url' => $url]))->map($post); |
|
123
|
|
|
$target = (new Stream_Topic())->map($topic); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (!empty($reason)) { |
|
|
|
|
|
|
127
|
|
|
$object->reasonName = $reason->name; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
stream(Stream_Delete::class, $object, $target); |
|
131
|
|
|
return $redirect->with('success', 'Post został usunięty.'); |
|
132
|
|
|
}); |
|
133
|
|
|
|
|
134
|
|
|
return $url; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|