Files
wekan/models/lib/emailView.js
T
Lauri Ojansivu ddb33d21d0 Board views Email and Chat.
Email shows the board as a Gmail-like mailbox in the board's own theme:
the Inbox is the list email to card puts new cards in, Sent and All mail
follow the cards' email, the board's lists are folders and its labels are
Gmail labels, coloured on every message. The messages are the shared
table page, newest first, paged on the server; a message opens as the
card popup, its comments the conversation. Compose and Reply send From
the member's own board address, so the answer returns to this board, and
by In-Reply-To to the same card as a comment.

Chat shows the board as a Slack / Rocket.Chat workspace in the current
theme: a tree of Workspaces, boards, swimlanes, lists and cards as
channels; the selected card in the middle; its comments as chat on the
other side. Tree | card | chat left to right, mirrored right to left.
Each pane folds, the two borders drag (and move with the arrow keys),
and widths and folds are saved on the member's profile. Swimlanes, lists,
cards, workspaces and boards reorder by drag and drop through the same
paths the board and All Boards use.

The new strings are translated in every locale.

Thanks to xet7 !
2026-10-13 16:56:29 +03:00

93 lines
3.8 KiB
JavaScript

'use strict';
// Board View / Email (#794): the pure parts - which cards a folder holds, the
// search, and what may be sent. No Meteor, so tests/emailView.test.cjs runs
// them with plain Node; the server (server/emailView.js) and the client
// (client/components/boards/emailView.js) share them.
// The page index id (models/lib/reportPageIndex.js) of this view's table.
const EMAIL_VIEW_PAGE_ID = 'board-email-view';
const ID = /^[A-Za-z0-9_-]{1,64}$/;
const MAX_RECIPIENTS = 20;
const MAX_SUBJECT = 255;
const MAX_TEXT = 100000;
// A plain address, no display name, no spaces, no CR/LF: what a header can
// not be split by.
const ADDRESS = /^[^\s@<>(),;:"\\[\]]+@[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+$/;
// 'inbox' | 'sent' | 'all' | 'list:<id>' | 'label:<id>' -> { kind, id }.
// Anything else is the Inbox.
function parseFolder(folder) {
const text = String(folder || '');
if (text === 'sent' || text === 'all' || text === 'inbox') return { kind: text, id: null };
const [kind, id] = text.split(':');
if ((kind === 'list' || kind === 'label') && ID.test(id || '')) return { kind, id };
return { kind: 'inbox', id: null };
}
// The Mongo selector for a folder. Archived cards are never mail.
function emailFolderSelector(boardId, folder, { inboxListId = null } = {}) {
const { kind, id } = parseFolder(folder);
const base = { boardId, archived: false, type: { $nin: ['template-container'] } };
switch (kind) {
case 'sent': return { ...base, 'email.direction': 'out' };
case 'all': return { ...base, email: { $exists: true } };
case 'list': return { ...base, listId: id };
case 'label': return { ...base, labelIds: id };
default:
// The Inbox is what ARRIVES in the email list: a message this view sent
// is in Sent, as in Gmail.
return { ...base, listId: inboxListId || '__none__', 'email.direction': { $ne: 'out' } };
}
}
const escapeRegex = text => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Search the subject, body and sender, case-insensitively, as plain text.
function emailSearchSelector(search) {
const text = String(search || '').trim().slice(0, 200);
if (!text) return {};
const re = { $regex: escapeRegex(text), $options: 'i' };
return { $or: [{ title: re }, { description: re }, { 'email.from.address': re }, { 'email.from.name': re }] };
}
function cleanAddresses(list) {
const out = [];
for (const raw of Array.isArray(list) ? list : []) {
for (const part of String(raw || '').split(/[,;]/)) {
const address = part.trim();
if (!address) continue;
if (!ADDRESS.test(address)) throw new Error('email-view-invalid-address');
if (!out.includes(address.toLowerCase())) out.push(address.toLowerCase());
}
}
return out;
}
// What a compose form may send. Throws an i18n error key.
function cleanOutgoingEmail({ to, cc, subject, text } = {}) {
const toList = cleanAddresses(to);
const ccList = cleanAddresses(cc).filter(a => !toList.includes(a));
if (!toList.length) throw new Error('email-view-no-recipient');
if (toList.length + ccList.length > MAX_RECIPIENTS) throw new Error('email-view-too-many-recipients');
const cleanSubject = String(subject || '').replace(/[\r\n]+/g, ' ').replace(/\s+/g, ' ').trim().slice(0, MAX_SUBJECT);
if (!cleanSubject) throw new Error('email-view-no-subject');
return { to: toList, cc: ccList, subject: cleanSubject, text: String(text || '').slice(0, MAX_TEXT) };
}
// A row's one-line preview, Gmail-style: subject - start of the body.
function snippet(description, max = 120) {
const line = String(description || '').replace(/^From: .*\n+/, '').replace(/\s+/g, ' ').trim();
return line.length > max ? `${line.slice(0, max - 1)}…` : line;
}
module.exports = {
EMAIL_VIEW_PAGE_ID,
MAX_RECIPIENTS,
parseFolder,
emailFolderSelector,
emailSearchSelector,
cleanOutgoingEmail,
snippet,
};