full
This commit is contained in:
46
utils/followHelpers.js
Normal file
46
utils/followHelpers.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const UserFollowModel = require('../models/UserFollowModel')
|
||||
const UserModel = require('../models/UserModel')
|
||||
|
||||
async function getFollowCounts (userId) {
|
||||
const [followersCount, followingCount] = await Promise.all([
|
||||
UserFollowModel.countDocuments({ followingId: userId }),
|
||||
UserFollowModel.countDocuments({ followerId: userId })
|
||||
])
|
||||
return { followersCount, followingCount }
|
||||
}
|
||||
|
||||
async function mapFollowUsers (ids, viewerId) {
|
||||
if (!ids.length) return []
|
||||
const users = await UserModel.find({ _id: { $in: ids } })
|
||||
.select('_id user_name first_name last_name profile_image is_verified verify_badge account_status')
|
||||
.lean()
|
||||
const byId = Object.fromEntries(users.map((u) => [String(u._id), u]))
|
||||
|
||||
let followingSet = new Set()
|
||||
if (viewerId) {
|
||||
const rows = await UserFollowModel.find({
|
||||
followerId: viewerId,
|
||||
followingId: { $in: ids }
|
||||
}).select('followingId').lean()
|
||||
followingSet = new Set(rows.map((r) => String(r.followingId)))
|
||||
}
|
||||
|
||||
return ids
|
||||
.map((id) => byId[String(id)])
|
||||
.filter(Boolean)
|
||||
.map((u) => ({
|
||||
_id: String(u._id),
|
||||
user_name: u.user_name || '',
|
||||
first_name: u.first_name || '',
|
||||
last_name: u.last_name || '',
|
||||
profile_image: u.profile_image || '',
|
||||
is_verified: u.is_verified || 'none',
|
||||
verify_badge: u.verify_badge || 'none',
|
||||
is_following: followingSet.has(String(u._id))
|
||||
}))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFollowCounts,
|
||||
mapFollowUsers
|
||||
}
|
||||
Reference in New Issue
Block a user