优化已读/未读消息

This commit is contained in:
kuaifan 2023-03-07 14:33:37 +08:00
parent 318effd1ec
commit 144fb5afba
7 changed files with 62 additions and 48 deletions

View File

@ -567,9 +567,8 @@ class DialogController extends AbstractController
'id' => $dialogUser->webSocketDialog->id, 'id' => $dialogUser->webSocketDialog->id,
'unread' => $dialogUser->webSocketDialog->unread, 'unread' => $dialogUser->webSocketDialog->unread,
'mention' => $dialogUser->webSocketDialog->mention, 'mention' => $dialogUser->webSocketDialog->mention,
'position_msgs' => $dialogUser->webSocketDialog->position_msgs, 'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString('millisecond'),
'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString(), 'user_ms' => Carbon::parse($dialogUser->updated_at)->valueOf()
'user_ms' => WebSocketDialogUser::userMs($dialogUser->updated_at)
]; ];
} }
return Base::retSuccess('success', $data); return Base::retSuccess('success', $data);
@ -593,8 +592,7 @@ class DialogController extends AbstractController
"id": 43, "id": 43,
"unread": 308, "unread": 308,
"mention": 11, "mention": 11,
"position_msgs": [], "user_at": "2020-12-12 00:00:00.000",
"user_at": "2020-12-12 00:00:00",
"user_ms": 1677558147167, "user_ms": 1677558147167,
} }
*/ */
@ -612,9 +610,8 @@ class DialogController extends AbstractController
'id' => $dialogUser->webSocketDialog->id, 'id' => $dialogUser->webSocketDialog->id,
'unread' => $dialogUser->webSocketDialog->unread, 'unread' => $dialogUser->webSocketDialog->unread,
'mention' => $dialogUser->webSocketDialog->mention, 'mention' => $dialogUser->webSocketDialog->mention,
'position_msgs' => $dialogUser->webSocketDialog->position_msgs, 'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString('millisecond'),
'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString(), 'user_ms' => Carbon::parse($dialogUser->updated_at)->valueOf()
'user_ms' => WebSocketDialogUser::userMs($dialogUser->updated_at)
]); ]);
} }
@ -1153,9 +1150,6 @@ class DialogController extends AbstractController
'id' => $dialogUser->webSocketDialog->id, 'id' => $dialogUser->webSocketDialog->id,
'unread' => $dialogUser->webSocketDialog->unread, 'unread' => $dialogUser->webSocketDialog->unread,
'mention' => $dialogUser->webSocketDialog->mention, 'mention' => $dialogUser->webSocketDialog->mention,
'position_msgs' => $dialogUser->webSocketDialog->position_msgs,
'user_at' => Carbon::now()->toDateTimeString(),
'user_ms' => WebSocketDialogUser::userMs(),
'mark_unread' => 0, 'mark_unread' => 0,
]; ];
break; break;
@ -1163,8 +1157,6 @@ class DialogController extends AbstractController
case 'unread': case 'unread':
$data = [ $data = [
'id' => $dialogUser->webSocketDialog->id, 'id' => $dialogUser->webSocketDialog->id,
'user_at' => Carbon::now()->toDateTimeString(),
'user_ms' => WebSocketDialogUser::userMs(),
'mark_unread' => 1, 'mark_unread' => 1,
]; ];
break; break;
@ -1172,10 +1164,12 @@ class DialogController extends AbstractController
default: default:
return Base::retError("参数错误"); return Base::retError("参数错误");
} }
$dialogUser->updated_at = $data['user_at'];
$dialogUser->mark_unread = $data['mark_unread']; $dialogUser->mark_unread = $data['mark_unread'];
$dialogUser->save(); $dialogUser->save();
return Base::retSuccess("success", $data); return Base::retSuccess("success", array_merge($data, [
'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString('millisecond'),
'user_ms' => Carbon::parse($dialogUser->updated_at)->valueOf(),
]));
} }
/** /**
@ -1579,7 +1573,7 @@ class DialogController extends AbstractController
if ($array) { if ($array) {
$dialog->updateInstance($array); $dialog->updateInstance($array);
$dialog->save(); $dialog->save();
WebSocketDialogUser::whereDialogId($dialog->id)->update(['updated_at' => Carbon::now()]); WebSocketDialogUser::whereDialogId($dialog->id)->update(['updated_at' => Carbon::now()->toDateTimeString('millisecond')]);
} }
// //
return Base::retSuccess('修改成功', $data); return Base::retSuccess('修改成功', $data);

View File

@ -80,19 +80,16 @@ class WebSocketDialog extends AbstractModel
return $data[$key] ?? $default; return $data[$key] ?? $default;
}; };
// //
$this->pinyin = Base::cn2pinyin($this->name); $time = Carbon::parse($this->user_at ?? $dialogUserFun('updated_at'));
$this->top_at = $this->top_at ?? $dialogUserFun('top_at'); $this->top_at = $this->top_at ?? $dialogUserFun('top_at');
$this->user_at = $this->user_at ?? $dialogUserFun('updated_at'); $this->user_at = $time->toDateTimeString('millisecond');
$this->user_ms = WebSocketDialogUser::userMs($this->user_at); $this->user_ms = $time->valueOf();
$this->quick_msgs = [];
// //
if (isset($this->search_msg_id)) { if (isset($this->search_msg_id)) {
// 最后消息 (搜索预览消息) // 最后消息 (搜索预览消息)
$this->last_msg = WebSocketDialogMsg::whereDialogId($this->id)->find($this->search_msg_id); $this->last_msg = WebSocketDialogMsg::whereDialogId($this->id)->find($this->search_msg_id);
$this->last_at = $this->last_msg?->created_at; $this->last_at = $this->last_msg?->created_at;
} else { } else {
// 最后消息
$this->last_msg = WebSocketDialogMsg::whereDialogId($this->id)->orderByDesc('id')->first();
// 未读信息 // 未读信息
$this->generateUnread($userid, $hasData); $this->generateUnread($userid, $hasData);
// 未读标记 // 未读标记
@ -103,8 +100,12 @@ class WebSocketDialog extends AbstractModel
$this->people = WebSocketDialogUser::whereDialogId($this->id)->count(); $this->people = WebSocketDialogUser::whereDialogId($this->id)->count();
// 有待办 // 有待办
$this->todo_num = WebSocketDialogMsgTodo::whereDialogId($this->id)->whereUserid($userid)->whereDoneAt(null)->count(); $this->todo_num = WebSocketDialogMsgTodo::whereDialogId($this->id)->whereUserid($userid)->whereDoneAt(null)->count();
// 最后消息
$this->last_msg = WebSocketDialogMsg::whereDialogId($this->id)->orderByDesc('id')->first();
} }
// 对方信息 // 对方信息
$this->pinyin = Base::cn2pinyin($this->name);
$this->quick_msgs = [];
$this->dialog_user = null; $this->dialog_user = null;
$this->group_info = null; $this->group_info = null;
$this->bot = 0; $this->bot = 0;
@ -175,14 +176,11 @@ class WebSocketDialog extends AbstractModel
* @param $positionData * @param $positionData
* @return $this * @return $this
*/ */
public function generateUnread($userid, $positionData = true) public function generateUnread($userid, $positionData = false)
{ {
$builder = WebSocketDialogMsgRead::whereDialogId($this->id)->whereUserid($userid)->whereReadAt(null); $builder = WebSocketDialogMsgRead::whereDialogId($this->id)->whereUserid($userid)->whereReadAt(null);
$this->unread = $builder->count(); $this->unread = $builder->count();
$this->mention = 0; $this->mention = $this->unread > 0 ? $builder->clone()->whereMention(1)->count() : 0;
if ($this->unread > 0) {
$this->mention = $builder->clone()->whereMention(1)->count();
}
if ($positionData) { if ($positionData) {
$array = []; $array = [];
// @我的消息 // @我的消息

View File

@ -855,7 +855,7 @@ class WebSocketDialogMsg extends AbstractModel
$dialogMsg->send = 1; $dialogMsg->send = 1;
$dialogMsg->key = $dialogMsg->generateMsgKey(); $dialogMsg->key = $dialogMsg->generateMsgKey();
$dialogMsg->save(); $dialogMsg->save();
WebSocketDialogUser::whereDialogId($dialog->id)->update(['updated_at' => $dialog->updated_at]); WebSocketDialogUser::whereDialogId($dialog->id)->update(['updated_at' => Carbon::now()->toDateTimeString('millisecond')]);
}); });
// //
$task = new WebSocketDialogMsgTask($dialogMsg->id); $task = new WebSocketDialogMsgTask($dialogMsg->id);

