gancio-upstream/server/api/models/event.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-07-31 01:03:21 +02:00
const config = require('config')
const moment = require('moment-timezone')
2020-03-31 18:15:21 +02:00
const htmlToText = require('html-to-text')
2019-07-31 01:03:21 +02:00
2019-06-06 23:54:32 +02:00
module.exports = (sequelize, DataTypes) => {
2019-12-04 01:18:05 +01:00
const Event = sequelize.define('event', {
2019-07-11 23:31:37 +02:00
id: {
2019-10-25 18:43:49 +02:00
allowNull: false,
2019-07-11 23:31:37 +02:00
type: DataTypes.INTEGER,
primaryKey: true,
2019-09-11 19:12:24 +02:00
autoIncrement: true
2019-07-11 23:31:37 +02:00
},
2019-06-06 23:54:32 +02:00
title: DataTypes.STRING,
slug: DataTypes.STRING,
description: DataTypes.TEXT,
multidate: DataTypes.BOOLEAN,
2019-06-07 17:02:33 +02:00
start_datetime: {
type: DataTypes.INTEGER,
index: true
},
end_datetime: {
type: DataTypes.INTEGER,
2019-06-06 23:54:32 +02:00
index: true
},
image_path: DataTypes.STRING,
is_visible: DataTypes.BOOLEAN,
2019-07-11 23:31:37 +02:00
recurrent: DataTypes.JSON,
2019-08-01 15:18:45 +02:00
likes: { type: DataTypes.JSON, defaultValue: [] },
boost: { type: DataTypes.JSON, defaultValue: [] }
2019-06-07 17:02:33 +02:00
}, {})
2019-06-26 14:44:21 +02:00
2019-12-04 01:18:05 +01:00
Event.associate = function (models) {
Event.belongsTo(models.place)
Event.belongsTo(models.user)
Event.belongsToMany(models.tag, { through: 'event_tags' })
Event.belongsToMany(models.notification, { through: 'event_notification' })
Event.hasMany(models.resource)
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
Event.belongsTo(models.event, { as: 'parent' })
2019-06-07 17:02:33 +02:00
}
2019-06-26 14:44:21 +02:00
2020-03-14 18:47:49 +01:00
Event.prototype.toAP = function (username, locale, follower = []) {
2020-02-10 00:46:23 +01:00
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
2019-09-26 22:46:04 +02:00
2020-03-31 18:15:21 +02:00
const plainDescription = htmlToText.fromString(this.description.replace('\n', '').slice(0, 1000))
const summary = `
📍 ${this.place.name}
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}
${plainDescription}
${tags.map(t => `#${t}`)}
`
2019-09-11 12:00:13 +02:00
2020-03-14 18:47:49 +01:00
const attachment = []
if (this.image_path) {
attachment.push({
type: 'Document',
2020-06-14 20:58:23 +02:00
mediaType: 'image/webp',
2020-03-14 18:47:49 +01:00
url: `${config.baseurl}/media/${this.image_path}`,
name: null,
blurHash: null
})
}
2019-07-31 01:03:21 +02:00
return {
2019-09-11 19:12:24 +02:00
id: `${config.baseurl}/federation/m/${this.id}`,
2020-06-14 20:58:23 +02:00
name: this.title,
2020-03-31 18:15:21 +02:00
url: `${config.baseurl}/event/${this.id}`,
2020-03-14 18:47:49 +01:00
type: 'Event',
2020-06-14 20:58:23 +02:00
startTime: moment.unix(this.start_datetime).locale(locale).format(),
endTime: moment.unix(this.end_datetime).locale(locale).format(),
location: {
name: this.place.name
},
2020-03-14 18:47:49 +01:00
attachment,
2019-11-06 09:58:06 +01:00
tag: tags.map(tag => ({
2019-09-11 19:12:24 +02:00
type: 'Hashtag',
2019-11-06 09:58:06 +01:00
name: '#' + tag,
href: '/tags/' + tag
2019-09-11 19:12:24 +02:00
})),
published: this.createdAt,
attributedTo: `${config.baseurl}/federation/u/${username}`,
2020-02-05 00:48:55 +01:00
to: follower || [],
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${username}/followers`],
2020-03-31 18:15:21 +02:00
summary,
2019-10-28 17:33:20 +01:00
sensitive: false
2019-07-31 01:03:21 +02:00
}
}
2019-12-04 01:18:05 +01:00
return Event
2019-06-09 00:45:50 +02:00
}