修复动态发布与互动问题
This commit is contained in:
@@ -40,6 +40,17 @@ async function fetchUsers(ids) {
|
||||
return out
|
||||
}
|
||||
|
||||
async function fetchUsersByOpenids(openids) {
|
||||
if (!openids.length) return []
|
||||
const out = []
|
||||
for (let i = 0; i < openids.length; i += 100) {
|
||||
const chunk = openids.slice(i, i + 100)
|
||||
const res = await db.collection('users').where({ openid: _.in(chunk) }).limit(100).get()
|
||||
out.push(...res.data)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
exports.main = async event => {
|
||||
const { OPENID } = cloud.getWXContext()
|
||||
if (!OPENID) throw new Error('no openid in wx context')
|
||||
@@ -70,22 +81,46 @@ exports.main = async event => {
|
||||
convRes = { data: [] }
|
||||
}
|
||||
|
||||
const peerOpenids = [
|
||||
...new Set(
|
||||
convRes.data
|
||||
.filter(c => (c.type || 'single') === 'single')
|
||||
.map(c => (c.memberOpenids || []).find(memberOpenid => memberOpenid !== OPENID))
|
||||
.filter(Boolean)
|
||||
)
|
||||
]
|
||||
const peerUsers = await fetchUsersByOpenids(peerOpenids)
|
||||
const peerUserByOpenid = new Map(peerUsers.map(user => [user.openid, user]))
|
||||
|
||||
const conversations = convRes.data.map(c => {
|
||||
const unreadOpenids = c.unreadOpenids || []
|
||||
const unreadCounts = c.unreadCounts || {}
|
||||
const unreadCount = unreadCounts[OPENID] || (unreadOpenids.includes(OPENID) ? 1 : 0)
|
||||
const type = c.type || 'single'
|
||||
const peerOpenid = type === 'single'
|
||||
? (c.memberOpenids || []).find(memberOpenid => memberOpenid !== OPENID)
|
||||
: ''
|
||||
const peer = peerOpenid ? peerUserByOpenid.get(peerOpenid) : null
|
||||
|
||||
return {
|
||||
_id: c._id,
|
||||
type: c.type || 'single',
|
||||
title: c.title || '未知用户',
|
||||
type,
|
||||
title: type === 'single'
|
||||
? peer?.nickname || c.title || '未知用户'
|
||||
: c.title || '系统消息',
|
||||
petName: c.petName || undefined,
|
||||
avatarKey: c.avatarKey || 'gradient-avatar-1',
|
||||
avatarKey: type === 'single'
|
||||
? peer?.avatarKey || c.avatarKey || 'gradient-avatar-1'
|
||||
: c.avatarKey || 'gradient-avatar-1',
|
||||
avatarUrl: type === 'single'
|
||||
? peer?.avatarUrl || c.avatarUrl || ''
|
||||
: c.avatarUrl || '',
|
||||
preview: c.lastMessage?.content || c.preview || '',
|
||||
timeText: formatTimeText(c.updatedAt || c.createdAt),
|
||||
unreadCount,
|
||||
pinned: Boolean(c.pinned),
|
||||
muted: Boolean(c.muted),
|
||||
online: Boolean(c.online)
|
||||
online: type === 'single' ? Boolean(peer?.online || c.online) : Boolean(c.online)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user