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

117 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-07-31 01:03:21 +02:00
const config = require('config')
2020-11-17 00:34:56 +01:00
const moment = require('dayjs')
2020-03-31 18:15:21 +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')
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,
slug: DataTypes.STRING,
description: DataTypes.TEXT,
multidate: DataTypes.BOOLEAN,
start_datetime: {
type: DataTypes.INTEGER,
index: true
},
end_datetime: {
type: DataTypes.INTEGER,
index: true
},
image_path: DataTypes.STRING,
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' })
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-03-05 14:33:33 +01:00
const plainDescription = htmlToText.fromString(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
2020-11-06 11:05:05 +01:00
<a href='${config.baseurl}/event/${this.id}'>${config.baseurl}/event/${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
`
2020-11-06 11:05:05 +01:00
// const attachment = []
// if (this.image_path) {
// attachment.push({
// type: 'Document',
// mediaType: 'image/jpeg',
// url: `${config.baseurl}/media/${this.image_path}`,
// name: null,
// blurHash: null
// })
// }
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