mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 20:45:00 -05:00
Member Settings / Notification Settings gets "ntfy phone notifications": a member pastes their ntfy topic's address, and an access token if the topic needs one, and each notification they get in WeKan - the same recipients, watching and muted activity kinds - also arrives in the ntfy app, with the board and card as the title, what happened as the message and a tap opening the card. It uses ntfy's JSON publishing, so a title in any language needs no header encoding, and goes through webhookFetch: the SSRF guard, Internal destinations for a self-hosted ntfy, the Powerbox on Sandstorm. An admin-only custom field value still reaches only board admins. One read per activity finds the recipients who turned it on; sending is deferred and best effort, so ntfy being down never touches e-mail or the tray. The address and token are a top-level user field, saved and read only through the member's own methods, not client-writable and never published. models/lib/ntfy.js parses topic addresses and builds the request; tests/ntfy.test.cjs covers the request, bad addresses, tokens with header injection, javascript: links, off, and that the setting is unpublished and sent only through webhookFetch. Documented in docs/Features/Notifications/ntfy.md. The new strings are English placeholders until the next translation pass. Thanks to xet7 ! Fixes #5148.
74 lines
3.5 KiB
JavaScript
74 lines
3.5 KiB
JavaScript
// #5148: ntfy as a per-member notification channel (models/lib/ntfy.js).
|
|
// The topic address and token are the member's own: read and saved through
|
|
// their own methods, kept outside `profile`, and never published, so other
|
|
// members cannot read them.
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { check } from 'meteor/check';
|
|
import { TAPi18n } from '/imports/i18n';
|
|
import { formatActivityNotificationTitle } from '/server/lib/activityNotificationTitle';
|
|
|
|
const { cleanNtfySettings, ntfyRequest } = require('/models/lib/ntfy');
|
|
const { redactForRecipient } = require('/models/lib/notificationDelivery');
|
|
|
|
async function sendToNtfy(user, settings, title, description, params) {
|
|
if (!settings || !settings.enabled) return;
|
|
const lan = typeof user.getLanguage === 'function' ? user.getLanguage() : 'en';
|
|
await TAPi18n.ensureLanguageLoaded(lan);
|
|
const Boards = require('/models/boards').default;
|
|
const board = params.boardId ? await Boards.findOneAsync(params.boardId) : null;
|
|
// A value of an admin-only custom field (#3141) reaches only board admins.
|
|
const safe = redactForRecipient(params, { isBoardAdmin: !!(board && board.hasAdmin && board.hasAdmin(user._id)) });
|
|
const request = ntfyRequest(settings, {
|
|
title: formatActivityNotificationTitle(title, safe, TAPi18n.__.bind(TAPi18n), lan),
|
|
message: `${safe.user || ''} ${TAPi18n.__(description, safe, lan)}`.trim(),
|
|
click: safe.url,
|
|
});
|
|
if (!request) return;
|
|
const { webhookFetch } = require('/server/sandstormEgress');
|
|
await webhookFetch(request.url, { method: 'POST', headers: request.headers, body: request.body, timeoutMs: 10000 });
|
|
}
|
|
|
|
// Called with an activity's prepared notification (server/models/activities.js
|
|
// prepareActivityNotification): the same recipients the tray and e-mail get -
|
|
// board access, watching and muted activity kinds already applied. Best
|
|
// effort, like a phone push, and outside the e-mail/tray plan: an ntfy server
|
|
// that is down never holds up or fails anybody's other notifications.
|
|
export function sendActivityToNtfy({ users, title, description, params } = {}) {
|
|
const recipients = (Array.isArray(users) ? users : []).filter(user => user && user._id);
|
|
if (!recipients.length) return;
|
|
Meteor.defer(async () => {
|
|
try {
|
|
// One read per activity, for only the recipients who turned ntfy on.
|
|
const own = await Meteor.users.find({ _id: { $in: recipients.map(u => u._id) }, 'ntfy.enabled': true,
|
|
loginDisabled: { $ne: true } }, { fields: { ntfy: 1 } }).fetchAsync();
|
|
const settingsOf = new Map(own.map(u => [u._id, cleanNtfySettings(u.ntfy).value]));
|
|
for (const user of recipients) {
|
|
if (!settingsOf.has(user._id)) continue;
|
|
try {
|
|
await sendToNtfy(user, settingsOf.get(user._id), title, description, params);
|
|
} catch (error) {
|
|
console.warn(`ntfy notification not delivered: ${error && error.message}`);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.warn(`ntfy notifications skipped: ${error && error.message}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
Meteor.methods({
|
|
async getNtfySettings() {
|
|
if (!this.userId) throw new Meteor.Error('not-logged-in');
|
|
const user = await Meteor.users.findOneAsync(this.userId, { fields: { ntfy: 1 } });
|
|
return cleanNtfySettings(user && user.ntfy).value;
|
|
},
|
|
async saveNtfySettings(input) {
|
|
check(input, Object);
|
|
if (!this.userId) throw new Meteor.Error('not-logged-in');
|
|
const { value, error } = cleanNtfySettings(input);
|
|
if (error) throw new Meteor.Error(error);
|
|
await Meteor.users.updateAsync(this.userId, { $set: { ntfy: value } });
|
|
return value;
|
|
},
|
|
});
|