-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathChatterPostController.php
More file actions
248 lines (207 loc) · 8.55 KB
/
ChatterPostController.php
File metadata and controls
248 lines (207 loc) · 8.55 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace DevDojo\Chatter\Controllers;
use Auth;
use Carbon\Carbon;
use DevDojo\Chatter\Events\ChatterAfterNewResponse;
use DevDojo\Chatter\Events\ChatterBeforeNewResponse;
use DevDojo\Chatter\Mail\ChatterDiscussionUpdated;
use DevDojo\Chatter\Models\Models;
use Event;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as Controller;
use Illuminate\Support\Facades\Mail;
use Purifier;
use Validator;
class ChatterPostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
/*$total = 10;
$offset = 0;
if ($request->total) {
$total = $request->total;
}
if ($request->offset) {
$offset = $request->offset;
}
$posts = Models::post()->with('user')->orderBy('created_at', 'DESC')->take($total)->offset($offset)->get();*/
// This is another unused route
// we return an empty array to not expose user data to the public
return response()->json([]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$stripped_tags_body = ['body' => strip_tags($request->body)];
$validator = Validator::make($stripped_tags_body, [
'body' => 'required|min:10',
],[
'body.required' => trans('chatter::alert.danger.reason.content_required'),
'body.min' => trans('chatter::alert.danger.reason.content_min'),
]);
// Event::fire(new ChatterBeforeNewResponse($request, $validator));
if (function_exists('chatter_before_new_response')) {
chatter_before_new_response($request, $validator);
}
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
if (config('chatter.security.limit_time_between_posts')) {
if ($this->notEnoughTimeBetweenPosts()) {
$minutes = trans_choice('chatter::messages.words.minutes', config('chatter.security.time_between_posts'));
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => trans('chatter::alert.danger.reason.prevent_spam', [
'minutes' => $minutes,
]),
];
return back()->with($chatter_alert)->withInput();
}
}
$request->request->add(['user_id' => Auth::user()->id]);
if (config('chatter.editor') == 'simplemde'):
$request->request->add(['markdown' => 1]);
endif;
$new_post = Models::post()->create($request->all());
$discussion = Models::discussion()->find($request->chatter_discussion_id);
$category = Models::category()->find($discussion->chatter_category_id);
if (!isset($category->slug)) {
$category = Models::category()->first();
}
if ($new_post->id) {
$discussion->last_reply_at = $discussion->freshTimestamp();
$discussion->save();
// Event::fire(new ChatterAfterNewResponse($request, $new_post));
if (function_exists('chatter_after_new_response')) {
chatter_after_new_response($request);
}
// if email notifications are enabled
if (config('chatter.email.enabled')) {
// Send email notifications about new post
$this->sendEmailNotifications($new_post->discussion);
}
$chatter_alert = [
'chatter_alert_type' => 'success',
'chatter_alert' => trans('chatter::alert.success.reason.submitted_to_post'),
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$discussion->slug)->with($chatter_alert);
} else {
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => trans('chatter::alert.danger.reason.trouble'),
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$discussion->slug)->with($chatter_alert);
}
}
private function notEnoughTimeBetweenPosts()
{
$user = Auth::user();
$past = Carbon::now()->subMinutes(config('chatter.security.time_between_posts'));
$last_post = Models::post()->where('user_id', '=', $user->id)->where('created_at', '>=', $past)->first();
if (isset($last_post)) {
return true;
}
return false;
}
private function sendEmailNotifications($discussion)
{
$users = $discussion->users->except(Auth::user()->id);
foreach ($users as $user) {
Mail::to($user)->queue(new ChatterDiscussionUpdated($discussion));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$stripped_tags_body = ['body' => strip_tags($request->body)];
$validator = Validator::make($stripped_tags_body, [
'body' => 'required|min:10',
],[
'body.required' => trans('chatter::alert.danger.reason.content_required'),
'body.min' => trans('chatter::alert.danger.reason.content_min'),
]);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
$post = Models::post()->find($id);
if (!Auth::guest() && (Auth::user()->id == $post->user_id)) {
if ($post->markdown) {
$post->body = $request->body;
} else {
$post->body = Purifier::clean($request->body);
}
$post->save();
$discussion = Models::discussion()->find($post->chatter_discussion_id);
$category = Models::category()->find($discussion->chatter_category_id);
if (!isset($category->slug)) {
$category = Models::category()->first();
}
$chatter_alert = [
'chatter_alert_type' => 'success',
'chatter_alert' => trans('chatter::alert.success.reason.updated_post'),
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$discussion->slug)->with($chatter_alert);
} else {
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => trans('chatter::alert.danger.reason.update_post'),
];
return redirect('/'.config('chatter.routes.home'))->with($chatter_alert);
}
}
/**
* Delete post.
*
* @param string $id
* @param \Illuminate\Http\Request
*
* @return \Illuminate\Routing\Redirect
*/
public function destroy($id, Request $request)
{
$post = Models::post()->with('discussion')->findOrFail($id);
if ($request->user()->id !== (int) $post->user_id) {
return redirect('/'.config('chatter.routes.home'))->with([
'chatter_alert_type' => 'danger',
'chatter_alert' => trans('chatter::alert.danger.reason.destroy_post'),
]);
}
if ($post->discussion->posts()->oldest()->first()->id === $post->id) {
if(config('chatter.soft_deletes')) {
$post->discussion->posts()->delete();
$post->discussion()->delete();
} else {
$post->discussion->posts()->forceDelete();
$post->discussion()->forceDelete();
}
return redirect('/'.config('chatter.routes.home'))->with([
'chatter_alert_type' => 'success',
'chatter_alert' => trans('chatter::alert.success.reason.destroy_post'),
]);
}
$post->delete();
$url = '/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$post->discussion->category->slug.'/'.$post->discussion->slug;
return redirect($url)->with([
'chatter_alert_type' => 'success',
'chatter_alert' => trans('chatter::alert.success.reason.destroy_from_discussion'),
]);
}
}