2019-07-31 01:03:21 +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')
|
|
|
|
|
2020-06-27 02:10:10 +02:00
|
|
|
const sequelize = require('./index')
|
|
|
|
|
|
|
|
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,
|
2021-07-15 16:18:56 +02:00
|
|
|
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' })
|
2021-04-09 23:54:17 +02:00
|
|
|
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' })
|
|
|
|
|
2021-04-13 18:04:53 +02:00
|
|
|
SequelizeSlugify.slugifyModel(Event, { source: ['title'] })
|
|
|
|
|
2020-11-06 11:05:05 +01:00
|
|
|
Event.prototype.toAPNote = 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 = `
|
|
|
|
${this.title}<br/><br/>
|
|
|
|
📍 ${this.place && this.place.name}<br/>
|
|
|
|
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}<br/><br/>
|
2020-06-27 02:10:10 +02:00
|
|
|
|
2020-11-06 11:05:05 +01:00
|
|
|
${plainDescription}<br/><br/>
|
2020-06-27 02:10:10 +02:00
|
|
|
|
2021-04-14 01:30:51 +02:00
|
|
|
<a href='${config.baseurl}/event/${this.slug || this.id}'>${config.baseurl}/event/${this.slug || this.id}</a><br/>
|
2020-06-27 02:10:10 +02:00
|
|
|
|
2020-11-06 11:05:05 +01:00
|
|
|
${tags && tags.map(t => `#${t}`)}
|
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 || '',
|
|
|
|
blurHash: null,
|
|
|
|
focalPoint: this.media[0].focalPoint || [0, 0]
|
|
|
|
})
|
|
|
|
}
|
2020-11-06 11:05:05 +01:00
|
|
|
// if (this.image_path) {
|
|
|
|
// }
|
2020-07-07 21:58:21 +02:00
|
|
|
|
2020-06-27 02:10:10 +02:00
|
|
|
return {
|
|
|
|
id: `${config.baseurl}/federation/m/${this.id}`,
|
2020-11-06 11:05:05 +01:00
|
|
|
// name: this.title,
|
2020-06-27 02:10:10 +02:00
|
|
|
url: `${config.baseurl}/event/${this.id}`,
|
2020-10-14 21:13:20 +02:00
|
|
|
type: 'Note',
|
2020-11-06 11:05:05 +01:00
|
|
|
// startTime: moment.unix(this.start_datetime).locale(locale).format(),
|
|
|
|
// endTime: moment.unix(this.end_datetime).locale(locale).format(),
|
|
|
|
// location: {
|
|
|
|
// name: this.place && this.place.name
|
|
|
|
// },
|
|
|
|
// attachment,
|
2021-03-05 14:33:33 +01:00
|
|
|
// tag: tags && tags.map(tag => ({
|
|
|
|
// type: 'Hashtag',
|
|
|
|
// name: '#' + tag,
|
|
|
|
// href: '/tags/' + tag
|
|
|
|
// })),
|
|
|
|
published: dayjs(this.createdAt).utc().format(),
|
2020-06-27 02:10:10 +02:00
|
|
|
attributedTo: `${config.baseurl}/federation/u/${username}`,
|
2020-11-06 11:05:05 +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,
|
|
|
|
summary: null
|
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
|