bookmark/unbookmark from fediverse
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
"author": "lesion",
|
"author": "lesion",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev:nuxt": "cross-env NODE_ENV=development nuxt dev",
|
"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",
|
"build": "nuxt build",
|
||||||
"start": "cross-env sequelize db:migrate && NODE_ENV=production node server/cli.js",
|
"start": "cross-env sequelize db:migrate && NODE_ENV=production node server/cli.js",
|
||||||
"lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
|
"lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
|
||||||
|
|||||||
@@ -1,19 +1,37 @@
|
|||||||
const { event: Event } = require('../api/models')
|
const { event: Event } = require('../api/models')
|
||||||
const config = require('config')
|
const config = require('config')
|
||||||
|
const debug = require('debug')('fediverse:ego')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
async boost (req, res) {
|
async boost (req, res) {
|
||||||
const event_id = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)[1]
|
const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
|
||||||
const event = await Event.findByPk(event_id)
|
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!')
|
if (!event) return res.status(404).send('Event not found!')
|
||||||
await event.update({ boost: [...event.boost, req.body.actor]})
|
await event.update({ boost: [...event.boost, req.body.actor]})
|
||||||
res.sendStatus(201)
|
res.sendStatus(201)
|
||||||
},
|
},
|
||||||
async like (req, res) {
|
|
||||||
const event_id = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)[1]
|
async bookmark (req, res) {
|
||||||
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!')
|
||||||
|
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!')
|
if (!event) return res.status(404).send('Event not found!')
|
||||||
await event.update({ likes: [...event.likes, req.body.actor]})
|
await event.update({ likes: [...event.likes, req.body.actor]})
|
||||||
res.sendStatus(201)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,21 +2,23 @@ const config = require('config')
|
|||||||
const Helpers = require('./helpers')
|
const Helpers = require('./helpers')
|
||||||
const { user: User } = require('../api/models')
|
const { user: User } = require('../api/models')
|
||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
|
const debug = require('debug')('follows')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
// follow request from fediverse
|
// 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
|
if (typeof body.object !== 'string') return
|
||||||
const username = body.object.replace(`${config.baseurl}/federation/u/`, '')
|
const username = body.object.replace(`${config.baseurl}/federation/u/`, '')
|
||||||
const user = await User.findOne({ where: { username }})
|
const user = await User.findOne({ where: { username }})
|
||||||
if (!user) {
|
if (!user) return res.sendStatus(404)
|
||||||
res.sendStatus(404)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// check for duplicate
|
// check for duplicate
|
||||||
if (user.followers.indexOf(body.actor) === -1) {
|
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] })
|
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')
|
const guid = crypto.randomBytes(16).toString('hex')
|
||||||
let message = {
|
let message = {
|
||||||
@@ -29,6 +31,7 @@ module.exports = {
|
|||||||
Helpers.signAndSend(message, user, body.actor)
|
Helpers.signAndSend(message, user, body.actor)
|
||||||
res.sendStatus(200)
|
res.sendStatus(200)
|
||||||
},
|
},
|
||||||
|
|
||||||
// unfollow request from fediverse
|
// unfollow request from fediverse
|
||||||
unfollow () {
|
unfollow () {
|
||||||
console.error('inside unfollow')
|
console.error('inside unfollow')
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ const request = require('request')
|
|||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
const config = require('config')
|
const config = require('config')
|
||||||
const httpSignature = require('http-signature')
|
const httpSignature = require('http-signature')
|
||||||
|
const debug = require('debug')('fediverse:helpers')
|
||||||
|
|
||||||
const actorCache = []
|
const actorCache = []
|
||||||
|
|
||||||
@@ -68,8 +69,10 @@ const Helpers = {
|
|||||||
async getActor(url, force=false) {
|
async getActor(url, force=false) {
|
||||||
// try with cache first if not forced
|
// try with cache first if not forced
|
||||||
if (!force && actorCache[url]) return actorCache[url]
|
if (!force && actorCache[url]) return actorCache[url]
|
||||||
|
debug('getActor %s', url)
|
||||||
const user = await fetch(url, { headers: {'Accept': 'application/jrd+json, application/json'} })
|
const user = await fetch(url, { headers: {'Accept': 'application/jrd+json, application/json'} })
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
|
.catch(debug)
|
||||||
actorCache[url] = user
|
actorCache[url] = user
|
||||||
return user
|
return user
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -38,14 +38,14 @@ router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => {
|
|||||||
|
|
||||||
switch(b.type) {
|
switch(b.type) {
|
||||||
case 'Follow':
|
case 'Follow':
|
||||||
Follows.follow(req, res, b, targetOrigin, domain)
|
Follows.follow(req, res)
|
||||||
break
|
break
|
||||||
case 'Undo':
|
case 'Undo':
|
||||||
// unfollow || unlike
|
// unfollow || unlike
|
||||||
if (b.object.type === 'Follow') {
|
if (b.object.type === 'Follow') {
|
||||||
Follows.unfollow(req, res, b, targetOrigin, domain)
|
Follows.unfollow(req, res, b, targetOrigin, domain)
|
||||||
} else if (b.object.type === 'Like') {
|
} else if (b.object.type === 'Like') {
|
||||||
console.error('Unlike!')
|
Ego.unbookmark(req, res)
|
||||||
} else if (b.object.type === 'Announce') {
|
} else if (b.object.type === 'Announce') {
|
||||||
console.error('Unboost')
|
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')
|
console.error('This is a note ! I probably should not receive this')
|
||||||
break
|
break
|
||||||
case 'Like':
|
case 'Like':
|
||||||
Ego.like(req, res)
|
Ego.bookmark(req, res)
|
||||||
break
|
break
|
||||||
case 'Delete':
|
case 'Delete':
|
||||||
console.error('Delete ?!?!')
|
console.error('Delete ?!?!')
|
||||||
|
|||||||
Reference in New Issue
Block a user