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