summaryrefslogtreecommitdiff
path: root/routes.js
diff options
context:
space:
mode:
authorDarius Kazemi <darius.kazemi@gmail.com>2020-01-04 23:07:22 -0800
committerDarius Kazemi <darius.kazemi@gmail.com>2020-01-06 21:44:48 -0800
commitdcbf6c7268261639d67b0c8502cd205f815ba2fa (patch)
tree56a6e521e9a4c68df8b522a7a70c58f49f0e8587 /routes.js
parent7fc93dbe9d4d99457a0e85c6c532112f415b7af2 (diff)
update federation doc
Diffstat (limited to 'routes.js')
-rwxr-xr-xroutes.js101
1 files changed, 57 insertions, 44 deletions
diff --git a/routes.js b/routes.js
index 5d2110a..920d36b 100755
--- a/routes.js
+++ b/routes.js
@@ -189,6 +189,7 @@ function createActivityPubActor(eventID, domain, pubkey, description, name, loca
'type': 'Person',
'preferredUsername': `${eventID}`,
'inbox': `https://${domain}/activitypub/inbox`,
+ 'outbox': `https://${domain}/${eventID}/outbox`,
'followers': `https://${domain}/${eventID}/followers`,
'summary': `<p>${description}</p>`,
'name': name,
@@ -362,6 +363,57 @@ function broadcastMessage(apObject, followers, eventID, callback) {
} // 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}/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');
@@ -2120,7 +2172,7 @@ function processInbox(req, res) {
.catch((err) => { res.sendStatus(500); addToLog("deleteComment", "error", "Attempt to delete comment " + req.body.object.id + "from event " + eventWithComment.id + " failed with error: " + err);});
});
}
- // if we are CC'ed on a public or unlisted Create/Note, then this is a comment to us we should replicate
+ // 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) {
// figure out what this is in reply to -- it should be addressed specifically to us
let {attributedTo, inReplyTo, to, cc} = req.body.object;
@@ -2177,16 +2229,10 @@ function processInbox(req, res) {
event.save()
.then(() => {
addToLog("addEventComment", "success", "Comment added to event " + eventID);
- // broadcast an identical message to all followers, will show in their home timeline
const guidObject = crypto.randomBytes(16).toString('hex');
- const jsonObject = {
- "@context": "https://www.w3.org/ns/activitystreams",
- "id": `https://${domain}/m/${guidObject}`,
- "name": `Comment on ${event.name}`,
- "type": "Note",
- "content": newComment.content,
- }
- broadcastMessage(jsonObject, event.followers, req.params.eventID)
+ const jsonObject = req.body.object;
+ jsonObject.attributedTo = newComment.actorId;
+ broadcastAnnounceMessage(jsonObject, event.followers, eventID)
console.log('added comment');
res.sendStatus(200);
})
@@ -2196,40 +2242,7 @@ function processInbox(req, res) {
});
} // end ourevent
} // end public message
- // if it's not a public message, AND it's not a vote let them know that we only support public messages right now
- else if (req.body.object.name !== 'Yes') {
- if (!cc) {
- cc = [];
- }
- // figure out which event(s) of ours it was addressing
- ourEvents = cc.concat(to).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];
- // get the user's actor info
- request({
- url: req.body.actor,
- headers: {
- 'Accept': 'application/activity+json',
- 'Content-Type': 'application/activity+json'
- }}, function (error, response, actor) {
- actor = JSON.parse(actor);
- const name = actor.preferredUsername || actor.name || req.body.actor;
- const jsonObject = {
- "@context": "https://www.w3.org/ns/activitystreams",
- "type": "Note",
- "inReplyTo": req.body.object.id,
- "content": `<span class=\"h-card\"><a href="${req.body.actor}" class="u-url mention">@<span>${name}</span></a></span> Sorry, this service only supports posting public messages to the event page. Try contacting the event organizer directly if you need to have a private conversation.`,
- "tag":[{"type":"Mention","href":req.body.actor,"name":name}]
- }
- res.sendStatus(200);
- sendDirectMessage(jsonObject, req.body.actor, eventID);
- }
- );
- }
- }
- }
+ } // CC'ed
}
router.use(function(req, res, next){