29 lines
1.2 KiB
JavaScript
29 lines
1.2 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 } = {}) {
|
|
await NotificationModel.create({ user_id: userId, type, title, description, project_post_id, actor_id })
|
|
|
|
if (io) {
|
|
try {
|
|
io.to(`user:${String(userId)}`).emit('notification', { type, title, description })
|
|
} catch (_) {}
|
|
}
|
|
|
|
sendPushToUser(userId, {
|
|
title,
|
|
description,
|
|
type,
|
|
data: project_post_id ? { project_post_id: String(project_post_id) } : undefined
|
|
}).catch(() => {})
|
|
}
|
|
|
|
module.exports = { notifyUser }
|