refactoring locales management
This commit is contained in:
@@ -272,12 +272,9 @@ const eventController = {
|
||||
// return created event to the client
|
||||
res.json(event)
|
||||
|
||||
// send notification (mastodon/email)
|
||||
// only if user is authenticated
|
||||
if (req.user && !event.recurrent) {
|
||||
const notifier = require('../../notifier')
|
||||
notifier.notifyEvent('Create', event.id)
|
||||
}
|
||||
// send notifications (mastodon / email)
|
||||
const notifier = require('../../notifier')
|
||||
notifier.notifyEvent('Create', event.id)
|
||||
} catch (e) {
|
||||
res.sendStatus(400)
|
||||
debug(e)
|
||||
|
||||
@@ -11,6 +11,7 @@ const generateKeyPair = util.promisify(crypto.generateKeyPair)
|
||||
|
||||
const defaultSettings = {
|
||||
instance_timezone: 'Europe/Rome',
|
||||
instance_locale: 'en',
|
||||
instance_name: config.title.toLowerCase().replace(/ /g, ''),
|
||||
allow_registration: true,
|
||||
allow_anon_event: true,
|
||||
|
||||
@@ -100,7 +100,7 @@ const userController = {
|
||||
const user = await User.create(req.body)
|
||||
debug(`Sending registration email to ${user.email}`)
|
||||
mail.send(user.email, 'register', { user, config }, req.settings.locale)
|
||||
mail.send(config.admin_email, 'admin_register', { user, config }, req.settings.locale)
|
||||
mail.send(config.admin_email, 'admin_register', { user, config })
|
||||
res.sendStatus(200)
|
||||
} catch (e) {
|
||||
res.status(404).json(e)
|
||||
|
||||
@@ -2,12 +2,13 @@ const Email = require('email-templates')
|
||||
const path = require('path')
|
||||
const moment = require('moment-timezone')
|
||||
const config = require('config')
|
||||
const settings = require('./controller/settings')
|
||||
const settingsController = require('./controller/settings')
|
||||
const debug = require('debug')('email')
|
||||
const { Task, TaskManager } = require('../taskManager')
|
||||
const locales = require('../../locales')
|
||||
|
||||
const mail = {
|
||||
send (addresses, template, locals, locale) {
|
||||
send (addresses, template, locals, locale = settingsController.settings.instance_locale) {
|
||||
const task = new Task({
|
||||
name: 'MAIL',
|
||||
removable: true,
|
||||
@@ -17,8 +18,8 @@ const mail = {
|
||||
TaskManager.add(task)
|
||||
},
|
||||
|
||||
_send (addresses, template, locales, locale) {
|
||||
debug(`Send ${template} email to ${addresses}`)
|
||||
_send (addresses, template, locals, locale) {
|
||||
debug(`Send ${template} email to ${addresses} with locale ${locale}`)
|
||||
const email = new Email({
|
||||
views: { root: path.join(__dirname, '..', 'emails') },
|
||||
htmlToText: true,
|
||||
@@ -38,12 +39,13 @@ const mail = {
|
||||
objectNotation: true,
|
||||
syncFiles: false,
|
||||
updateFiles: false,
|
||||
defaultLocale: settings.locale,
|
||||
locale: settings.locale,
|
||||
locales: ['it', 'es', 'en', 'ca']
|
||||
defaultLocale: settingsController.settings.instance_locale || 'en',
|
||||
locale,
|
||||
locales: Object.keys(locales)
|
||||
},
|
||||
transport: config.smtp
|
||||
})
|
||||
|
||||
const msg = {
|
||||
template,
|
||||
message: {
|
||||
@@ -51,10 +53,10 @@ const mail = {
|
||||
bcc: config.admin_email
|
||||
},
|
||||
locals: {
|
||||
...locales,
|
||||
...locals,
|
||||
locale,
|
||||
config: { title: config.title, baseurl: config.baseurl, description: config.description, admin_email: config.admin_email },
|
||||
datetime: datetime => moment.unix(datetime).format('ddd, D MMMM HH:mm')
|
||||
datetime: datetime => moment.unix(datetime).locale(locale).format('ddd, D MMMM HH:mm')
|
||||
}
|
||||
}
|
||||
return email.send(msg)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const config = require('config')
|
||||
const moment = require('moment-timezone')
|
||||
const settingsController = require('../controller/settings')
|
||||
// const debug = require('debug')('event:modals')
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
@@ -39,7 +40,7 @@ module.exports = (sequelize, DataTypes) => {
|
||||
Event.belongsTo(models.event, { as: 'parent' })
|
||||
}
|
||||
|
||||
Event.prototype.toNoteAP = function (username, follower = []) {
|
||||
Event.prototype.toNoteAP = function (username, locale, follower = []) {
|
||||
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
|
||||
const tag_links = tags.map(t => {
|
||||
return `<a href='/tags/${t}' class='mention hashtag status-link' rel='tag'><span>#${t}</span></a>`
|
||||
@@ -47,8 +48,8 @@ module.exports = (sequelize, DataTypes) => {
|
||||
|
||||
const content = `<a href='${config.baseurl}/event/${this.id}'>${this.title}</a><br/>
|
||||
📍 ${this.place.name}<br/>
|
||||
📅 ${moment.unix(this.start_datetime).format('dddd, D MMMM (HH:mm)')}<br/><br/>
|
||||
${this.description.length > 300 ? this.description.substr(0, 300) + '...' : this.description}<br/>
|
||||
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}<br/><br/>
|
||||
${this.description.length > 500 ? this.description.substr(0, 500) + '...' : this.description}<br/>
|
||||
${tag_links} <br/>`
|
||||
|
||||
const attachment = []
|
||||
|
||||
@@ -80,7 +80,9 @@ const Helpers = {
|
||||
to: recipients[sharedInbox],
|
||||
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${settingsController.settings.instance_name}/followers`],
|
||||
actor: `${config.baseurl}/federation/u/${settingsController.settings.instance_name}`,
|
||||
object: event.toNoteAP(settingsController.settings.instance_name, recipients[sharedInbox])
|
||||
object: event.toNoteAP(settingsController.settings.instance_name,
|
||||
settingsController.settings.instance_locale,
|
||||
recipients[sharedInbox])
|
||||
}
|
||||
body['@context'] = [
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
|
||||
@@ -33,7 +33,7 @@ router.get('/m/:event_id', async (req, res) => {
|
||||
|
||||
const event = await Event.findByPk(req.params.event_id, { include: [User, Tag, Place] })
|
||||
if (!event) { return res.status(404).send('Not found') }
|
||||
return res.json(event.toNoteAP(event.user.username))
|
||||
return res.json(event.toNoteAP(event.user.username, req.settings.locale))
|
||||
})
|
||||
|
||||
// get any message coming from federation
|
||||
|
||||
@@ -115,7 +115,7 @@ module.exports = {
|
||||
type: 'OrderedCollectionPage',
|
||||
totalItems: events.length,
|
||||
partOf: `${config.baseurl}/federation/u/${name}/outbox`,
|
||||
orderedItems: events.map(e => ({ ...e.toNoteAP(name), actor: `${config.baseurl}/federation/u/${name}` }))
|
||||
orderedItems: events.map(e => ({ ...e.toNoteAP(name, req.settings.locale), actor: `${config.baseurl}/federation/u/${name}` }))
|
||||
// user.events.map(e => ({
|
||||
// id: `${config.baseurl}/federation/m/${e.id}#create`,
|
||||
// type: 'Create',
|
||||
|
||||
@@ -60,15 +60,15 @@ module.exports = {
|
||||
// announcement: 'TODO: HTML First presentation post'
|
||||
// })
|
||||
|
||||
// send confirmed event to mastodon
|
||||
// send confirmed events to mastodon
|
||||
await db.notification.create({ action: 'Create', type: 'ap', filters: { is_visible: true } })
|
||||
await db.notification.create({ action: 'Update', type: 'ap', filters: { is_visible: true } })
|
||||
await db.notification.create({ action: 'Delete', type: 'ap', filters: { is_visible: true } })
|
||||
|
||||
// send anon event to administrator
|
||||
// send anon events to admin
|
||||
await db.notification.create({ action: 'Create', type: 'admin_email', filters: { is_visible: false } })
|
||||
|
||||
// TODO
|
||||
// TODO email's notifications
|
||||
// await db.notification.create({ action: 'Create', type: 'email', filters: { is_visible: true } })
|
||||
|
||||
// close db connection
|
||||
|
||||
@@ -9,6 +9,7 @@ const { JSDOM } = require('jsdom')
|
||||
const { window } = new JSDOM('<!DOCTYPE html>')
|
||||
const domPurify = DOMPurify(window)
|
||||
const URL = require('url')
|
||||
const locales = require('../locales')
|
||||
|
||||
domPurify.addHook('beforeSanitizeElements', node => {
|
||||
if (node.hasAttribute && node.hasAttribute('href')) {
|
||||
@@ -52,8 +53,7 @@ module.exports = {
|
||||
|
||||
// set locale and user locale
|
||||
const acceptedLanguages = req.headers['accept-language']
|
||||
const supportedLanguages = ['en', 'it', 'es', 'ca']
|
||||
acceptLanguage.languages(supportedLanguages)
|
||||
acceptLanguage.languages(Object.keys(locales))
|
||||
req.settings.locale = acceptLanguage.get(acceptedLanguages)
|
||||
req.settings.user_locale = settingsController.user_locale[req.settings.locale]
|
||||
moment.locale(req.settings.locale)
|
||||
|
||||
@@ -15,10 +15,11 @@ const notifier = {
|
||||
debug('Send %s notification %s', notification.type, notification.action)
|
||||
let p
|
||||
switch (notification.type) {
|
||||
case 'mail':
|
||||
return mail.send(notification.email, 'event', { event, config, notification })
|
||||
// case 'mail': TODO: locale?
|
||||
// return mail.send(notification.email, 'event', { event, notification })
|
||||
case 'admin_email':
|
||||
p = mail.send([config.smtp.auth.user, config.admin_email], 'event', { event, to_confirm: !event.is_visible, config, notification })
|
||||
p = mail.send(config.admin_email, 'event',
|
||||
{ event, to_confirm: !event.is_visible, notification })
|
||||
promises.push(p)
|
||||
break
|
||||
case 'ap':
|
||||
|
||||
Reference in New Issue
Block a user