Files
gancio/server/api/models/event.js

124 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-09-27 11:14:11 +02:00
const config = require('../../config')
2020-11-17 00:34:56 +01:00
const moment = require('dayjs')
2021-04-14 01:30:51 +02:00
const { htmlToText } = require('html-to-text')
2019-07-31 01:03:21 +02:00
2020-06-27 02:10:10 +02:00
const { Model, DataTypes } = require('sequelize')
2021-04-13 18:04:53 +02:00
const SequelizeSlugify = require('sequelize-slugify')
2021-09-27 11:14:11 +02:00
const sequelize = require('./index').sequelize
2020-06-27 02:10:10 +02:00
const Resource = require('./resource')
const Notification = require('./notification')
2020-07-08 00:57:28 +02:00
const EventNotification = require('./eventnotification')
2020-06-27 02:10:10 +02:00
const Place = require('./place')
const User = require('./user')
const Tag = require('./tag')
2021-03-05 14:33:33 +01:00
const utc = require('dayjs/plugin/utc')
const dayjs = require('dayjs')
dayjs.extend(utc)
2020-06-27 02:10:10 +02:00
class Event extends Model {}
Event.init({
id: {
allowNull: false,
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: DataTypes.STRING,
2021-04-13 18:04:53 +02:00
slug: {
type: DataTypes.STRING,
index: true,
unique: true
},
2020-06-27 02:10:10 +02:00
description: DataTypes.TEXT,
multidate: DataTypes.BOOLEAN,
start_datetime: {
type: DataTypes.INTEGER,
index: true
},
end_datetime: {
type: DataTypes.INTEGER,
index: true
},
image_path: DataTypes.STRING,
media: DataTypes.JSON,
2020-06-27 02:10:10 +02:00
is_visible: DataTypes.BOOLEAN,
recurrent: DataTypes.JSON,
likes: { type: DataTypes.JSON, defaultValue: [] },
boost: { type: DataTypes.JSON, defaultValue: [] }
}, { sequelize, modelName: 'event' })
Event.belongsTo(Place)
Place.hasMany(Event)
Event.belongsTo(User)
2020-07-08 00:57:28 +02:00
User.hasMany(Event)
2020-06-27 02:10:10 +02:00
Event.belongsToMany(Tag, { through: 'event_tags' })
Tag.belongsToMany(Event, { through: 'event_tags' })
2020-06-27 02:10:10 +02:00
2020-07-08 00:57:28 +02:00
Event.belongsToMany(Notification, { through: EventNotification })
Notification.belongsToMany(Event, { through: EventNotification })
2019-06-26 14:44:21 +02:00
2020-06-27 02:10:10 +02:00
Event.hasMany(Resource)
Resource.belongsTo(Event)
2020-07-08 00:57:28 +02:00
2020-06-27 02:10:10 +02:00
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
Event.belongsTo(Event, { as: 'parent' })
SequelizeSlugify.slugifyModel(Event, { source: ['title'], overwrite: false })
2021-04-13 18:04:53 +02:00
2021-11-10 11:03:55 +01:00
Event.prototype.toAP = function (username, locale, to = []) {
2020-06-27 02:10:10 +02:00
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
2021-04-14 01:30:51 +02:00
const plainDescription = htmlToText(this.description && this.description.replace('\n', '').slice(0, 1000))
2020-11-06 11:05:05 +01:00
const content = `
2021-11-09 13:08:11 +01:00
📍 ${this.place && this.place.name}
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}
2020-06-27 02:10:10 +02:00
2021-11-09 13:08:11 +01:00
${plainDescription}
2020-06-27 02:10:10 +02:00
`
2021-07-19 12:29:35 +02:00
const attachment = []
if (this.media && this.media.length) {
attachment.push({
type: 'Document',
mediaType: 'image/jpeg',
url: `${config.baseurl}/media/${this.media[0].url}`,
name: this.media[0].name || this.title || '',
2021-07-19 12:29:35 +02:00
blurHash: null,
focalPoint: this.media[0].focalPoint || [0, 0]
})
}
2020-07-07 21:58:21 +02:00
2020-06-27 02:10:10 +02:00
return {
id: `${config.baseurl}/federation/m/${this.id}`,
name: this.title,
2021-11-09 13:08:11 +01:00
url: `${config.baseurl}/event/${this.slug || this.id}`,
type: 'Event',
startTime: moment.unix(this.start_datetime).locale(locale).format(),
endTime: this.end_datetime ? moment.unix(this.end_datetime).locale(locale).format() : null,
2021-11-09 13:08:11 +01:00
location: {
name: this.place.name,
address: this.place.address
2021-11-09 13:08:11 +01:00
},
2021-09-01 11:17:07 +02:00
attachment,
2021-11-09 13:08:11 +01:00
tag: tags && tags.map(tag => ({
type: 'Hashtag',
name: '#' + tag,
href: '/tags/' + tag
})),
2021-03-05 14:33:33 +01:00
published: dayjs(this.createdAt).utc().format(),
2020-06-27 02:10:10 +02:00
attributedTo: `${config.baseurl}/federation/u/${username}`,
2021-11-09 13:08:11 +01:00
to: ['https://www.w3.org/ns/activitystreams#Public'],
2021-03-05 14:33:33 +01:00
cc: [`${config.baseurl}/federation/u/${username}/followers`],
content,
2021-11-09 13:08:11 +01:00
summary: content
2020-06-27 02:10:10 +02:00
}
2019-06-09 00:45:50 +02:00
}
2020-06-27 02:10:10 +02:00
module.exports = Event