diff options
author | Darius Kazemi <darius.kazemi@gmail.com> | 2020-01-18 11:16:34 -0800 |
---|---|---|
committer | Darius Kazemi <darius.kazemi@gmail.com> | 2020-01-18 11:28:08 -0800 |
commit | d51ed04584ada2682e203cce143f31ca6d0866bd (patch) | |
tree | 298f949a6b572abc3810a6304c769606fbf79ab8 | |
parent | 70982d89085c76f569b511c6245fb9a549d1825f (diff) |
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.
-rw-r--r-- | activitypub.js | 15 | ||||
-rw-r--r-- | config/domain-example.js | 1 | ||||
-rwxr-xr-x | routes.js | 17 | ||||
-rwxr-xr-x | views/event.handlebars | 2 |
4 files changed, 29 insertions, 6 deletions
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 = `<p>${description}</p>`; 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') { diff --git a/config/domain-example.js b/config/domain-example.js index b84e210..c019a4a 100644 --- a/config/domain-example.js +++ b/config/domain-example.js @@ -4,6 +4,7 @@ module.exports = { 'port': '3000', 'email': 'contact@example.com', 'sitename': 'gathio', + 'isFederated': true, // If left blank, this defaults to https://yourdomain.com/images/gathio-email-logo.gif. Set a full URL here to change it to your own logo (or just change the file itself) 'logo_url': '' }; @@ -28,6 +28,7 @@ const domain = require('./config/domain.js').domain; const contactEmail = require('./config/domain.js').email; const siteName = require('./config/domain.js').sitename; const siteLogo = require('./config/domain.js').logo_url; +const isFederated = require('./config/domain.js').isFederated; const ap = require('./activitypub.js'); // Extra marked renderer (used to render plaintext event description for page metadata) @@ -199,6 +200,7 @@ router.get('/new/event/public', (req, res) => { // return the JSON for the featured/pinned post for this event router.get('/:eventID/featured', (req, res) => { + if (!isFederated) return res.sendStatus(404); const {eventID} = req.params; const guidObject = crypto.randomBytes(16).toString('hex'); const featured = { @@ -214,6 +216,7 @@ router.get('/:eventID/featured', (req, res) => { // return the JSON for a given activitypub message router.get('/:eventID/m/:hash', (req, res) => { + if (!isFederated) return res.sendStatus(404); const {hash, eventID} = req.params; const id = `https://${domain}/${eventID}/m/${hash}`; @@ -246,6 +249,7 @@ router.get('/:eventID/m/:hash', (req, res) => { // return the webfinger record required for the initial activitypub handshake router.get('/.well-known/webfinger', (req, res) => { + if (!isFederated) return res.sendStatus(404); let resource = req.query.resource; if (!resource || !resource.includes('acct:')) { return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.'); @@ -379,6 +383,7 @@ router.get('/:eventID', (req, res) => { res.set("X-Robots-Tag", "noindex"); res.render('event', { domain: domain, + isFederated: isFederated, email: contactEmail, title: event.name, escapedName: escapedName, @@ -422,6 +427,7 @@ router.get('/:eventID', (req, res) => { }) router.get('/:eventID/followers', (req, res) => { + if (!isFederated) return res.sendStatus(404); const eventID = req.params.eventID; Event.findOne({ id: eventID @@ -661,9 +667,9 @@ router.post('/newevent', async (req, res) => { usersCanComment: req.body.interactionCheckbox ? true : false, maxAttendees: req.body.maxAttendees, firstLoad: true, - activityPubActor: ap.createActivityPubActor(eventID, domain, pair.public, marked(req.body.eventDescription), req.body.eventName, req.body.eventLocation, eventImageFilename, startUTC, endUTC, req.body.timezone), - activityPubEvent: ap.createActivityPubEvent(req.body.eventName, startUTC, endUTC, req.body.timezone, req.body.eventDescription, req.body.eventLocation), - activityPubMessages: [ { id: `https://${domain}/${eventID}/m/featuredPost`, content: JSON.stringify(ap.createFeaturedPost(eventID, req.body.eventName, startUTC, endUTC, req.body.timezone, req.body.eventDescription, req.body.eventLocation)) } ], + activityPubActor: isFederated ? ap.createActivityPubActor(eventID, domain, pair.public, marked(req.body.eventDescription), req.body.eventName, req.body.eventLocation, eventImageFilename, startUTC, endUTC, req.body.timezone) : null, + activityPubEvent: isFederated ? ap.createActivityPubEvent(req.body.eventName, startUTC, endUTC, req.body.timezone, req.body.eventDescription, req.body.eventLocation) : null, + activityPubMessages: isFederated ? [ { id: `https://${domain}/${eventID}/m/featuredPost`, content: JSON.stringify(ap.createFeaturedPost(eventID, req.body.eventName, startUTC, endUTC, req.body.timezone, req.body.eventDescription, req.body.eventLocation)) } ] : [], publicKey: pair.public, privateKey: pair.private }); @@ -875,8 +881,8 @@ router.post('/editevent/:eventID/:editToken', (req, res) => { usersCanComment: req.body.interactionCheckbox ? true : false, maxAttendees: req.body.maxAttendeesCheckbox ? req.body.maxAttendees : null, eventGroup: isPartOfEventGroup ? eventGroup._id : null, - activityPubActor: ap.updateActivityPubActor(JSON.parse(event.activityPubActor), req.body.eventDescription, req.body.eventName, req.body.eventLocation, eventImageFilename, startUTC, endUTC, req.body.timezone), - activityPubEvent: ap.updateActivityPubEvent(JSON.parse(event.activityPubEvent), req.body.eventName, req.body.startUTC, req.body.endUTC, req.body.timezone), + activityPubActor: isFederated ? ap.updateActivityPubActor(JSON.parse(event.activityPubActor), req.body.eventDescription, req.body.eventName, req.body.eventLocation, eventImageFilename, startUTC, endUTC, req.body.timezone) : null, + activityPubEvent: isFederated ? ap.updateActivityPubEvent(JSON.parse(event.activityPubEvent), req.body.eventName, req.body.startUTC, req.body.endUTC, req.body.timezone) : null, } let diffText = '<p>This event was just updated with new information.</p><ul>'; let displayDate; @@ -1531,6 +1537,7 @@ router.post('/deletecomment/:eventID/:commentID/:editToken', (req, res) => { }); router.post('/activitypub/inbox', (req, res) => { + if (!isFederated) return res.sendStatus(404); // validate the incoming message const signature = req.get('Signature'); let signature_header = signature.split(',').map(pair => { diff --git a/views/event.handlebars b/views/event.handlebars index 6f9dae1..52a68bc 100755 --- a/views/event.handlebars +++ b/views/event.handlebars @@ -72,6 +72,7 @@ <i class="fas fa-copy"></i> Copy </button> </li> + {{#if isFederated}} <li> <span class="fa-li"> <i class="fas fa-fw fa-share-square"></i> @@ -81,6 +82,7 @@ <i class="fas fa-copy"></i> Copy </button> </li> + {{/if}} </ul> </div> </div> |