View File

@ -35,6 +35,8 @@ use Carbon\Carbon;
*/ */
class WebSocketDialogUser extends AbstractModel class WebSocketDialogUser extends AbstractModel
{ {
protected $dateFormat = 'Y-m-d H:i:s.v';
/** /**
* @return \Illuminate\Database\Eloquent\Relations\HasOne * @return \Illuminate\Database\Eloquent\Relations\HasOne
*/ */
@ -42,17 +44,4 @@ class WebSocketDialogUser extends AbstractModel
{ {
return $this->hasOne(WebSocketDialog::class, 'id', 'dialog_id'); return $this->hasOne(WebSocketDialog::class, 'id', 'dialog_id');
} }
/**
* 转时间戳加上现在的毫秒
* @param $time
* @return int
*/
public static function userMs($time = 0)
{
if (empty($time)) {
return Carbon::now()->getPreciseTimestamp(3);
}
return intval(Carbon::parse($time)->timestamp . substr(Carbon::now()->getPreciseTimestamp(3), -3));
}
} }

View File

@ -8,7 +8,6 @@ use App\Models\User;
use App\Models\WebSocketDialog; use App\Models\WebSocketDialog;
use App\Models\WebSocketDialogMsg; use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogMsgRead; use App\Models\WebSocketDialogMsgRead;
use App\Models\WebSocketDialogUser;
use App\Module\Base; use App\Module\Base;
use Carbon\Carbon; use Carbon\Carbon;
use Hhxsv5\LaravelS\Swoole\Task\Task; use Hhxsv5\LaravelS\Swoole\Task\Task;
@ -164,8 +163,8 @@ class WebSocketDialogMsgTask extends AbstractTask
'silence' => $item['silence'] ? 1 : 0, 'silence' => $item['silence'] ? 1 : 0,
'data' => array_merge($msg->toArray(), [ 'data' => array_merge($msg->toArray(), [
'mention' => $item['mention'], 'mention' => $item['mention'],
'user_at' => Carbon::parse($item['updated'])->toDateTimeString(), 'user_at' => Carbon::parse($item['updated'])->toDateTimeString('millisecond'),
'user_ms' => WebSocketDialogUser::userMs($item['updated']), 'user_ms' => Carbon::parse($item['updated'])->valueOf(),
]), ]),
] ]
]; ];
@ -210,8 +209,8 @@ class WebSocketDialogMsgTask extends AbstractTask
'mode' => 'chat', 'mode' => 'chat',
'silence' => $this->silence ? 1 : 0, 'silence' => $this->silence ? 1 : 0,
'data' => array_merge($msg->toArray(), [ 'data' => array_merge($msg->toArray(), [
'user_at' => Carbon::parse($msg->created_at)->toDateTimeString(), 'user_at' => Carbon::parse($msg->created_at)->toDateTimeString('millisecond'),
'user_ms' => WebSocketDialogUser::userMs($msg->created_at), 'user_ms' => Carbon::parse($msg->created_at)->valueOf(),
]), ]),
] ]
]; ];

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateWebSocketDialogUsersUpdatedAt extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('web_socket_dialog_users', function (Blueprint $table) {
$pre = DB::connection()->getTablePrefix();
DB::statement("ALTER TABLE `{$pre}web_socket_dialog_users` MODIFY COLUMN `created_at` timestamp(3) NULL DEFAULT NULL AFTER `important`");
DB::statement("ALTER TABLE `{$pre}web_socket_dialog_users` MODIFY COLUMN `updated_at` timestamp(3) NULL DEFAULT NULL AFTER `created_at`");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('web_socket_dialog_users', function (Blueprint $table) {
//
});
}
}

