conversation.js 6.7 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9
const {
  db,
  dbCmd,
  $,
} = require('uni-im-utils')

// 获取会话列表
async function getConversationList({
  // 会话条数
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
10 11 12 13 14
  limit = 15,
  // 跳过的会话条数,主要用于跳过会话更新时间相等的会话
  skip = 0,
  // 最大的会话的最后一条消息的创建时间
  maxLastMsgCreateTime = false,
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16 17 18 19 20
  // 最大的会话更新时间
  maxUpdateTime = false,
  // 最小的会话更新时间
  minUpdateTime = false,
  // 不为false则表示获取指定的会话
  conversation_id = false,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
21 22
  // 是否要区分是否为置顶
  distinguishPinned = false,
DCloud_JSON's avatar
DCloud_JSON 已提交
23
  sort = {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
24 25 26 27 28
    pinned: -1,
    last_msg_create_time: -1,
    _id: -1
  },
  type = false
DCloud_JSON's avatar
DCloud_JSON 已提交
29 30 31 32 33 34
}) {
  
  let matchObj = {
    // 限制只能查询当前用户自己的会话记录
    user_id: this.current_uid,
    // group_id: dbCmd.exists(false), //是否查询群聊会话
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    // unread_count: dbCmd.gt(0)
  }
  if(type){
    const action = {
      unread: () => {
        matchObj.unread_count = dbCmd.gt(0)
      },
      group: () => {
        matchObj.group_id = dbCmd.exists(true)
      },
      single: () => {
        matchObj.friend_uid = dbCmd.exists(true)
      },
      todo: () => {
        matchObj.is_todo = dbCmd.exists(true)
      },
      is_star: () => {
        matchObj.is_star = dbCmd.exists(true)
      }
    }
    if(typeof type === 'object'){
      if(Object.keys(type)[0] === 'group_type'){
        matchObj.group_type = type.group_type
      }
    }else{
      action[type]?.()
    }
    
    // console.error('action========',type,'group_type:'+matchObj?.group_type);
DCloud_JSON's avatar
DCloud_JSON 已提交
64
  }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
65 66
  
  // 如果指定了会话id,则只查询指定的会话
DCloud_JSON's avatar
DCloud_JSON 已提交
67 68 69
  if (conversation_id) {
    matchObj.id = conversation_id
  } else {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
70 71 72 73 74 75 76
    // 默认不查询置顶会话
    if (distinguishPinned) {
      matchObj.pinned = dbCmd.neq(true)
    }
    matchObj.leave = dbCmd.neq(true)
    matchObj.hidden = dbCmd.neq(true)
    
DCloud_JSON's avatar
DCloud_JSON 已提交
77
    if (maxUpdateTime) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
78 79 80 81 82 83 84 85 86 87
      sort = {
        pinned: -1,
        update_time: -1,
        _id: -1
      }
      if(skip){
        matchObj.update_time = dbCmd.lte(maxUpdateTime)
      }else{
        matchObj.update_time = dbCmd.lt(maxUpdateTime)
      }
DCloud_JSON's avatar
DCloud_JSON 已提交
88
    } else if (minUpdateTime) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
89 90 91 92 93
      sort = {
        pinned: -1,
        update_time: -1,
        _id: -1
      }
DCloud_JSON's avatar
DCloud_JSON 已提交
94
      matchObj.update_time = dbCmd.gt(minUpdateTime)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    } else if (maxLastMsgCreateTime) {
      if(skip){
        matchObj.last_msg_create_time = dbCmd.lte(maxLastMsgCreateTime)
      }else{
        matchObj.last_msg_create_time = dbCmd.lt(maxLastMsgCreateTime)
      }
    } else{
      if (distinguishPinned) {
        // 没有指定时间范围时(应用刚启动时第一次加载),把所有置顶会话都查出来
        matchObj = dbCmd.or([
          {
            ...matchObj,
            pinned: true
          },
          matchObj
        ])
      }
DCloud_JSON's avatar
DCloud_JSON 已提交
112 113 114
    }
  }
  
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
115
  console.log('getConversationList matchObj', matchObj,sort,'limit:'+limit,'skip:'+skip);
DCloud_JSON's avatar
DCloud_JSON 已提交
116 117 118 119 120 121
  
  // 计算请求时间
  let startTime = Date.now()
  let res = await db.collection('uni-im-conversation').aggregate()
    .match(matchObj)
    .sort(sort)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
122
    .skip(skip)
