1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api\Profile; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Blacklist; |
6
|
|
|
use Apps\ActiveRecord\Message; |
7
|
|
|
use Ffcms\Core\App; |
8
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
9
|
|
|
use Ffcms\Core\Helper\Type\Any; |
10
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
11
|
|
|
use Ffcms\Core\Network\Request; |
12
|
|
|
use Ffcms\Core\Network\Response; |
13
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait ActionListMessageDialog |
17
|
|
|
* @package Apps\Controller\Api\Profile |
18
|
|
|
* @property Request $request |
19
|
|
|
* @property Response $response |
20
|
|
|
* @method void setJsonHeader |
21
|
|
|
*/ |
22
|
|
|
trait ActionListMessageDialog |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Load user dialog list based on offset |
26
|
|
|
* @param int $offset |
27
|
|
|
* @param int $new |
28
|
|
|
* @return string |
29
|
|
|
* @throws ForbiddenException |
30
|
|
|
*/ |
31
|
|
|
public function listMessageDialog($offset = 0, $new = 0): ?string |
32
|
|
|
{ |
33
|
|
|
// check is user auth |
34
|
|
|
if (!App::$User->isAuth()) { |
35
|
|
|
throw new ForbiddenException('Auth required'); |
36
|
|
|
} |
37
|
|
|
$this->setJsonHeader(); |
38
|
|
|
|
39
|
|
|
// check is offset is int |
40
|
|
|
if ($offset !== 0 && !Any::isInt($offset)) { |
41
|
|
|
$offset = 0; |
42
|
|
|
} |
43
|
|
|
++$offset; |
44
|
|
|
|
45
|
|
|
// get user person |
46
|
|
|
$user = App::$User->identity(); |
47
|
|
|
|
48
|
|
|
$records = Message::select('readed', 'target_id', 'sender_id', Capsule::raw('max(created_at) as cmax')) |
|
|
|
|
49
|
|
|
->where('target_id', '=', $user->id) |
50
|
|
|
->orWhere('sender_id', '=', $user->id) |
51
|
|
|
->orderBy('readed', 'ASC') //- error happens, cuz readed is boolean in pgsql |
52
|
|
|
->orderBy('cmax', 'DESC') |
53
|
|
|
->groupBy(['sender_id', 'target_id', 'readed']) // multiple order's can throw exception on some kind of database engines |
54
|
|
|
->take($offset * self::MSG_USER_LIST) |
|
|
|
|
55
|
|
|
->get(); |
56
|
|
|
|
57
|
|
|
$userList = []; |
58
|
|
|
$unreadList = []; |
59
|
|
|
|
60
|
|
|
if (Any::isInt($new) && $new > 0 && App::$User->isExist($new)) { |
61
|
|
|
$userList[] = $new; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$records->each(function($row) use (&$userList, $user){ |
65
|
|
|
// target is not myself? then i'm - sender (remote user is target: my->to_user) |
66
|
|
|
if ($row->target_id !== $user->id) { |
67
|
|
|
$userList[] = $row->target_id; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// sender is not myself? then i'm - target (remote user is sender user->to_me) |
71
|
|
|
if ($row->sender_id !== $user->id) { |
72
|
|
|
$userList[] = $row->sender_id; |
73
|
|
|
if ((bool)$row->readed !== true) { |
74
|
|
|
$unreadList[] = $row->sender_id; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
// store only unique users in dialog |
80
|
|
|
$userList = array_unique($userList, SORT_NUMERIC); |
81
|
|
|
// generate json response based on userList and unreadList |
82
|
|
|
$response = []; |
83
|
|
|
foreach ($userList as $user_id) { |
84
|
|
|
$identity = App::$User->identity($user_id); |
85
|
|
|
if (!$identity) { |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$response[] = [ |
90
|
|
|
'user_id' => $user_id, |
91
|
|
|
'user_nick' => $identity->profile->getNickname(), |
92
|
|
|
'user_avatar' => $identity->profile->getAvatarUrl('small'), |
93
|
|
|
'message_new' => Arr::in($user_id, $unreadList), |
94
|
|
|
'user_block' => !Blacklist::check($user->id, $identity->id) |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return json_encode(['status' => 1, 'data' => $response]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.