making better use of the notification system
This commit is contained in:
@@ -988,7 +988,7 @@ class NotificationSystem {
|
||||
init() {
|
||||
this.bindEvents();
|
||||
this.poll();
|
||||
setInterval(() => this.poll(), 60000); // Poll every minute
|
||||
setInterval(() => this.poll(), 10000); // Poll every 10 seconds
|
||||
}
|
||||
|
||||
bindEvents() {
|
||||
@@ -1065,6 +1065,30 @@ class NotificationSystem {
|
||||
}
|
||||
|
||||
renderItem(n) {
|
||||
if (n.type === 'approve') {
|
||||
const link = `/${n.item_id}`;
|
||||
return `
|
||||
<a href="${link}" class="notif-item ${n.is_read ? '' : 'unread'}" data-id="${n.id}">
|
||||
<div>
|
||||
<strong>Your Upload has been approved</strong>
|
||||
</div>
|
||||
<small class="notif-time">${new Date(n.created_at).toLocaleString()}</small>
|
||||
</a>
|
||||
`;
|
||||
}
|
||||
|
||||
if (n.type === 'admin_pending') {
|
||||
const link = '/admin/approve';
|
||||
return `
|
||||
<a href="${link}" class="notif-item ${n.is_read ? '' : 'unread'}" data-id="${n.id}">
|
||||
<div>
|
||||
<strong>A new upload needs approval</strong>
|
||||
</div>
|
||||
<small class="notif-time">${new Date(n.created_at).toLocaleString()}</small>
|
||||
</a>
|
||||
`;
|
||||
}
|
||||
|
||||
let typeText = 'Start';
|
||||
if (n.type === 'comment_reply') typeText = 'replied to you';
|
||||
else if (n.type === 'subscription') typeText = 'commented in a thread you follow';
|
||||
|
||||
@@ -125,7 +125,7 @@ export default (router, tpl) => {
|
||||
if (req.url.qs?.id) {
|
||||
const id = +req.url.qs.id;
|
||||
const f0ck = await db`
|
||||
select dest, mime
|
||||
select dest, mime, username, id
|
||||
from "items"
|
||||
where
|
||||
id = ${id} and
|
||||
@@ -138,6 +138,19 @@ export default (router, tpl) => {
|
||||
});
|
||||
}
|
||||
|
||||
// Notify User
|
||||
try {
|
||||
const uploader = await db`select id from "user" where login = ${f0ck[0].username} limit 1`;
|
||||
if (uploader.length > 0) {
|
||||
await db`
|
||||
INSERT INTO notifications (user_id, type, reference_id, item_id)
|
||||
VALUES (${uploader[0].id}, 'approve', 0, ${id})
|
||||
`;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[ADMIN APPROVE] Failed to notify user:', err);
|
||||
}
|
||||
|
||||
await db`update "items" set active = 'true', is_deleted = false where id = ${id}`;
|
||||
|
||||
// Check if files need moving (if they are in deleted/)
|
||||
|
||||
@@ -9,10 +9,11 @@ export default (router, tpl) => {
|
||||
try {
|
||||
const notifications = await db`
|
||||
SELECT n.id, n.type, n.item_id, n.reference_id, n.created_at, n.is_read,
|
||||
u.user as from_user, u.id as from_user_id
|
||||
COALESCE(u.user, 'System') as from_user,
|
||||
COALESCE(u.id, 0) as from_user_id
|
||||
FROM notifications n
|
||||
JOIN comments c ON n.reference_id = c.id
|
||||
JOIN "user" u ON c.user_id = u.id
|
||||
LEFT JOIN comments c ON n.reference_id = c.id
|
||||
LEFT JOIN "user" u ON c.user_id = u.id
|
||||
WHERE n.user_id = ${req.session.id} AND n.is_read = false
|
||||
ORDER BY n.created_at DESC
|
||||
LIMIT 20
|
||||
|
||||
@@ -241,6 +241,19 @@ export const handleUpload = async (req, res) => {
|
||||
`;
|
||||
}
|
||||
|
||||
// Notify Admins
|
||||
try {
|
||||
const admins = await db`select id from "user" where admin = true`;
|
||||
for (const admin of admins) {
|
||||
await db`
|
||||
INSERT INTO notifications (user_id, type, reference_id, item_id)
|
||||
VALUES (${admin.id}, 'admin_pending', 0, ${itemid})
|
||||
`;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[UPLOAD HANDLER] Failed to notify admins:', err);
|
||||
}
|
||||
|
||||
return sendJson(res, {
|
||||
success: true,
|
||||
msg: 'Upload successful! Your upload is pending admin approval.',
|
||||
|
||||
Reference in New Issue
Block a user