From ca83f1c67516fa6a1e58210ec0ab78285e586f4a Mon Sep 17 00:00:00 2001 From: les Date: Thu, 8 Aug 2019 16:52:13 +0200 Subject: [PATCH] bookmark/unbookmark from fediverse --- package.json | 2 +- server/federation/ego.js | 28 +++++++++++++++++++++++----- server/federation/follows.js | 15 +++++++++------ server/federation/helpers.js | 3 +++ server/federation/index.js | 6 +++--- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index a8ca5d57..721b24a4 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "lesion", "scripts": { "dev:nuxt": "cross-env NODE_ENV=development nuxt dev", - "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server", + "dev": "cross-env DEBUG=fediverse:* NODE_ENV=development nodemon server/index.js --watch server", "build": "nuxt build", "start": "cross-env sequelize db:migrate && NODE_ENV=production node server/cli.js", "lint": "eslint --ext .js,.vue --ignore-path .gitignore .", diff --git a/server/federation/ego.js b/server/federation/ego.js index 1e11e544..c951bdcd 100644 --- a/server/federation/ego.js +++ b/server/federation/ego.js @@ -1,19 +1,37 @@ const { event: Event } = require('../api/models') const config = require('config') +const debug = require('debug')('fediverse:ego') module.exports = { async boost (req, res) { - const event_id = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)[1] - const event = await Event.findByPk(event_id) + const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`) + if (!match || match.length<2) return res.status(404).send('Event not found!') + debug('boost %s', match[1]) + const event = await Event.findByPk(Number(match[1])) if (!event) return res.status(404).send('Event not found!') await event.update({ boost: [...event.boost, req.body.actor]}) res.sendStatus(201) }, - async like (req, res) { - const event_id = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)[1] - const event = await Event.findByPk(event_id) + + async bookmark (req, res) { + const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`) + if (!match || match.length<2) return res.status(404).send('Event not found!') + const event = await Event.findByPk(Number(match[1])) + debug('%s bookmark %s (%d)', req.body.actor, event.title, event.likes.length) if (!event) return res.status(404).send('Event not found!') await event.update({ likes: [...event.likes, req.body.actor]}) res.sendStatus(201) + }, + + async unbookmark (req, res) { + const body = req.body + const object = body.object + const match = object.object.match(`${config.baseurl}/federation/m/(.*)`) + if (!match || match.length<2) return res.status(404).send('Event not found!') + const event = await Event.findByPk(Number(match[1])) + debug('%s unbookmark %s (%d)', body.actor, event.title, event.likes.length) + if (!event) return res.status(404).send('Event not found!') + await event.update({ likes: [...event.likes.filter(actor => actor!==body.actor)]}) + res.sendStatus(201) } } \ No newline at end of file diff --git a/server/federation/follows.js b/server/federation/follows.js index 9e86d810..16d5daf4 100644 --- a/server/federation/follows.js +++ b/server/federation/follows.js @@ -2,21 +2,23 @@ const config = require('config') const Helpers = require('./helpers') const { user: User } = require('../api/models') const crypto = require('crypto') +const debug = require('debug')('follows') module.exports = { // follow request from fediverse - async follow (req, res, body, targetOrigin, domain) { + async follow (req, res) { + const body = req.body if (typeof body.object !== 'string') return const username = body.object.replace(`${config.baseurl}/federation/u/`, '') const user = await User.findOne({ where: { username }}) - if (!user) { - res.sendStatus(404) - return - } + if (!user) return res.sendStatus(404) + // check for duplicate if (user.followers.indexOf(body.actor) === -1) { - console.error('ok this is a new follower: ', body.actor) + debug('%s followed by %s (%d)', username, body.actor, user.followers.length) await user.update({ followers: [...user.followers, body.actor] }) + } else { + debug('duplicate %s followed by %s', username, body.actor) } const guid = crypto.randomBytes(16).toString('hex') let message = { @@ -29,6 +31,7 @@ module.exports = { Helpers.signAndSend(message, user, body.actor) res.sendStatus(200) }, + // unfollow request from fediverse unfollow () { console.error('inside unfollow') diff --git a/server/federation/helpers.js b/server/federation/helpers.js index dac91932..c9c1b7b7 100644 --- a/server/federation/helpers.js +++ b/server/federation/helpers.js @@ -3,6 +3,7 @@ const request = require('request') const crypto = require('crypto') const config = require('config') const httpSignature = require('http-signature') +const debug = require('debug')('fediverse:helpers') const actorCache = [] @@ -68,8 +69,10 @@ const Helpers = { async getActor(url, force=false) { // try with cache first if not forced if (!force && actorCache[url]) return actorCache[url] + debug('getActor %s', url) const user = await fetch(url, { headers: {'Accept': 'application/jrd+json, application/json'} }) .then(res => res.json()) + .catch(debug) actorCache[url] = user return user }, diff --git a/server/federation/index.js b/server/federation/index.js index 55990f1b..e86bbb0a 100644 --- a/server/federation/index.js +++ b/server/federation/index.js @@ -38,14 +38,14 @@ router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => { switch(b.type) { case 'Follow': - Follows.follow(req, res, b, targetOrigin, domain) + Follows.follow(req, res) break case 'Undo': // unfollow || unlike if (b.object.type === 'Follow') { Follows.unfollow(req, res, b, targetOrigin, domain) } else if (b.object.type === 'Like') { - console.error('Unlike!') + Ego.unbookmark(req, res) } else if (b.object.type === 'Announce') { console.error('Unboost') } @@ -57,7 +57,7 @@ router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => { console.error('This is a note ! I probably should not receive this') break case 'Like': - Ego.like(req, res) + Ego.bookmark(req, res) break case 'Delete': console.error('Delete ?!?!')