View File

@ -2077,7 +2077,7 @@ export default {
const original = state.cacheDialogs[index] const original = state.cacheDialogs[index]
const nowTime = data.user_ms const nowTime = data.user_ms
const originalTime = original.user_ms || 0 const originalTime = original.user_ms || 0
if (nowTime < originalTime) { if (nowTime <= originalTime) {
typeof data.unread !== "undefined" && delete data.unread typeof data.unread !== "undefined" && delete data.unread
typeof data.mention !== "undefined" && delete data.mention typeof data.mention !== "undefined" && delete data.mention
typeof data.position_msgs !== "undefined" && delete data.position_msgs typeof data.position_msgs !== "undefined" && delete data.position_msgs
@ -2574,7 +2574,7 @@ export default {
saveBefore() saveBefore()
const resData = result.data; const resData = result.data;
if ($A.isJson(resData.dialog)) { if ($A.isJson(resData.dialog)) {
setTimeout(_ => dispatch("saveDialog", resData.dialog), 300) // 延迟更新对话详情是因为等消息处理完 setTimeout(_ => dispatch("saveDialog", resData.dialog), 10) // 延迟更新对话详情是因为等消息处理完
// //
const ids = resData.list.map(({id}) => id) const ids = resData.list.map(({id}) => id)
state.dialogMsgs = state.dialogMsgs.filter(item => { state.dialogMsgs = state.dialogMsgs.filter(item => {