Repository navigation
Expand file tree
/
Copy pathApi_Group_InviteController.php
More file actions
256 lines (209 loc) · 8.63 KB
/
Copy pathApi_Group_InviteController.php
File metadata and controls
256 lines (209 loc) · 8.63 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Created by PhpStorm.
* User: childeYin<尹少爷>
* Date: 20/07/2018
* Time: 4:11 PM
*/
class Api_Group_InviteController extends Api_Group_BaseController
{
private $classNameForRequest = '\Zaly\Proto\Site\ApiGroupInviteRequest';
private $classNameForResponse = '\Zaly\Proto\Site\ApiGroupInviteResponse';
public $userId;
public $defaultMaxGroupMembers = -1;
public function rpcRequestClassName()
{
return $this->classNameForRequest;
}
/**
* @param \Zaly\Proto\Site\ApiGroupInviteRequest $request
* @param \Google\Protobuf\Internal\Message $transportData
*/
public function rpc(\Google\Protobuf\Internal\Message $request, \Google\Protobuf\Internal\Message $transportData)
{
///处理request,
$tag = __CLASS__ . '-' . __FUNCTION__;
try {
$groupId = $request->getGroupId();
$userIds = $request->getUserIds();
if (!$groupId) {
$errorCode = $this->zalyError->errorGroupIdExists;
$errorInfo = $this->zalyError->getErrorInfo($errorCode);
$this->setRpcError($errorCode, $errorInfo);
throw new Exception($errorInfo);
}
$userList = [];
foreach ($userIds as $key => $val) {
$userList[] = $val;
}
if (!$userIds) {
$errorCode = $this->zalyError->errorUserIdExists;
$errorInfo = $this->zalyError->getErrorInfo($errorCode);
$this->setRpcError($errorCode, $errorInfo);
throw new Exception($errorInfo);
}
//获取群组资料信息
$groupInfo = $this->getGroupInfo($groupId);
//TODO 判断当前群组 进人规则.只能群主拉人,需要判断是不是群主
//// 成员拉人,判断拉人者 是不是该群成员
switch ($groupInfo['permissionJoin']) {
case \Zaly\Proto\Core\GroupJoinPermissionType::GroupJoinPermissionAdmin:
$this->isGroupAdmin($groupId);
break;
case \Zaly\Proto\Core\GroupJoinPermissionType::GroupJoinPermissionMember:
$this->isGroupMember($groupId);
break;
}
// TODO 判断群中人数
$groupUserCount = $this->getGroupUserCount($groupId);
$siteMaxGroupMembers = $groupInfo['maxMembers'];
if ($siteMaxGroupMembers == $this->defaultMaxGroupMembers) {
$siteConfigObj = $this->ctx->SiteConfig;
$siteConfig = $this->ctx->SiteConfigTable->selectSiteConfig($siteConfigObj::SITE_MAX_GROUP_MEMBERS);
$siteMaxGroupMembers = $siteConfig[$siteConfigObj::SITE_MAX_GROUP_MEMBERS];
}
$newGroupUserCount = $groupUserCount + count($userIds);
if ($siteMaxGroupMembers <= $groupUserCount || $siteMaxGroupMembers < $newGroupUserCount) {
$errorCode = $this->zalyError->errorGroupMemberCount;
$errorInfo = $this->zalyError->getErrorInfo($errorCode);
$this->setRpcError($errorCode, $errorInfo);
throw new Exception($errorInfo);
}
/// 公开的直接进入
$this->addMemberToGroup($userList, $groupId);
$this->setRpcError($this->defaultErrorCode, "");
$this->rpcReturn($transportData->getAction(), new $this->classNameForResponse());
$this->finish_request();
//更新群头像
if ($groupUserCount < 9) {
$this->updateGroupAvatar($groupId);
}
//代码入群消息
$this->proxyNewGroupMemberMessage($this->userId, $groupId, $userList);
//代发群组公告notice
//proxy send group-description notice to group
$this->proxyGroupDescriptionNotice($this->userId, $groupInfo, $userList);
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex->getMessage());
$this->rpcReturn($transportData->getAction(), new $this->classNameForResponse());
}
}
private function addMemberToGroup($userIds, $groupId)
{
$tag = __CLASS__ . "_" . __FUNCTION__;
try {
if (!$userIds) {
$errorCode = $this->zalyError->errorUserIdExists;
$errorInfo = $this->zalyError->getErrorInfo($errorCode);
$this->setRpcError($errorCode, $errorInfo);
throw new Exception($errorInfo);
}
$existsUserId = $this->ctx->SiteGroupUserTable->getUserIdExistInGroup($userIds, $groupId);
$notExistsUserId = $userIds;
if ($existsUserId) {
$notExistsUserId = array_diff($userIds, $existsUserId);
}
if (!count($notExistsUserId)) {
return true;
}
//$groupPointer
$groupPointer = $this->ctx->SiteGroupMessageTable->queryMaxIdByGroup($groupId);
$this->ctx->BaseTable->db->beginTransaction();
foreach ($notExistsUserId as $userId) {
//insert group Message pointer
// $groupMessagePointerInfo = [];
$this->ctx->SiteGroupMessageTable->updatePointer($groupId, $userId, "", $groupPointer);
//insert into siteGroupUser
$groupUserInfo = [
'groupId' => $groupId,
'userId' => $userId,
'memberType' => \Zaly\Proto\Core\GroupMemberType::GroupMemberNormal,
'timeJoin' => $this->ctx->ZalyHelper->getMsectime()
];
$this->ctx->BaseTable->insertData($this->ctx->SiteGroupUserTable->table, $groupUserInfo, $this->ctx->SiteGroupUserTable->columns);
}
$this->ctx->BaseTable->db->commit();
} catch (Exception $ex) {
$this->ctx->BaseTable->db->rollback();
$this->ctx->Wpf_Logger->error($tag, $ex->getMessage());
$errorCode = $this->zalyError->errorGroupInvite;
$errorInfo = $this->zalyError->getErrorInfo($errorCode);
$this->setRpcError($errorCode, $errorInfo);
throw new Exception("invite failed");
}
}
//currentUserId ==proxy send to ==>userIds
//"description",
//"descriptionType",
private function proxyGroupDescriptionNotice($currentUserId, $groupInfo, $userIds)
{
$groupId = $groupInfo['groupId'];
$desc = $groupInfo['description'];
$descType = $groupInfo['descriptionType'];
if (empty($desc) || Zaly\Proto\Core\GroupDescriptionType::GroupDescriptionMarkdown == $descType) {
return false;
}
$desc = "[群介绍]" . $desc;
foreach ($userIds as $memberId) {
$this->ctx->Message_Client->proxyGroupAsU2NoticeMessage($memberId, $currentUserId, $groupId, $desc);
}
}
//proxy join group members Message
/**
* @param $currentUserId
* @param $groupId
* @param $userIds
*/
private function proxyNewGroupMemberMessage($currentUserId, $groupId, $userIds)
{
$fromUserId = $currentUserId;
$msgType = Zaly\Proto\Core\MessageType::MessageNotice;
$noticeText = $this->buildUserNotice($fromUserId, $userIds);
if (empty($noticeText)) {
return;
}
$this->ctx->Message_Client->proxyGroupNoticeMessage($fromUserId, $groupId, $noticeText);
}
private function buildUserNotice($fromUserId, $userIds)
{
if (empty($userIds)) {
return "";
}
$nameBody = "";
if (isset($fromUserId)) {
$name = $this->getUserName($fromUserId);
if ($name) {
$nameBody .= $name . ZalyText::$keyGroupInvite;
}
}
foreach ($userIds as $num => $userId) {
$name = $this->getUserName($userId);
if ($name) {
if ($num == 0) {
$nameBody .= $name;
} else {
$nameBody .= "," . $name;
}
}
}
$nameBody .= ZalyText::$keyGroupJoin;
return $nameBody;
}
/**
* @param $userId
* @return null
*/
private function getUserName($userId)
{
$userInfo = $this->ctx->SiteUserTable->getUserByUserId($userId);
if (!empty($userInfo)) {
$userName = $userInfo['nickname'];
if (empty($userName)) {
$userName = $userInfo['loginName'];
}
return $userName;
} else {
return null;
}
}
}