|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Blacklist; |
|
6
|
|
|
use Apps\ActiveRecord\Message; |
|
7
|
|
|
use Apps\ActiveRecord\ProfileRating; |
|
8
|
|
|
use Apps\ActiveRecord\WallAnswer; |
|
9
|
|
|
use Apps\ActiveRecord\WallPost; |
|
10
|
|
|
use Extend\Core\Arch\ApiController; |
|
11
|
|
|
use Ffcms\Core\App; |
|
12
|
|
|
use Ffcms\Core\Exception\JsonException; |
|
13
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
|
14
|
|
|
use Ffcms\Core\Helper\Date; |
|
15
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
16
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
17
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
|
18
|
|
|
|
|
19
|
|
|
class Profile extends ApiController |
|
20
|
|
|
{ |
|
21
|
|
|
const ITEM_PER_PAGE = 10; |
|
22
|
|
|
const ANSWER_DELAY = 60; // in seconds |
|
23
|
|
|
|
|
24
|
|
|
const MSG_USER_LIST = 10; |
|
25
|
|
|
const MSG_TEXT_LIST = 20; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get wall answer's count by post-ids list |
|
29
|
|
|
* @param int $postIds |
|
30
|
|
|
* @throws JsonException |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function actionWallanswercount($postIds) |
|
34
|
|
|
{ |
|
35
|
|
|
// set header |
|
36
|
|
|
$this->setJsonHeader(); |
|
37
|
|
|
// check query length |
|
38
|
|
|
if (Str::likeEmpty($postIds)) { |
|
39
|
|
|
throw new JsonException('Wrong input count'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$list = explode(',', $postIds); |
|
43
|
|
|
$itemCount = count($list); |
|
44
|
|
|
// empty or is biggest then limit? |
|
45
|
|
|
if ($itemCount < 1 || $itemCount > self::ITEM_PER_PAGE) { |
|
46
|
|
|
throw new JsonException('Wrong input count'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// prepare response |
|
50
|
|
|
$response = []; |
|
51
|
|
|
foreach ($list as $post) { |
|
52
|
|
|
$response[$post] = WallAnswer::where('post_id', '=', $post)->count(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// display json data |
|
56
|
|
|
return json_encode([ |
|
57
|
|
|
'status' => 1, |
|
58
|
|
|
'data' => $response |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Show all answers for this post id |
|
64
|
|
|
* @param int $postId |
|
65
|
|
|
* @throws JsonException |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function actionShowwallanswers($postId) |
|
69
|
|
|
{ |
|
70
|
|
|
// check input post id num |
|
71
|
|
|
if (!Obj::isLikeInt($postId) || $postId < 1) { |
|
72
|
|
|
throw new JsonException('Wrong input data'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// try to find this post |
|
76
|
|
|
$object = WallPost::find($postId); |
|
77
|
|
|
|
|
78
|
|
|
if ($object === null || $object === false) { |
|
79
|
|
|
throw new JsonException('Wrong input data'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$response = []; |
|
83
|
|
|
|
|
84
|
|
|
foreach ($object->getAnswer()->orderBy('id', 'DESC')->get() as $answer) { |
|
85
|
|
|
// get user object and profile |
|
86
|
|
|
$user = $answer->getUser(); |
|
87
|
|
|
$profile = $user->getProfile(); |
|
88
|
|
|
// check if user exist |
|
89
|
|
|
if ($user === null || $user->id < 1) { |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
// generate response array |
|
93
|
|
|
$response[] = [ |
|
94
|
|
|
'answer_id' => $answer->id, |
|
95
|
|
|
'user_id' => $answer->user_id, |
|
96
|
|
|
'user_nick' => App::$Security->strip_tags($profile->getNickname()), |
|
97
|
|
|
'user_avatar' => $profile->getAvatarUrl('small'), |
|
98
|
|
|
'answer_message' => App::$Security->strip_tags($answer->message), |
|
99
|
|
|
'answer_date' => Date::convertToDatetime($answer->created_at, Date::FORMAT_TO_SECONDS) |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return json_encode(['status' => 1, 'data' => $response]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Add new post answer from AJAX post |
|
108
|
|
|
* @param int $postId |
|
109
|
|
|
* @throws JsonException |
|
110
|
|
|
*/ |
|
111
|
|
|
public function actionSendwallanswer($postId) |
|
112
|
|
|
{ |
|
113
|
|
|
// not auth? what are you doing there? ;) |
|
114
|
|
|
if (!App::$User->isAuth()) { |
|
115
|
|
|
throw new JsonException('Auth required'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// no post id? wtf you doing man! |
|
119
|
|
|
if (!Obj::isLikeInt($postId) || $postId < 1) { |
|
120
|
|
|
throw new JsonException('Wrong input data'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$viewer = App::$User->identity(); |
|
124
|
|
|
|
|
125
|
|
|
// get message from post and validate minlength |
|
126
|
|
|
$message = App::$Request->get('message'); |
|
127
|
|
|
$message = App::$Security->strip_tags($message); |
|
128
|
|
|
if (!Obj::isString($message) || Str::length($message) < 3) { |
|
|
|
|
|
|
129
|
|
|
throw new JsonException('Wrong input data'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// try to find this post |
|
133
|
|
|
$wallPost = WallPost::where('id', '=', $postId); |
|
134
|
|
|
if ($wallPost->count() < 1) { |
|
135
|
|
|
throw new JsonException('Wrong input data'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$wallRow = $wallPost->first(); |
|
139
|
|
|
$target_id = $wallRow->target_id; |
|
140
|
|
|
// check if in blacklist |
|
141
|
|
|
if (!Blacklist::check($viewer->id, $target_id)) { |
|
|
|
|
|
|
142
|
|
|
throw new JsonException('User is blocked!'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// check delay between user last post and current |
|
146
|
|
|
$lastAnswer = WallAnswer::where('user_id', '=', App::$User->identity()->getId())->orderBy('created_at', 'DESC')->first(); |
|
147
|
|
|
if (null !== $lastAnswer && false !== $lastAnswer) { |
|
148
|
|
|
$now = time(); |
|
149
|
|
|
$answerTime = Date::convertToTimestamp($lastAnswer->created_at); |
|
150
|
|
|
$cfgs = \Apps\ActiveRecord\App::getConfigs('app', 'Profile'); |
|
151
|
|
|
// hmm, maybe past less then delay required? |
|
152
|
|
|
if ($now - (int)$cfgs['delayBetweenPost'] < $answerTime) { |
|
153
|
|
|
throw new JsonException('Delay between answers not pass'); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// make new row ;) |
|
158
|
|
|
$answers = new WallAnswer(); |
|
159
|
|
|
$answers->post_id = $postId; |
|
160
|
|
|
$answers->user_id = App::$User->identity()->getId(); |
|
161
|
|
|
$answers->message = $message; |
|
162
|
|
|
$answers->save(); |
|
163
|
|
|
|
|
164
|
|
|
// send "ok" response |
|
165
|
|
|
$this->setJsonHeader(); |
|
166
|
|
|
return json_encode(['status' => 1, 'message' => 'ok']); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Delete answer by answer owner or wall owner |
|
171
|
|
|
* @param $answerId |
|
172
|
|
|
* @throws JsonException |
|
173
|
|
|
*/ |
|
174
|
|
|
public function actionDeleteanswerowner($answerId) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->setJsonHeader(); |
|
177
|
|
|
// hello script kiddy, you must be auth ;) |
|
178
|
|
|
if (!App::$User->isAuth()) { |
|
179
|
|
|
throw new JsonException('Auth required'); |
|
180
|
|
|
} |
|
181
|
|
|
// answer id must be an unsigned integer |
|
182
|
|
|
if (!Obj::isLikeInt($answerId) || $answerId < 1) { |
|
183
|
|
|
throw new JsonException('Wrong input data'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$findAnswer = WallAnswer::find($answerId); |
|
187
|
|
|
|
|
188
|
|
|
// check if this answer id exist |
|
189
|
|
|
if (null === $findAnswer || false === $findAnswer) { |
|
190
|
|
|
throw new JsonException('Wrong input data'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// get current viewer |
|
194
|
|
|
$viewer = App::$User->identity(); |
|
195
|
|
|
// get post info |
|
196
|
|
|
$postInfo = $findAnswer->getWallPost(); |
|
197
|
|
|
|
|
198
|
|
|
// if not a target user of answer and not answer owner - lets throw exception |
|
199
|
|
|
if($postInfo->target_id !== $viewer->id && $findAnswer->user_id !== $viewer->id) { |
|
|
|
|
|
|
200
|
|
|
throw new JsonException('Access declined!'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
// all is ok, lets remove this answer ;) |
|
204
|
|
|
$findAnswer->delete(); |
|
205
|
|
|
|
|
206
|
|
|
return json_encode([ |
|
207
|
|
|
'status' => 1, |
|
208
|
|
|
'message' => 'ok' |
|
209
|
|
|
]); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Load user dialog list based on offset |
|
214
|
|
|
* @param int $offset |
|
215
|
|
|
* @param int $new |
|
216
|
|
|
* @throws JsonException |
|
217
|
|
|
*/ |
|
218
|
|
|
public function actionListmessagedialog($offset = 0, $new = 0) |
|
219
|
|
|
{ |
|
220
|
|
|
// check is user auth |
|
221
|
|
|
if (!App::$User->isAuth()) { |
|
222
|
|
|
throw new JsonException('Auth required'); |
|
223
|
|
|
} |
|
224
|
|
|
$this->setJsonHeader(); |
|
225
|
|
|
|
|
226
|
|
|
// check is offset is int |
|
227
|
|
|
if ($offset !== 0 && !Obj::isLikeInt($offset)) { |
|
228
|
|
|
$offset = 0; |
|
229
|
|
|
} |
|
230
|
|
|
++$offset; |
|
231
|
|
|
|
|
232
|
|
|
// get user person |
|
233
|
|
|
$user = App::$User->identity(); |
|
234
|
|
|
|
|
235
|
|
|
$records = Message::select('*', Capsule::raw('max(created_at) as cmax'), Capsule::raw('min(readed) as tread')) |
|
236
|
|
|
->where('target_id', '=', $user->id) |
|
|
|
|
|
|
237
|
|
|
->orWhere('sender_id', '=', $user->id) |
|
|
|
|
|
|
238
|
|
|
->orderBy('cmax', 'DESC') |
|
239
|
|
|
->groupBy(['target_id', 'sender_id']) // group by ignore orderBy ... make some shit |
|
240
|
|
|
->take($offset * self::MSG_USER_LIST) |
|
241
|
|
|
->get(); |
|
242
|
|
|
|
|
243
|
|
|
$userList = []; |
|
244
|
|
|
$unreadList = []; |
|
245
|
|
|
|
|
246
|
|
|
if (Obj::isLikeInt($new) && $new > 0 && App::$User->isExist($new)) { |
|
247
|
|
|
$userList[] = $new; |
|
248
|
|
|
} |
|
249
|
|
|
// there is 2 way of messages: me->user; user->me, try to parse it |
|
250
|
|
|
foreach ($records as $row) { |
|
251
|
|
|
// target is not myself? then i'm - sender (remote user is target: my->to_user) |
|
252
|
|
|
if ($row->target_id !== $user->id) { |
|
|
|
|
|
|
253
|
|
|
$userList[] = $row->target_id; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
// sender is not myself? then i'm - target (remote user is sender user->to_me) |
|
257
|
|
|
if ($row->sender_id !== $user->id) { |
|
|
|
|
|
|
258
|
|
|
$userList[] = $row->sender_id; |
|
259
|
|
|
if ((int)$row->tread === 0) { |
|
260
|
|
|
$unreadList[] = $row->sender_id; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// store only unique users in dialog |
|
266
|
|
|
$userList = array_unique($userList, SORT_NUMERIC); |
|
267
|
|
|
// generate json response based on userList and unreadList |
|
268
|
|
|
$response = []; |
|
269
|
|
|
foreach ($userList as $user_id) { |
|
270
|
|
|
$identity = App::$User->identity($user_id); |
|
271
|
|
|
if (null === $identity) { |
|
272
|
|
|
continue; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
$response[] = [ |
|
276
|
|
|
'user_id' => $user_id, |
|
277
|
|
|
'user_nick' => App::$Security->strip_tags($identity->getProfile()->getNickname()), |
|
278
|
|
|
'user_avatar' => $identity->getProfile()->getAvatarUrl('small'), |
|
279
|
|
|
'message_new' => Arr::in($user_id, $unreadList), |
|
280
|
|
|
'user_block' => !Blacklist::check($user->id, $identity->id) |
|
|
|
|
|
|
281
|
|
|
]; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
return json_encode(['status' => 1, 'data' => $response]); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Get new p.m. count for current user |
|
289
|
|
|
* @throws JsonException |
|
290
|
|
|
*/ |
|
291
|
|
|
public function actionMessagesnewcount() |
|
292
|
|
|
{ |
|
293
|
|
|
// check if authed |
|
294
|
|
|
if (!App::$User->isAuth()) { |
|
295
|
|
|
throw new JsonException('Auth required'); |
|
296
|
|
|
} |
|
297
|
|
|
$this->setJsonHeader(); |
|
298
|
|
|
|
|
299
|
|
|
// get user object |
|
300
|
|
|
$user = App::$User->identity(); |
|
301
|
|
|
|
|
302
|
|
|
// get new message count |
|
303
|
|
|
$query = Message::where('target_id', '=', $user->id) |
|
|
|
|
|
|
304
|
|
|
->where('readed', '=', 0)->count(); |
|
305
|
|
|
|
|
306
|
|
|
// set response as json |
|
307
|
|
|
return json_encode(['status' => 1, 'count' => $query]); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* List messages with correspondent |
|
312
|
|
|
* @param $cor_id |
|
313
|
|
|
* @throws JsonException |
|
314
|
|
|
*/ |
|
315
|
|
|
public function actionMessageList($cor_id) |
|
316
|
|
|
{ |
|
317
|
|
|
if (!App::$User->isAuth()) { |
|
318
|
|
|
throw new JsonException('Auth required'); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
if (!Obj::isLikeInt($cor_id) || $cor_id < 1) { |
|
322
|
|
|
throw new JsonException('Corresponded id is wrong'); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
// get special types for this action |
|
326
|
|
|
$queryType = App::$Request->get('type'); |
|
327
|
|
|
$queryId = (int)App::$Request->get('id'); |
|
328
|
|
|
// get current user object |
|
329
|
|
|
$user = App::$User->identity(); |
|
330
|
|
|
|
|
331
|
|
|
if (Arr::in($queryType, ['before', 'after']) && (!Obj::isLikeInt($queryId) || $queryId < 1)) { |
|
332
|
|
|
throw new JsonException('Bad input data'); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
$messages = null; |
|
|
|
|
|
|
336
|
|
|
// sounds like a Hindi code, but we need more closures to organize where conditions |
|
337
|
|
|
// after raw: select * from `ffcms_messages` where `id` > ? and ((`target_id` = ? and `sender_id` = ?) or (`target_id` = ? and `sender_id` = ?)) order by `created_at` desc |
|
|
|
|
|
|
338
|
|
|
// before raw: select * from `ffcms_messages` where (`target_id` = ? and `sender_id` = ?) or (`target_id` = ? and `sender_id` = ?) order by `created_at` desc |
|
339
|
|
|
// default raw: select * from `ffcms_messages` where `id` < ? and ((`target_id` = ? and `sender_id` = ?) or (`target_id` = ? and `sender_id` = ?)) order by `created_at` desc |
|
|
|
|
|
|
340
|
|
|
switch ($queryType) { |
|
341
|
|
View Code Duplication |
case 'after': |
|
|
|
|
|
|
342
|
|
|
$messages = Message::where('id', '>', $queryId) |
|
343
|
|
|
->where(function ($query) use ($cor_id, $user) { |
|
344
|
|
|
$query->where(function ($q) use ($cor_id, $user){ |
|
345
|
|
|
$q->where('target_id', '=', $user->getId()) |
|
346
|
|
|
->where('sender_id', '=', $cor_id); |
|
347
|
|
|
})->orWhere(function ($q) use ($cor_id, $user){ |
|
348
|
|
|
$q->where('target_id', '=', $cor_id) |
|
349
|
|
|
->where('sender_id', '=', $user->getId()); |
|
350
|
|
|
}); |
|
351
|
|
|
}); |
|
352
|
|
|
break; |
|
353
|
|
View Code Duplication |
case 'before': |
|
|
|
|
|
|
354
|
|
|
$messages = Message::where('id', '<', $queryId) |
|
355
|
|
|
->where(function ($query) use ($cor_id, $user) { |
|
356
|
|
|
$query->where(function ($q) use ($cor_id, $user){ |
|
357
|
|
|
$q->where('target_id', '=', $user->getId()) |
|
358
|
|
|
->where('sender_id', '=', $cor_id); |
|
359
|
|
|
})->orWhere(function ($q) use ($cor_id, $user){ |
|
360
|
|
|
$q->where('target_id', '=', $cor_id) |
|
361
|
|
|
->where('sender_id', '=', $user->getId()); |
|
362
|
|
|
}); |
|
363
|
|
|
}); |
|
364
|
|
|
break; |
|
365
|
|
|
default: |
|
366
|
|
|
$messages = Message::where(function($query) use ($cor_id, $user) { |
|
367
|
|
|
$query->where('target_id', '=', $user->getId()) |
|
368
|
|
|
->where('sender_id', '=', $cor_id); |
|
369
|
|
|
})->orWhere(function($query) use ($cor_id, $user) { |
|
370
|
|
|
$query->where('target_id', '=', $cor_id) |
|
371
|
|
|
->where('sender_id', '=', $user->getId()); |
|
372
|
|
|
}); |
|
373
|
|
|
break; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
// set response header |
|
377
|
|
|
$this->setJsonHeader(); |
|
378
|
|
|
|
|
379
|
|
|
$messages->orderBy('created_at', 'DESC') |
|
380
|
|
|
->take(self::MSG_TEXT_LIST); |
|
381
|
|
|
|
|
382
|
|
|
// check if messages exist |
|
383
|
|
|
if ($messages->count() < 1) { |
|
384
|
|
|
return json_encode(['status' => 0, 'text' => 'No messages']); |
|
385
|
|
|
return; |
|
|
|
|
|
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
// build response |
|
389
|
|
|
$response = null; |
|
390
|
|
|
foreach ($messages->get() as $msg) { |
|
391
|
|
|
$response[] = [ |
|
392
|
|
|
'id' => $msg->id, |
|
393
|
|
|
'my' => $msg->sender_id === $user->id, |
|
|
|
|
|
|
394
|
|
|
'message' => App::$Security->strip_tags($msg->message), |
|
395
|
|
|
'date' => Date::convertToDatetime($msg->created_at, Date::FORMAT_TO_SECONDS), |
|
396
|
|
|
'readed' => $msg->readed |
|
397
|
|
|
]; |
|
398
|
|
|
// update status to readed |
|
399
|
|
|
if ($msg->readed !== 1 && $msg->sender_id !== $user->id) { |
|
|
|
|
|
|
400
|
|
|
$msg->readed = 1; |
|
401
|
|
|
$msg->save(); |
|
402
|
|
|
} |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
return json_encode([ |
|
406
|
|
|
'status' => 1, |
|
407
|
|
|
'data' => array_reverse($response), |
|
408
|
|
|
'blocked' => !Blacklist::check($user->id, $cor_id) |
|
|
|
|
|
|
409
|
|
|
]); |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* Send message via AJAX |
|
414
|
|
|
* @param $target_id |
|
415
|
|
|
* @throws JsonException |
|
416
|
|
|
*/ |
|
417
|
|
|
public function actionMessagesend($target_id) |
|
418
|
|
|
{ |
|
419
|
|
|
// check if user is auth |
|
420
|
|
|
if (!App::$User->isAuth()) { |
|
421
|
|
|
throw new JsonException('Auth required'); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
// get current user object |
|
425
|
|
|
$user = App::$User->identity(); |
|
426
|
|
|
|
|
427
|
|
|
if (!Blacklist::check($user->id, $target_id)) { |
|
|
|
|
|
|
428
|
|
|
throw new JsonException('In blacklist'); |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
// check input params |
|
432
|
|
|
$msg = App::$Security->strip_tags(App::$Request->get('message')); |
|
433
|
|
|
if (!Obj::isLikeInt($target_id) || $target_id < 1 || Str::length($msg) < 1) { |
|
|
|
|
|
|
434
|
|
|
throw new JsonException('Wrong input data'); |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
$this->setJsonHeader(); |
|
438
|
|
|
|
|
439
|
|
|
// try to save message |
|
440
|
|
|
$message = new Message(); |
|
441
|
|
|
$message->target_id = $target_id; |
|
442
|
|
|
$message->sender_id = $user->id; |
|
|
|
|
|
|
443
|
|
|
$message->message = $msg; |
|
444
|
|
|
$message->save(); |
|
445
|
|
|
|
|
446
|
|
|
return json_encode(['status' => 1]); |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
public function actionChangerating() |
|
450
|
|
|
{ |
|
451
|
|
|
if (!App::$User->isAuth()) { |
|
452
|
|
|
throw new JsonException('Auth required'); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
$this->setJsonHeader(); |
|
456
|
|
|
|
|
457
|
|
|
// get operation type and target user id |
|
458
|
|
|
$target_id = (int)App::$Request->get('target'); |
|
459
|
|
|
$type = App::$Request->get('type'); |
|
460
|
|
|
|
|
461
|
|
|
// check type of query |
|
462
|
|
|
if ($type !== '+' && $type !== '-') { |
|
463
|
|
|
throw new JsonException('Wrong data'); |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
// check if passed user id is exist |
|
467
|
|
View Code Duplication |
if (!Obj::isLikeInt($target_id) || $target_id < 1 || !App::$User->isExist($target_id)) { |
|
|
|
|
|
|
468
|
|
|
throw new JsonException('Wrong user info'); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
$cfg = \Apps\ActiveRecord\App::getConfigs('app', 'Profile'); |
|
472
|
|
|
// check if rating is enabled for website |
|
473
|
|
|
if ((int)$cfg['rating'] !== 1) { |
|
474
|
|
|
throw new JsonException('Rating is disabled'); |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
// get target and sender objects |
|
478
|
|
|
$target = App::$User->identity($target_id); |
|
479
|
|
|
$sender = App::$User->identity(); |
|
480
|
|
|
|
|
481
|
|
|
// disable self-based changes ;) |
|
482
|
|
|
if ($target->getId() === $sender->getId()) { |
|
483
|
|
|
throw new JsonException('Self change prevented'); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
// check delay |
|
487
|
|
|
$diff = Date::convertToTimestamp(time() - $cfg['ratingDelay'], Date::FORMAT_SQL_TIMESTAMP); |
|
|
|
|
|
|
488
|
|
|
|
|
489
|
|
|
$query = ProfileRating::where('target_id', '=', $target->getId()) |
|
490
|
|
|
->where('sender_id', '=', $sender->getId()) |
|
491
|
|
|
->where('created_at', '>=', $diff) |
|
492
|
|
|
->orderBy('id', 'DESC'); |
|
493
|
|
|
if ($query !== null && $query->count() > 0) { |
|
494
|
|
|
throw new JsonException('Delay required'); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
// delay is ok, lets insert a row |
|
498
|
|
|
$record = new ProfileRating(); |
|
499
|
|
|
$record->target_id = $target->getId(); |
|
500
|
|
|
$record->sender_id = $sender->getId(); |
|
501
|
|
|
$record->type = $type; |
|
502
|
|
|
$record->save(); |
|
503
|
|
|
|
|
504
|
|
|
// update target profile |
|
505
|
|
|
$profile = $target->getProfile(); |
|
506
|
|
|
if ($type === '+') { |
|
507
|
|
|
$profile->rating += 1; |
|
508
|
|
|
} else { |
|
509
|
|
|
$profile->rating -= 1; |
|
510
|
|
|
} |
|
511
|
|
|
$profile->save(); |
|
512
|
|
|
|
|
513
|
|
|
return json_encode(['status' => 1, 'data' => 'ok']); |
|
514
|
|
|
} |
|
515
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.