use oauth2 password flow for webclient

This commit is contained in:
les
2020-01-27 00:47:03 +01:00
parent 6352cb3d12
commit b706333c85
21 changed files with 367 additions and 448 deletions

View File

@@ -9,23 +9,7 @@ const debug = require('debug')('controller:event')
const eventController = {
/** add a resource to event
* @todo not used anywhere, should we use with webmention?
* @todo should we use this for roply coming from fediverse?
*/
// async addComment (req, res) {
// // comments could be added to an event or to another comment
// let event = await Event.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } } })
// if (!event) {
// const comment = await Resource.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } }, include: Event })
// event = comment.event
// }
// const comment = new Comment(req.body)
// event.addComment(comment)
// res.json(comment)
// },
async getMeta (req, res) {
async _getMeta () {
const places = await Place.findAll({
order: [[Sequelize.literal('weigth'), 'DESC']],
attributes: {
@@ -44,7 +28,11 @@ const eventController = {
}
})
res.json({ tags, places })
return { places, tags }
},
async getMeta (req, res) {
res.json(await eventController._getMeta())
},
async getNotifications (event, action) {
@@ -197,131 +185,113 @@ const eventController = {
res.sendStatus(200)
},
async addRecurrent (start, places, where_tags, limit) {
const where = {
is_visible: true,
recurrent: { [Op.ne]: null }
// placeId: places
}
// async addRecurrent (start, places, where_tags, limit) {
// const where = {
// is_visible: true,
// recurrent: { [Op.ne]: null }
// // placeId: places
// }
const events = await Event.findAll({
where,
limit,
attributes: {
exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'description', 'createdAt', 'updatedAt', 'placeId']
},
order: ['start_datetime', [Tag, 'weigth', 'DESC']],
include: [
{ model: Resource, required: false, attributes: ['id'] },
{ model: Tag, ...where_tags, attributes: ['tag'], through: { attributes: [] } },
{ model: Place, required: false, attributes: ['id', 'name', 'address'] }
]
})
// const events = await Event.findAll({
// where,
// limit,
// attributes: {
// exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'description', 'createdAt', 'updatedAt', 'placeId']
// },
// order: ['start_datetime', [Tag, 'weigth', 'DESC']],
// include: [
// { model: Resource, required: false, attributes: ['id'] },
// { model: Tag, ...where_tags, attributes: ['tag'], through: { attributes: [] } },
// { model: Place, required: false, attributes: ['id', 'name', 'address'] }
// ]
// })
debug(`Found ${events.length} recurrent events`)
let allEvents = []
_.forEach(events, e => {
allEvents = allEvents.concat(eventController.createEventsFromRecurrent(e.get(), start))
})
// let allEvents = []
// _.forEach(events, e => {
// allEvents = allEvents.concat(eventController.createEventsFromRecurrent(e.get(), start))
// })
debug(`Created ${allEvents.length} events`)
return allEvents
},
// return allEvents
// },
// build singular events from a recurrent pattern
createEventsFromRecurrent (e, start, dueTo = null) {
const events = []
const recurrent = JSON.parse(e.recurrent)
if (!recurrent.frequency) { return false }
if (!dueTo) {
dueTo = moment.unix(start).add(2, 'month')
}
let cursor = moment.unix(start).startOf('week')
const start_date = moment.unix(e.start_datetime)
const duration = moment.unix(e.end_datetime).diff(start_date, 's')
const frequency = recurrent.frequency
const days = recurrent.days
const type = recurrent.type
// // build singular events from a recurrent pattern
// createEventsFromRecurrent (e, start, dueTo = null) {
// const events = []
// const recurrent = JSON.parse(e.recurrent)
// if (!recurrent.frequency) { return false }
// if (!dueTo) {
// dueTo = start.add(2, 'month')
// }
// let cursor = start.startOf('week')
// const start_date = moment.unix(e.start_datetime)
// const duration = moment.unix(e.end_datetime).diff(start_date, 's')
// const frequency = recurrent.frequency
// const days = recurrent.days
// const type = recurrent.type
// default frequency is '1d' => each day
const toAdd = { n: 1, unit: 'day' }
// // default frequency is '1d' => each day
// const toAdd = { n: 1, unit: 'day' }
// each week or 2 (search for the first specified day)
if (frequency === '1w' || frequency === '2w') {
cursor.add(days[0] - 1, 'day')
if (frequency === '2w') {
const nWeeks = cursor.diff(e.start_datetime, 'w') % 2
if (!nWeeks) { cursor.add(1, 'week') }
}
toAdd.n = Number(frequency[0])
toAdd.unit = 'week'
// cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
}
// // each week or 2 (search for the first specified day)
// if (frequency === '1w' || frequency === '2w') {
// cursor.add(days[0] - 1, 'day')
// if (frequency === '2w') {
// const nWeeks = cursor.diff(e.start_datetime, 'w') % 2
// if (!nWeeks) { cursor.add(1, 'week') }
// }
// toAdd.n = Number(frequency[0])
// toAdd.unit = 'week'
// // cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
// }
cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
// cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
// each month or 2
if (frequency === '1m' || frequency === '2m') {
// find first match
toAdd.n = 1
toAdd.unit = 'month'
if (type === 'weekday') {
// // each month or 2
// if (frequency === '1m' || frequency === '2m') {
// // find first match
// toAdd.n = 1
// toAdd.unit = 'month'
// if (type === 'weekday') {
} else if (type === 'ordinal') {
// } else if (type === 'ordinal') {
}
}
// }
// }
// add event at specified frequency
while (true) {
const first_event_of_week = cursor.clone()
days.forEach(d => {
if (type === 'ordinal') {
cursor.date(d)
} else {
cursor.day(d - 1)
}
if (cursor.isAfter(dueTo) || cursor.isBefore(start)) { return }
e.start_datetime = cursor.unix()
e.end_datetime = e.start_datetime + duration
events.push(Object.assign({}, e))
})
if (cursor.isAfter(dueTo)) { break }
cursor = first_event_of_week.add(toAdd.n, toAdd.unit)
cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
}
// // add event at specified frequency
// while (true) {
// const first_event_of_week = cursor.clone()
// days.forEach(d => {
// if (type === 'ordinal') {
// cursor.date(d)
// } else {
// cursor.day(d - 1)
// }
// if (cursor.isAfter(dueTo) || cursor.isBefore(start)) { return }
// e.start_datetime = cursor.unix()
// e.end_datetime = e.start_datetime + duration
// events.push(Object.assign({}, e))
// })
// if (cursor.isAfter(dueTo)) { break }
// cursor = first_event_of_week.add(toAdd.n, toAdd.unit)
// cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
// }
return events
},
// return events
// },
/**
* Select events based on params
*/
async select (req, res) {
const start = req.query.start || moment().unix()
const limit = req.query.limit || 100
const show_recurrent = req.query.show_recurrent || true
const filter_tags = req.query.tags || ''
const filter_places = req.query.places || ''
debug(`select limit:${limit} rec:${show_recurrent} tags:${filter_tags} places:${filter_places}`)
let where_tags = {}
async _select (start = moment.unix(), limit = 100, show_recurrent = true) {
const where = {
// confirmed event only
is_visible: true,
start_datetime: { [Op.gt]: start },
recurrent: null
start_datetime: { [Op.gt]: start }
}
if (filter_tags) {
where_tags = { where: { tag: filter_tags.split(',') } }
if (!show_recurrent) {
where.recurrent = null
}
if (filter_places) {
where.placeId = filter_places.split(',')
}
let events = await Event.findAll({
const events = await Event.findAll({
where,
limit,
attributes: {
@@ -331,80 +301,77 @@ const eventController = {
order: ['start_datetime', [Tag, 'weigth', 'DESC']],
include: [
{ model: Resource, required: false, attributes: ['id'] },
{ model: Tag, ...where_tags, attributes: ['tag'], through: { attributes: [] } },
{ model: Tag, attributes: ['tag'], required: false, through: { attributes: [] } },
{ model: Place, required: false, attributes: ['id', 'name', 'address'] }
]
})
let recurrentEvents = []
events = _.map(events, e => e.get())
if (show_recurrent) {
recurrentEvents = await eventController.addRecurrent(start, where.placeId, where_tags, limit)
events = _.concat(events, recurrentEvents)
}
// flat tags
events = _(events).map(e => {
e.tags = e.tags.map(t => t.tag)
return _(events).map(e => {
e = e.get()
e.tags = e.tags ? e.tags.map(t => t && t.tag) : []
return e
})
// allEvents.sort((a,b) => a.start_datetime-b.start_datetime)
res.json(events.sort((a, b) => a.start_datetime - b.start_datetime))
// res.json(recurrentEvents)
},
/**
* Select events based on params
*/
async select (req, res) {
const start = req.query.start || moment().unix()
const limit = req.query.limit || 100
const show_recurrent = req.query.show_recurrent || true
res.json(await eventController._select(start, limit, show_recurrent))
// const filter_tags = req.query.tags || ''
// const filter_places = req.query.places || ''
// debug(`select limit:${limit} rec:${show_recurrent} tags:${filter_tags} places:${filter_places}`)
// let where_tags = {}
// const where = {
// // confirmed event only
// is_visible: true,
// start_datetime: { [Op.gt]: start },
// recurrent: null
// }
// if (filter_tags) {
// where_tags = { where: { tag: filter_tags.split(',') } }
// }
// if (filter_places) {
// where.placeId = filter_places.split(',')
// }
// let events = await Event.findAll({
// where,
// limit,
// attributes: {
// exclude: ['slug', 'likes', 'boost', 'userId', 'is_visible', 'description', 'createdAt', 'updatedAt', 'placeId']
// // include: [[Sequelize.fn('COUNT', Sequelize.col('activitypub_id')), 'ressources']]
// },
// order: ['start_datetime', [Tag, 'weigth', 'DESC']],
// include: [
// { model: Resource, required: false, attributes: ['id'] },
// { model: Tag, ...where_tags, attributes: ['tag'], through: { attributes: [] } },
// { model: Place, required: false, attributes: ['id', 'name', 'address'] }
// ]
// })
// let recurrentEvents = []
// events = _.map(events, e => e.get())
// if (show_recurrent) {
// recurrentEvents = await eventController.addRecurrent(moment.unix(start), where.placeId, where_tags, limit)
// events = _.concat(events, recurrentEvents)
// }
// // flat tags
// events = _(events).map(e => {
// e.tags = e.tags.map(t => t.tag)
// return e
// })
// res.json(events.sort((a, b) => a.start_datetime - b.start_datetime))
}
// async getAll (req, res) {
// // this is due how v-calendar shows dates
// const start = moment()
// .year(req.params.year)
// .month(req.params.month)
// .startOf('month')
// .startOf('week')
// let end = moment()
// .year(req.params.year)
// .month(req.params.month)
// .endOf('month')
// const shownDays = end.diff(start, 'days')
// if (shownDays <= 35) { end = end.add(1, 'week') }
// end = end.endOf('week')
// let events = await Event.findAll({
// where: {
// // return only confirmed events
// is_visible: true,
// [Op.or]: [
// // return all recurrent events regardless start_datetime
// { recurrent: { [Op.ne]: null } },
// // and events in specified range
// { start_datetime: { [Op.between]: [start.unix(), end.unix()] } }
// ]
// },
// attributes: { exclude: ['createdAt', 'updatedAt', 'placeId'] },
// order: [[Tag, 'weigth', 'DESC']],
// include: [
// { model: Resource, required: false, attributes: ['id'] },
// { model: Tag, required: false },
// { model: Place, required: false, attributes: ['id', 'name', 'address'] }
// ]
// })
// events = events.map(e => e.get()).map(e => {
// e.tags = e.tags.map(t => t.tag)
// return e
// })
// let allEvents = events.filter(e => !e.recurrent || e.recurrent.length === 0)
// events.filter(e => e.recurrent && e.recurrent.length).forEach(e => {
// const events = createEventsFromRecurrent(e, end)
// if (events) { allEvents = allEvents.concat(events) }
// })
// // allEvents.sort((a,b) => a.start_datetime-b.start_datetime)
// res.json(allEvents.sort((a, b) => a.start_datetime - b.start_datetime))
// }
}
module.exports = eventController