forked from johnnylei/message_system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageManager.php
226 lines (192 loc) · 7.07 KB
/
MessageManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* Created by PhpStorm.
* User: johnny
* Date: 17-1-4
* Time: 下午12:01
*/
namespace johnnylei\message_system;
use yii\data\Pagination;
use johnnylei\message_system\Message;
use yii\base\Component;
use yii\base\Exception;
use Yii;
class MessageManager extends Component
{
public $userInformation;
const CreateTimeDesc = 1;
const CreateTimeAsc = 2;
const BeforeSendMessage = 'BeforeSendMessage';
const AfterSendMessage = 'afterSendMessage';
const SystemMessageQueue = 'SystemMessageQueue'; // 系统消息队列
public function beforeSendMessage($event) {
$this->trigger(self::BeforeSendMessage, $event);
return $event->isValidate;
}
public function afterSendMessage($event) {
$this->trigger(self::AfterSendMessage, $event);
return $event->isValidate;
}
/**
* @param $message ['title'=>'xxxx', 'body'=>'ssss', 'type'=>xxx, '']
* @param $queue
* @throws Exception
*/
public function send($message, $queue, $enableAddMessageNumber = true) {
if(!is_array($message)) {
throw new Exception('$message should be array');
}
if (!isset($message['title']) || empty($message['title'])) {
throw new Exception('$message title be set');
}
if (!isset($message['body']) || empty($message['body'])) {
throw new Exception('$message body be set');
}
if (!is_array($queue)) {
$queue = [$queue];
}
$transaction = Yii::$app->db->beginTransaction();
try {
$event = new MessageEvent($message);
if(!$this->beforeSendMessage($event)) {
throw new Exception('before send message failed');
}
$formData = $message + [
'create_time'=>time(),
];
// 生成消息体
$messageRecord = new Message();
if(!$messageRecord->insertRecord($formData)) {
throw new Exception('insert message failed');
}
// 获取订阅这个消息队列的用户
$subscription = new MessageQueueSubscription();
$subscribers = [];
foreach ($queue as $item) {
$subscribers = array_merge($subscribers, $subscription->getSubscriber($item));
}
// 分发消息
$messageUserMap = new MessageUserMap();
if ($enableAddMessageNumber) {
$messageUserMap->on(MessageUserMap::AfterInsertMessage, [$this, 'addMessageNumber']);
}
foreach ($subscribers as $subscriber) {
if(!$messageUserMap->insertRecord([
'user_id'=>$subscriber,
'message_id'=>$messageRecord->id,
])) {
throw new Exception('send message failed');
}
}
if(!$this->afterSendMessage($event)) {
throw new Exception('after send message failed');
}
}catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
$transaction->commit();
return true;
}
public function myMessageList($order_by = null, $page_size = 10) {
$query = Message::find()->select([
't1.*',
't2.checked',
't2.checked_time',
't2.id primary_id'
])->alias('t1')
->leftJoin(BaseRecord::MessageUserMap. ' t2', 't1.id = t2.message_id')
->andWhere(['t2.user_id'=>Yii::$app->getUser()->getId()]);
if(!empty($this->pagination)) {
$pagination = Yii::createObject($this->pagination);
$pagination->totalCount = $query->count();
$pagination->pageSize = $page_size;
} else {
$pagination = new Pagination([
'totalCount'=>$query->count(),
'pageSize'=>$page_size,
]);
}
$map = [
self::CreateTimeAsc => [
't1.create_time'=>SORT_ASC,
],
self::CreateTimeDesc => [
't1.create_time'=>SORT_DESC
]
];
$_order_by = empty($order_by)?[
't2.id'=>SORT_DESC
]:$map[$order_by];
$list = $query->orderBy($_order_by)->offset($pagination->offset)->limit($pagination->limit)->asArray()->all();
return [
'list'=>$list,
'pagination'=>$pagination,
];
}
public function checkMessage($message_id) {
$messageUserMap = new MessageUserMap();
$messageUserMap->on(MessageUserMap::AfterCheckMessage, [$this, 'minusMessageNumber']);
return $messageUserMap->checkMessage($message_id, Yii::$app->getUser()->getId());
}
public function deleteMessage($message_id) {
$messageUserMap = new MessageUserMap();
$messageUserMap->on(MessageUserMap::AfterDeleteMessage, [$this, 'checkMessageValid']);
$messageUserMap->on(MessageUserMap::AfterDeleteMessage, [$this, 'minusMessageNumber']);
return $messageUserMap->deleteMessage($message_id, Yii::$app->getUser()->getId());
}
/**
* 查看一下这个消息体不是否有效,无效的消息是指所有人都删除的消息
* @param $e
*/
public function checkMessageValid($e) {
$message_id = $e->message['message_id'];
$record = MessageUserMap::find()->andWhere(['message_id'=>$message_id])->one();
if(!empty($record)) {
return true;
}
$message = new Message();
$message->deleteRecord($message_id);
}
public function addMessageNumber($e) {
if(empty($this->userInformation)) {
return ;
}
$userInformation = Yii::createObject($this->userInformation);
if(!$userInformation instanceof UserInformationInterface) {
$e->isValidate = false;
throw new Exception('invalid user information');
}
if(!$userInformation->addMessageNumber($e->message['user_id'])) {
$e->isValidate = false;
}
}
public function minusMessageNumber($e) {
if(empty($this->userInformation)) {
return ;
}
$userInformation = Yii::createObject($this->userInformation);
if(!$userInformation instanceof UserInformationInterface) {
$e->isValidate = false;
throw new Exception('invalid user information');
}
if(!$userInformation->minusMessageNumber($e->message['user_id'])) {
$e->isValidate = false;
}
}
public function receive() {
$query = Message::find()->select([
't1.*',
't2.checked',
't2.checked_time',
't2.id primary_id'
])->alias('t1')
->leftJoin(BaseRecord::MessageUserMap . ' t2', 't1.id = t2.message_id')
->andWhere([
'and',
['t2.user_id'=>Yii::$app->getUser()->getId()],
['t2.checked'=>MessageUserMap::UnChecked]
]);
return $query->orderBy(['t1.id'=>SORT_DESC])->asArray()->one();
}
}