Commit 1a4d4a97 authored by les's avatar les
Browse files

Merge branch 'feat/ap_event' into dev

parents 06135a2b 95784c09
const config = require('config')
const moment = require('moment-timezone')
const htmlToText = require('html-to-text')
module.exports = (sequelize, DataTypes) => {
const Event = sequelize.define('event', {
......@@ -38,35 +39,36 @@ module.exports = (sequelize, DataTypes) => {
Event.belongsTo(models.event, { as: 'parent' })
}
Event.prototype.toNoteAP = function (username, locale, follower = []) {
Event.prototype.toAP = function (username, locale, follower = []) {
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>`
}).join(' ')
const content = `<a href='${config.baseurl}/event/${this.id}'>${this.title}</a><br/>
📍 ${this.place.name}<br/>
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}<br/><br/>
${this.description.length > 500 ? this.description.substr(0, 500) + '...' : this.description}<br/>
${tag_links} <br/>`
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)')}
// const attachment = []
// if (this.image_path) {
// attachment.push({
// type: 'Document',
// mediaType: 'image/jpeg',
// url: `${config.baseurl}/media/${this.image_path}`,
// name: null,
// blurHash: null
// })
// }
${plainDescription}
${tags.map(t => `#${t}`)}
`
const attachment = []
if (this.image_path) {
attachment.push({
type: 'Document',
mediaType: 'image/jpeg',
url: `${config.baseurl}/media/${this.image_path}`,
name: null,
blurHash: null
})
}
return {
id: `${config.baseurl}/federation/m/${this.id}`,
url: `${config.baseurl}/federation/m/${this.id}`,
type: 'Note',
// do not send attachment, in mastodon a link preview is shown instead
// attachment,
url: `${config.baseurl}/event/${this.id}`,
type: 'Event',
attachment,
tag: tags.map(tag => ({
type: 'Hashtag',
name: '#' + tag,
......@@ -76,8 +78,8 @@ module.exports = (sequelize, DataTypes) => {
attributedTo: `${config.baseurl}/federation/u/${username}`,
to: follower || [],
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${username}/followers`],
content,
summary: null,
name: this.title,
summary,
sensitive: false
}
}
......
......@@ -80,7 +80,7 @@ const Helpers = {
to: recipients[sharedInbox],
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${settingsController.settings.instance_name}/followers`],
actor: `${config.baseurl}/federation/u/${settingsController.settings.instance_name}`,
object: event.toNoteAP(settingsController.settings.instance_name,
object: event.toAP(settingsController.settings.instance_name,
settingsController.settings.instance_locale,
recipients[sharedInbox])
}
......
......@@ -33,7 +33,7 @@ router.get('/m/:event_id', async (req, res) => {
const event = await Event.findByPk(req.params.event_id, { include: [User, Tag, Place] })
if (!event) { return res.status(404).send('Not found') }
return res.json(event.toNoteAP(event.user.username, req.settings.locale))
return res.json(event.toAP(event.user.username, req.settings.locale))
})
// get any message coming from federation
......@@ -72,6 +72,8 @@ router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => {
if (b.object.type === 'Note') {
debug('Create a resource!')
await Resources.create(req, res)
} else if (b.object.type === 'Event') {
debug('Event type is coming!!')
} else {
// await Resources.create(req, res)
debug('Create with unsupported Object or not a reply => %s ', b.object.type)
......@@ -80,9 +82,16 @@ router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => {
}
})
function redirect_on_html_accepted (req, res, next) {
if (req.accepts('html')) {
return res.redirect(settingsController.settings.baseurl)
}
return next()
}
router.get('/u/:name/outbox', Users.outbox)
router.get('/u/:name/followers', Users.followers)
router.get('/u/:name', Users.get)
router.get('/u/:name', redirect_on_html_accepted, Users.get)
// Handle 404
router.use((req, res) => {
......
......@@ -116,7 +116,7 @@ module.exports = {
type: 'OrderedCollectionPage',
totalItems: events.length,
partOf: `${config.baseurl}/federation/u/${name}/outbox`,
orderedItems: events.map(e => ({ ...e.toNoteAP(name, req.settings.locale), actor: `${config.baseurl}/federation/u/${name}` }))
orderedItems: events.map(e => ({ ...e.toAP(name, req.settings.locale), actor: `${config.baseurl}/federation/u/${name}` }))
// user.events.map(e => ({
// id: `${config.baseurl}/federation/m/${e.id}#create`,
// type: 'Create',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment