#278 [mail][ban] send email on ban

This commit is contained in:
Avris 2021-12-04 00:40:08 +01:00
parent 54f9e8d165
commit 8e0915c48e
3 changed files with 19 additions and 4 deletions

View File

@ -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

View File

@ -1,4 +1,4 @@
export default [
module.exports = [
'propagation of totalitarian regimes',
'hate speech',
'racism',

View File

@ -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 = {
<p style="border: 1px solid #aaa;border-radius: 8px;overflow: hidden;text-align: center;user-select: all;font-size: 24px; padding:8px;letter-spacing: 8px; font-weight: bold;">{{code}}</p>
<p style="font-size: 12px; color: #777">[[user.login.email.extra]]</p>
`,
}
},
ban: {
subject: '[[ban.header]]',
text: `[[ban.header]]\n\n[[ban.reason]][[quotation.colon]] %reason%\n\n[[quotation.start]]${terms}[[quotation.end]]`,
html: `
<p>[[ban.header]]</p>
<p>[[ban.reason]][[quotation.colon]] %reason%</p>
<p style="font-size: 12px; color: #777">[[quotation.start]]${terms}[[quotation.end]]</p>
`,
},
}
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('.')) {