39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
const StoryModel = require('../models/StoryModel')
|
|
|
|
/**
|
|
* برای یه لیست از userId (مثلاً نویسندههای کامنتهای یه صفحه)، نقشهی has_story/
|
|
* has_unviewed_story برمیگردونه — همون منطقِ حلقهی استوری که postEngagement.js
|
|
* برای نویسندهی پستها محاسبه میکنه، جدا شده تا هر جای دیگه (مثلاً کامنتها) هم
|
|
* قابل استفاده باشه بدون وابستگی به فیلدهای مخصوصِ پست
|
|
*/
|
|
async function resolveStoryFlags (userIds, viewerId) {
|
|
const ids = [...new Set((userIds || []).filter(Boolean).map(String))]
|
|
if (!ids.length) return {}
|
|
|
|
const activeStories = await StoryModel.find({ user_id: { $in: ids }, expires_at: { $gt: new Date() } })
|
|
.select('user_id viewers')
|
|
.lean()
|
|
|
|
const storiesByAuthor = {}
|
|
activeStories.forEach((s) => {
|
|
const key = String(s.user_id)
|
|
if (!storiesByAuthor[key]) storiesByAuthor[key] = []
|
|
storiesByAuthor[key].push(s)
|
|
})
|
|
|
|
const flags = {}
|
|
for (const id of Object.keys(storiesByAuthor)) {
|
|
const stories = storiesByAuthor[id]
|
|
const hasUnviewed = stories.some(
|
|
(s) => !s.viewers?.some((v) => String(v.user_id) === String(viewerId))
|
|
)
|
|
flags[id] = {
|
|
has_story: true,
|
|
has_unviewed_story: viewerId ? hasUnviewed : true
|
|
}
|
|
}
|
|
return flags
|
|
}
|
|
|
|
module.exports = { resolveStoryFlags }
|