This commit is contained in:
les
2020-11-06 11:05:05 +01:00
parent 0ac2edf69d
commit 47ed677362
10 changed files with 161 additions and 144 deletions

View File

@@ -57,43 +57,43 @@ Resource.belongsTo(Event)
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
Event.belongsTo(Event, { as: 'parent' })
Event.prototype.toAP = function (username, locale, to = []) {
Event.prototype.toAPNote = function (username, locale, to = []) {
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
const plainDescription = htmlToText.fromString(this.description.replace('\n', '').slice(0, 1000))
const summary = `
📍 ${this.place && this.place.name}
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}
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/>
${plainDescription}
${plainDescription}<br/><br/>
<a href='${config.baseurl}/event/${this.id}'>${config.baseurl}/event/${this.id}</a><br/>
${tags && 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
})
}
to.push('https://www.w3.org/ns/activitystreams#Public')
// 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}`,
name: this.title,
// name: this.title,
url: `${config.baseurl}/event/${this.id}`,
type: 'Note',
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,
// 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,
tag: tags && tags.map(tag => ({
type: 'Hashtag',
name: '#' + tag,
@@ -101,9 +101,9 @@ Event.prototype.toAP = function (username, locale, to = []) {
})),
published: this.createdAt,
attributedTo: `${config.baseurl}/federation/u/${username}`,
to,
cc: [`${config.baseurl}/federation/u/${username}/followers`],
summary
to: 'https://www.w3.org/ns/activitystreams#Public',
// cc: [`${config.baseurl}/federation/u/${username}/followers`],
content
}
}

View File

@@ -78,17 +78,19 @@ const Helpers = {
const body = {
id: `${config.baseurl}/federation/m/${event.id}#create`,
type,
to: recipients[sharedInbox],
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${settingsController.settings.instance_name}/followers`],
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: [...recipients[sharedInbox], `${config.baseurl}/federation/u/${settingsController.settings.instance_name}/followers`],
actor: `${config.baseurl}/federation/u/${settingsController.settings.instance_name}`,
object: event.toAP(settingsController.settings.instance_name,
object: event.toAPNote(settingsController.settings.instance_name,
settingsController.settings.instance_locale,
recipients[sharedInbox])
}
body['@context'] = [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{ Hashtag: 'as:Hashtag' }]
{
Hashtag: 'as:Hashtag'
}]
Helpers.signAndSend(body, sharedInbox)
}
},

View File

@@ -37,7 +37,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.toAP(event.user.username, req.settings.locale))
return res.json(event.toAPNote(event.user.username, req.settings.locale))
})
// get any message coming from federation

View File

@@ -12,7 +12,6 @@ module.exports = {
const name = req.params.name
if (!name) { return res.status(400).send('Bad request.') }
// const user = await User.findOne({ where: { username: name } })
if (name !== req.settings.instance_name) { return res.status(404).send(`No record found for ${name}`) }
const ret = {
'@context': [
@@ -74,8 +73,8 @@ module.exports = {
id: `${config.baseurl}/federation/u/${name}/followers`,
type: 'OrderedCollection',
totalItems: followers.length,
first: `${config.baseurl}/federation/u/${name}/followers?page=true`,
last: `${config.baseurl}/federation/u/${name}/followers?page=true`,
// first: `${config.baseurl}/federation/u/${name}/followers?page=true`,
// last: `${config.baseurl}/federation/u/${name}/followers?page=true`,
orderedItems: followers.map(f => f.ap_id)
})
}
@@ -84,7 +83,7 @@ module.exports = {
id: `${config.baseurl}/federation/u/${name}/followers?page=${page}`,
type: 'OrderedCollectionPage',
totalItems: followers.length,
partOf: `${config.baseurl}/federation/u/${name}/followers`,
// partOf: `${config.baseurl}/federation/u/${name}/followers`,
orderedItems: followers.map(f => f.ap_id)
})
},
@@ -92,35 +91,41 @@ module.exports = {
async outbox (req, res) {
// TODO
const name = req.params.name
const page = req.query.page
// const page = req.query.page
if (!name) { return res.status(400).send('Bad request.') }
if (name !== req.settings.instance_name) { return res.status(404).send(`No record found for ${name}`) }
const events = await Event.findAll({ include: [{ model: Tag, required: false }, Place] })
debug('Inside outbox, should return all events from this user')
debug('Inside outbox, should return all events from this user -> ', events.length)
// https://www.w3.org/TR/activitypub/#outbox
res.type('application/activity+json; charset=utf-8')
if (!page) {
return res.json({
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${config.baseurl}/federation/u/${name}/outbox`,
type: 'OrderedCollection',
totalItems: events.length,
first: `${config.baseurl}/federation/u/${name}/outbox?page=true`,
last: `${config.baseurl}/federation/u/${name}/outbox?page=true`
})
}
// if (!page) {
// return res.json({
// '@context': 'https://www.w3.org/ns/activitystreams',
// id: `${config.baseurl}/federation/u/${name}/outbox`,
// type: 'OrderedCollection',
// totalItems: events.length,
// first: `${config.baseurl}/federation/u/${name}/outbox?page=true`,
// last: `${config.baseurl}/federation/u/${name}/outbox?page=true`
// })
// }
debug('With pagination %s', page)
// debug('With pagination %s', page)
return res.json({
'@context': ['https://www.w3.org/ns/activitystreams', { Hashtag: 'as:Hashtag' }],
id: `${config.baseurl}/federation/u/${name}/outbox?page=${page}`,
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{
hashtag: 'as:Hashtag'
}
],
id: `${config.baseurl}/federation/u/${name}/outbox`,
type: 'OrderedCollectionPage',
totalItems: events.length,
partOf: `${config.baseurl}/federation/u/${name}/outbox`,
// partOf: `${config.baseurl}/federation/u/${name}/outbox`,
orderedItems:
events.map(e => ({
id: `${config.baseurl}/federation/m/${e.id}#create`,
@@ -129,7 +134,7 @@ module.exports = {
cc: [`${config.baseurl}/federation/u/${name}/followers`],
published: e.createdAt,
actor: `${config.baseurl}/federation/u/${name}`,
object: e.toAP(name, req.settings.locale)
object: e.toAPNote(name, req.settings.locale)
}))
})
}

View File

@@ -23,6 +23,8 @@ domPurify.addHook('beforeSanitizeElements', node => {
if (node.hasAttribute && node.hasAttribute('href')) {
const href = node.getAttribute('href')
const text = node.textContent
// remove FB tracking param
if (href.includes('fbclid=')) {
try {
const url = new URL.URL(href)
@@ -113,7 +115,9 @@ module.exports = {
try {
const response = await axios.get(URL)
Microformats.get({ html: response.data, filter: ['h-event'] }, (err, data) => {
if (!data.items.length || !data.items[0].properties) return res.sendStatus(404)
if (err || !data.items.length || !data.items[0].properties) {
return res.sendStatus(404)
}
const event = data.items[0].properties
return res.json({
title: get(event, 'name[0]', ''),
@@ -129,7 +133,7 @@ module.exports = {
// const event = dom.window.document.querySelected(".h-event")
// console.error(event)
// console.error(response)
} catch(e){
} catch (e) {
console.error(e)
}