diff --git a/uniCloud/cloudfunctions/uni-im-co/msg.js b/uniCloud/cloudfunctions/uni-im-co/msg.js index 8da874c7454609417a0b5386f2cd747676171947..ba5c383d8bfe7794315b7bf86140e0e83414452b 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,