优化群聊操作

This commit is contained in:
kuaifan
2022-04-14 22:50:39 +08:00
parent fcd6b1ddec
commit 6ca13bf263
6 changed files with 205 additions and 52 deletions

View File

@@ -84,7 +84,10 @@ class WebSocketDialog extends AbstractModel
public function joinGroup($userid)
{
if ($this->type !== 'group') {
return false;
throw new ApiException('此操作仅限群组');
}
if ($this->group_type !== 'user') {
throw new ApiException('此操作仅限个人群组');
}
AbstractModel::transaction(function () use ($userid) {
foreach (is_array($userid) ? $userid : [$userid] as $value) {
@@ -106,6 +109,12 @@ class WebSocketDialog extends AbstractModel
*/
public function exitGroup($userid)
{
if ($this->type !== 'group') {
throw new ApiException('此操作仅限群组');
}
if ($this->group_type !== 'user') {
throw new ApiException('此操作仅限个人群组');
}
$builder = WebSocketDialogUser::whereDialogId($this->id);
if (is_array($userid)) {
$builder->whereIn('userid', $userid);
@@ -125,6 +134,29 @@ class WebSocketDialog extends AbstractModel
return true;
}
/**
* 解散群组
* @return bool
*/
public function disbandGroup()
{
if ($this->type !== 'group') {
throw new ApiException('此操作仅限群组');
}
if ($this->group_type !== 'user') {
throw new ApiException('此操作仅限个人群组');
}
return AbstractModel::transaction(function() {
WebSocketDialogUser::whereDialogId($this->id)->chunkById(100, function($list) {
/** @var WebSocketDialogUser $item */
foreach ($list as $item) {
$item->delete();
}
});
return $this->delete();
});
}
/**
* 获取对话(同时检验对话身份)
* @param $dialog_id
@@ -148,7 +180,7 @@ class WebSocketDialog extends AbstractModel
}
}
if (!WebSocketDialogUser::whereDialogId($dialog->id)->whereUserid($userid)->exists()) {
throw new ApiException('不在成员列表内');
throw new ApiException('不在成员列表内', ['dialog_id' => $dialog_id], -4003);
}
return $dialog;
}