This commit is contained in:
lesion
2019-07-31 01:43:08 +02:00
parent a25efc9e59
commit a09b567e97
3 changed files with 28 additions and 17 deletions

View File

@@ -9,6 +9,7 @@ const mail = require('../mail')
const { user: User, event: Event, tag: Tag, place: Place } = require('../models')
const settingsController = require('./settings')
const notifier = require('../../notifier')
const federation = require('../../federation/helpers')
const userController = {
async login(req, res) {
@@ -121,6 +122,8 @@ const userController = {
// send response to client
res.json(event)
federation.sendEvent(event, user)
// send notification (mastodon/email/confirmation)
notifier.notifyEvent(event.id)

View File

@@ -37,7 +37,7 @@ module.exports = (sequelize, DataTypes) => {
event.hasMany(models.comment)
}
event.prototype.toAP = function (username) {
event.prototype.toAP = function (username, follower) {
return {
id: `${config.baseurl}/federation/m/c_${this.id}`,
type: 'Create',
@@ -48,6 +48,7 @@ module.exports = (sequelize, DataTypes) => {
published: this.createdAt,
attributedTo: `${config.baseurl}/federation/u/${username}`,
to: 'https://www.w3.org/ns/activitystreams#Public',
cc: [follower],
content: this.title
}
}

View File

@@ -4,32 +4,29 @@ const crypto = require('crypto')
const config = require('config')
const Helpers = {
async sendToFollowers (message, user) {
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) {
async signAndSend(message, user, to) {//, domain, req, res, targetOrigin) {
// get the URI of the actor object and append 'inbox' to it
let inbox = message.object.actor+'/inbox'
let inboxFragment = inbox.replace(targetOrigin,'')
const targetDomain = new URL(targetOrigin).host
const toInbox = to + '/inbox'
const toOrigin = new URL(to).hostname
const toPath = toInbox.replace(toOrigin, '')
// get the private key
const privkey = user.rsa.privateKey
const signer = crypto.createSign('sha256')
let d = new Date()
let stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${d.toUTCString()}`
const d = new Date()
const stringToSign = `(request-target): post ${toPath}\nhost: ${toOrigin}\ndate: ${d.toUTCString()}`
console.error('stringToSign ', stringToSign)
signer.update(stringToSign)
signer.end()
const signature = signer.sign(privkey)
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({
url: inbox,
headers: {
'Host': targetDomain,
'Host': toOrigin,
'Date': d.toUTCString(),
'Signature': header
},
@@ -55,7 +52,17 @@ const Helpers = {
'actor': `${config.baseurl}/federation/u/${user.username}`,
'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)
}
}
}