mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-12 11:19:56 +00:00
perf: 优化会话搜索
This commit is contained in:
parent
02654c8327
commit
e149e276d5
@ -137,13 +137,26 @@ class DialogController extends AbstractController
|
|||||||
})->orderBy('userid')
|
})->orderBy('userid')
|
||||||
->take(20 - count($list))
|
->take(20 - count($list))
|
||||||
->get();
|
->get();
|
||||||
$users->transform(function (User $item) {
|
$users->transform(function (User $item) use ($user) {
|
||||||
|
$id = 'u:' . $item->userid;
|
||||||
|
$lastAt = null;
|
||||||
|
$lastMsg = null;
|
||||||
|
$dialog = WebSocketDialog::getUserDialog($user->userid, $item->userid, now()->addDay());
|
||||||
|
if ($dialog) {
|
||||||
|
$id = $dialog->id;
|
||||||
|
$row = WebSocketDialogMsg::whereDialogId($dialog->id)->orderByDesc('id')->first();
|
||||||
|
if ($row) {
|
||||||
|
$lastAt = Carbon::parse($row->created_at)->toDateTimeString();
|
||||||
|
$lastMsg = WebSocketDialog::lastMsgFormat($row->toArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
return [
|
return [
|
||||||
'id' => 'u:' . $item->userid,
|
'id' => $id,
|
||||||
'type' => 'user',
|
'type' => 'user',
|
||||||
'name' => $item->nickname,
|
'name' => $item->nickname,
|
||||||
'dialog_user' => $item,
|
'dialog_user' => $item,
|
||||||
'last_msg' => null,
|
'last_at' => $lastAt,
|
||||||
|
'last_msg' => $lastMsg,
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
$list = array_merge($list, $users->toArray());
|
$list = array_merge($list, $users->toArray());
|
||||||
|
|||||||
@ -250,15 +250,7 @@ class WebSocketDialog extends AbstractModel
|
|||||||
// 最后消息
|
// 最后消息
|
||||||
$data['last_msg'] = $data['last_msg'] ?? WebSocketDialogMsg::whereDialogId($data['id'])->orderByDesc('id')->first()?->toArray();
|
$data['last_msg'] = $data['last_msg'] ?? WebSocketDialogMsg::whereDialogId($data['id'])->orderByDesc('id')->first()?->toArray();
|
||||||
}
|
}
|
||||||
|
$data['last_msg'] = self::lastMsgFormat($data['last_msg']);
|
||||||
// 最后消息处理
|
|
||||||
if ($data['last_msg'] && $data['last_msg']['type'] != 'preview') {
|
|
||||||
$msgData = $data['last_msg'];
|
|
||||||
$msgData['emoji'] = Base::array_only_recursive($msgData['emoji'], ['symbol']);
|
|
||||||
$msgData['msg'] = ['preview' => WebSocketDialogMsg::previewMsg($msgData)];
|
|
||||||
$msgData['type'] = 'preview';
|
|
||||||
$data['last_msg'] = array_intersect_key($msgData, array_flip(['id', 'type', 'msg', 'userid', 'percentage', 'emoji', 'created_at']));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 对方信息
|
// 对方信息
|
||||||
$data['pinyin'] = Base::cn2pinyin($data['name']);
|
$data['pinyin'] = Base::cn2pinyin($data['name']);
|
||||||
@ -345,6 +337,23 @@ class WebSocketDialog extends AbstractModel
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化最后消息
|
||||||
|
* @param array $lastMsg
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function lastMsgFormat($lastMsg)
|
||||||
|
{
|
||||||
|
if ($lastMsg && $lastMsg['type'] != 'preview') {
|
||||||
|
$msgData = $lastMsg;
|
||||||
|
$msgData['emoji'] = Base::array_only_recursive($msgData['emoji'], ['symbol']);
|
||||||
|
$msgData['msg'] = ['preview' => WebSocketDialogMsg::previewMsg($msgData)];
|
||||||
|
$msgData['type'] = 'preview';
|
||||||
|
$lastMsg = array_intersect_key($msgData, array_flip(['id', 'type', 'msg', 'userid', 'percentage', 'emoji', 'created_at']));
|
||||||
|
}
|
||||||
|
return $lastMsg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取未读数据
|
* 获取未读数据
|
||||||
* @param $dialogId
|
* @param $dialogId
|
||||||
@ -682,27 +691,22 @@ class WebSocketDialog extends AbstractModel
|
|||||||
* 获取会员对话(没有自动创建)
|
* 获取会员对话(没有自动创建)
|
||||||
* @param User $user 发起会话的会员
|
* @param User $user 发起会话的会员
|
||||||
* @param int $receiver 另一个会员ID
|
* @param int $receiver 另一个会员ID
|
||||||
* @return self|null
|
* @return WebSocketDialog|null
|
||||||
*/
|
*/
|
||||||
public static function checkUserDialog($user, $receiver)
|
public static function checkUserDialog($user, $receiver)
|
||||||
{
|
{
|
||||||
if ($user->userid == $receiver) {
|
if ($user->userid == $receiver) {
|
||||||
$receiver = 0;
|
$receiver = 0;
|
||||||
}
|
}
|
||||||
$dialogUser = self::select(['web_socket_dialogs.*'])
|
$dialogUser = self::getUserDialog($user->userid, $receiver, 0, $cacheKey);
|
||||||
->join('web_socket_dialog_users as u1', 'web_socket_dialogs.id', '=', 'u1.dialog_id')
|
|
||||||
->join('web_socket_dialog_users as u2', 'web_socket_dialogs.id', '=', 'u2.dialog_id')
|
|
||||||
->where('u1.userid', $user->userid)
|
|
||||||
->where('u2.userid', $receiver)
|
|
||||||
->where('web_socket_dialogs.type', 'user')
|
|
||||||
->first();
|
|
||||||
if ($dialogUser) {
|
if ($dialogUser) {
|
||||||
return $dialogUser;
|
return $dialogUser;
|
||||||
}
|
}
|
||||||
if ($receiver > 0 && $user->isTemp() && !User::whereUserid($receiver)->whereBot(1)->exists() ) {
|
if ($receiver > 0 && $user->isTemp() && !User::whereUserid($receiver)->whereBot(1)->exists() ) {
|
||||||
throw new ApiException('无法发起会话,请联系管理员。');
|
throw new ApiException('无法发起会话,请联系管理员。');
|
||||||
}
|
}
|
||||||
return AbstractModel::transaction(function () use ($receiver, $user) {
|
return AbstractModel::transaction(function () use ($cacheKey, $receiver, $user) {
|
||||||
|
Cache::forget($cacheKey);
|
||||||
$dialog = self::createInstance([
|
$dialog = self::createInstance([
|
||||||
'type' => 'user',
|
'type' => 'user',
|
||||||
]);
|
]);
|
||||||
@ -719,6 +723,41 @@ class WebSocketDialog extends AbstractModel
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户对话(支持缓存)
|
||||||
|
* @param $userid1
|
||||||
|
* @param $userid2
|
||||||
|
* @param $ttl
|
||||||
|
* @param null $cacheKey
|
||||||
|
* @return \Illuminate\Database\Eloquent\Builder|WebSocketDialog|null
|
||||||
|
*/
|
||||||
|
public static function getUserDialog($userid1, $userid2, $ttl, &$cacheKey = null)
|
||||||
|
{
|
||||||
|
$userids = [$userid1, $userid2];
|
||||||
|
sort($userids);
|
||||||
|
$cacheKey = "Dialog::user:" . implode('-', $userids);
|
||||||
|
if (empty($ttl)) {
|
||||||
|
return WebSocketDialog::query()
|
||||||
|
->whereType('user')
|
||||||
|
->whereExists(function ($query) use ($userids) {
|
||||||
|
$query->select(DB::raw(1))
|
||||||
|
->from('web_socket_dialog_users')
|
||||||
|
->whereColumn('web_socket_dialog_users.dialog_id', 'web_socket_dialogs.id')
|
||||||
|
->where('web_socket_dialog_users.userid', $userids[0]);
|
||||||
|
})
|
||||||
|
->whereExists(function ($query) use ($userids) {
|
||||||
|
$query->select(DB::raw(1))
|
||||||
|
->from('web_socket_dialog_users')
|
||||||
|
->whereColumn('web_socket_dialog_users.dialog_id', 'web_socket_dialogs.id')
|
||||||
|
->where('web_socket_dialog_users.userid', $userids[1]);
|
||||||
|
})
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
return Cache::remember($cacheKey, $ttl, function() use ($userids) {
|
||||||
|
return self::getUserDialog($userids[0], $userids[1], 0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送消息文件
|
* 发送消息文件
|
||||||
*
|
*
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user