perf: 未读消息邮件提醒,提醒时把所有未读消息都加上,而不是只提示指定时间的

This commit is contained in:
kuaifan 2022-05-17 19:30:20 +08:00
parent e4524ff2c5
commit 3f17c0689c

View File

@ -73,29 +73,32 @@ class EmailNoticeTask extends AbstractTask
if ($setting['notice_msg'] === 'open') { if ($setting['notice_msg'] === 'open') {
$userMinute = intval($setting['msg_unread_user_minute']); $userMinute = intval($setting['msg_unread_user_minute']);
$groupMinute = intval($setting['msg_unread_group_minute']); $groupMinute = intval($setting['msg_unread_group_minute']);
if ($userMinute > -1) { \DB::statement("SET SQL_MODE=''");
WebSocketDialogMsg::select(['web_socket_dialog_msgs.*', 'r.id as r_id', 'r.userid as r_userid']) $builder = WebSocketDialogMsg::select(['web_socket_dialog_msgs.*', 'r.id as r_id', 'r.userid as r_userid'])
->join('web_socket_dialog_msg_reads as r', 'web_socket_dialog_msgs.id', '=', 'r.msg_id') ->join('web_socket_dialog_msg_reads as r', 'web_socket_dialog_msgs.id', '=', 'r.msg_id')
->where("web_socket_dialog_msgs.dialog_type", "user")
->where("r.email", 0)
->whereNull("r.read_at") ->whereNull("r.read_at")
->where("r.email", 0);
if ($userMinute > -1) {
$builder->clone()
->where("web_socket_dialog_msgs.dialog_type", "user")
->whereBetween("web_socket_dialog_msgs.created_at", [ ->whereBetween("web_socket_dialog_msgs.created_at", [
Carbon::now()->subMinutes($userMinute + 10), Carbon::now()->subMinutes($userMinute + 10),
Carbon::now()->subMinutes($userMinute) Carbon::now()->subMinutes($userMinute)
])->chunkById(100, function ($rows) { ])
->groupBy('r_userid')
->chunkById(100, function ($rows) {
$this->unreadMsgEmail($rows, "user"); $this->unreadMsgEmail($rows, "user");
}); });
} }
if ($groupMinute > -1) { if ($groupMinute > -1) {
WebSocketDialogMsg::select(['web_socket_dialog_msgs.*', 'r.id as r_id', 'r.userid as r_userid']) $builder->clone()
->join('web_socket_dialog_msg_reads as r', 'web_socket_dialog_msgs.id', '=', 'r.msg_id')
->where("web_socket_dialog_msgs.dialog_type", "group") ->where("web_socket_dialog_msgs.dialog_type", "group")
->where("r.email", 0)
->whereNull("r.read_at")
->whereBetween("web_socket_dialog_msgs.created_at", [ ->whereBetween("web_socket_dialog_msgs.created_at", [
Carbon::now()->subMinutes($groupMinute + 10), Carbon::now()->subMinutes($groupMinute + 10),
Carbon::now()->subMinutes($groupMinute) Carbon::now()->subMinutes($groupMinute)
])->chunkById(100, function ($rows) { ])
->groupBy('r_userid')
->chunkById(100, function ($rows) {
$this->unreadMsgEmail($rows, "group"); $this->unreadMsgEmail($rows, "group");
}); });
} }
@ -174,6 +177,16 @@ class EmailNoticeTask extends AbstractTask
{ {
$array = $rows->groupBy('r_userid'); $array = $rows->groupBy('r_userid');
foreach ($array as $userid => $data) { foreach ($array as $userid => $data) {
$data = WebSocketDialogMsg::select(['web_socket_dialog_msgs.*', 'r.id as r_id', 'r.userid as r_userid'])
->join('web_socket_dialog_msg_reads as r', 'web_socket_dialog_msgs.id', '=', 'r.msg_id')
->whereNull("r.read_at")
->where("r.email", 0)
->where("web_socket_dialog_msgs.dialog_type", $dialogType)
->take(100)
->get();
if (empty($data)) {
continue;
}
$user = User::find($userid); $user = User::find($userid);
if (empty($user)) { if (empty($user)) {
continue; continue;
@ -181,9 +194,6 @@ class EmailNoticeTask extends AbstractTask
if (!Base::isEmail($user->email)) { if (!Base::isEmail($user->email)) {
continue; continue;
} }
if (count($data) === 0) {
continue;
}
$setting = Base::setting('emailSetting'); $setting = Base::setting('emailSetting');
$msgType = $dialogType === "group" ? "群聊" : "个人"; $msgType = $dialogType === "group" ? "群聊" : "个人";
$subject = env('APP_NAME') . " 未读{$msgType}消息提醒"; $subject = env('APP_NAME') . " 未读{$msgType}消息提醒";
@ -199,6 +209,7 @@ class EmailNoticeTask extends AbstractTask
$dialogId = 0; $dialogId = 0;
$dialogName = null; $dialogName = null;
foreach ($items as $item) { foreach ($items as $item) {
$item->cancelAppend();
$item->userInfo = User::userid2basic($item->userid); $item->userInfo = User::userid2basic($item->userid);
$item->preview = $item->previewMsg(true); $item->preview = $item->previewMsg(true);
if (empty($dialogId)) { if (empty($dialogId)) {
@ -212,7 +223,7 @@ class EmailNoticeTask extends AbstractTask
$dialogName = $item->userInfo->nickname; $dialogName = $item->userInfo->nickname;
} }
} else { } else {
$dialogName = $item->webSocketDialog?->name; $dialogName = $item->webSocketDialog?->getGroupName();
} }
} }
} }
@ -237,9 +248,9 @@ class EmailNoticeTask extends AbstractTask
} catch (\Throwable $e) { } catch (\Throwable $e) {
info("unreadMsgEmail: " . $e->getMessage()); info("unreadMsgEmail: " . $e->getMessage());
} }
} WebSocketDialogMsgRead::whereIn('id', $data->pluck('r_id'))->update([
WebSocketDialogMsgRead::whereIn('id', $rows->pluck('r_id'))->update([
'email' => 1 'email' => 1
]); ]);
} }
}
} }