mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
The send-email action gets a member picker under To: typing filters the board's active members by user name or full name, and a pick adds @username to To. Addresses are still typed into To as before. When the rule runs, each @username becomes that member's current email address, so the rule - and whoever edits it - holds a user name, never somebody's address, and a member who changes address is still reached. A @name of somebody who is not an active member of the board, or has no address, is dropped rather than sent as text, and nobody is mailed twice. models/lib/ruleEmailMembers.js holds the parsing, the choices and the expansion; tests/ruleEmailMembers.test.cjs covers them, non-members, duplicates, an address with @ inside, and that the picker never reads an email address. rulesHelper imports it, so the ruleEmailFailure harness, which runs rulesHelper without require, is given it as it is given ruleParts. The new strings are English placeholders until the next translation pass. Thanks to chenghongxi and xet7 ! Fixes #2435.
55 lines
2.2 KiB
JavaScript
55 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
// #2435: a rule email's To field names board members as `@username`, picked
|
|
// from a member list with autocomplete, beside free addresses as before.
|
|
// The address is looked up when the rule runs, so the rule (and whoever
|
|
// reads it) holds a user name, never somebody's email address, and a member
|
|
// who changes their address is still reached.
|
|
//
|
|
// `lookup(username)` answers that MEMBER's address, or nothing - it is given
|
|
// only the board's active members - so a `@name` of somebody outside the
|
|
// board, or without an address, is dropped rather than sent as text an SMTP
|
|
// server would reject. Everything that is not a `@username` is kept as it
|
|
// was. Pure: tests/ruleEmailMembers.test.cjs.
|
|
|
|
const MENTION = /^@([A-Za-z0-9._-]{1,100})$/;
|
|
|
|
function mentionedUsernames(to) {
|
|
return String(to || '').split(',').map(s => s.trim())
|
|
.map(s => (s.match(MENTION) || [])[1]).filter(Boolean);
|
|
}
|
|
|
|
function expandMemberMentions(to, lookup) {
|
|
const seen = new Set();
|
|
const out = [];
|
|
for (const part of String(to || '').split(',').map(s => s.trim()).filter(Boolean)) {
|
|
const m = part.match(MENTION);
|
|
const address = m ? lookup(m[1]) : part;
|
|
if (!address) continue;
|
|
const key = String(address).toLowerCase();
|
|
if (seen.has(key)) continue;
|
|
seen.add(key);
|
|
out.push(address);
|
|
}
|
|
return out.join(', ');
|
|
}
|
|
|
|
// The picker: the board's members, matching what has been typed after `@`.
|
|
function memberChoices(members, typed, limit = 20) {
|
|
const q = String(typed || '').replace(/^@/, '').trim().toLowerCase();
|
|
return (Array.isArray(members) ? members : [])
|
|
.filter(m => m && m.username)
|
|
.filter(m => !q || m.username.toLowerCase().includes(q) || String(m.fullname || '').toLowerCase().includes(q))
|
|
.slice(0, limit)
|
|
.map(m => ({ value: `@${m.username}`, label: m.fullname ? `${m.fullname} (@${m.username})` : `@${m.username}` }));
|
|
}
|
|
|
|
// Adds a picked `@username` to the To field's comma list, once.
|
|
function addRecipient(current, value) {
|
|
const parts = String(current || '').split(',').map(s => s.trim()).filter(Boolean);
|
|
if (!parts.includes(value)) parts.push(value);
|
|
return parts.join(', ');
|
|
}
|
|
|
|
module.exports = { mentionedUsernames, expandMemberMentions, memberChoices, addRecipient };
|