39 lines
1.9 KiB
JavaScript
39 lines
1.9 KiB
JavaScript
const NotificationModel = require('../models/NotificationModel')
|
|
const { sendPushToUser } = require('./pushNotification')
|
|
|
|
/**
|
|
* Creates a Notification row and, if a socket.io instance is available,
|
|
* pushes it live to the user's `user:{id}` room so it can be surfaced
|
|
* immediately (e.g. as a browser Notification) without waiting for a poll.
|
|
* همچنین یه پوش FCM هم برای گوشی کاربر میفرسته — این تنها نقطهایه که باعث میشه
|
|
* همهی نوتیفیکیشنهایی که از این تابع رد میشن بهصورت پوش هم نمایش داده بشن
|
|
*/
|
|
async function notifyUser (io, userId, { type, title, description, project_post_id, actor_id, image, link, actorUsername } = {}) {
|
|
await NotificationModel.create({ user_id: userId, type, title, description, project_post_id, actor_id, image })
|
|
|
|
if (io) {
|
|
try {
|
|
io.to(`user:${String(userId)}`).emit('notification', { type, title, description, image })
|
|
} catch (_) {}
|
|
}
|
|
|
|
const data = {}
|
|
if (project_post_id) data.project_post_id = String(project_post_id)
|
|
// مسیرِ نسبیِ آماده (وقتی خودِ فراخوان از قبل username/id لازم رو داره) — سرویسورکرِ
|
|
// وب دیگه لازم نیست خودش لوکاپِ اضافه بزنه تا بفهمه دقیقاً کجا باز کنه
|
|
if (link) data.link = link
|
|
// یوزرنیمِ عاملِ نوتیف (مثلاً صاحبِ استوری برای story_mention) — اندروید بدونِ این
|
|
// مجبور بود یه درخواستِ اضافه بزنه تا از رویِ آیدی به یوزرنیم برسه
|
|
if (actorUsername) data.actorUsername = actorUsername
|
|
|
|
sendPushToUser(userId, {
|
|
title,
|
|
description,
|
|
type,
|
|
data: Object.keys(data).length ? data : undefined,
|
|
image
|
|
}).catch(() => {})
|
|
}
|
|
|
|
module.exports = { notifyUser }
|