1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers\Discuss; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Illuminate\Http\RedirectResponse; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
9
|
|
|
use Xetaravel\Events\Experiences\PostWasCreatedEvent; |
10
|
|
|
use Xetaravel\Events\Experiences\PostWasSolvedEvent; |
11
|
|
|
use Xetaravel\Events\Rubies\PostWasSolvedEvent as RubiesPostWasSolvedEvent; |
12
|
|
|
use Xetaravel\Models\DiscussConversation; |
13
|
|
|
use Xetaravel\Models\DiscussPost; |
14
|
|
|
use Xetaravel\Models\Repositories\DiscussPostRepository; |
15
|
|
|
use Xetaravel\Models\Repositories\DiscussUserRepository; |
16
|
|
|
use Xetaravel\Models\User; |
17
|
|
|
use Xetaravel\Models\Validators\DiscussPostValidator; |
18
|
|
|
|
19
|
|
|
class PostController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Create a post for a conversation. |
23
|
|
|
* |
24
|
|
|
* @param \Illuminate\Http\Request $request |
25
|
|
|
* |
26
|
|
|
* @return \Illuminate\Http\RedirectResponse |
27
|
|
|
*/ |
28
|
|
|
public function create(Request $request): RedirectResponse |
29
|
|
|
{ |
30
|
|
|
$conversation = DiscussConversation::findOrFail($request->conversation_id); |
|
|
|
|
31
|
|
|
|
32
|
|
|
// Users that have the permission "manage.discuss" can bypass this rule. (Default to Administrator) |
33
|
|
View Code Duplication |
if (DiscussPost::isFlooding('xetaravel.flood.discuss.post') && !Auth::user()->hasPermission('manage.discuss')) { |
|
|
|
|
34
|
|
|
return back() |
35
|
|
|
->withInput() |
36
|
|
|
->with('danger', 'Wow, keep calm bro, and try to not flood !'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
DiscussPostValidator::create($request->all())->validate(); |
40
|
|
|
$post = DiscussPostRepository::create($request->all()); |
41
|
|
|
$user = DiscussUserRepository::create($request->all()); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$parser = new MentionParser($post); |
44
|
|
|
$content = $parser->parse($post->content); |
45
|
|
|
|
46
|
|
|
$post->content = $content; |
47
|
|
|
$post->save(); |
48
|
|
|
|
49
|
|
|
event(new PostWasCreatedEvent($post, Auth::user())); |
|
|
|
|
50
|
|
|
|
51
|
|
|
return redirect() |
52
|
|
|
->route('discuss.post.show', ['id' => $post->getKey()]) |
53
|
|
|
->with('success', 'Your reply has been posted successfully !'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Redirect an user to a conversation, page and post. |
58
|
|
|
* |
59
|
|
|
* @param \Illuminate\Http\Request $request |
60
|
|
|
* @param int $id The ID of the post. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Http\RedirectResponse |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function show(Request $request, int $id): RedirectResponse |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$post = DiscussPost::findOrFail($id); |
67
|
|
|
|
68
|
|
|
$postsBefore = DiscussPost::where([ |
69
|
|
|
['conversation_id', $post->conversation_id], |
70
|
|
|
['created_at', '<', $post->created_at] |
71
|
|
|
])->count(); |
72
|
|
|
|
73
|
|
|
$postsPerPage = config('xetaravel.pagination.discuss.post_per_page'); |
74
|
|
|
|
75
|
|
|
$page = floor($postsBefore / $postsPerPage) + 1; |
76
|
|
|
$page = ($page > 1) ? $page : 1; |
77
|
|
|
|
78
|
|
|
$request->session()->keep(['primary', 'danger', 'warning', 'success', 'info']); |
|
|
|
|
79
|
|
|
|
80
|
|
|
return redirect() |
81
|
|
|
->route( |
82
|
|
|
'discuss.conversation.show', |
83
|
|
|
[ |
84
|
|
|
'slug' => $post->conversation->slug, |
85
|
|
|
'id' => $post->conversation->id, |
86
|
|
|
'page' => $page, |
87
|
|
|
'#post-' . $post->getKey() |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Handle a delete action for the post. |
94
|
|
|
* |
95
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
96
|
|
|
* @param int $id |
97
|
|
|
* |
98
|
|
|
* @return \Illuminate\Http\RedirectResponse |
99
|
|
|
*/ |
100
|
|
|
public function delete(int $id): RedirectResponse |
101
|
|
|
{ |
102
|
|
|
$post = DiscussPost::findOrFail($id); |
103
|
|
|
|
104
|
|
|
$this->authorize('delete', $post); |
105
|
|
|
|
106
|
|
|
if ($post->conversation->first_post_id == $post->getKey()) { |
107
|
|
|
return redirect() |
108
|
|
|
->route('discuss.post.show', ['id' => $post->getKey()]) |
109
|
|
|
->with('danger', 'You can not delete the first post of a discussion !'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($post->delete()) { |
113
|
|
|
return redirect() |
114
|
|
|
->route( |
115
|
|
|
'discuss.conversation.show', |
116
|
|
|
['id' => $post->conversation->getKey(), 'slug' => $post->conversation->slug] |
117
|
|
|
) |
118
|
|
|
->with('success', 'This post has been deleted successfully !'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return redirect() |
122
|
|
|
->route('discuss.post.show', ['id' => $post->getKey()]) |
123
|
|
|
->with('danger', 'An error occurred while deleting this post !'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Mark as solved. |
128
|
|
|
* |
129
|
|
|
* @param int $id |
130
|
|
|
* |
131
|
|
|
* @return \Illuminate\Http\RedirectResponse |
132
|
|
|
*/ |
133
|
|
|
public function solved(int $id): RedirectResponse |
134
|
|
|
{ |
135
|
|
|
$post = DiscussPost::findOrFail($id); |
136
|
|
|
|
137
|
|
|
$this->authorize('solved', $post); |
138
|
|
|
|
139
|
|
|
if ($post->getKey() == $post->conversation->solved_post_id) { |
140
|
|
|
return back() |
141
|
|
|
->with('danger', 'This post is already the solved post !'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (!is_null($post->conversation->solved_post_id)) { |
145
|
|
|
return back() |
146
|
|
|
->with('danger', 'This conversation has already a solved post !'); |
147
|
|
|
} |
148
|
|
|
$conversation = DiscussConversation::findOrFail($post->conversation_id); |
149
|
|
|
|
150
|
|
|
$conversation->solved_post_id = $post->getKey(); |
151
|
|
|
$conversation->is_solved = true; |
152
|
|
|
$conversation->save(); |
153
|
|
|
|
154
|
|
|
$post->is_solved = true; |
155
|
|
|
$post->save(); |
156
|
|
|
|
157
|
|
|
event(new PostWasSolvedEvent($post, Auth::user())); |
|
|
|
|
158
|
|
|
|
159
|
|
|
event(new RubiesPostWasSolvedEvent($post, Auth::user())); |
|
|
|
|
160
|
|
|
|
161
|
|
|
return redirect() |
162
|
|
|
->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()]) |
163
|
|
|
->with('success', 'This reply as been marked as solved !'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Handle an edit action for the post. |
168
|
|
|
* |
169
|
|
|
* @param Request $request |
170
|
|
|
* @param int $id The id of the post to edit. |
171
|
|
|
* |
172
|
|
|
* @return \Illuminate\Http\RedirectResponse |
173
|
|
|
*/ |
174
|
|
|
public function edit(Request $request, int $id) : RedirectResponse |
175
|
|
|
{ |
176
|
|
|
$post = DiscussPost::findOrFail($id); |
177
|
|
|
|
178
|
|
|
if (!Auth::user()->can('update', $post)) { |
|
|
|
|
179
|
|
|
return back() |
180
|
|
|
->with('danger', 'You\'re not authorized to edit this message.'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
DiscussPostValidator::edit($request->all())->validate(); |
184
|
|
|
|
185
|
|
|
$parser = new MentionParser($post); |
186
|
|
|
$content = $parser->parse($request->input('content')); |
|
|
|
|
187
|
|
|
|
188
|
|
|
$post->content = $content; |
189
|
|
|
$post->is_edited = true; |
190
|
|
|
$post->edit_count++; |
191
|
|
|
$post->edited_user_id = Auth::id(); |
192
|
|
|
$post->edited_at = Carbon::now(); |
193
|
|
|
$post->save(); |
194
|
|
|
|
195
|
|
|
return redirect() |
196
|
|
|
->route('discuss.post.show', ['id' => $id]) |
197
|
|
|
->with('success', 'Your post has been edited successfully !'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the edit json template. |
202
|
|
|
* |
203
|
|
|
* @param int $id |
204
|
|
|
* |
205
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
|
|
|
|
206
|
|
|
*/ |
207
|
|
|
public function editTemplate(int $id) |
208
|
|
|
{ |
209
|
|
|
$post = DiscussPost::find($id); |
210
|
|
|
|
211
|
|
|
if (!Auth::user()->can('update', $post) || !$post) { |
|
|
|
|
212
|
|
|
return response()->json([ |
213
|
|
|
'error' => true, |
214
|
|
|
'message' => 'You\'re not authorized to edit this message or this message has been deleted.' |
215
|
|
|
]); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return response( |
219
|
|
|
view('Discuss::post.editTemplate', ['post' => $post]), |
|
|
|
|
220
|
|
|
200, |
221
|
|
|
['Content-Type' => 'application/json'] |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.