From 77ce0bfd3b0f22a42c6dc4ad9e339b1da904e13b Mon Sep 17 00:00:00 2001 From: DCloud_JSON Date: Mon, 24 Jun 2024 21:53:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E8=A2=AB=E8=A7=A3?= =?UTF-8?q?=E6=95=A3/=E8=B8=A2=E5=87=BA=E7=BE=A4=E7=9A=84=E7=BE=A4?= =?UTF-8?q?=E6=88=90=E5=91=98=E6=94=B6=E4=B8=8D=E5=88=B0=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E9=80=9A=E7=9F=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uniCloud/cloudfunctions/uni-im-co/msg.js | 63 ++++++++++++++---------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/uniCloud/cloudfunctions/uni-im-co/msg.js b/uniCloud/cloudfunctions/uni-im-co/msg.js index 8da874c..ba5c383 100644 --- a/uniCloud/cloudfunctions/uni-im-co/msg.js +++ b/uniCloud/cloudfunctions/uni-im-co/msg.js @@ -548,41 +548,22 @@ async function _createOrUpdateConversation(conversation_id, msgData, chat_source delete updateObj.unread_count } - // 如果消息是:群解散 的触发动作通知,则更新会话表的leave字段为true - if (msgData.action === "group-dissolved") { - updateObj.leave = true - } - // 所有接收者的会话表更新 let res = await dbUniImConversation .where({ id: conversation_id, - user_id: dbCmd.neq(msgData.from_uid) + user_id: dbCmd.neq(msgData.from_uid), + leave: dbCmd.neq(true) }) .update(updateObj) // console.log(res); - // 特殊用户的会话表更新 + // 更新发送者的会话( 发送者的消息未读数不需要 +1 ) delete updateObj.unread_count - let updateUids = [] - if (msgData.action === 'group-exit') { - // 主动退群:更新发送者的会话 - delete updateObj.last_msg_note - updateObj.leave = true - updateUids.push(msgData.from_uid) - } else if (msgData.action === 'group-expel') { - // 踢出群:更新被踢者的会话 - delete updateObj.last_msg_note - updateObj.leave = true - updateUids.push(...msgData.body.user_id_list) - } else { - // 其它情况:更新发送者的会话 - updateUids.push(msgData.from_uid) - } res = await dbUniImConversation .where({ id: conversation_id, - user_id: dbCmd.in(updateUids) + user_id: msgData.from_uid }) .update(updateObj) // console.log(res); @@ -847,7 +828,6 @@ async function sendMsgToGroup({ user_id: dbCmd.neq(pushParam.payload.data.from_uid) } - if (before_id) { getMemberwhere._id = dbCmd.gt(before_id) } @@ -892,13 +872,44 @@ async function sendMsgToGroup({ return sum }, []) console.log('查到需要接收消息的设备数:', push_clientids.length); + + // 消息接收者用户id + const receiverUids = member.map(user => user.user_id) + const msgData = pushParam.payload.data + + // console.error('###########msgData.action', msgData.action); + // 更新:退群、解散群、被踢出群 相关的用户会话 + if(receiverUids.length && ["group-exit","group-expel","group-dissolved"].includes(msgData.action)){ + const updateUids = [] + if (msgData.action == 'group-exit') { + // 主动退群时,仅退群用户所在“消息推送批次”时更新会话即可 + if(receiverUids.includes(msgData.from_uid)){ + updateUids.push(msgData.from_uid) + } + } else { + // 群解散、被踢出群时,所有“本批次”的用户会话都需要更新(注:分批 解散/退出群 的设计模式) + updateUids.push(...receiverUids) + // 解散群时,在第一批次时,增加更新操作者(群主)的会话 + if(!before_id && msgData.action == 'group-dissolved'){ + updateUids.push(msgData.from_uid) + } + } + if(updateUids.length){ + let res = await dbUniImConversation + .where({ + id: msgData.conversation_id, + user_id: dbCmd.in(updateUids) + }) + .update({"leave":true}) + // console.error('###########更新退群或者被踢出群的用户会话', res); + } + } // WS 推送:群聊消息推送给相关用户 - let msgData = pushParam.payload.data if (!mute && !_isMuteMsg(msgData) && (await _isReadableMsg(msgData))) { const { invokeExts } = require('uni-im-ext') await invokeExts('push-msg-notify', { - to_uids: member.map(user => user.user_id), + to_uids: receiverUids, msg: { type: 'incr-conversation', conversation_id: msgData.conversation_id, -- GitLab