front end, config
This commit is contained in:
@@ -31,12 +31,10 @@ api.route('/user/event')
|
||||
api.route('/user/event/:id')
|
||||
.delete(isAuth, userController.delEvent)
|
||||
|
||||
api.get('/event/meta', eventController.getMeta)
|
||||
api.route('/event/:event_id')
|
||||
.get(eventController.get)
|
||||
|
||||
api.route('/event/meta')
|
||||
.get(eventController.getMeta)
|
||||
|
||||
|
||||
api.get('/export/feed', exportController.feed)
|
||||
api.get('/export/ics', exportController.ics)
|
||||
|
||||
@@ -10,7 +10,7 @@ const Auth = {
|
||||
jwt.verify(token, config.secret, async (err, decoded) => {
|
||||
if (err) return res.status(403).send({ message: 'Failed to authenticate token ' + err })
|
||||
console.log('DECODED TOKEN', decoded)
|
||||
req.user = await User.findOne({ where: {email: decoded.email}})
|
||||
req.user = await User.findOne({ where: { email: decoded.email } })
|
||||
next()
|
||||
})
|
||||
},
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
const env = process.env.NODE_ENV
|
||||
module.exports = require('../config/config.' + env + '.js')
|
||||
const conf = require('../config/config.' + env + '.json')
|
||||
module.exports = conf
|
||||
|
||||
@@ -9,34 +9,33 @@ const moment = require('moment')
|
||||
moment.locale('it')
|
||||
const botController = {
|
||||
bots: [],
|
||||
async initialize() {
|
||||
async initialize () {
|
||||
console.log('initialize bots')
|
||||
const botUsers = await User.findAll({where: { mastodon_auth: { [Op.ne]: null }}})
|
||||
const botUsers = await User.findAll({ where: { mastodon_auth: { [Op.ne]: null } } })
|
||||
console.log(botUsers)
|
||||
botController.bots = botUsers.map(user => {
|
||||
console.log('initialize bot ', user.name)
|
||||
console.log('.. ', user.mastodon_auth)
|
||||
const { client_id, client_secret, access_token } = user.mastodon_auth
|
||||
const bot = new Mastodon({ access_token, api_url: `https://${user.instance}/api/v1/` })
|
||||
console.log(bot)
|
||||
const bot = new Mastodon({ access_token, api_url: `https://${user.mastodon_instance}/api/v1/` })
|
||||
const listener = bot.stream('streaming/direct')
|
||||
listener.on('message', botController.message)
|
||||
listener.on('error', botController.error)
|
||||
return {email: user.email, bot}
|
||||
return { email: user.email, bot }
|
||||
})
|
||||
console.log(botController.bots)
|
||||
},
|
||||
add (user) {
|
||||
const bot = new Mastodon({ access_token: user.mastodon_auth.access_token, api_url: `https://${user.instance}/api/v1/` })
|
||||
const bot = new Mastodon({ access_token: user.mastodon_auth.access_token, api_url: `https://${user.mastodon_instance}/api/v1/` })
|
||||
const listener = bot.stream('streaming/direct')
|
||||
listener.on('message', botController.message)
|
||||
listener.on('error', botController.error)
|
||||
botController.bots.push({ email: user.email, bot})
|
||||
botController.bots.push({ email: user.email, bot })
|
||||
},
|
||||
post(user, event) {
|
||||
post (user, event) {
|
||||
const { bot } = botController.bots.filter(b => b.email === user.email)[0]
|
||||
const status = `${event.title} @ ${event.place.name} ${moment(event.start_datetime).format('ddd, D MMMM HH:mm')} -
|
||||
${event.description} - ${event.tags.map(t => '#'+t.tag).join(' ')} ${config.baseurl}/event/${event.id}`
|
||||
${event.description} - ${event.tags.map(t => '#' + t.tag).join(' ')} ${config.baseurl}/event/${event.id}`
|
||||
return bot.post('/statuses', { status, visibility: 'private' })
|
||||
},
|
||||
async message (msg) {
|
||||
@@ -44,17 +43,17 @@ ${event.description} - ${event.tags.map(t => '#'+t.tag).join(' ')} ${config.base
|
||||
console.log(msg.data.accounts)
|
||||
const replyid = msg.data.in_reply_to_id || msg.data.last_status.in_reply_to_id
|
||||
if (!replyid) return
|
||||
const event = await Event.findOne( {where: {activitypub_id: replyid}} )
|
||||
if (!event) {
|
||||
// const event = await Event.findOne({ where: { activitypub_id: replyid } })
|
||||
// if (!event) {
|
||||
// check for comment..
|
||||
const comment = await Comment.findOne( {where: { }})
|
||||
}
|
||||
const comment = await Comment.create({activitypub_id: msg.data.last_status.id, text: msg.data.last_status.content, author: msg.data.accounts[0].username })
|
||||
event.addComment(comment)
|
||||
console.log(event)
|
||||
// const comment = await Comment.findOne( {where: { }})
|
||||
// }
|
||||
// const comment = await Comment.create({activitypub_id: msg.data.last_status.id, text: msg.data.last_status.content, author: msg.data.accounts[0].username })
|
||||
// event.addComment(comment)
|
||||
// console.log(event)
|
||||
// const comment = await Comment.findOne( { where: {activitypub_id: msg.data.in_reply_to}} )
|
||||
// console.log('dentro message ', data)
|
||||
return
|
||||
|
||||
// add comment to specified event
|
||||
// let comment
|
||||
//if (!event) {
|
||||
@@ -69,6 +68,5 @@ ${event.description} - ${event.tags.map(t => '#'+t.tag).join(' ')} ${config.base
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
setTimeout(botController.initialize, 2000)
|
||||
module.exports = botController
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
const jwt = require('jsonwebtoken')
|
||||
const { User, Event, Comment, Tag, Place } = require('../model')
|
||||
const config = require('../config')
|
||||
const mail = require('../mail')
|
||||
const moment = require('moment')
|
||||
const Sequelize = require('sequelize')
|
||||
|
||||
@@ -9,9 +6,9 @@ const eventController = {
|
||||
|
||||
async addComment (req, res) {
|
||||
// comment could be added to an event or to another comment
|
||||
let event = await Event.findOne({where: {activitypub_id: req.body.id}})
|
||||
let event = await Event.findOne({ where: { activitypub_id: req.body.id } })
|
||||
if (!event) {
|
||||
const comment = await Comment.findOne({where: {activitypub_id: req.body.id}, include: Event})
|
||||
const comment = await Comment.findOne({ where: { activitypub_id: req.body.id }, include: Event })
|
||||
event = comment.event
|
||||
}
|
||||
const comment = new Comment(req.body)
|
||||
@@ -19,16 +16,11 @@ const eventController = {
|
||||
res.json(comment)
|
||||
},
|
||||
|
||||
// async boost (req, res) {
|
||||
// const event = await Event.findById(req.body.id)
|
||||
// req.user.addBoost(event)
|
||||
// res.status(200)
|
||||
// },
|
||||
|
||||
async getMeta(req, res) {
|
||||
async getMeta (req, res) {
|
||||
console.log('GET META')
|
||||
const places = await Place.findAll()
|
||||
const tags = await Tag.findAll()
|
||||
res.json({tags, places})
|
||||
res.json({ tags, places })
|
||||
},
|
||||
|
||||
async updateTag (req, res) {
|
||||
@@ -41,17 +33,15 @@ const eventController = {
|
||||
}
|
||||
},
|
||||
|
||||
async get(req, res) {
|
||||
async get (req, res) {
|
||||
const id = req.params.event_id
|
||||
const event = await Event.findByPk(id, { include: [User, Tag, Comment, Place]})
|
||||
const event = await Event.findByPk(id, { include: [User, Tag, Comment, Place] })
|
||||
res.json(event)
|
||||
},
|
||||
|
||||
async getAll (req, res) {
|
||||
const start = moment().year(req.params.year).month(req.params.month).startOf('month').subtract(1, 'week')
|
||||
const end = moment().year(req.params.year).month(req.params.month).endOf('month').add(1, 'week')
|
||||
console.log('start', start)
|
||||
console.log('end', end)
|
||||
const events = await Event.findAll({
|
||||
where: {
|
||||
[Sequelize.Op.and]: [
|
||||
@@ -63,7 +53,7 @@ const eventController = {
|
||||
include: [User, Comment, Tag, Place]
|
||||
})
|
||||
res.json(events)
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -10,12 +10,12 @@ const bot = require('./bot')
|
||||
const userController = {
|
||||
async login (req, res) {
|
||||
// find the user
|
||||
const user = await User.findOne({where: { email: req.body.email }})
|
||||
const user = await User.findOne({ where: { email: req.body.email } })
|
||||
if (!user) {
|
||||
res.status(404).json({ success: false, message: 'AUTH_FAIL' })
|
||||
} else if (user) {
|
||||
if (!user.is_active) {
|
||||
res.status(403).json({success: false, message: 'NOT)CONFIRMED'})
|
||||
res.status(403).json({ success: false, message: 'NOT)CONFIRMED' })
|
||||
}
|
||||
// check if password matches
|
||||
else if (!await user.comparePassword(req.body.password)) {
|
||||
@@ -42,10 +42,9 @@ const userController = {
|
||||
},
|
||||
|
||||
async delEvent (req, res) {
|
||||
//check if event is mine
|
||||
// check if event is mine
|
||||
const event = await Event.findByPk(req.params.id)
|
||||
if (event && (req.user.is_admin || req.user.id === event.userId))
|
||||
{
|
||||
if (event && (req.user.is_admin || req.user.id === event.userId)) {
|
||||
await event.destroy()
|
||||
res.sendStatus(200)
|
||||
} else {
|
||||
@@ -67,30 +66,32 @@ const userController = {
|
||||
eventDetails.image_path = req.file.path
|
||||
}
|
||||
|
||||
//create place
|
||||
// create place
|
||||
let place
|
||||
try {
|
||||
place = await Place.findOrCreate({where: {name: body.place_name},
|
||||
defaults: {address: body.place_address }})
|
||||
.spread((place, created) => place)
|
||||
} catch(e) {
|
||||
place = await Place.findOrCreate({ where: { name: body.place_name },
|
||||
defaults: { address: body.place_address } })
|
||||
.spread((place, created) => place)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
let event = await Event.create(eventDetails)
|
||||
await event.setPlace(place)
|
||||
|
||||
|
||||
// create/assign tags
|
||||
console.log(body.tags)
|
||||
if (body.tags) {
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t})), {ignoreDuplicates: true})
|
||||
const tags = await Tag.findAll({where: { tag: body.tags }})
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
|
||||
const tags = await Tag.findAll({ where: { tag: body.tags } })
|
||||
await event.addTags(tags)
|
||||
}
|
||||
await req.user.addEvent(event)
|
||||
event = await Event.findByPk(event.id, {include: [User, Tag, Place]})
|
||||
event = await Event.findByPk(event.id, { include: [User, Tag, Place] })
|
||||
// check if bot exists
|
||||
if (req.user.mastodon_auth) {
|
||||
const post = await bot.post(req.user, event)
|
||||
event.activitypub_id = post.id
|
||||
event.save()
|
||||
}
|
||||
return res.json(event)
|
||||
},
|
||||
@@ -101,25 +102,27 @@ const userController = {
|
||||
await event.update(body)
|
||||
let place
|
||||
try {
|
||||
place = await Place.findOrCreate({where: {name: body.place_name},
|
||||
defaults: {address: body.place_address }})
|
||||
.spread((place, created) => place)
|
||||
} catch(e) {
|
||||
console.log('catch', e)
|
||||
place = await Place.findOrCreate({ where: { name: body.place_name },
|
||||
defaults: { address: body.place_address } })
|
||||
.spread((place, created) => place)
|
||||
} catch (e) {
|
||||
console.log('error', e)
|
||||
}
|
||||
await event.setPlace(place)
|
||||
await event.setTags([])
|
||||
console.log(body.tags)
|
||||
if (body.tags) {
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t})), {ignoreDuplicates: true})
|
||||
const tags = await Tag.findAll({where: { tag: body.tags }})
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
|
||||
const tags = await Tag.findAll({ where: { tag: body.tags } })
|
||||
await event.addTags(tags)
|
||||
}
|
||||
const newEvent = await Event.findByPk(event.id, {include: [User, Tag, Place]})
|
||||
}
|
||||
const newEvent = await Event.findByPk(event.id, { include: [User, Tag, Place] })
|
||||
// check if bot exists
|
||||
if (req.user.mastodon_auth) {
|
||||
const post = await bot.post(req.user, newEvent)
|
||||
}
|
||||
event.activitypub_id = post.id
|
||||
await event.save()
|
||||
}
|
||||
return res.json(newEvent)
|
||||
},
|
||||
|
||||
@@ -133,7 +136,7 @@ const userController = {
|
||||
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`, 'eventi', 'read write', `${config.baseurl}/settings`)
|
||||
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret, `https://${instance}`, 'read write', `${config.baseurl}/settings`)
|
||||
console.log(req.user)
|
||||
req.user.instance = instance
|
||||
req.user.mastodon_instance = instance
|
||||
req.user.mastodon_auth = { client_id, client_secret }
|
||||
await req.user.save()
|
||||
res.json(url)
|
||||
@@ -142,13 +145,13 @@ const userController = {
|
||||
async code (req, res) {
|
||||
const code = req.body.code
|
||||
const { client_id, client_secret } = req.user.mastodon_auth
|
||||
const instance = req.user.instance
|
||||
const instance = req.user.mastodon_instance
|
||||
try {
|
||||
const token = await Mastodon.getAccessToken(client_id, client_secret, code, `https://${instance}`, '${config.baseurl}/settings')
|
||||
const mastodon_auth = { client_id, client_secret, access_token: token}
|
||||
const token = await Mastodon.getAccessToken(client_id, client_secret, code, `https://${instance}`, `${config.baseurl}/settings`)
|
||||
const mastodon_auth = { client_id, client_secret, access_token: token }
|
||||
req.user.mastodon_auth = mastodon_auth
|
||||
await req.user.save()
|
||||
await botController.add(token)
|
||||
await bot.add(token)
|
||||
res.json(req.user)
|
||||
} catch (e) {
|
||||
res.json(e)
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
const Sequelize = require('sequelize')
|
||||
const env = process.env.NODE_ENV || 'development'
|
||||
const conf = require('../config/config.' + env + '.js')
|
||||
const conf = require('../config/config.' + env + '.json')
|
||||
const db = new Sequelize(conf.db)
|
||||
|
||||
|
||||
db.sync({ force: true })
|
||||
// db.sync({ force: true })
|
||||
// db.sync()
|
||||
|
||||
module.exports = db
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const Email = require('email-templates')
|
||||
const path = require('path')
|
||||
const config = require('./config');
|
||||
const config = require('./config')
|
||||
|
||||
const mail = {
|
||||
send (addresses, template, locals) {
|
||||
|
||||
@@ -6,28 +6,28 @@ const Event = db.define('event', {
|
||||
title: Sequelize.STRING,
|
||||
description: Sequelize.STRING,
|
||||
multidate: Sequelize.BOOLEAN,
|
||||
start_datetime: { type: Sequelize.DATE, index: true},
|
||||
end_datetime: {type: Sequelize.DATE, index: true},
|
||||
start_datetime: { type: Sequelize.DATE, index: true },
|
||||
end_datetime: { type: Sequelize.DATE, index: true },
|
||||
image_path: Sequelize.STRING,
|
||||
activitypub_id: { type: Sequelize.INTEGER, index: true },
|
||||
activitypub_id: { type: Sequelize.INTEGER, index: true }
|
||||
})
|
||||
|
||||
const Tag = db.define('tag', {
|
||||
tag: { type: Sequelize.STRING, index: true, unique: true, primaryKey: true},
|
||||
tag: { type: Sequelize.STRING, index: true, unique: true, primaryKey: true },
|
||||
color: { type: Sequelize.STRING }
|
||||
})
|
||||
|
||||
const Comment = db.define('comment', {
|
||||
activitypub_id: { type: Sequelize.INTEGER, index: true },
|
||||
author: Sequelize.STRING,
|
||||
text: Sequelize.STRING,
|
||||
text: Sequelize.STRING
|
||||
})
|
||||
|
||||
const MailSubscription = db.define('subscription' , {
|
||||
const MailSubscription = db.define('subscription', {
|
||||
filters: Sequelize.JSON,
|
||||
mail: Sequelize.TEXT,
|
||||
send_on_add: Sequelize.BOOLEAN,
|
||||
send_reminder: Sequelize.INTEGER,
|
||||
send_reminder: Sequelize.INTEGER
|
||||
})
|
||||
|
||||
const Place = db.define('place', {
|
||||
@@ -38,15 +38,13 @@ const Place = db.define('place', {
|
||||
Comment.belongsTo(Event)
|
||||
Event.hasMany(Comment)
|
||||
|
||||
Event.belongsToMany(Tag, {through: 'tagEvent'})
|
||||
Tag.belongsToMany(Event, {through: 'tagEvent'})
|
||||
Event.belongsToMany(Tag, { through: 'tagEvent' })
|
||||
Tag.belongsToMany(Event, { through: 'tagEvent' })
|
||||
|
||||
Event.belongsToMany(User, {through: 'boost'})
|
||||
Event.belongsTo(User)
|
||||
Event.belongsTo(Place)
|
||||
|
||||
User.hasMany(Event)
|
||||
Place.hasMany(Event)
|
||||
User.belongsToMany(User, {through: 'userFollower', as: 'follower'})
|
||||
|
||||
module.exports = { Event, Comment, Tag, Place }
|
||||
module.exports = { Event, Comment, Tag, Place, MailSubscription }
|
||||
|
||||
@@ -3,19 +3,20 @@ const db = require('../db')
|
||||
const Sequelize = require('sequelize')
|
||||
|
||||
const User = db.define('user', {
|
||||
email: {
|
||||
email: {
|
||||
type: Sequelize.STRING,
|
||||
unique: {msg: 'Email already exists'},
|
||||
index: true, allowNull: false },
|
||||
unique: { msg: 'Email already exists' },
|
||||
index: true,
|
||||
allowNull: false
|
||||
},
|
||||
description: Sequelize.TEXT,
|
||||
password: Sequelize.STRING,
|
||||
is_admin: Sequelize.BOOLEAN,
|
||||
is_active: Sequelize.BOOLEAN,
|
||||
instance: Sequelize.STRING,
|
||||
mastodon_instance: Sequelize.STRING,
|
||||
mastodon_auth: Sequelize.JSON
|
||||
})
|
||||
|
||||
|
||||
User.prototype.comparePassword = async function (pwd) {
|
||||
if (!this.password) return false
|
||||
const ret = await bcrypt.compare(pwd, this.password)
|
||||
|
||||
Reference in New Issue
Block a user