front end, config

This commit is contained in:
lesion
2019-02-26 01:17:52 +01:00
parent 887157f2a9
commit bae404f422
58 changed files with 12862 additions and 157 deletions

View File

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

View File

@@ -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)
},
}
}

View File

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