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

87 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-07-31 01:03:21 +02:00
const config = require('config')
2019-08-07 19:36:10 +02:00
const moment = require('moment')
2019-07-31 01:03:21 +02:00
2019-06-06 23:54:32 +02:00
module.exports = (sequelize, DataTypes) => {
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,
// parent: DataTypes.INTEGER
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-06-07 17:02:33 +02:00
event.associate = function (models) {
2019-06-06 23:54:32 +02:00
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.comment)
2019-06-07 17:02:33 +02:00
}
2019-06-26 14:44:21 +02:00
2019-09-11 21:20:44 +02:00
event.prototype.toAP = function (username, follower = []) {
2019-11-06 09:58:06 +01:00
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, ' '))
const tag_links = tags.map(t => {
return `<a href='/tags/${t}' class='mention hashtag status-link' rel='tag'><span>#${t}</span></a>`
2019-10-20 14:22:55 +02:00
}).join(' ')
2019-09-26 22:46:04 +02:00
2019-11-06 09:58:06 +01:00
// @todo: each instance support different note's length
2019-09-18 12:55:33 +02:00
const content = `<a href='${config.baseurl}/event/${this.id}'>${this.title}</a><br/>
2019-11-06 09:58:06 +01:00
📍 ${this.place.name}<br/>
📅 ${moment.unix(this.start_datetime).format('dddd, D MMMM (HH:mm)')}<br/><br/>
2019-09-11 12:00:13 +02:00
${this.description.length > 200 ? this.description.substr(0, 200) + '...' : this.description}<br/>
2019-11-06 09:58:06 +01:00
${tag_links} <br/>`
2019-09-11 12:00:13 +02:00
2019-09-11 19:12:24 +02:00
const attachment = []
2019-09-11 12:00:13 +02:00
if (this.image_path) {
attachment.push({
type: 'Document',
mediaType: 'image/jpeg',
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}`,
url: `${config.baseurl}/federation/m/${this.id}`,
2019-09-26 22:46:04 +02:00
type: 'Note',
2019-11-06 09:58:06 +01:00
// do not send attachment, in mastodon a link preview is shown instead
2019-09-26 22:46:04 +02: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}`,
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: follower || [],
content,
summary: null,
2019-10-28 17:33:20 +01:00
sensitive: false
2019-07-31 01:03:21 +02:00
}
}
2019-06-07 17:02:33 +02:00
return event
2019-06-09 00:45:50 +02:00
}