|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.me/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
|
10
|
|
|
* @license https://vistart.me/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace rhosocial\base\models\models; |
|
14
|
|
|
|
|
15
|
|
|
use MongoDB\BSON\Binary; |
|
16
|
|
|
use rhosocial\base\models\queries\BaseMongoMessageQuery; |
|
17
|
|
|
use rhosocial\base\models\queries\BaseUserQuery; |
|
18
|
|
|
use rhosocial\base\models\traits\MessageTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @version 1.0 |
|
22
|
|
|
* @author vistart <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class BaseMongoMessageModel extends BaseMongoBlameableModel |
|
25
|
|
|
{ |
|
26
|
|
|
use MessageTrait; |
|
27
|
|
|
|
|
28
|
|
|
public $updatedByAttribute = false; |
|
29
|
|
|
public $expiredAt = 604800; // 7 days. |
|
30
|
|
|
|
|
31
|
2 |
|
public function init() |
|
32
|
|
|
{ |
|
33
|
2 |
|
if (!is_string($this->queryClass) || empty($this->queryClass)) { |
|
34
|
2 |
|
$this->queryClass = BaseMongoMessageQuery::class; |
|
35
|
|
|
} |
|
36
|
2 |
|
if ($this->skipInit) { |
|
37
|
2 |
|
return; |
|
38
|
|
|
} |
|
39
|
2 |
|
$this->initMessageEvents(); |
|
40
|
2 |
|
parent::init(); |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get recipient. |
|
45
|
|
|
* @return BaseUserQuery |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getRecipient() |
|
48
|
|
|
{ |
|
49
|
|
|
if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
$hostClass = $this->hostClass; |
|
53
|
|
|
$model = $hostClass::buildNoInitModel(); |
|
54
|
|
|
return $this->hasOne($hostClass::className(), [$model->guidAttribute => 'otherAttribute']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get updated_by attribute. |
|
59
|
|
|
* @return string|null |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getOtherAttribute() |
|
62
|
|
|
{ |
|
63
|
|
|
$updatedByAttribute = $this->updatedByAttribute; |
|
64
|
|
|
return (!is_string($updatedByAttribute) || empty($updatedByAttribute)) ? null : $this->$updatedByAttribute->getData(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set recipient. |
|
69
|
|
|
* @param BaseUserModel $user |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function setRecipient($user) |
|
73
|
|
|
{ |
|
74
|
2 |
|
if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) { |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
2 |
|
if ($user instanceof BaseUserModel) { |
|
78
|
2 |
|
$user = $user->getGUID(); |
|
79
|
|
|
} |
|
80
|
2 |
|
$otherGuidAttribute = $this->otherGuidAttribute; |
|
81
|
2 |
|
return $this->$otherGuidAttribute = new Binary($user, Binary::TYPE_UUID); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get mutual attributes rules. |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function getMutualRules() |
|
89
|
|
|
{ |
|
90
|
2 |
|
$rules = []; |
|
91
|
2 |
|
if (is_string($this->otherGuidAttribute)) { |
|
92
|
|
|
$rules = [ |
|
93
|
2 |
|
[$this->otherGuidAttribute, 'required'], |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
2 |
|
return $rules; |
|
97
|
|
|
} |
|
98
|
|
|
} |