Commit ca83f1c6 authored by les's avatar les
Browse files

bookmark/unbookmark from fediverse

parent fc2c82ab
......@@ -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 .",
......
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
......@@ -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')
......
......@@ -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
},
......
......@@ -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 ?!?!')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment