@@ -61,6 +61,7 @@ api.get('/event/:event_id', eventController.get)
|
||||
|
||||
// confirm event
|
||||
api.get('/event/confirm/:event_id', isAuth, isAdmin, eventController.confirm)
|
||||
api.get('/event/unconfirm/:event_id', isAuth, isAdmin, eventController.unconfirm)
|
||||
|
||||
// export events (rss/ics)
|
||||
api.get('/export/:type', exportController.export)
|
||||
|
||||
@@ -82,6 +82,18 @@ const eventController = {
|
||||
}
|
||||
},
|
||||
|
||||
async unconfirm (req, res) {
|
||||
const id = req.params.event_id
|
||||
const event = await Event.findByPk(id)
|
||||
|
||||
try {
|
||||
await event.update({ is_visible: false })
|
||||
res.send(200)
|
||||
} catch (e) {
|
||||
res.send(404)
|
||||
}
|
||||
},
|
||||
|
||||
async getUnconfirmed (req, res) {
|
||||
const events = await Event.findAll({
|
||||
where: {
|
||||
|
||||
@@ -198,6 +198,9 @@ const userController = {
|
||||
async update (req, res) {
|
||||
const user = await User.findByPk(req.body.id)
|
||||
if (user) {
|
||||
if (!user.is_active && req.body.is_active) {
|
||||
await mail.send(user.email, 'confirm', { user, config })
|
||||
}
|
||||
await user.update(req.body)
|
||||
res.json(user)
|
||||
} else {
|
||||
@@ -209,7 +212,7 @@ const userController = {
|
||||
const n_users = await User.count()
|
||||
try {
|
||||
if (n_users === 0) {
|
||||
// admin will be the first registered user
|
||||
// the first registered user will be an active admin
|
||||
req.body.is_active = req.body.is_admin = true
|
||||
} else {
|
||||
req.body.is_active = false
|
||||
|
||||
19
app/cron.js
19
app/cron.js
@@ -14,7 +14,7 @@ async function sendNotification (notification, event, eventNotification) {
|
||||
const p = mail.send(notification.email, 'event', { event })
|
||||
promises.push(p)
|
||||
break
|
||||
case 'mail_admin':
|
||||
case 'admin_email':
|
||||
const admins = await User.findAll({ where: { is_admin: true } })
|
||||
promises.push(admins.map(admin =>
|
||||
mail.send(admin.email, 'event', { event, to_confirm: true, notification })))
|
||||
@@ -26,7 +26,7 @@ async function sendNotification (notification, event, eventNotification) {
|
||||
promises.push(b)
|
||||
}
|
||||
// user publish
|
||||
if (event.user && event.user.mastodon_auth) {
|
||||
if (event.user && event.user.mastodon_auth && event.user.mastodon_auth.access_token) {
|
||||
const b = bot.post(event.user.mastodon_auth, event).then(ret => {
|
||||
event.activitypub_id = ret.id
|
||||
return event.save()
|
||||
@@ -45,17 +45,24 @@ async function sendNotification (notification, event, eventNotification) {
|
||||
async function loop () {
|
||||
settings = await settingsController.settings()
|
||||
// get all event notification in queue
|
||||
const eventNotifications = await EventNotification.findAll()
|
||||
const eventNotifications = await EventNotification.findAll({ where: { status: 'new' } })
|
||||
const promises = eventNotifications.map(async e => {
|
||||
const event = await Event.findByPk(e.eventId, { include: [User, Place, Tag] })
|
||||
if (!event.place) return
|
||||
const notification = await Notification.findByPk(e.notificationId)
|
||||
await sendNotification(notification, event, e)
|
||||
e.destroy()
|
||||
try {
|
||||
await sendNotification(notification, event, e)
|
||||
e.status = 'sent'
|
||||
e.save()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
e.status = 'error'
|
||||
return e.save()
|
||||
}
|
||||
})
|
||||
|
||||
return Promise.all(promises)
|
||||
}
|
||||
|
||||
setInterval(loop, 20000)
|
||||
setInterval(loop, 260000)
|
||||
loop()
|
||||
|
||||
4
app/emails/confirm/html.pug
Normal file
4
app/emails/confirm/html.pug
Normal file
@@ -0,0 +1,4 @@
|
||||
p= t('confirm_email')
|
||||
|
||||
hr
|
||||
small #{config.baseurl}
|
||||
@@ -1,6 +1,4 @@
|
||||
h4 Gancio
|
||||
|
||||
p= t('registration_email')
|
||||
|
||||
small --
|
||||
small https://cisti.org
|
||||
hr
|
||||
small #{config.baseurl}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"registration_email": "Ciao, la tua registrazione sarà confermata nei prossimi giorni. Riceverai una conferma non temere."
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"registration_email": "registration_email"
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"registration_email": "Ciao, la tua registrazione sarà confermata nei prossimi giorni. Riceverai una conferma non temere."
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"registration_email": "registration_email"
|
||||
}
|
||||
26
app/migrations/20190316230454-notification_status.js
Normal file
26
app/migrations/20190316230454-notification_status.js
Normal file
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
/*
|
||||
Add altering commands here.
|
||||
Return a promise to correctly handle asynchronicity.
|
||||
|
||||
Example:
|
||||
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
|
||||
*/
|
||||
return queryInterface.addColumn('EventNotifications', 'status',
|
||||
{ type: Sequelize.ENUM, values: ['new', 'sent', 'error'], index: true, defaultValue: 'new' })
|
||||
},
|
||||
|
||||
down: (queryInterface, Sequelize) => {
|
||||
/*
|
||||
Add reverting commands here.
|
||||
Return a promise to correctly handle asynchronicity.
|
||||
|
||||
Example:
|
||||
return queryInterface.dropTable('users');
|
||||
*/
|
||||
return queryInterface.removeColumn('EventNotifications', 'status')
|
||||
}
|
||||
};
|
||||
@@ -48,7 +48,15 @@ Event.hasMany(Comment)
|
||||
Event.belongsToMany(Tag, { through: 'tagEvent' })
|
||||
Tag.belongsToMany(Event, { through: 'tagEvent' })
|
||||
|
||||
const EventNotification = db.define('EventNotification')
|
||||
const EventNotification = db.define('EventNotification', {
|
||||
status: {
|
||||
type: Sequelize.ENUM,
|
||||
values: ['new', 'sent', 'error'],
|
||||
defaultValue: 'new',
|
||||
index: true
|
||||
}
|
||||
})
|
||||
|
||||
Event.belongsToMany(Notification, { through: EventNotification })
|
||||
Notification.belongsToMany(Event, { through: EventNotification })
|
||||
|
||||
|
||||
37
app/models/index.js
Normal file
37
app/models/index.js
Normal file
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const Sequelize = require('sequelize');
|
||||
const basename = path.basename(__filename);
|
||||
const env = process.env.NODE_ENV || 'development';
|
||||
const config = require(__dirname + '/../config/config.json')[env];
|
||||
const db = {};
|
||||
|
||||
let sequelize;
|
||||
if (config.use_env_variable) {
|
||||
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
||||
} else {
|
||||
sequelize = new Sequelize(config.database, config.username, config.password, config);
|
||||
}
|
||||
|
||||
fs
|
||||
.readdirSync(__dirname)
|
||||
.filter(file => {
|
||||
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
|
||||
})
|
||||
.forEach(file => {
|
||||
const model = sequelize['import'](path.join(__dirname, file));
|
||||
db[model.name] = model;
|
||||
});
|
||||
|
||||
Object.keys(db).forEach(modelName => {
|
||||
if (db[modelName].associate) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
db.sequelize = sequelize;
|
||||
db.Sequelize = Sequelize;
|
||||
|
||||
module.exports = db;
|
||||
Reference in New Issue
Block a user