From 2f6c428a403a006f7878861d2f0356c3294519be Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Sat, 15 Jun 2024 20:15:29 -0400 Subject: [PATCH] feat: send notification when file gets shared --- packages/backend/src/routers/share.js | 20 ++++++++++++++++++++ packages/backend/src/util/expressutil.js | 1 + 2 files changed, 21 insertions(+) diff --git a/packages/backend/src/routers/share.js b/packages/backend/src/routers/share.js index 7054c429..0a769693 100644 --- a/packages/backend/src/routers/share.js +++ b/packages/backend/src/routers/share.js @@ -13,6 +13,8 @@ const { TYPE_DIRECTORY } = require('../filesystem/FSNodeContext'); const { PermissionUtil } = require('../services/auth/PermissionService'); const { validate } = require('uuid'); const configurable_auth = require('../middleware/configurable_auth'); +const { UsernameNotifSelector } = require('../services/NotificationService'); +const { quot } = require('../util/strutil'); const uuidv4 = require('uuid').v4; @@ -47,6 +49,7 @@ const handler_item_by_username = async (req, res) => { const svc_token = req.services.get('token'); const svc_email = req.services.get('email'); const svc_permission = req.services.get('permission'); + const svc_notification = req.services.get('notification'); console.log('which actor exists?', req.actor, @@ -85,7 +88,9 @@ const handler_item_by_username = async (req, res) => { } let email_path = path; + let is_dir = true; if ( await node.get('type') !== TYPE_DIRECTORY ) { + is_dir = false; // remove last component email_path = email_path.slice(0, path.lastIndexOf('/')+1); } @@ -106,6 +111,21 @@ const handler_item_by_username = async (req, res) => { await svc_email.send_email({ email: recipient.email }, email_tmpl, email_values); + const wut = is_dir ? 'directory' : 'file'; + svc_notification.notify(UsernameNotifSelector(username), { + source: 'sharing', + icon: 'shared.svg', + title: 'A file was shared with you!', + template: 'file-shared-with-you', + fields: { + username: req.user.username, + type: wut, + filename: await node.get('name'), + }, + text: `The user ${quot(req.user.username)} shared a ${wut} ` + + `with you called ${quot(await node.get('name'))}` + }); + res.send({}); }; diff --git a/packages/backend/src/util/expressutil.js b/packages/backend/src/util/expressutil.js index a7863702..2c950486 100644 --- a/packages/backend/src/util/expressutil.js +++ b/packages/backend/src/util/expressutil.js @@ -5,6 +5,7 @@ const Endpoint = function Endpoint (spec) { attach (route) { const eggspress_options = { allowedMethods: spec.methods ?? ['GET'], + ...(spec.mw ? { mw: spec.mw } : {}), }; const eggspress_router = eggspress( spec.route,