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\Exception\NativeException; |
10
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
11
|
|
|
use Ffcms\Core\Helper\Date; |
12
|
|
|
use Ffcms\Core\Helper\Type\Any; |
13
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
14
|
|
|
use Ffcms\Core\Network\Request; |
15
|
|
|
use Ffcms\Core\Network\Response; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ActionMessageList |
19
|
|
|
* @package Apps\Controller\Api\Profile |
20
|
|
|
* @property Request $request |
21
|
|
|
* @property Response $response |
22
|
|
|
* @method void setJsonHeader |
23
|
|
|
*/ |
24
|
|
|
trait ActionMessageList |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* List messages with correspondent |
28
|
|
|
* @param $corId |
29
|
|
|
* @return string |
30
|
|
|
* @throws ForbiddenException |
31
|
|
|
* @throws NotFoundException |
32
|
|
|
* @throws NativeException |
33
|
|
|
*/ |
34
|
|
|
public function messageList($corId): ?string |
35
|
|
|
{ |
36
|
|
|
if (!App::$User->isAuth()) { |
37
|
|
|
throw new ForbiddenException('Auth required'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!Any::isInt($corId) || $corId < 1) { |
41
|
|
|
throw new NotFoundException('Corresponded id is wrong'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// get special types for this action |
45
|
|
|
$queryType = $this->request->get('type'); |
46
|
|
|
$queryId = (int)$this->request->get('id'); |
47
|
|
|
// get current user object |
48
|
|
|
$user = App::$User->identity(); |
49
|
|
|
|
50
|
|
|
if (Arr::in($queryType, ['before', 'after']) && (!Any::isInt($queryId) || $queryId < 1)) { |
51
|
|
|
throw new NativeException('Bad input data'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$messages = null; |
|
|
|
|
55
|
|
|
// sounds like a Hindi code, but we need more closures to organize where conditions |
56
|
|
|
// 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 |
|
|
|
|
57
|
|
|
// before raw: select * from `ffcms_messages` where (`target_id` = ? and `sender_id` = ?) or (`target_id` = ? and `sender_id` = ?) order by `created_at` desc |
58
|
|
|
// 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 |
|
|
|
|
59
|
|
|
switch ($queryType) { |
60
|
|
|
case 'after': |
|
|
|
|
61
|
|
|
$messages = Message::where('id', '>', $queryId) |
62
|
|
|
->where(function ($query) use ($corId, $user) { |
63
|
|
|
$query->where(function ($q) use ($cor_id, $user) { |
|
|
|
|
64
|
|
|
$q->where('target_id', '=', $user->getId()) |
65
|
|
|
->where('sender_id', '=', $cor_id); |
66
|
|
|
})->orWhere(function ($q) use ($cor_id, $user) { |
67
|
|
|
$q->where('target_id', '=', $cor_id) |
68
|
|
|
->where('sender_id', '=', $user->getId()); |
69
|
|
|
}); |
70
|
|
|
}); |
71
|
|
|
break; |
72
|
|
|
case 'before': |
73
|
|
|
$messages = Message::where('id', '<', $queryId) |
74
|
|
|
->where(function ($query) use ($corId, $user) { |
75
|
|
|
$query->where(function ($q) use ($cor_id, $user) { |
|
|
|
|
76
|
|
|
$q->where('target_id', '=', $user->getId()) |
77
|
|
|
->where('sender_id', '=', $cor_id); |
78
|
|
|
})->orWhere(function ($q) use ($cor_id, $user) { |
79
|
|
|
$q->where('target_id', '=', $cor_id) |
80
|
|
|
->where('sender_id', '=', $user->getId()); |
81
|
|
|
}); |
82
|
|
|
}); |
83
|
|
|
break; |
84
|
|
|
default: |
85
|
|
|
$messages = Message::where(function ($query) use ($corId, $user) { |
86
|
|
|
$query->where('target_id', '=', $user->getId()) |
87
|
|
|
->where('sender_id', '=', $corId); |
88
|
|
|
})->orWhere(function ($query) use ($corId, $user) { |
89
|
|
|
$query->where('target_id', '=', $corId) |
90
|
|
|
->where('sender_id', '=', $user->getId()); |
91
|
|
|
}); |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// set response header |
96
|
|
|
$this->setJsonHeader(); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$messages->orderBy('created_at', 'DESC') |
99
|
|
|
->take(self::MSG_TEXT_LIST); |
100
|
|
|
|
101
|
|
|
// check if messages exist |
102
|
|
|
if ($messages->count() < 1) { |
103
|
|
|
return json_encode(['status' => 0, 'text' => 'No messages']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// build response |
107
|
|
|
$response = null; |
108
|
|
|
foreach ($messages->get() as $msg) { |
109
|
|
|
/** @var Message $msg */ |
110
|
|
|
$response[] = [ |
111
|
|
|
'id' => $msg->id, |
112
|
|
|
'my' => $msg->sender_id === $user->id, |
113
|
|
|
'message' => $msg->message, |
114
|
|
|
'date' => Date::convertToDatetime($msg->created_at, Date::FORMAT_TO_SECONDS), |
115
|
|
|
'readed' => $msg->readed |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
// update status to readed |
119
|
|
|
if ((bool)$msg->readed !== true && $msg->sender_id !== $user->id) { |
120
|
|
|
$msg->readed = true; |
121
|
|
|
$msg->save(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return json_encode([ |
126
|
|
|
'status' => 1, |
127
|
|
|
'data' => array_reverse($response), |
128
|
|
|
'blocked' => !Blacklist::check($user->id, $corId) |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.