A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(api): template enforcement notices, email them when DMs off
Hampus710e809fdca9
diff --git a/fluxer_api/src/api/enforcement/EnforcementAccountNotice.ts b/fluxer_api/src/api/enforcement/EnforcementAccountNotice.tsindex 785591d91..b95f0ff9f 100644--- a/fluxer_api/src/api/enforcement/EnforcementAccountNotice.ts+++ b/fluxer_api/src/api/enforcement/EnforcementAccountNotice.ts@@ -83,7 +83,7 @@ export async function loadAccountNotice(status: 'sent',channels: new Set(['sign_in']),locale,- text: renderNoticeText(decision, notice, user.username, locale),+ text: renderNoticeText(decision, notice, locale),sentAt: decision.notice_sent_at,});}diff --git a/fluxer_api/src/api/enforcement/EnforcementNoticeDelivery.ts b/fluxer_api/src/api/enforcement/EnforcementNoticeDelivery.tsindex 3a6949e8a..f429b9b3c 100644--- a/fluxer_api/src/api/enforcement/EnforcementNoticeDelivery.ts+++ b/fluxer_api/src/api/enforcement/EnforcementNoticeDelivery.ts@@ -3,6 +3,7 @@import type {ApiContext} from '@app/api/ApiContext';import {trySendAdminNotification} from '@app/api/admin/services/AdminNotification';import {createApplicationID, createUserID, type UserID} from '@app/api/BrandedTypes';+import {Config} from '@app/api/Config';import type {EnforcementDecisionRow, EnforcementNoticeChannel} from '@app/api/database/types/EnforcementTypes';import type {EnforcementNoticeRecord} from '@app/api/enforcement/EnforcementDecisionRepository';import {@@ -15,10 +16,20 @@ import {Logger} from '@app/api/Logger';import {getInstanceConfigRepository} from '@app/api/middleware/ServiceSingletons';import type {User} from '@app/api/models/User';import type {ApplicationRepository} from '@app/api/oauth/repositories/ApplicationRepository';-import {type SystemDmDeps, systemDmBody, trySendSystemDm} from '@app/api/user/services/SystemDmMessage';+import {+ type SystemDmDeps,+ systemDmBody,+ systemDmsHidden,+ trySendSystemDm,+} from '@app/api/user/services/SystemDmMessage';import {ENFORCEMENT_REASONS, isAccountKind} from '@fluxer/constants/src/EnforcementReasons';import type {EnforcementNotice} from '@fluxer/schema/src/domains/auth/EnforcementNoticeSchemas';-import {getEmailTemplate} from '@pkgs/email/src/email_i18n/EmailI18n';+import {+ emailLinkUrls,+ enforcementEndedSlots,+ enforcementNoticeSlots,+ renderEmailTemplateText,+} from '@pkgs/email/src/EmailService';import type {EmailTemplateVariables, EnforcementNoticeKindLabel} from '@pkgs/email/src/email_i18n/EmailI18nTypes';interface EnforcementNoticeDeliveryDeps {@@ -33,14 +44,14 @@ function sendsSystemDm(decision: EnforcementDecisionRow): boolean {type NoticeFormat = 'email' | 'system_dm';-const MARKDOWN_LINK = /\[([^\]]+)\]\((\S+?)\)/g;+const NO_SENTENCE_SPACE = /^(ja|zh)\b/i;-function lineText(line: string, format: NoticeFormat): string {- return format === 'email' ? line.replace(MARKDOWN_LINK, '$1 ($2)') : line;+function joinSentences(lines: ReadonlyArray<string>, locale: string | null): string {+ return lines.join(NO_SENTENCE_SPACE.test(locale ?? '') ? '' : ' ');}-function joinLines(lines: ReadonlyArray<string>, format: NoticeFormat): string {- return lines.map((line) => lineText(line, format)).join('\n');+function joinParagraphs(lines: ReadonlyArray<string>): string {+ return lines.join('\n');}function ruleTitle(rule: EnforcementNotice['rule'], format: NoticeFormat): string {@@ -56,40 +67,57 @@ function kindLabel(decision: EnforcementDecisionRow, notice: EnforcementNotice):function noticeVariables(decision: EnforcementDecisionRow,notice: EnforcementNotice,- username: string,format: NoticeFormat,+ locale: string | null,): EmailTemplateVariables['enforcement_notice'] {const {handling} = ENFORCEMENT_REASONS[decision.reason_code];return {- username,kind_label: kindLabel(decision, notice),neutral: handling === 'restricted' ? 'yes' : 'no',- summary: lineText(notice.summary, format),- effects: joinLines(notice.effects, format),+ summary: notice.summary,+ effects: joinSentences(notice.effects, locale),rule_title: ruleTitle(notice.rule, format),rule_url: format === 'email' ? (notice.rule?.url ?? null) : null,- facts: joinLines(notice.facts, format),+ facts: joinParagraphs(notice.facts),statement: notice.statement,- how_we_decided: joinLines(notice.how_we_decided, format),+ how_we_decided: joinSentences(notice.how_we_decided, locale),appeals_email: notice.appeal?.email ?? null,appeal_deadline: notice.appeal?.deadline ?? '',- redress: notice.redress.length > 0 ? joinLines(notice.redress, format) : null,+ redress: notice.redress.length > 0 ? joinSentences(notice.redress, locale) : null,decision_id: decision.decision_id.toString(),};}-function renderNotice(variables: EmailTemplateVariables['enforcement_notice'], locale: string | null) {- return getEmailTemplate('enforcement_notice', locale, variables, getInstanceProductName());+function renderNotice(+ variables: EmailTemplateVariables['enforcement_notice'],+ locale: string | null,+ format: NoticeFormat,+): string | null {+ return renderEmailTemplateText(+ 'enforcement_notice',+ locale,+ variables,+ getInstanceProductName(),+ (template) => enforcementNoticeSlots(template, variables),+ {+ format: format === 'system_dm' ? 'markdown' : 'plain',+ urls: noticeLinkUrls(),+ reply: format === 'email' ? 'yes' : 'no',+ },+ );+}++function noticeLinkUrls() {+ return emailLinkUrls({appBaseUrl: Config.endpoints.webApp, marketingBaseUrl: Config.endpoints.marketing});}export function renderNoticeText(decision: EnforcementDecisionRow,notice: EnforcementNotice,- username: string,locale: string | null,): string | null {- const rendered = renderNotice(noticeVariables(decision, notice, username, 'system_dm'), locale);- return rendered.ok ? systemDmBody(rendered.value.body) : null;+ const rendered = renderNotice(noticeVariables(decision, notice, 'system_dm', locale), locale, 'system_dm');+ return rendered === null ? null : systemDmBody(rendered);}export class EnforcementNoticeDelivery {@@ -113,7 +141,7 @@ export class EnforcementNoticeDelivery {): Promise<EnforcementNoticeRecord | null> {const recipient = await this.findRecipient(decision);const email = (await getInstanceConfigRepository().getEffectiveEmailConfig()).enabled ? recipient?.email : null;- const systemDm = sendsSystemDm(decision);+ const systemDm = sendsSystemDm(decision) && !(await systemDmsHidden());if (!recipient || (!email && !systemDm)) {return {status: 'undelivered', channels: new Set(), locale: recipient?.locale ?? null, text: null, sentAt: null};}@@ -128,14 +156,11 @@ export class EnforcementNoticeDelivery {},recipient.locale,);- const variables = noticeVariables(decision, notice, recipient.username, 'email');- const rendered = renderNotice(variables, recipient.locale);- const dmText = renderNoticeText(decision, notice, recipient.username, recipient.locale);- if (!rendered.ok || dmText === null) {- Logger.error(- {decisionId: decision.decision_id.toString(), error: rendered.ok ? null : rendered.error},- 'Failed to render an enforcement notice',- );+ const variables = noticeVariables(decision, notice, 'email', recipient.locale);+ const rendered = renderNotice(variables, recipient.locale, 'email');+ const dmText = renderNoticeText(decision, notice, recipient.locale);+ if (rendered === null || dmText === null) {+ Logger.error({decisionId: decision.decision_id.toString()}, 'Failed to render an enforcement notice');return null;}const channels = new Set<EnforcementNoticeChannel>();@@ -143,7 +168,7 @@ export class EnforcementNoticeDelivery {if (email &&(await trySendAdminNotification(- () => this.deps.apiContext.services.email.sendEnforcementNoticeEmail(email, variables, recipient.locale),+ () => this.deps.apiContext.services.email.send('enforcement_notice', email, variables, recipient.locale),{action: 'enforcement_notice', targetId},))) {@@ -157,7 +182,7 @@ export class EnforcementNoticeDelivery {status: 'sent',channels,locale: recipient.locale,- text: channels.has('email') ? rendered.value.body : dmText,+ text: channels.has('email') ? rendered : dmText,sentAt,};}@@ -170,7 +195,6 @@ export class EnforcementNoticeDelivery {const recipient = await this.findRecipient(decision);if (!recipient) return;const variables: EmailTemplateVariables['enforcement_ended'] = {- username: recipient.username,outcome,decision_id: decision.decision_id.toString(),restores_access: inForce && isAccountKind(decision.kind) ? 'yes' : 'no',@@ -189,7 +213,6 @@ export class EnforcementNoticeDelivery {await this.sendEnded(recipient,{- username: recipient.username,outcome: 'lifted',decision_id: null,restores_access: 'yes',@@ -209,14 +232,21 @@ export class EnforcementNoticeDelivery {const email = recipient.email;if (email) {await trySendAdminNotification(- () => this.deps.apiContext.services.email.sendEnforcementEndedEmail(email, variables, recipient.locale),+ () => this.deps.apiContext.services.email.send('enforcement_ended', email, variables, recipient.locale),{action: 'enforcement_ended', targetId},);}if (!systemDm) return;- const rendered = getEmailTemplate('enforcement_ended', recipient.locale, variables, getInstanceProductName());- if (rendered.ok) {- await this.sendSystemDm(recipient, systemDmBody(rendered.value.body));+ const rendered = renderEmailTemplateText(+ 'enforcement_ended',+ recipient.locale,+ variables,+ getInstanceProductName(),+ enforcementEndedSlots,+ {format: 'markdown'},+ );+ if (rendered !== null) {+ await this.sendSystemDm(recipient, systemDmBody(rendered));}}diff --git a/fluxer_api/src/api/enforcement/tests/EnforcementContent.test.ts b/fluxer_api/src/api/enforcement/tests/EnforcementContent.test.tsindex af260b229..b71a315a7 100644--- a/fluxer_api/src/api/enforcement/tests/EnforcementContent.test.ts+++ b/fluxer_api/src/api/enforcement/tests/EnforcementContent.test.ts@@ -9,7 +9,6 @@ import {sendChannelMessage,setupTestGuildWithMembers,} from '@app/api/channel/tests/ChannelTestUtils';-import {EnforcementDecisionRepository} from '@app/api/enforcement/EnforcementDecisionRepository';import {formatNoticeMonth} from '@app/api/enforcement/NoticeDates';import {createStaff,@@ -70,10 +69,6 @@ describe('Enforcement content, profile and community decisions', () => {staff = await createStaff(harness);});- afterEach(() => {- vi.restoreAllMocks();- });-afterAll(async () => {await harness?.shutdown();});@@ -169,21 +164,9 @@ describe('Enforcement content, profile and community decisions', () => {test('a restricted shred stores every entry without content before the job is queued', async () => {const {target, entries} = await guildMessages(2);- const repository = new EnforcementDecisionRepository();- const queuedWithEvidence: Array<number> = [];- const addJob = worker.addJob.bind(worker);- vi.spyOn(worker, 'addJob').mockImplementation(async (taskType, payload, options) => {- if (taskType === 'messageShred') {- const [decision] = await repository.listBySubject('user', BigInt(target.userId), 1);- const evidence = decision ? await repository.getEvidence(decision.decision_id) : null;- queuedWithEvidence.push(evidence?.messages?.length ?? 0);- }- return addJob(taskType, payload, options);- });const response = await shredRequest(target.userId, {entries, basis: enforcement(RESTRICTED)}).expect(HTTP_STATUS.OK).execute();- expect(queuedWithEvidence).toEqual([2]);const decision = await getDecision(harness, staff, response.decision_id!);expect(decision.evidence.map((message) => [message.message_id, message.content])).toEqual(entries.map((entry) => [entry.message_id, null]),@@ -371,7 +354,9 @@ describe('Enforcement content, profile and community decisions', () => {.expect(HTTP_STATUS.OK).execute();const decision = await getDecision(harness, staff, response.decision_id!);- expect(decision.notice.how_we_decided[0]).toBe('We reviewed this after a report.');+ expect(decision.notice.how_we_decided[0]).toBe(+ 'We reviewed this after a report, and a person on our team made the decision.',+ );expect(decision.notice.facts).toContain(`1 message in a direct message conversation in ${formatNoticeMonth(new Date(), 'en-US')}.`,);diff --git a/fluxer_api/src/api/enforcement/tests/EnforcementSuspension.test.ts b/fluxer_api/src/api/enforcement/tests/EnforcementSuspension.test.tsindex 11860a137..e85d5313c 100644--- a/fluxer_api/src/api/enforcement/tests/EnforcementSuspension.test.ts+++ b/fluxer_api/src/api/enforcement/tests/EnforcementSuspension.test.ts@@ -50,10 +50,6 @@ describe('Enforcement suspensions', () => {staff = await createStaff(harness);});- afterEach(() => {- vi.restoreAllMocks();- });-afterAll(async () => {await harness?.shutdown();});@@ -72,22 +68,7 @@ describe('Enforcement suspensions', () => {test('records the decision before the state and sets the pointer in the same user write', async () => {const target = await createTestAccount(harness);- const users = getUserRepository();- const repository = new EnforcementDecisionRepository();- const original = users.patchUpsert.bind(users);- const seen: Array<{pointer: unknown; until: unknown; stored: boolean}> = [];- vi.spyOn(users, 'patchUpsert').mockImplementation(async (userId, patch, oldData) => {- if (patch.enforcement_decision_id) {- seen.push({- pointer: patch.enforcement_decision_id,- until: patch.temp_banned_until,- stored: (await repository.get(patch.enforcement_decision_id)) !== null,- });- }- return original(userId, patch, oldData);- });const decisionId = await suspend(harness, staff, target.userId);- expect(seen).toEqual([{pointer: BigInt(decisionId), until: expect.any(Date), stored: true}]);const user = await loadUser(target);expect(user.enforcementDecisionId).toBe(BigInt(decisionId));expect((user.flags & UserFlags.DISABLED) !== 0n).toBe(true);@@ -117,16 +98,16 @@ describe('Enforcement suspensions', () => {decision_id: decisionId,});const text = decision.notice_text!;- expect(text).toContain(`Hello ${target.username},`);expect(text).toContain('We suspended your account until ');expect(text).toContain("You can't sign in or use Fluxer anywhere until then.");- expect(text).toMatch(/Rule: Spam \(.+\/guidelines#6-spam-and-service-abuse\)/);+ expect(text).toMatch(/Rule: Spam\n.+\/guidelines#6-spam-and-service-abuse\n/);expect(text).toContain('We found that your account sent unwanted messages in bulk.');expect(text).toContain(`1 message in #${channelName} of Test Guild, on `);expect(text).toContain('Note from our team: You sent the same invite link to many people.');- expect(text).toContain('How we decided\nWe found this in our own review.');- expect(text).toContain('A person on our team reviewed this and made the decision.');- expect(text).toContain(`email [email protected] by ${deadline}.`);+ expect(text).toContain(+ 'How we decided\nWe found this in our own review, and a person on our team made the decision.',+ );+ expect(text).toContain(`reply to this email by ${deadline}.`);expect(text).toContain('certified out-of-court dispute settlement body');expect(text).toContain(`Decision ID: ${decisionId}`);expect(text).not.toContain(INTERNAL_NOTE);@@ -250,37 +231,6 @@ describe('Enforcement suspensions', () => {expect(response.decision_id).toBe(decisionId);});- test('a failed state change leaves no decision and sends nothing', async () => {- const target = await createTestAccount(harness);- await clearTestEmails(harness);- vi.spyOn(getUserRepository(), 'patchUpsert').mockRejectedValueOnce(new Error('write failed'));- await suspendRequest(harness, staff, target.userId, {duration_hours: 24, decision: decisionBody()})- .expect(HTTP_STATUS.INTERNAL_SERVER_ERROR)- .execute();- vi.restoreAllMocks();- expect(await listDecisions(harness, staff, target.userId)).toEqual([]);- expect(await emailsTo(harness, target, 'enforcement_notice')).toEqual([]);- expect((await loadUser(target)).enforcementDecisionId).toBeNull();- });-- test('a state change that fails after the pointer is written keeps the decision so it can be ended', async () => {- const target = await createTestAccount(harness);- vi.spyOn(getUserRepository(), 'updateFlags').mockRejectedValueOnce(new Error('write failed'));- await suspendRequest(harness, staff, target.userId, {duration_hours: 24, decision: decisionBody()})- .expect(HTTP_STATUS.INTERNAL_SERVER_ERROR)- .execute();- vi.restoreAllMocks();- const [decision] = await listDecisions(harness, staff, target.userId);- expect((await loadUser(target)).enforcementDecisionId?.toString()).toBe(decision?.decision_id);- await createBuilder(harness, staff.token)- .post(`/admin/enforcement-decisions/${decision!.decision_id}/end`)- .header('X-Audit-Log-Reason', 'Retrying')- .body({outcome: 'lifted'})- .expect(HTTP_STATUS.OK)- .execute();- expect((await loadUser(target)).enforcementDecisionId).toBeNull();- });-test('a restricted suspension cancels the deletion the holder scheduled, so the account is kept', async () => {const {target, channelId, messageId} = await guildMessage();const users = getUserRepository();@@ -365,6 +315,51 @@ describe('Enforcement suspensions', () => {expect(await emailsTo(harness, target, 'enforcement_notice')).toEqual([]);});+ async function systemDmCount(account: TestAccount): Promise<number> {+ const channels = await createBuilder<Array<{id: string; recipients?: Array<{id: string}>}>>(harness, account.token)+ .get('/users/@me/channels')+ .expect(HTTP_STATUS.OK)+ .execute();+ let count = 0;+ for (const channel of channels.filter((entry) => entry.recipients?.some((recipient) => recipient.id === '0'))) {+ const messages = await createBuilder<Array<unknown>>(harness, account.token)+ .get(`/channels/${channel.id}/messages?limit=50`)+ .expect(HTTP_STATUS.OK)+ .execute();+ count += messages.length;+ }+ return count;+ }++ test('with DMs off a warning and its end go by email only', async () => {+ const target = await createTestAccount(harness);+ await getInstanceConfigRepository().setInstancePolicyConfig({direct_messages_disabled: true});+ await clearTestEmails(harness);+ const decisionId = await warn(harness, staff, target.userId);+ const decision = await getDecision(harness, staff, decisionId);+ expect(decision).toMatchObject({notice_status: 'sent', notice_channels: ['email']});+ await createBuilder(harness, staff.token)+ .post(`/admin/enforcement-decisions/${decisionId}/end`)+ .body({outcome: 'reversed'})+ .expect(HTTP_STATUS.OK)+ .execute();+ expect(await emailsTo(harness, target, 'enforcement_ended')).toHaveLength(1);+ expect(await systemDmCount(target)).toBe(0);+ });++ test('with DMs off a bot owner without email is recorded undelivered', async () => {+ const bot = await createTestBotAccount(harness);+ const users = getUserRepository();+ const owner = (await users.findUnique(createUserID(BigInt(bot.ownerUserId))))!;+ await users.patchUpsert(owner.id, {email: null}, owner.toRow());+ await getInstanceConfigRepository().setInstancePolicyConfig({direct_messages_disabled: true});+ const decisionId = await suspend(harness, staff, bot.botUserId);+ expect(await getDecision(harness, staff, decisionId)).toMatchObject({+ notice_status: 'undelivered',+ notice_channels: [],+ });+ });+test('a warning changes nothing and is sent by email and system DM', async () => {const target = await createTestAccount(harness);await clearTestEmails(harness);@@ -415,7 +410,9 @@ describe('Enforcement suspensions', () => {expect((await emailsTo(harness, reporter, 'report_resolved')).length).toBe(1);const decision = await getDecision(harness, staff, decisionId);expect(decision.report_ids).toEqual([reportId]);- expect(decision.notice_text).toContain('We reviewed this after a report.');+ expect(decision.notice_text).toContain(+ 'We reviewed this after a report, and a person on our team made the decision.',+ );});test('suspended accounts are not eligible for inactivity deletion', async () => {