From 8029cfcd9221da9164d731ab3e7c20740f52fab7 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 6 Jan 2020 21:35:42 -0800 Subject: lots of refactoring --- activitypub.js | 937 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 937 insertions(+) create mode 100644 activitypub.js (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js new file mode 100644 index 0000000..3d7fb10 --- /dev/null +++ b/activitypub.js @@ -0,0 +1,937 @@ +const domain = require('./config/domain.js').domain; +const contactEmail = require('./config/domain.js').email; +const siteName = require('./config/domain.js').sitename +const request = require('request'); +const addToLog = require('./helpers.js').addToLog; +const crypto = require('crypto'); +const shortid = require('shortid'); +var moment = require('moment-timezone'); +const mongoose = require('mongoose'); +const Event = mongoose.model('Event'); +const EventGroup = mongoose.model('EventGroup'); +var sanitizeHtml = require('sanitize-html'); + +function createActivityPubActor(eventID, domain, pubkey, description, name, location, imageFilename, startUTC, endUTC, timezone) { + let actor = { + '@context': [ + 'https://www.w3.org/ns/activitystreams', + 'https://w3id.org/security/v1' + ], + + 'id': `https://${domain}/${eventID}`, + 'type': 'Person', + 'preferredUsername': `${eventID}`, + 'inbox': `https://${domain}/activitypub/inbox`, + 'outbox': `https://${domain}/${eventID}/outbox`, + 'followers': `https://${domain}/${eventID}/followers`, + 'summary': `

${description}

`, + 'name': name, + 'featured': `https://${domain}/${eventID}/featured`, + + 'publicKey': { + 'id': `https://${domain}/${eventID}#main-key`, + 'owner': `https://${domain}/${eventID}`, + 'publicKeyPem': pubkey + } + }; + if (location) { + actor.summary += `

Location: ${location}.

` + } + let displayDate; + if (startUTC && timezone) { + displayDate = moment.tz(startUTC, timezone).format('D MMMM YYYY h:mm a'); + actor.summary += `

Starting ${displayDate} ${timezone}.

`; + } + if (imageFilename) { + actor.icon = { + 'type': 'Image', + 'mediaType': 'image/jpg', + 'url': `https://${domain}/events/${imageFilename}`, + }; + } + return JSON.stringify(actor); +} + +function createActivityPubEvent(name, startUTC, endUTC, timezone, description, location) { + const guid = crypto.randomBytes(16).toString('hex'); + console.log(startUTC); + let eventObject = { + "@context": "https://www.w3.org/ns/activitystreams", + 'id': `https://${domain}/${guid}`, + "name": name, + "type": "Event", + "startTime": moment.tz(startUTC, timezone).format(), + "endTime": moment.tz(endUTC, timezone).format(), + "content": description, + "location": location + } + return JSON.stringify(eventObject); +} + +function createFeaturedPost(eventID, name, startUTC, endUTC, timezone, description, location) { + const featured = { + "@context": "https://www.w3.org/ns/activitystreams", + "id": `https://${domain}/${eventID}/m/featuredPost`, + "type": "Note", + "name": "Test", + 'cc': 'https://www.w3.org/ns/activitystreams#Public', + "content": `

This is an event that was posted on ${siteName}. If you follow this account, you'll see updates in your timeline about the event. If your software supports polls, you should get a poll in your DMs asking if you want to RSVP. You can reply and RSVP right from there. If your software has an event calendar built in, you should get an event in your inbox that you can RSVP to like you respond to any event.

For more information on how to interact with this, check out this link.

`, + 'attributedTo': `https://${domain}/${eventID}`, + } + return featured; +} + +function updateActivityPubEvent(oldEvent, name, startUTC, endUTC, timezone, description, location) { + // we want to persist the old ID no matter what happens to the Event itself + const id = oldEvent.id; + let eventObject = { + "@context": "https://www.w3.org/ns/activitystreams", + 'id': id, + "name": name, + "type": "Event", + "startTime": moment.tz(startUTC, timezone).format(), + "endTime": moment.tz(endUTC, timezone).format(), + "content": description, + "location": location + } + return JSON.stringify(eventObject); +} + + +function updateActivityPubActor(actor, description, name, location, imageFilename, startUTC, endUTC, timezone) { + if (!actor) return; + actor.summary = `

${description}

`; + actor.name = name; + if (location) { + actor.summary += `

Location: ${location}.

` + } + let displayDate; + if (startUTC && timezone) { + displayDate = moment.tz(startUTC, timezone).format('D MMMM YYYY h:mm a'); + actor.summary += `

Starting ${displayDate} ${timezone}.

`; + } + if (imageFilename) { + actor.icon = { + 'type': 'Image', + 'mediaType': 'image/jpg', + 'url': `https://${domain}/events/${imageFilename}`, + }; + } + return JSON.stringify(actor); +} + +function signAndSend(message, eventID, targetDomain, inbox, callback) { + let inboxFragment = inbox.replace('https://'+targetDomain,''); + // get the private key + Event.findOne({ + id: eventID + }) + .then((event) => { + if (event) { + const privateKey = event.privateKey; + const signer = crypto.createSign('sha256'); + let d = new Date(); + let stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${d.toUTCString()}`; + signer.update(stringToSign); + signer.end(); + const signature = signer.sign(privateKey); + const signature_b64 = signature.toString('base64'); + const header = `keyId="https://${domain}/${eventID}",headers="(request-target) host date",signature="${signature_b64}"`; + request({ + url: inbox, + headers: { + 'Host': targetDomain, + 'Date': d.toUTCString(), + 'Signature': header + }, + method: 'POST', + json: true, + body: message + }, function (error, response){ + if (error) { + console.log('Error:', error, response.body); + callback(error, null, 500); + } + else { + console.log('Response:', response.statusCode); + // Add the message to the database + const messageID = message.id; + const newMessage = { + id: message.id, + content: JSON.stringify(message) + }; + Event.findOne({ + id: eventID, + }, function(err,event) { + if (!event) return; + event.activityPubMessages.push(newMessage); + // also add the message's object if it has one + if (message.object && message.object.id) { + event.activityPubMessages.push({ + id: message.object.id, + content: JSON.stringify(message.object) + }); + } + event.save() + .then(() => { + addToLog("addActivityPubMessage", "success", "ActivityPubMessage added to event " + eventID); + console.log('successful ActivityPubMessage add'); + callback(null, message.id, 200); + }) + .catch((err) => { addToLog("addActivityPubMessage", "error", "Attempt to add ActivityPubMessage to event " + eventID + " failed with error: " + err); + console.log('error', err) + callback(err, null, 500); + }); + }) + } + }); + } + else { + callback(`No record found for ${eventID}.`, null, 404); + } + }); +} + +// this function sends something to the timeline of every follower in the followers array +// it's also an unlisted public message, meaning non-followers can see the message if they look at +// the profile but it doesn't spam federated timelines +function broadcastCreateMessage(apObject, followers, eventID, callback) { + callback = callback || function() {}; + let guidCreate = crypto.randomBytes(16).toString('hex'); + console.log('broadcasting'); + // iterate over followers + for (const follower of followers) { + let actorId = follower.actorId; + let myURL = new URL(actorId); + let targetDomain = myURL.hostname; + // get the inbox + Event.findOne({ + id: eventID, + }, function(err, event) { + console.log('found the event for broadcast') + if (event) { + const follower = event.followers.find(el => el.actorId === actorId); + if (follower) { + const actorJson = JSON.parse(follower.actorJson); + const inbox = actorJson.inbox; + console.log('found the inbox for', actorId) + const createMessage = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': `https://${domain}/${eventID}/m/${guidCreate}`, + 'type': 'Create', + 'actor': `https://${domain}/${eventID}`, + 'to': [actorId], + 'cc': 'https://www.w3.org/ns/activitystreams#Public', + 'object': apObject + }; + signAndSend(createMessage, eventID, targetDomain, inbox, function(err, resp, status) { + if (err) { + console.log(`Didn't sent to ${actorId}, status ${status} with error ${err}`); + } + else { + console.log('sent to', actorId); + } + }); + } + else { + callback(`No follower found with the id ${actorId}`, null, 404); + } + } + else { + callback(`No event found with the id ${eventID}`, null, 404); + } + }); + } // end followers +} + + +// sends an Announce for the apObject +function broadcastAnnounceMessage(apObject, followers, eventID, callback) { + callback = callback || function() {}; + let guidUpdate = crypto.randomBytes(16).toString('hex'); + console.log('broadcasting announce'); + // iterate over followers + for (const follower of followers) { + let actorId = follower.actorId; + let myURL = new URL(actorId); + let targetDomain = myURL.hostname; + // get the inbox + Event.findOne({ + id: eventID, + }, function(err, event) { + console.log('found the event for broadcast') + if (event) { + const follower = event.followers.find(el => el.actorId === actorId); + if (follower) { + const actorJson = JSON.parse(follower.actorJson); + const inbox = actorJson.inbox; + const announceMessage = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': `https://${domain}/${eventID}/m/${guidUpdate}`, + 'cc': 'https://www.w3.org/ns/activitystreams#Public', + 'type': 'Announce', + 'actor': `https://${domain}/${eventID}`, + 'object': apObject, + 'to': actorId + }; + signAndSend(announceMessage, eventID, targetDomain, inbox, function(err, resp, status) { + if (err) { + console.log(`Didn't sent to ${actorId}, status ${status} with error ${err}`); + } + else { + console.log('sent to', actorId); + } + }); + } + else { + console.log(`No follower found with the id ${actorId}`); + callback(`No follower found with the id ${actorId}`, null, 404); + } + } + else { + console.log(`No event found with the id ${eventID}`); + callback(`No event found with the id ${eventID}`, null, 404); + } + }); + } // end followers +} + +// sends an Update for the apObject +function broadcastUpdateMessage(apObject, followers, eventID, callback) { + callback = callback || function() {}; + let guidUpdate = crypto.randomBytes(16).toString('hex'); + console.log('broadcasting update'); + // iterate over followers + for (const follower of followers) { + let actorId = follower.actorId; + let myURL = new URL(actorId); + let targetDomain = myURL.hostname; + // get the inbox + Event.findOne({ + id: eventID, + }, function(err, event) { + console.log('found the event for broadcast') + if (event) { + const follower = event.followers.find(el => el.actorId === actorId); + if (follower) { + const actorJson = JSON.parse(follower.actorJson); + const inbox = actorJson.inbox; + const createMessage = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': `https://${domain}/${eventID}/m/${guidUpdate}`, + 'type': 'Update', + 'actor': `https://${domain}/${eventID}`, + 'object': apObject + }; + signAndSend(createMessage, eventID, targetDomain, inbox, function(err, resp, status) { + if (err) { + console.log(`Didn't sent to ${actorId}, status ${status} with error ${err}`); + } + else { + console.log('sent to', actorId); + } + }); + } + else { + callback(`No follower found with the id ${actorId}`, null, 404); + } + } + else { + callback(`No event found with the id ${eventID}`, null, 404); + } + }); + } // end followers +} + +function broadcastDeleteMessage(apObject, followers, eventID, callback) { + callback = callback || function() {}; + // we need to build an array of promises for each message we're sending, run Promise.all(), and then that will resolve when every message has been sent (or failed) + // per spec, each promise will execute *as it is built*, which is fine, we just need the guarantee that they are all done + let promises = []; + + let guidUpdate = crypto.randomBytes(16).toString('hex'); + console.log('building promises'); + // iterate over followers + for (const follower of followers) { + promises.push(new Promise((resolve, reject) => { + let actorId = follower.actorId; + let myURL = new URL(actorId); + let targetDomain = myURL.hostname; + // get the inbox + Event.findOne({ + id: eventID, + }, function(err, event) { + console.log('found the event for broadcast'); + if (event) { + const follower = event.followers.find(el => el.actorId === actorId); + if (follower) { + const actorJson = JSON.parse(follower.actorJson); + const inbox = actorJson.inbox; + const createMessage = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': `https://${domain}/${eventID}/m/${guidUpdate}`, + 'type': 'Delete', + 'actor': `https://${domain}/${eventID}`, + 'object': apObject + }; + signAndSend(createMessage, eventID, targetDomain, inbox, function(err, resp, status) { + if (err) { + console.log(`Didn't send to ${actorId}, status ${status} with error ${err}`); + reject(`Didn't send to ${actorId}, status ${status} with error ${err}`); + } + else { + console.log('sent to', actorId); + resolve('sent to', actorId); + } + }); + } + else { + console.log(`No follower found with the id ${actorId}`, null, 404); + reject(`No follower found with the id ${actorId}`, null, 404); + } + } + else { + console.log(`No event found with the id ${eventID}`, null, 404); + reject(`No event found with the id ${eventID}`, null, 404); + } + }); + })); + } // end followers + + Promise.all(promises.map(p => p.catch(e => e))).then(statuses => { + console.log('DONE') + console.log(statuses) + callback(statuses); + }); +} + +// this sends a message "to:" an individual fediverse user +function sendDirectMessage(apObject, actorId, eventID, callback) { + callback = callback || function() {}; + const guidCreate = crypto.randomBytes(16).toString('hex'); + const guidObject = crypto.randomBytes(16).toString('hex'); + let d = new Date(); + + apObject.published = d.toISOString(); + apObject.attributedTo = `https://${domain}/${eventID}`; + apObject.to = actorId; + apObject.id = `https://${domain}/${eventID}/m/${guidObject}`; + apObject.content = unescape(apObject.content) + + let createMessage = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': `https://${domain}/${eventID}/m/${guidCreate}`, + 'type': 'Create', + 'actor': `https://${domain}/${eventID}`, + 'to': [actorId], + 'object': apObject + }; + + let myURL = new URL(actorId); + let targetDomain = myURL.hostname; + // get the inbox + Event.findOne({ + id: eventID, + }, function(err, event) { + if (event) { + const follower = event.followers.find(el => el.actorId === actorId); + if (follower) { + const actorJson = JSON.parse(follower.actorJson); + const inbox = actorJson.inbox; + signAndSend(createMessage, eventID, targetDomain, inbox, callback); + } + else { + callback(`No follower found with the id ${actorId}`, null, 404); + } + } + else { + callback(`No event found with the id ${eventID}`, null, 404); + } + }); +} + +function sendAcceptMessage(thebody, eventID, targetDomain, callback) { + callback = callback || function() {}; + const guid = crypto.randomBytes(16).toString('hex'); + const actorId = thebody.actor; + let message = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': `https://${domain}/${guid}`, + 'type': 'Accept', + 'actor': `https://${domain}/${eventID}`, + 'object': thebody, + }; + // get the inbox + Event.findOne({ + id: eventID, + }, function(err, event) { + if (event) { + const follower = event.followers.find(el => el.actorId === actorId); + if (follower) { + const actorJson = JSON.parse(follower.actorJson); + const inbox = actorJson.inbox; + signAndSend(message, eventID, targetDomain, inbox, callback); + } + } + else { + callback(`Could not find event ${eventID}`, null, 404); + } + }); +} + +function _handleFollow(req, res) { + const myURL = new URL(req.body.actor); + let targetDomain = myURL.hostname; + let eventID = req.body.object.replace(`https://${domain}/`,''); + // Add the user to the DB of accounts that follow the account + // get the follower's username + request({ + url: req.body.actor, + headers: { + 'Accept': 'application/activity+json', + 'Content-Type': 'application/activity+json' + }}, function (error, response, body) { + body = JSON.parse(body) + const name = body.preferredUsername || body.name || body.attributedTo; + const newFollower = { + actorId: req.body.actor, + followId: req.body.id, + name: name, + actorJson: JSON.stringify(body) + }; + Event.findOne({ + id: eventID, + }, function(err,event) { + // if this account is NOT already in our followers list, add it + if (event && !event.followers.map(el => el.actorId).includes(req.body.actor)) { + console.log('made it!') + event.followers.push(newFollower); + event.save() + .then(() => { + addToLog("addEventFollower", "success", "Follower added to event " + eventID); + console.log('successful follower add'); + // Accept the follow request + sendAcceptMessage(req.body, eventID, targetDomain, function(err, resp, status) { + if (err) { + console.log(`Didn't send Accept to ${req.body.actor}, status ${status} with error ${err}`); + } + else { + console.log('sent Accept to', req.body.actor); + // ALSO send an ActivityPub Event activity since this person is "interested" in the event, as indicated by the Follow + const jsonEventObject = JSON.parse(event.activityPubEvent); + // send direct message to user + sendDirectMessage(jsonEventObject, newFollower.actorId, event.id); + + // if users can self-RSVP, send a Question to the new follower + if (event.usersCanAttend) { + const jsonObject = { + "@context": "https://www.w3.org/ns/activitystreams", + "name": `RSVP to ${event.name}`, + "type": "Question", + "content": `@${name} Will you attend ${event.name}? (If you reply "Yes", you'll be listed as an attendee on the event page.)`, + "oneOf": [ + {"type":"Note","name": "Yes"}, + {"type":"Note","name": "No"}, + {"type":"Note","name": "Maybe"} + ], + "endTime":event.start.toISOString(), + "tag":[{"type":"Mention","href":req.body.actor,"name":name}] + } + // send direct message to user + sendDirectMessage(jsonObject, req.body.actor, eventID, function (error, response, statuscode) { + if (error) { + console.log(error); + return res.status(statuscode).json(error); + } + else { + return res.status(statuscode).json({messageid: response}); + } + }); + } + } + }); + }) + .catch((err) => { + addToLog("addEventFollower", "error", "Attempt to add follower to event " + eventID + " failed with error: " + err); + return res.status(500).send('Database error, please try again :('); + }); + } + else { + // this person is already a follower so just say "ok" + return res.status(200); + } + }) + }) //end request +} + +function _handleUndoFollow(req, res) { + // get the record of all followers for this account + const eventID = req.body.object.object.replace(`https://${domain}/`,''); + Event.findOne({ + id: eventID, + }, function(err,event) { + if (!event) return; + // check to see if the Follow object's id matches the id we have on record + // is this even someone who follows us + const indexOfFollower = event.followers.findIndex(el => el.actorId === req.body.object.actor); + if (indexOfFollower !== -1) { + // does the id we have match the id we are being given + if (event.followers[indexOfFollower].followId === req.body.object.id) { + // we have a match and can trust the Undo! remove this person from the followers list + event.followers.splice(indexOfFollower, 1); + event.save() + .then(() => { + addToLog("removeEventFollower", "success", "Follower removed from event " + eventID); + return res.sendStatus(200); + }) + .catch((err) => { + addToLog("removeEventFollower", "error", "Attempt to remove follower from event " + eventID + " failed with error: " + err); + return res.send('Database error, please try again :('); + }); + } + } + }); +} + +function _handleAcceptEvent(req, res) { + let {name, attributedTo, inReplyTo, to, actor} = req.body; + if (Array.isArray(to)) { + to = to[0]; + } + const eventID = to.replace(`https://${domain}/`,''); + Event.findOne({ + id: eventID, + }, function(err,event) { + if (!event) return; + // does the id we got match the id of a thing we sent out + const message = event.activityPubMessages.find(el => el.id === req.body.object); + if (message) { + // it's a match + request({ + url: actor, + headers: { + 'Accept': 'application/activity+json', + 'Content-Type': 'application/activity+json' + }}, function (error, response, body) { + body = JSON.parse(body) + // if this account is NOT already in our attendees list, add it + if (!event.attendees.map(el => el.id).includes(actor)) { + const attendeeName = body.preferredUsername || body.name || actor; + const newAttendee = { + name: attendeeName, + status: 'attending', + id: actor + }; + event.attendees.push(newAttendee); + event.save() + .then((fullEvent) => { + addToLog("addEventAttendee", "success", "Attendee added to event " + req.params.eventID); + // get the new attendee with its hidden id from the full event + let fullAttendee = fullEvent.attendees.find(el => el.id === actor); + // send a "click here to remove yourself" link back to the user as a DM + const jsonObject = { + "@context": "https://www.w3.org/ns/activitystreams", + "name": `RSVP to ${event.name}`, + "type": "Note", + "content": `@${newAttendee.name} Thanks for RSVPing! You can remove yourself from the RSVP list by clicking here: https://${domain}/oneclickunattendevent/${event.id}/${fullAttendee._id}`, + "tag":[{"type":"Mention","href":newAttendee.id,"name":newAttendee.name}] + } + // send direct message to user + sendDirectMessage(jsonObject, newAttendee.id, event.id); + return res.sendStatus(200); + }) + .catch((err) => { + addToLog("addEventAttendee", "error", "Attempt to add attendee to event " + req.params.eventID + " failed with error: " + err); + return res.status(500).send('Database error, please try again :('); + }); + } + else { + // it's a duplicate and this person is already rsvped so just say OK + return res.status(200).send("Attendee is already registered."); + } + }); + } + }); +} + +function _handleUndoAcceptEvent(req, res) { + let {name, attributedTo, inReplyTo, to, actor} = req.body; + if (Array.isArray(to)) { + to = to[0]; + } + const eventID = to.replace(`https://${domain}/`,''); + console.log(eventID) + Event.findOne({ + id: eventID, + }, function(err,event) { + if (!event) return; + // does the id we got match the id of a thing we sent out + console.log('EVENT MESSAGES') + console.log(event.activityPubMessages); + const message = event.activityPubMessages.find(el => el.id === req.body.object.object); + if (message) { + // it's a match + console.log('match!!!!') + Event.update( + { id: eventID }, + { $pull: { attendees: { id: actor } } } + ) + .then(response => { + console.log(response) + addToLog("oneClickUnattend", "success", "Attendee removed via one click unattend " + req.params.eventID); + }); + } + }); +} + +function _handleCreateNote(req, res) { + console.log('create note inreplyto!!!') + // figure out what this is in reply to -- it should be addressed specifically to us + let {name, attributedTo, inReplyTo, to} = req.body.object; + // if it's an array just grab the first element, since a poll should only broadcast back to the pollster + if (Array.isArray(to)) { + to = to[0]; + } + const eventID = to.replace(`https://${domain}/`,''); + // make sure this person is actually a follower + Event.findOne({ + id: eventID, + }, function(err,event) { + if (!event) return; + // is this even someone who follows us + const indexOfFollower = event.followers.findIndex(el => el.actorId === req.body.object.attributedTo); + if (indexOfFollower !== -1) { + console.log('this person does follow us!') + // compare the inReplyTo to its stored message, if it exists and it's going to the right follower then this is a valid reply + const message = event.activityPubMessages.find(el => { + const content = JSON.parse(el.content); + return inReplyTo === (content.object && content.object.id); + }); + if (message) { + console.log(message); + const content = JSON.parse(message.content); + // check if the message we sent out was sent to the actor this incoming message is attributedTo + if (content.to[0] === attributedTo) { + // it's a match, this is a valid poll response, add RSVP to database + // fetch the profile information of the user + request({ + url: attributedTo, + headers: { + 'Accept': 'application/activity+json', + 'Content-Type': 'application/activity+json' + }}, function (error, response, body) { + body = JSON.parse(body) + // if this account is NOT already in our attendees list, add it + if (!event.attendees.map(el => el.id).includes(attributedTo)) { + const attendeeName = body.preferredUsername || body.name || attributedTo; + const newAttendee = { + name: attendeeName, + status: 'attending', + id: attributedTo + }; + event.attendees.push(newAttendee); + event.save() + .then((fullEvent) => { + addToLog("addEventAttendee", "success", "Attendee added to event " + req.params.eventID); + // get the new attendee with its hidden id from the full event + let fullAttendee = fullEvent.attendees.find(el => el.id === attributedTo); + // send a "click here to remove yourself" link back to the user as a DM + const jsonObject = { + "@context": "https://www.w3.org/ns/activitystreams", + "name": `RSVP to ${event.name}`, + "type": "Note", + "content": `@${newAttendee.name} Thanks for RSVPing! You can remove yourself from the RSVP list by clicking here: https://${domain}/oneclickunattendevent/${event.id}/${fullAttendee._id}`, + "tag":[{"type":"Mention","href":newAttendee.id,"name":newAttendee.name}] + } + // send direct message to user + sendDirectMessage(jsonObject, newAttendee.id, event.id); + return res.sendStatus(200); + }) + .catch((err) => { + addToLog("addEventAttendee", "error", "Attempt to add attendee to event " + req.params.eventID + " failed with error: " + err); + return res.status(500).send('Database error, please try again :('); + }); + } + else { + // it's a duplicate and this person is already rsvped so just say OK + return res.status(200).send("Attendee is already registered."); + } + }); + } + } + } + }); +} + +function _handleDelete(req, res) { + const deleteObjectId = req.body.object.id; + // find all events with comments from the author + Event.find({ + "comments.actorId":req.body.actor + }, function(err,events) { + if (!events) { + return res.sendStatus(404); + } + + // find the event with THIS comment from the author + let eventWithComment = events.find(event => { + let comments = event.comments; + return comments.find(comment => { + if (!comment.activityJson) { + return false; + } + return JSON.parse(comment.activityJson).object.id === req.body.object.id; + }) + }); + + if (!eventWithComment) { + return res.sendStatus(404); + } + + // delete the comment + // find the index of the comment, it should have an activityJson field because from an AP server you can only delete an AP-originated comment (and of course it needs to be yours) + let indexOfComment = eventWithComment.comments.findIndex(comment => { + return comment.activityJson && JSON.parse(comment.activityJson).object.id === req.body.object.id; + }); + eventWithComment.comments.splice(indexOfComment, 1); + eventWithComment.save() + .then(() => { + addToLog("deleteComment", "success", "Comment deleted from event " + eventWithComment.id); + console.log('deleted comment!') + return res.sendStatus(200); + }) + .catch((err) => { + addToLog("deleteComment", "error", "Attempt to delete comment " + req.body.object.id + "from event " + eventWithComment.id + " failed with error: " + err); + return res.sendStatus(500); + }); + }); +} + +function _handleCreateNoteComment(req, res) { + // figure out what this is in reply to -- it should be addressed specifically to us + let {attributedTo, inReplyTo, to, cc} = req.body.object; + // normalize cc into an array + if (typeof cc === 'string') { + cc = [cc]; + } + // normalize to into an array + if (typeof to === 'string') { + to = [to]; + } + + // if this is a public message (in the to or cc fields) + if (to.includes('https://www.w3.org/ns/activitystreams#Public') || (Array.isArray(cc) && cc.includes('https://www.w3.org/ns/activitystreams#Public'))) { + // figure out which event(s) of ours it was addressing + let ourEvents = cc.filter(el => el.includes(`https://${domain}/`)) + .map(el => el.replace(`https://${domain}/`,'')); + // comments should only be on one event. if more than one, ignore (spam, probably) + if (ourEvents.length === 1) { + let eventID = ourEvents[0]; + // add comment + let commentID = shortid.generate(); + // get the actor for the commenter + request({ + url: req.body.actor, + headers: { + 'Accept': 'application/activity+json', + 'Content-Type': 'application/activity+json' + }}, function (error, response, actor) { + if (!error) { + const parsedActor = JSON.parse(actor); + const name = parsedActor.preferredUsername || parsedActor.name || req.body.actor; + const newComment = { + id: commentID, + actorId: req.body.actor, + activityId: req.body.object.id, + author: name, + content: sanitizeHtml(req.body.object.content, {allowedTags: [], allowedAttributes: {}}).replace('@'+eventID,''), + timestamp: moment(), + activityJson: JSON.stringify(req.body), + actorJson: actor + }; + + Event.findOne({ + id: eventID, + }, function(err,event) { + if (!event) { + return res.sendStatus(404); + } + if (!event.usersCanComment) { + return res.sendStatus(200); + } + event.comments.push(newComment); + event.save() + .then(() => { + addToLog("addEventComment", "success", "Comment added to event " + eventID); + const guidObject = crypto.randomBytes(16).toString('hex'); + const jsonObject = req.body.object; + jsonObject.attributedTo = newComment.actorId; + broadcastAnnounceMessage(jsonObject, event.followers, eventID) + console.log('added comment'); + return res.sendStatus(200); + }) + .catch((err) => { + addToLog("addEventComment", "error", "Attempt to add comment to event " + eventID + " failed with error: " + err); console.log('error', err) + res.status(500).send('Database error, please try again :(' + err); + }); + }); + } + }); + } // end ourevent + } // end public message +} + +function processInbox(req, res) { + console.log('PROCESS INBOX') + console.log(req.body, req.body.type); + try { + if (req.body.object) console.log('containing object of type', req.body.object.type); + // if a Follow activity hits the inbox + if (typeof req.body.object === 'string' && req.body.type === 'Follow') { + _handleFollow(req, res); + } + // if an Undo activity with a Follow object hits the inbox + if (req.body && req.body.type === 'Undo' && req.body.object && req.body.object.type === 'Follow') { + _handleUndoFollow(req, res); + } + // if an Accept activity with the id of the Event we sent out hits the inbox, it is an affirmative RSVP + if (req.body && req.body.type === 'Accept' && req.body.object && typeof req.body.object === 'string') { + _handleAcceptEvent(req, res); + } + // if an Undo activity containing an Accept containing the id of the Event we sent out hits the inbox, it is an undo RSVP + if (req.body && req.body.type === 'Undo' && req.body.object && req.body.object.object && typeof req.body.object.object === 'string' && req.body.object.type !== 'Follow') { + _handleUndoAcceptEvent(req, res); + } + // if a Create activity with a Note object hits the inbox, and it's a reply, it might be a vote in a poll + if (req.body && req.body.type === 'Create' && req.body.object && req.body.object.type === 'Note' && req.body.object.inReplyTo && req.body.object.to) { + _handleCreateNote(req, res); + } + // if a Delete activity hits the inbox, it might a deletion of a comment + if (req.body && req.body.type === 'Delete') { + _handleDelete(req, res); + } + // if we are CC'ed on a public or unlisted Create/Note, then this is a comment to us we should boost (Announce) to our followers + if (req.body && req.body.type === 'Create' && req.body.object && req.body.object.type === 'Note' && req.body.object.to) { + _handleCreateNoteComment(req, res); + } // CC'ed + } + catch(e) { + console.log('Error:', e) + } +} + +module.exports = { + processInbox, + sendAcceptMessage, + sendDirectMessage, + broadcastAnnounceMessage, + broadcastUpdateMessage, + broadcastDeleteMessage, + broadcastCreateMessage, + signAndSend, + createActivityPubActor, + updateActivityPubActor, + createActivityPubEvent, + updateActivityPubEvent, + createFeaturedPost, +} -- cgit v1.2.3 From 9ff73b7a4c218fdd0c3fff7b7947ff8e965da4a9 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 6 Jan 2020 21:47:38 -0800 Subject: update wiki link --- activitypub.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js index 3d7fb10..7d9a903 100644 --- a/activitypub.js +++ b/activitypub.js @@ -75,7 +75,7 @@ function createFeaturedPost(eventID, name, startUTC, endUTC, timezone, descripti "type": "Note", "name": "Test", 'cc': 'https://www.w3.org/ns/activitystreams#Public', - "content": `

This is an event that was posted on ${siteName}. If you follow this account, you'll see updates in your timeline about the event. If your software supports polls, you should get a poll in your DMs asking if you want to RSVP. You can reply and RSVP right from there. If your software has an event calendar built in, you should get an event in your inbox that you can RSVP to like you respond to any event.

For more information on how to interact with this, check out this link.

`, + "content": `

This is an event that was posted on ${siteName}. If you follow this account, you'll see updates in your timeline about the event. If your software supports polls, you should get a poll in your DMs asking if you want to RSVP. You can reply and RSVP right from there. If your software has an event calendar built in, you should get an event in your inbox that you can RSVP to like you respond to any event.

For more information on how to interact with this, check out this link.

`, 'attributedTo': `https://${domain}/${eventID}`, } return featured; -- cgit v1.2.3 From a40bf2370726b51829cdbb2cbd57f491bf764478 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 6 Jan 2020 22:22:41 -0800 Subject: more refactors --- activitypub.js | 134 ++++++++++++++++++++++++++------------------------------- 1 file changed, 60 insertions(+), 74 deletions(-) (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js index 7d9a903..9d69ab5 100644 --- a/activitypub.js +++ b/activitypub.js @@ -175,11 +175,9 @@ function signAndSend(message, eventID, targetDomain, inbox, callback) { event.save() .then(() => { addToLog("addActivityPubMessage", "success", "ActivityPubMessage added to event " + eventID); - console.log('successful ActivityPubMessage add'); callback(null, message.id, 200); }) .catch((err) => { addToLog("addActivityPubMessage", "error", "Attempt to add ActivityPubMessage to event " + eventID + " failed with error: " + err); - console.log('error', err) callback(err, null, 500); }); }) @@ -195,26 +193,22 @@ function signAndSend(message, eventID, targetDomain, inbox, callback) { // this function sends something to the timeline of every follower in the followers array // it's also an unlisted public message, meaning non-followers can see the message if they look at // the profile but it doesn't spam federated timelines -function broadcastCreateMessage(apObject, followers, eventID, callback) { - callback = callback || function() {}; +function broadcastCreateMessage(apObject, followers, eventID) { let guidCreate = crypto.randomBytes(16).toString('hex'); - console.log('broadcasting'); - // iterate over followers - for (const follower of followers) { - let actorId = follower.actorId; - let myURL = new URL(actorId); - let targetDomain = myURL.hostname; - // get the inbox - Event.findOne({ - id: eventID, - }, function(err, event) { - console.log('found the event for broadcast') - if (event) { - const follower = event.followers.find(el => el.actorId === actorId); - if (follower) { + Event.findOne({ + id: eventID, + }, function(err, event) { + if (event) { + // iterate over followers + for (const follower of followers) { + let actorId = follower.actorId; + let myURL = new URL(actorId); + let targetDomain = myURL.hostname; + // get the inbox + const followerFound = event.followers.find(el => el.actorId === actorId); + if (followerFound) { const actorJson = JSON.parse(follower.actorJson); const inbox = actorJson.inbox; - console.log('found the inbox for', actorId) const createMessage = { '@context': 'https://www.w3.org/ns/activitystreams', 'id': `https://${domain}/${eventID}/m/${guidCreate}`, @@ -234,35 +228,32 @@ function broadcastCreateMessage(apObject, followers, eventID, callback) { }); } else { - callback(`No follower found with the id ${actorId}`, null, 404); + console.log(`No follower found with the id ${actorId}`); } - } - else { - callback(`No event found with the id ${eventID}`, null, 404); - } - }); - } // end followers + } // end followers + } // end if event + else { + console.log(`No event found with the id ${eventID}`); + } + }); } // sends an Announce for the apObject -function broadcastAnnounceMessage(apObject, followers, eventID, callback) { - callback = callback || function() {}; +function broadcastAnnounceMessage(apObject, followers, eventID) { let guidUpdate = crypto.randomBytes(16).toString('hex'); - console.log('broadcasting announce'); - // iterate over followers - for (const follower of followers) { - let actorId = follower.actorId; - let myURL = new URL(actorId); - let targetDomain = myURL.hostname; - // get the inbox - Event.findOne({ - id: eventID, - }, function(err, event) { - console.log('found the event for broadcast') - if (event) { - const follower = event.followers.find(el => el.actorId === actorId); - if (follower) { + Event.findOne({ + id: eventID, + }, function(err, event) { + if (event) { + // iterate over followers + for (const follower of followers) { + let actorId = follower.actorId; + let myURL = new URL(actorId); + let targetDomain = myURL.hostname; + // get the inbox + const followerFound = event.followers.find(el => el.actorId === actorId); + if (followerFound) { const actorJson = JSON.parse(follower.actorJson); const inbox = actorJson.inbox; const announceMessage = { @@ -276,7 +267,7 @@ function broadcastAnnounceMessage(apObject, followers, eventID, callback) { }; signAndSend(announceMessage, eventID, targetDomain, inbox, function(err, resp, status) { if (err) { - console.log(`Didn't sent to ${actorId}, status ${status} with error ${err}`); + console.log(`Didn't send to ${actorId}, status ${status} with error ${err}`); } else { console.log('sent to', actorId); @@ -285,35 +276,30 @@ function broadcastAnnounceMessage(apObject, followers, eventID, callback) { } else { console.log(`No follower found with the id ${actorId}`); - callback(`No follower found with the id ${actorId}`, null, 404); } - } - else { - console.log(`No event found with the id ${eventID}`); - callback(`No event found with the id ${eventID}`, null, 404); - } - }); - } // end followers + } // end followers + } // end if event + else { + console.log(`No event found with the id ${eventID}`); + } + }); } // sends an Update for the apObject -function broadcastUpdateMessage(apObject, followers, eventID, callback) { - callback = callback || function() {}; +function broadcastUpdateMessage(apObject, followers, eventID) { let guidUpdate = crypto.randomBytes(16).toString('hex'); - console.log('broadcasting update'); // iterate over followers - for (const follower of followers) { - let actorId = follower.actorId; - let myURL = new URL(actorId); - let targetDomain = myURL.hostname; - // get the inbox - Event.findOne({ - id: eventID, - }, function(err, event) { - console.log('found the event for broadcast') - if (event) { - const follower = event.followers.find(el => el.actorId === actorId); - if (follower) { + Event.findOne({ + id: eventID, + }, function(err, event) { + if (event) { + for (const follower of followers) { + let actorId = follower.actorId; + let myURL = new URL(actorId); + let targetDomain = myURL.hostname; + // get the inbox + const followerFound = event.followers.find(el => el.actorId === actorId); + if (followerFound) { const actorJson = JSON.parse(follower.actorJson); const inbox = actorJson.inbox; const createMessage = { @@ -333,14 +319,14 @@ function broadcastUpdateMessage(apObject, followers, eventID, callback) { }); } else { - callback(`No follower found with the id ${actorId}`, null, 404); + console.log(`No follower found with the id ${actorId}`); } - } - else { - callback(`No event found with the id ${eventID}`, null, 404); - } - }); - } // end followers + } // end followers + } + else { + console.log(`No event found with the id ${eventID}`); + } + }); } function broadcastDeleteMessage(apObject, followers, eventID, callback) { @@ -394,7 +380,7 @@ function broadcastDeleteMessage(apObject, followers, eventID, callback) { console.log(`No event found with the id ${eventID}`, null, 404); reject(`No event found with the id ${eventID}`, null, 404); } - }); + }); // end event })); } // end followers -- cgit v1.2.3 From 6dc03139921414d24f0e24efba224bf0c8e0581f Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 6 Jan 2020 22:26:29 -0800 Subject: delete console logs --- activitypub.js | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js index 9d69ab5..22404cf 100644 --- a/activitypub.js +++ b/activitypub.js @@ -54,7 +54,6 @@ function createActivityPubActor(eventID, domain, pubkey, description, name, loca function createActivityPubEvent(name, startUTC, endUTC, timezone, description, location) { const guid = crypto.randomBytes(16).toString('hex'); - console.log(startUTC); let eventObject = { "@context": "https://www.w3.org/ns/activitystreams", 'id': `https://${domain}/${guid}`, @@ -336,7 +335,6 @@ function broadcastDeleteMessage(apObject, followers, eventID, callback) { let promises = []; let guidUpdate = crypto.randomBytes(16).toString('hex'); - console.log('building promises'); // iterate over followers for (const follower of followers) { promises.push(new Promise((resolve, reject) => { @@ -347,7 +345,6 @@ function broadcastDeleteMessage(apObject, followers, eventID, callback) { Event.findOne({ id: eventID, }, function(err, event) { - console.log('found the event for broadcast'); if (event) { const follower = event.followers.find(el => el.actorId === actorId); if (follower) { @@ -385,8 +382,6 @@ function broadcastDeleteMessage(apObject, followers, eventID, callback) { } // end followers Promise.all(promises.map(p => p.catch(e => e))).then(statuses => { - console.log('DONE') - console.log(statuses) callback(statuses); }); } @@ -490,12 +485,10 @@ function _handleFollow(req, res) { }, function(err,event) { // if this account is NOT already in our followers list, add it if (event && !event.followers.map(el => el.actorId).includes(req.body.actor)) { - console.log('made it!') event.followers.push(newFollower); event.save() .then(() => { addToLog("addEventFollower", "success", "Follower added to event " + eventID); - console.log('successful follower add'); // Accept the follow request sendAcceptMessage(req.body, eventID, targetDomain, function(err, resp, status) { if (err) { @@ -646,24 +639,19 @@ function _handleUndoAcceptEvent(req, res) { to = to[0]; } const eventID = to.replace(`https://${domain}/`,''); - console.log(eventID) Event.findOne({ id: eventID, }, function(err,event) { if (!event) return; // does the id we got match the id of a thing we sent out - console.log('EVENT MESSAGES') - console.log(event.activityPubMessages); const message = event.activityPubMessages.find(el => el.id === req.body.object.object); if (message) { // it's a match - console.log('match!!!!') Event.update( { id: eventID }, { $pull: { attendees: { id: actor } } } ) .then(response => { - console.log(response) addToLog("oneClickUnattend", "success", "Attendee removed via one click unattend " + req.params.eventID); }); } @@ -671,7 +659,6 @@ function _handleUndoAcceptEvent(req, res) { } function _handleCreateNote(req, res) { - console.log('create note inreplyto!!!') // figure out what this is in reply to -- it should be addressed specifically to us let {name, attributedTo, inReplyTo, to} = req.body.object; // if it's an array just grab the first element, since a poll should only broadcast back to the pollster @@ -687,14 +674,12 @@ function _handleCreateNote(req, res) { // is this even someone who follows us const indexOfFollower = event.followers.findIndex(el => el.actorId === req.body.object.attributedTo); if (indexOfFollower !== -1) { - console.log('this person does follow us!') // compare the inReplyTo to its stored message, if it exists and it's going to the right follower then this is a valid reply const message = event.activityPubMessages.find(el => { const content = JSON.parse(el.content); return inReplyTo === (content.object && content.object.id); }); if (message) { - console.log(message); const content = JSON.parse(message.content); // check if the message we sent out was sent to the actor this incoming message is attributedTo if (content.to[0] === attributedTo) { @@ -783,7 +768,6 @@ function _handleDelete(req, res) { eventWithComment.save() .then(() => { addToLog("deleteComment", "success", "Comment deleted from event " + eventWithComment.id); - console.log('deleted comment!') return res.sendStatus(200); }) .catch((err) => { @@ -853,11 +837,10 @@ function _handleCreateNoteComment(req, res) { const jsonObject = req.body.object; jsonObject.attributedTo = newComment.actorId; broadcastAnnounceMessage(jsonObject, event.followers, eventID) - console.log('added comment'); return res.sendStatus(200); }) .catch((err) => { - addToLog("addEventComment", "error", "Attempt to add comment to event " + eventID + " failed with error: " + err); console.log('error', err) + addToLog("addEventComment", "error", "Attempt to add comment to event " + eventID + " failed with error: " + err); res.status(500).send('Database error, please try again :(' + err); }); }); @@ -868,10 +851,7 @@ function _handleCreateNoteComment(req, res) { } function processInbox(req, res) { - console.log('PROCESS INBOX') - console.log(req.body, req.body.type); try { - if (req.body.object) console.log('containing object of type', req.body.object.type); // if a Follow activity hits the inbox if (typeof req.body.object === 'string' && req.body.type === 'Follow') { _handleFollow(req, res); -- cgit v1.2.3 From a4392c4c663b6b23da7320d95b5a4b23f474f213 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 6 Jan 2020 23:16:52 -0800 Subject: minor fixes --- activitypub.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js index 22404cf..5940ac0 100644 --- a/activitypub.js +++ b/activitypub.js @@ -510,8 +510,6 @@ function _handleFollow(req, res) { "content": `@${name} Will you attend ${event.name}? (If you reply "Yes", you'll be listed as an attendee on the event page.)`, "oneOf": [ {"type":"Note","name": "Yes"}, - {"type":"Note","name": "No"}, - {"type":"Note","name": "Maybe"} ], "endTime":event.start.toISOString(), "tag":[{"type":"Mention","href":req.body.actor,"name":name}] @@ -865,7 +863,7 @@ function processInbox(req, res) { _handleAcceptEvent(req, res); } // if an Undo activity containing an Accept containing the id of the Event we sent out hits the inbox, it is an undo RSVP - if (req.body && req.body.type === 'Undo' && req.body.object && req.body.object.object && typeof req.body.object.object === 'string' && req.body.object.type !== 'Follow') { + if (req.body && req.body.type === 'Undo' && req.body.object && req.body.object.object && typeof req.body.object.object === 'string' && req.body.object.type === 'Accept') { _handleUndoAcceptEvent(req, res); } // if a Create activity with a Note object hits the inbox, and it's a reply, it might be a vote in a poll -- cgit v1.2.3 From 5ffe6115740cfa293322e1961e7c9996e32ce2a4 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 6 Jan 2020 23:29:24 -0800 Subject: move function to activitypub file --- activitypub.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js index 5940ac0..983738b 100644 --- a/activitypub.js +++ b/activitypub.js @@ -884,6 +884,20 @@ function processInbox(req, res) { } } +function createWebfinger(eventID, domain) { + return { + 'subject': `acct:${eventID}@${domain}`, + + 'links': [ + { + 'rel': 'self', + 'type': 'application/activity+json', + 'href': `https://${domain}/${eventID}` + } + ] + }; +} + module.exports = { processInbox, sendAcceptMessage, @@ -898,4 +912,5 @@ module.exports = { createActivityPubEvent, updateActivityPubEvent, createFeaturedPost, + createWebfinger, } -- cgit v1.2.3 From d51ed04584ada2682e203cce143f31ca6d0866bd Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Sat, 18 Jan 2020 11:16:34 -0800 Subject: Adding Federation toggle There is now a toggle in the `config/domain-example.js` file called `isFederated`. When set to `true`, ActivityPub functions as normal. When set to false, the ActivityPub functions all immediately return without doing anything, and AP routes return 404s. --- activitypub.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js index 983738b..f49648b 100644 --- a/activitypub.js +++ b/activitypub.js @@ -1,6 +1,7 @@ const domain = require('./config/domain.js').domain; const contactEmail = require('./config/domain.js').email; -const siteName = require('./config/domain.js').sitename +const siteName = require('./config/domain.js').sitename; +const isFederated = require('./config/domain.js').isFederated; const request = require('request'); const addToLog = require('./helpers.js').addToLog; const crypto = require('crypto'); @@ -12,6 +13,7 @@ const EventGroup = mongoose.model('EventGroup'); var sanitizeHtml = require('sanitize-html'); function createActivityPubActor(eventID, domain, pubkey, description, name, location, imageFilename, startUTC, endUTC, timezone) { + if (!isFederated) return {}; let actor = { '@context': [ 'https://www.w3.org/ns/activitystreams', @@ -53,6 +55,7 @@ function createActivityPubActor(eventID, domain, pubkey, description, name, loca } function createActivityPubEvent(name, startUTC, endUTC, timezone, description, location) { + if (!isFederated) return {}; const guid = crypto.randomBytes(16).toString('hex'); let eventObject = { "@context": "https://www.w3.org/ns/activitystreams", @@ -81,6 +84,7 @@ function createFeaturedPost(eventID, name, startUTC, endUTC, timezone, descripti } function updateActivityPubEvent(oldEvent, name, startUTC, endUTC, timezone, description, location) { + if (!isFederated) return; // we want to persist the old ID no matter what happens to the Event itself const id = oldEvent.id; let eventObject = { @@ -98,6 +102,7 @@ function updateActivityPubEvent(oldEvent, name, startUTC, endUTC, timezone, desc function updateActivityPubActor(actor, description, name, location, imageFilename, startUTC, endUTC, timezone) { + if (!isFederated) return; if (!actor) return; actor.summary = `

${description}

`; actor.name = name; @@ -120,6 +125,7 @@ function updateActivityPubActor(actor, description, name, location, imageFilenam } function signAndSend(message, eventID, targetDomain, inbox, callback) { + if (!isFederated) return; let inboxFragment = inbox.replace('https://'+targetDomain,''); // get the private key Event.findOne({ @@ -193,6 +199,7 @@ function signAndSend(message, eventID, targetDomain, inbox, callback) { // it's also an unlisted public message, meaning non-followers can see the message if they look at // the profile but it doesn't spam federated timelines function broadcastCreateMessage(apObject, followers, eventID) { + if (!isFederated) return; let guidCreate = crypto.randomBytes(16).toString('hex'); Event.findOne({ id: eventID, @@ -240,6 +247,7 @@ function broadcastCreateMessage(apObject, followers, eventID) { // sends an Announce for the apObject function broadcastAnnounceMessage(apObject, followers, eventID) { + if (!isFederated) return; let guidUpdate = crypto.randomBytes(16).toString('hex'); Event.findOne({ id: eventID, @@ -286,6 +294,7 @@ function broadcastAnnounceMessage(apObject, followers, eventID) { // sends an Update for the apObject function broadcastUpdateMessage(apObject, followers, eventID) { + if (!isFederated) return; let guidUpdate = crypto.randomBytes(16).toString('hex'); // iterate over followers Event.findOne({ @@ -329,6 +338,7 @@ function broadcastUpdateMessage(apObject, followers, eventID) { } function broadcastDeleteMessage(apObject, followers, eventID, callback) { + if (!isFederated) return; callback = callback || function() {}; // we need to build an array of promises for each message we're sending, run Promise.all(), and then that will resolve when every message has been sent (or failed) // per spec, each promise will execute *as it is built*, which is fine, we just need the guarantee that they are all done @@ -388,6 +398,7 @@ function broadcastDeleteMessage(apObject, followers, eventID, callback) { // this sends a message "to:" an individual fediverse user function sendDirectMessage(apObject, actorId, eventID, callback) { + if (!isFederated) return; callback = callback || function() {}; const guidCreate = crypto.randomBytes(16).toString('hex'); const guidObject = crypto.randomBytes(16).toString('hex'); @@ -432,6 +443,7 @@ function sendDirectMessage(apObject, actorId, eventID, callback) { } function sendAcceptMessage(thebody, eventID, targetDomain, callback) { + if (!isFederated) return; callback = callback || function() {}; const guid = crypto.randomBytes(16).toString('hex'); const actorId = thebody.actor; @@ -849,6 +861,7 @@ function _handleCreateNoteComment(req, res) { } function processInbox(req, res) { + if (!isFederated) return res.sendStatus(404); try { // if a Follow activity hits the inbox if (typeof req.body.object === 'string' && req.body.type === 'Follow') { -- cgit v1.2.3 From fc42ff1944a60225e4f111e1ebb1175f0e82f604 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 20 Jan 2020 18:31:17 -0800 Subject: changing federation toggle --- activitypub.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js index f49648b..1a67639 100644 --- a/activitypub.js +++ b/activitypub.js @@ -13,7 +13,6 @@ const EventGroup = mongoose.model('EventGroup'); var sanitizeHtml = require('sanitize-html'); function createActivityPubActor(eventID, domain, pubkey, description, name, location, imageFilename, startUTC, endUTC, timezone) { - if (!isFederated) return {}; let actor = { '@context': [ 'https://www.w3.org/ns/activitystreams', @@ -55,7 +54,6 @@ function createActivityPubActor(eventID, domain, pubkey, description, name, loca } function createActivityPubEvent(name, startUTC, endUTC, timezone, description, location) { - if (!isFederated) return {}; const guid = crypto.randomBytes(16).toString('hex'); let eventObject = { "@context": "https://www.w3.org/ns/activitystreams", @@ -84,7 +82,6 @@ function createFeaturedPost(eventID, name, startUTC, endUTC, timezone, descripti } function updateActivityPubEvent(oldEvent, name, startUTC, endUTC, timezone, description, location) { - if (!isFederated) return; // we want to persist the old ID no matter what happens to the Event itself const id = oldEvent.id; let eventObject = { @@ -102,7 +99,6 @@ function updateActivityPubEvent(oldEvent, name, startUTC, endUTC, timezone, desc function updateActivityPubActor(actor, description, name, location, imageFilename, startUTC, endUTC, timezone) { - if (!isFederated) return; if (!actor) return; actor.summary = `

${description}

`; actor.name = name; -- cgit v1.2.3 From 2fafa00849b627a6996243e4070325b8efb05428 Mon Sep 17 00:00:00 2001 From: Darius Kazemi Date: Mon, 20 Jan 2020 18:37:06 -0800 Subject: Better error messages --- activitypub.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'activitypub.js') diff --git a/activitypub.js b/activitypub.js index 1a67639..2a7c18a 100644 --- a/activitypub.js +++ b/activitypub.js @@ -150,11 +150,9 @@ function signAndSend(message, eventID, targetDomain, inbox, callback) { body: message }, function (error, response){ if (error) { - console.log('Error:', error, response.body); callback(error, null, 500); } else { - console.log('Response:', response.statusCode); // Add the message to the database const messageID = message.id; const newMessage = { @@ -222,7 +220,7 @@ function broadcastCreateMessage(apObject, followers, eventID) { }; signAndSend(createMessage, eventID, targetDomain, inbox, function(err, resp, status) { if (err) { - console.log(`Didn't sent to ${actorId}, status ${status} with error ${err}`); + console.log(`Didn't send to ${actorId}, status ${status} with error ${err}`); } else { console.log('sent to', actorId); @@ -315,7 +313,7 @@ function broadcastUpdateMessage(apObject, followers, eventID) { }; signAndSend(createMessage, eventID, targetDomain, inbox, function(err, resp, status) { if (err) { - console.log(`Didn't sent to ${actorId}, status ${status} with error ${err}`); + console.log(`Didn't send to ${actorId}, status ${status} with error ${err}`); } else { console.log('sent to', actorId); @@ -525,7 +523,7 @@ function _handleFollow(req, res) { // send direct message to user sendDirectMessage(jsonObject, req.body.actor, eventID, function (error, response, statuscode) { if (error) { - console.log(error); + console.log('Error sending direct message:', error); return res.status(statuscode).json(error); } else { @@ -889,7 +887,7 @@ function processInbox(req, res) { } // CC'ed } catch(e) { - console.log('Error:', e) + console.log('Error in processing inbox:', e) } } -- cgit v1.2.3