From 8e0915c48e5b46fb314b3549d24733c6203ce22a Mon Sep 17 00:00:00 2001 From: Avris Date: Sat, 4 Dec 2021 00:40:08 +0100 Subject: [PATCH] #278 [mail][ban] send email on ban --- server/routes/admin.js | 5 +++-- src/forbidden.js | 2 +- src/mailer.js | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/server/routes/admin.js b/server/routes/admin.js index dedd96ed..faed12db 100644 --- a/server/routes/admin.js +++ b/server/routes/admin.js @@ -1,12 +1,12 @@ import { Router } from 'express'; import SQL from 'sql-template-strings'; import avatar from '../avatar'; -import {config as socialLoginConfig} from "../social"; import {buildDict, now, shuffle, handleErrorAsync, buildLocaleList} from "../../src/helpers"; import locales from '../../src/locales'; import {calculateStats, statsFile} from '../../src/stats'; import fs from 'fs'; import { caches } from "../../src/cache"; +import mailer from "../../src/mailer"; const router = Router(); @@ -142,7 +142,7 @@ router.post('/admin/ban/:username', handleErrorAsync(async (req, res) => { return res.status(401).json({error: 'Unauthorised'}); } - const user = await req.db.get(SQL`SELECT id FROM users WHERE usernameNorm = ${normalise(req.params.username)}`); + const user = await req.db.get(SQL`SELECT id, email FROM users WHERE usernameNorm = ${normalise(req.params.username)}`); if (!user) { return res.status(400).json({error: 'No such user'}); } @@ -158,6 +158,7 @@ router.post('/admin/ban/:username', handleErrorAsync(async (req, res) => { bannedBy = ${req.user.id} WHERE id = ${user.id} `); + mailer(user.email, 'ban', {reason: req.body.reason}); } else { await req.db.get(SQL` UPDATE users diff --git a/src/forbidden.js b/src/forbidden.js index d1889ab4..27e98eab 100644 --- a/src/forbidden.js +++ b/src/forbidden.js @@ -1,4 +1,4 @@ -export default [ +module.exports = [ 'propagation of totalitarian regimes', 'hate speech', 'racism', diff --git a/src/mailer.js b/src/mailer.js index 010d3e19..caf4e2a4 100644 --- a/src/mailer.js +++ b/src/mailer.js @@ -1,6 +1,7 @@ const mailer = require('mailer'); const fs = require('fs'); const Suml = require('suml'); +const forbidden = require('./forbidden'); const color = '#C71585'; const logo = fs.readFileSync(__dirname + '/../node_modules/@fortawesome/fontawesome-pro/svgs/light/tags.svg').toString('utf-8'); @@ -30,6 +31,8 @@ const sendEmail = (to, subject, body = undefined, html = undefined) => { }); }; +const terms = `It is forbidden to post on the Service any Content that might break the law or violate social norms, including but not limited to: ${forbidden.join(', ')}` + const templates = { base: { subject: `[[title]] ยป {{content}}`, @@ -61,7 +64,16 @@ const templates = {

{{code}}

[[user.login.email.extra]]

`, - } + }, + ban: { + subject: '[[ban.header]]', + text: `[[ban.header]]\n\n[[ban.reason]][[quotation.colon]] %reason%\n\n[[quotation.start]]${terms}[[quotation.end]]`, + html: ` +

[[ban.header]]

+

[[ban.reason]][[quotation.colon]] %reason%

+

[[quotation.start]]${terms}[[quotation.end]]

+ `, + }, } const applyTemplate = (template, context, params) => { @@ -71,6 +83,8 @@ const applyTemplate = (template, context, params) => { template = templates.base[context].replace('{{content}}', template); } + template = template.replace(/%reason%/g, '{{reason}}'); // TODO + template = template.replace(/\[\[([^\]]+)]]/g, m => { let x = translations; for (let part of m.substring(2, m.length - 2).split('.')) {