..
This commit is contained in:
@@ -9,6 +9,7 @@ const mail = require('../mail')
|
|||||||
const { user: User, event: Event, tag: Tag, place: Place } = require('../models')
|
const { user: User, event: Event, tag: Tag, place: Place } = require('../models')
|
||||||
const settingsController = require('./settings')
|
const settingsController = require('./settings')
|
||||||
const notifier = require('../../notifier')
|
const notifier = require('../../notifier')
|
||||||
|
const federation = require('../../federation/helpers')
|
||||||
|
|
||||||
const userController = {
|
const userController = {
|
||||||
async login(req, res) {
|
async login(req, res) {
|
||||||
@@ -121,6 +122,8 @@ const userController = {
|
|||||||
// send response to client
|
// send response to client
|
||||||
res.json(event)
|
res.json(event)
|
||||||
|
|
||||||
|
federation.sendEvent(event, user)
|
||||||
|
|
||||||
// send notification (mastodon/email/confirmation)
|
// send notification (mastodon/email/confirmation)
|
||||||
notifier.notifyEvent(event.id)
|
notifier.notifyEvent(event.id)
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
event.hasMany(models.comment)
|
event.hasMany(models.comment)
|
||||||
}
|
}
|
||||||
|
|
||||||
event.prototype.toAP = function (username) {
|
event.prototype.toAP = function (username, follower) {
|
||||||
return {
|
return {
|
||||||
id: `${config.baseurl}/federation/m/c_${this.id}`,
|
id: `${config.baseurl}/federation/m/c_${this.id}`,
|
||||||
type: 'Create',
|
type: 'Create',
|
||||||
@@ -48,6 +48,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
published: this.createdAt,
|
published: this.createdAt,
|
||||||
attributedTo: `${config.baseurl}/federation/u/${username}`,
|
attributedTo: `${config.baseurl}/federation/u/${username}`,
|
||||||
to: 'https://www.w3.org/ns/activitystreams#Public',
|
to: 'https://www.w3.org/ns/activitystreams#Public',
|
||||||
|
cc: [follower],
|
||||||
content: this.title
|
content: this.title
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,32 +4,29 @@ const crypto = require('crypto')
|
|||||||
const config = require('config')
|
const config = require('config')
|
||||||
|
|
||||||
const Helpers = {
|
const Helpers = {
|
||||||
async sendToFollowers (message, user) {
|
async signAndSend(message, user, to) {//, domain, req, res, targetOrigin) {
|
||||||
const followers = user.followers
|
|
||||||
console.error('send to ', followers)
|
|
||||||
for(let follower of followers) {
|
|
||||||
console.error('send message to ', follower)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async signAndSend(message, user, domain, req, res, targetOrigin) {
|
|
||||||
// get the URI of the actor object and append 'inbox' to it
|
// get the URI of the actor object and append 'inbox' to it
|
||||||
let inbox = message.object.actor+'/inbox'
|
const toInbox = to + '/inbox'
|
||||||
let inboxFragment = inbox.replace(targetOrigin,'')
|
const toOrigin = new URL(to).hostname
|
||||||
const targetDomain = new URL(targetOrigin).host
|
const toPath = toInbox.replace(toOrigin, '')
|
||||||
// get the private key
|
// get the private key
|
||||||
const privkey = user.rsa.privateKey
|
const privkey = user.rsa.privateKey
|
||||||
const signer = crypto.createSign('sha256')
|
const signer = crypto.createSign('sha256')
|
||||||
let d = new Date()
|
const d = new Date()
|
||||||
let stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${d.toUTCString()}`
|
const stringToSign = `(request-target): post ${toPath}\nhost: ${toOrigin}\ndate: ${d.toUTCString()}`
|
||||||
|
console.error('stringToSign ', stringToSign)
|
||||||
|
|
||||||
signer.update(stringToSign)
|
signer.update(stringToSign)
|
||||||
signer.end()
|
signer.end()
|
||||||
const signature = signer.sign(privkey)
|
const signature = signer.sign(privkey)
|
||||||
const signature_b64 = signature.toString('base64')
|
const signature_b64 = signature.toString('base64')
|
||||||
let header = `keyId="${config.baseurl}/federation/u/${user.username}",headers="(request-target) host date",signature="${signature_b64}"`
|
const header = `keyId="${config.baseurl}/federation/u/${user.username}",headers="(request-target) host date",signature="${signature_b64}"`
|
||||||
|
console.error('header ', header)
|
||||||
request({
|
request({
|
||||||
url: inbox,
|
url: inbox,
|
||||||
headers: {
|
headers: {
|
||||||
'Host': targetDomain,
|
'Host': toOrigin,
|
||||||
'Date': d.toUTCString(),
|
'Date': d.toUTCString(),
|
||||||
'Signature': header
|
'Signature': header
|
||||||
},
|
},
|
||||||
@@ -55,7 +52,17 @@ const Helpers = {
|
|||||||
'actor': `${config.baseurl}/federation/u/${user.username}`,
|
'actor': `${config.baseurl}/federation/u/${user.username}`,
|
||||||
'object': body,
|
'object': body,
|
||||||
}
|
}
|
||||||
Helpers.signAndSend(message, user, domain, req, res, targetOrigin)
|
// Helpers.signAndSend(message, user, domain, req, res, targetOrigin)
|
||||||
|
},
|
||||||
|
async sendEvent(event, user) {
|
||||||
|
console.error('devo inviare un evento ai followers')
|
||||||
|
const followers = user.followers
|
||||||
|
console.error('send to ', followers)
|
||||||
|
for(let follower of followers) {
|
||||||
|
console.error('send message to ', follower)
|
||||||
|
const body = event.toAP(user.username, follower)
|
||||||
|
Helpers.signAndSend(body, user, follower)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user