Added API methods to follow/unfollow AP accounts

This commit is contained in:
Niko 2022-02-15 23:26:55 +01:00
parent ebaa58cb82
commit 458f7c28fa
3 changed files with 68 additions and 0 deletions

View File

@ -2,4 +2,5 @@ module.exports = {
url: require('./api-utils.js').url,
auth: require('./api/auth.js'),
feed: require('./api/feed.js'),
accounts: require('./api/accounts.js'),
}

65
api/src/api/accounts.js Normal file
View File

@ -0,0 +1,65 @@
const crypto = require('crypto')
const auth = require('../auth.js')
const utils = require('../api-utils.js')
async function getFollow(req, res) {
return await db.table.objects().findOne({
actor: apID(res.locals.user.username),
to: req.query.url,
type: 'Follow',
})
}
module.exports = {
follow: {
post: [auth.enforceSession, async (req, res) => {
if (!utils.isURLValid(req.query.url))
return res.json({ error: 'invalid_url' })
const follow = await getFollow(req, res)
if (follow !== null)
return res.json({ error: 'already_following' })
const actor = await apex.store.getObject(
apID(res.locals.user.username), true)
const payload = {
id: utils.apRandomURL(),
type: 'Follow',
actor: apID(res.locals.user.username),
object: req.query.url,
to: req.query.url,
}
await apex.addToOutbox(actor, payload)
await apex.store.saveObject(payload)
res.json({ result: 1 })
}],
},
unfollow: {
post: [auth.enforceSession, async (req, res) => {
if (!utils.isURLValid(req.query.url))
return res.json({ error: 'invalid_url' })
const follow = await getFollow(req, res)
if (follow === null)
return res.json({ error: 'no_following' })
const actor = await apex.store.getObject(
apID(res.locals.user.username), true)
const payload = {
id: utils.apRandomURL(),
type: 'Undo',
actor: apID(res.locals.user.username),
to: req.query.url,
object: {
id: follow.id,
type: 'Follow',
actor: apID(res.locals.user.username),
object: req.query.url,
},
}
await apex.addToOutbox(actor, payload)
await db.table.objects().deleteOne({ _id: follow._id })
res.json({ result: 1 })
}],
},
}

View File

@ -67,6 +67,8 @@ app.on('apex-inbox', activity.federation.inbox)
app.route(api.url('/auth/register')).post(api.auth.register.post)
app.route(api.url('/auth/login')).post(api.auth.login.post)
app.route(api.url('/feed/meet')).get(api.feed.meet.get)
app.route(api.url('/accounts/follow')).post(api.accounts.follow.post)
app.route(api.url('/accounts/unfollow')).post(api.accounts.unfollow.post)
// initialize
client.connect({ useNewUrlParser: true })