49 lines
1.7 KiB
JavaScript
49 lines
1.7 KiB
JavaScript
const UserModel = require('./../models/UserModel')
|
|
const { notifyUser } = require('./notifyUser')
|
|
const { buildStorageUrl } = require('./storageUrl')
|
|
|
|
// همون الگوی tagNotification.js — قبلاً منشنکردنِ کاربر توی استوری هیچ نوتیفی
|
|
// (نه دیتابیس، نه پوش) تولید نمیکرد؛ این تابع بعد از ساختِ استوری، برای هر اورلیِ
|
|
// mention یه notifyUser صدا میزنه
|
|
let ioInstance = null
|
|
function setIo (io) {
|
|
ioInstance = io
|
|
}
|
|
|
|
async function notifyStoryMentions (story, ownerId) {
|
|
try {
|
|
const overlays = Array.isArray(story?.overlays) ? story.overlays : []
|
|
const mentionedIds = [
|
|
...new Set(
|
|
overlays
|
|
.filter((o) => o?.type === 'mention' && o.mention_user_id)
|
|
.map((o) => String(o.mention_user_id))
|
|
)
|
|
].filter((id) => id !== String(ownerId))
|
|
|
|
if (!mentionedIds.length) return
|
|
|
|
const owner = await UserModel.findById(ownerId).select('user_name profile_image')
|
|
const ownerUsername = owner?.user_name || 'کاربر'
|
|
const ownerImage = buildStorageUrl(owner?.profile_image) || undefined
|
|
|
|
await Promise.all(
|
|
mentionedIds.map((userId) =>
|
|
notifyUser(ioInstance, userId, {
|
|
type: 'story_mention',
|
|
title: ownerUsername,
|
|
description: 'شما را در استوریاش نام برد',
|
|
project_post_id: story._id,
|
|
actor_id: ownerId,
|
|
image: ownerImage,
|
|
actorUsername: owner?.user_name || undefined
|
|
})
|
|
)
|
|
)
|
|
} catch (error) {
|
|
console.error('Error creating story mention notifications:', error)
|
|
}
|
|
}
|
|
|
|
module.exports = { notifyStoryMentions, setIo }
|