DCloud_JSON's avatar
DCloud_JSON 已提交
123 124 125 126 127 128 129 130 131 132
    .limit(limit)
    // 联查获得最新的对话记录
    .lookup({
      from: "uni-im-msg",
      let: {
        id: '$id'
      },
      pipeline: $.pipeline()
        .match(dbCmd.expr($.eq(['$conversation_id', '$$id'])))
        .sort({update_time: -1})
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
133
        .limit(10)
DCloud_JSON's avatar
DCloud_JSON 已提交
134
        .project({
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
135
          __text: 0,
DCloud_JSON's avatar
DCloud_JSON 已提交
136 137
        })
        .done(),
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
138
      as: 'msgList'
DCloud_JSON's avatar
DCloud_JSON 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    })
    // 联查获得对话的群信息
    .lookup({
      from: "uni-im-group",
      let: {
        group_id: '$group_id'
      },
      pipeline: $.pipeline()
        .match(
          dbCmd.expr($.eq(['$_id', '$$group_id']))
        )
        .project({
          user_id: 1,
          name: 1,
          introduction: 1,
          notification: 1,
          avatar_file: 1,
          join_option: 1,
          mute_all_members: 1,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
158 159 160 161 162 163 164
          member_count: 1,
          type: 1,
          notification: 1,
          create_time: 1,
          update_time: 1,
          last_update_time: 1,
          ext: 1
DCloud_JSON's avatar
DCloud_JSON 已提交
165 166 167 168 169 170
        })
        .done(),
      as: 'group_info'
    })
    .end()
  // log请求时间
DCloud_JSON's avatar
DCloud_JSON 已提交
171
  // console.error('getConversationList耗时', Date.now() - startTime,startTime)
DCloud_JSON's avatar
DCloud_JSON 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  
  // // 计时
  // startTime = Date.now()
  // // 查每个会话的最后一条消息
  // console.error('res.data',res.data,res.data.map(item => item.id));
  // let getLastVisibleMsgsRes = await uniCloud.database().collection('uni-im-msg')
  //                              .where({
  //                                conversation_id: dbCmd.in(res.data.map(item => item.id))
  //                              })
  //                              .orderBy('update_time', 'desc')
  //                              .limit(limit)
  //                              .get()
  // // log请求时间
  // console.error('getLastVisibleMsgsRes耗时', Date.now() - startTime,startTime)
  // let lastVisibleMsgs = {}
  // getLastVisibleMsgsRes.data.forEach(item => {
  //   lastVisibleMsgs[item.conversation_id] = item
  // })
  // console.error('lastVisibleMsgs',lastVisibleMsgs);
  // res.data.forEach(item => {
  //   item.last_visible_msg = lastVisibleMsgs[item.id]
  //   if (item.last_visible_msg) {
  //     if (item.last_visible_msg.is_revoke) {
  //       item.last_visible_msg.body = "消息已经被撤回"
  //     }
  //   }
  // })
  

  const dbJQL = uniCloud.databaseForJQL({
    clientInfo: this.clientInfo
  })
  let friend_uids = res.data.map(item => item.friend_uid).filter(i => i)
  // 计算请求时间
  startTime = Date.now()
  let usersInfoRes = await dbJQL.collection('uni-id-users')
    .where(`_id in ${JSON.stringify(friend_uids)}`)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
209 210
    .field('_id,avatar_file,nickname,realname_auth')
    .limit(friend_uids.length)
DCloud_JSON's avatar
DCloud_JSON 已提交
211 212
    .get()
  // log请求时间
DCloud_JSON's avatar
DCloud_JSON 已提交
213
  // console.error('get user耗时', Date.now() - startTime,startTime)
DCloud_JSON's avatar
DCloud_JSON 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
  
  // 计算请求时间
  startTime = Date.now()
  
  let usersInfoObj = {}
  usersInfoRes.data.forEach(item => {
    usersInfoObj[item._id] = item
  })

  res.data.forEach(item => {
    item.user_info = usersInfoObj[item.friend_uid]
    item.group_info = item.group_info[0]
    // 处理特殊情况
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
227 228 229
    item.msgList.map(msg => {
      if(msg.is_revoke){
        msg.body = '消息已经被撤回'
DCloud_JSON's avatar
DCloud_JSON 已提交
230
      }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
231
    })
DCloud_JSON's avatar
DCloud_JSON 已提交
232
  })
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
233
  
DCloud_JSON's avatar
DCloud_JSON 已提交
234
  // log请求时间
DCloud_JSON's avatar
DCloud_JSON 已提交
235
  // console.error('forEach user_info耗时', Date.now() - startTime,startTime)
DCloud_JSON's avatar
DCloud_JSON 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  return {
    data: res.data,
    errCode: 0
  }
}

async function clearUnreadCount(){
  let res = await db.collection('uni-im-conversation').where({
    user_id: this.current_uid,
    unread_count: dbCmd.gt(0)
  }).update({
    unread_count: 0
  })
  console.error('clearUnreadCount',res);
  return {
    errCode: 0,
    data: res.updated
  }
}

module.exports = {
  getConversationList,
  clearUnreadCount
}