1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api\Profile; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Message; |
6
|
|
|
use Apps\ActiveRecord\UserNotification; |
7
|
|
|
use Ffcms\Core\App; |
8
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
9
|
|
|
use Ffcms\Core\Network\Request; |
10
|
|
|
use Ffcms\Core\Network\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait ActionNotifications |
14
|
|
|
* @package Apps\Controller\Api\Profile |
15
|
|
|
* @property Request $request |
16
|
|
|
* @property Response $response |
17
|
|
|
* @method void setJsonHeader() |
18
|
|
|
*/ |
19
|
|
|
trait ActionNotifications |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get user p.m and notifications count |
23
|
|
|
* @return string |
24
|
|
|
* @throws ForbiddenException |
25
|
|
|
*/ |
26
|
|
|
public function notifications(): ?string |
27
|
|
|
{ |
28
|
|
|
// check if authed |
29
|
|
|
if (!App::$User->isAuth()) { |
30
|
|
|
throw new ForbiddenException('Auth required'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->setJsonHeader(); |
34
|
|
|
|
35
|
|
|
// get user object |
36
|
|
|
$user = App::$User->identity(); |
37
|
|
|
|
38
|
|
|
// get messages count |
39
|
|
|
$messagesCount = Message::where('target_id', '=', $user->id) |
40
|
|
|
->where('readed', '=', 0)->count(); |
41
|
|
|
|
42
|
|
|
// get notifications count |
43
|
|
|
$notificationsCount = UserNotification::where('user_id', '=', $user->id) |
44
|
|
|
->where('readed', '=', 0)->count(); |
45
|
|
|
|
46
|
|
|
// render json output |
47
|
|
|
return json_encode([ |
48
|
|
|
'status' => 1, |
49
|
|
|
'notify' => $notificationsCount, |
50
|
|
|
'messages' => $messagesCount, |
51
|
|
|
'summary' => $notificationsCount + $messagesCount |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|