1 | <?php |
||
2 | |||
3 | namespace Apps\Model\Front\Profile; |
||
4 | |||
5 | use Apps\ActiveRecord\WallPost as WallRecords; |
||
6 | use Ffcms\Core\App; |
||
7 | use Ffcms\Core\Arch\Model; |
||
8 | use Ffcms\Core\Helper\Date; |
||
9 | use Ffcms\Core\Helper\Text; |
||
10 | use Ffcms\Core\Helper\Type\Any; |
||
11 | use Ffcms\Core\Helper\Type\Obj; |
||
12 | use Ffcms\Core\Interfaces\iUser; |
||
13 | |||
14 | /** |
||
15 | * Class FormWallPost. Add wall post business logic model |
||
16 | * @package Apps\Model\Front\Profile |
||
17 | */ |
||
18 | class FormWallPost extends Model |
||
19 | { |
||
20 | const MAX_MESSAGE_LENGTH = 5000; // 5000 symbols |
||
21 | const POST_GLOBAL_DELAY = 30; // delay between 2 posts from 1 user in seconds |
||
22 | |||
23 | public $message; |
||
24 | |||
25 | /** |
||
26 | * Validate rules for message field |
||
27 | * @return array |
||
28 | */ |
||
29 | public function rules(): array |
||
30 | { |
||
31 | return [ |
||
32 | ['message', 'required', null, true, true], |
||
33 | ['message', 'length_min', 5, null, true, true], |
||
34 | ['message', 'length_max', self::MAX_MESSAGE_LENGTH, null, true, true] |
||
35 | ]; |
||
36 | } |
||
37 | |||
38 | public function types(): array |
||
39 | { |
||
40 | return [ |
||
41 | 'message' => 'html' |
||
42 | ]; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Make post to user wall from $viewer to $target instance of iUser interface objects |
||
47 | * @param iUser $target |
||
48 | * @param iUser $viewer |
||
49 | * @param int $delay |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function makePost(iUser $target, iUser $viewer, $delay = 60) |
||
53 | { |
||
54 | if (!$target || !$viewer) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
55 | return false; |
||
56 | } |
||
57 | |||
58 | if (!Any::isInt($delay) || $delay < 0) { |
||
59 | $delay = static::POST_GLOBAL_DELAY; |
||
60 | } |
||
61 | |||
62 | $find = WallRecords::where('sender_id', '=', $viewer->id) |
||
63 | ->orderBy('updated_at', 'desc') |
||
64 | ->first(); |
||
65 | if (!$find) { |
||
66 | $lastPostTime = Date::convertToTimestamp($find->updated_at); |
||
67 | if (time() - $lastPostTime < $delay) { // break execution, passed time is less then default delay |
||
68 | return false; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | // save new post to db |
||
73 | $record = new WallRecords(); |
||
74 | $record->target_id = $target->id; |
||
75 | $record->sender_id = $viewer->id; |
||
76 | $record->message = $this->message; |
||
77 | $record->save(); |
||
78 | |||
79 | // add user notification |
||
80 | if ($target->id !== $viewer->id) { |
||
81 | $notify = new EntityAddNotification($target->id); |
||
82 | $notify->add('profile/show/' . $target->id . '#wall-post-' . $record->id, EntityAddNotification::MSG_ADD_WALLPOST, ['snippet' => Text::snippet($this->message, 50)]); |
||
83 | } |
||
84 | |||
85 | // cleanup message |
||
86 | $this->message = null; |
||
87 | return true; |
||
88 | } |
||
89 | } |
||
90 |