1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api\Profile; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\Blacklist; |
6
|
|
|
use Apps\ActiveRecord\WallAnswer; |
7
|
|
|
use Apps\ActiveRecord\WallPost; |
8
|
|
|
use Apps\Model\Front\Profile\EntityAddNotification; |
9
|
|
|
use Ffcms\Core\App; |
10
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
11
|
|
|
use Ffcms\Core\Exception\NativeException; |
12
|
|
|
use Ffcms\Core\Helper\Date; |
13
|
|
|
use Ffcms\Core\Helper\Type\Any; |
14
|
|
|
use Ffcms\Core\Helper\Type\Str; |
15
|
|
|
use Ffcms\Core\Network\Request; |
16
|
|
|
use Ffcms\Core\Network\Response; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait ActionSendWallAnswer |
20
|
|
|
* @package Apps\Controller\Api\Profile |
21
|
|
|
* @property Request $request |
22
|
|
|
* @property Response $response |
23
|
|
|
* @method void setJsonHeader() |
24
|
|
|
*/ |
25
|
|
|
trait ActionSendWallAnswer |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Add new post answer from AJAX post |
29
|
|
|
* @param string $postId |
30
|
|
|
* @return string |
31
|
|
|
* @throws ForbiddenException |
32
|
|
|
* @throws NativeException |
33
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
34
|
|
|
*/ |
35
|
|
|
public function sendWallAnswer(string $postId): ?string |
36
|
|
|
{ |
37
|
|
|
$this->setJsonHeader(); |
38
|
|
|
|
39
|
|
|
// not auth? what are you doing there? ;) |
40
|
|
|
if (!App::$User->isAuth()) { |
41
|
|
|
throw new ForbiddenException('Auth required'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// no post id? wtf you doing man! |
45
|
|
|
if (!Any::isInt($postId) || $postId < 1) { |
46
|
|
|
throw new NativeException('Wrong input data'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// get current(sender) user object |
50
|
|
|
$viewer = App::$User->identity(); |
51
|
|
|
|
52
|
|
|
// get message from post and validate minlength |
53
|
|
|
$message = $this->request->get('message'); |
54
|
|
|
$message = App::$Security->strip_tags($message); |
55
|
|
|
if (!Any::isStr($message) || Str::length($message) < 3) { |
|
|
|
|
56
|
|
|
throw new ForbiddenException('Wrong input data'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// try to find this post |
60
|
|
|
$wallPost = WallPost::where('id', '=', $postId); |
61
|
|
|
if ($wallPost->count() < 1) { |
62
|
|
|
throw new NativeException('Wrong input data'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$wallRow = $wallPost->first(); |
66
|
|
|
$targetId = $wallRow->target_id; |
|
|
|
|
67
|
|
|
// check if in blacklist |
68
|
|
|
if (!Blacklist::check($viewer->id, $targetId)) { |
69
|
|
|
throw new ForbiddenException('User is blocked!'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// check delay between user last post and current |
73
|
|
|
$lastAnswer = WallAnswer::where('user_id', '=', App::$User->identity()->getId()) |
74
|
|
|
->orderBy('created_at', 'DESC') |
75
|
|
|
->first(); |
76
|
|
|
if (!$lastAnswer) { |
77
|
|
|
$now = time(); |
78
|
|
|
$answerTime = Date::convertToTimestamp($lastAnswer->created_at); |
79
|
|
|
$cfgs = \Apps\ActiveRecord\App::getConfigs('app', 'Profile'); |
80
|
|
|
// hmm, maybe past less then delay required? |
81
|
|
|
if ($now - (int)$cfgs['delayBetweenPost'] < $answerTime) { |
82
|
|
|
throw new ForbiddenException('Delay between answers not pass'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// make new row ;) |
87
|
|
|
$answers = new WallAnswer(); |
88
|
|
|
$answers->post_id = $postId; |
89
|
|
|
$answers->user_id = $viewer->id; |
90
|
|
|
$answers->message = $message; |
|
|
|
|
91
|
|
|
$answers->save(); |
92
|
|
|
|
93
|
|
|
// add notification for target user |
94
|
|
|
if ($viewer->id !== $targetId) { |
95
|
|
|
$notify = new EntityAddNotification($targetId); |
96
|
|
|
$notify->add('/profile/show/' . $targetId . '#wall-post-' . $wallRow->id, EntityAddNotification::MSG_ADD_WALLANSWER, [ |
97
|
|
|
'snippet' => Text::snippet($message, 50), |
|
|
|
|
98
|
|
|
'post' => $wallRow->message |
|
|
|
|
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return json_encode(['status' => 1, 'message' => 'ok']); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|