1
0
Fork 0
peertube/server/controllers/api/pods.ts

89 lines
3.0 KiB
TypeScript
Raw Normal View History

2017-11-13 17:48:28 +00:00
import * as Bluebird from 'bluebird'
2017-06-05 19:53:49 +00:00
import * as express from 'express'
2017-11-10 16:27:49 +00:00
import { getFormattedObjects } from '../../helpers'
2017-11-13 17:48:28 +00:00
import { getOrCreateAccount } from '../../helpers/activitypub'
2017-11-13 16:39:41 +00:00
import { getApplicationAccount } from '../../helpers/utils'
2017-11-13 17:48:28 +00:00
import { REMOTE_SCHEME } from '../../initializers/constants'
2017-05-22 18:58:25 +00:00
import { database as db } from '../../initializers/database'
2017-11-13 16:39:41 +00:00
import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares'
2017-11-13 17:48:28 +00:00
import { setBodyHostsPort } from '../../middlewares/pods'
2017-11-13 16:39:41 +00:00
import { setFollowingSort } from '../../middlewares/sort'
2017-11-13 17:48:28 +00:00
import { followValidator } from '../../middlewares/validators/pods'
2017-11-13 16:39:41 +00:00
import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort'
2017-11-13 17:48:28 +00:00
import { sendFollow } from '../../lib/activitypub/send-request'
2017-05-15 20:22:03 +00:00
const podsRouter = express.Router()
2017-11-13 16:39:41 +00:00
podsRouter.get('/following',
paginationValidator,
2017-11-13 16:39:41 +00:00
followingSortValidator,
setFollowingSort,
setPagination,
2017-11-13 16:39:41 +00:00
asyncMiddleware(listFollowing)
)
2017-11-13 17:48:28 +00:00
podsRouter.post('/follow',
followValidator,
setBodyHostsPort,
asyncMiddleware(follow)
)
2017-11-13 16:39:41 +00:00
podsRouter.get('/followers',
paginationValidator,
followersSortValidator,
setFollowersSort,
setPagination,
asyncMiddleware(listFollowers)
2017-05-15 20:22:03 +00:00
)
// ---------------------------------------------------------------------------
export {
podsRouter
}
// ---------------------------------------------------------------------------
2017-11-13 16:39:41 +00:00
async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) {
const applicationAccount = await getApplicationAccount()
const resultList = await db.Account.listFollowingForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) {
const applicationAccount = await getApplicationAccount()
const resultList = await db.Account.listFollowersForApi(applicationAccount.id, req.query.start, req.query.count, req.query.sort)
2017-10-25 09:55:06 +00:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
2017-05-15 20:22:03 +00:00
}
2017-11-13 17:48:28 +00:00
async function follow (req: express.Request, res: express.Response, next: express.NextFunction) {
const hosts = req.body.hosts as string[]
const fromAccount = await getApplicationAccount()
const tasks: Bluebird<any>[] = []
for (const host of hosts) {
const url = REMOTE_SCHEME.HTTP + '://' + host
const targetAccount = await getOrCreateAccount(url)
// We process each host in a specific transaction
// First, we add the follow request in the database
// Then we send the follow request to other account
const p = db.sequelize.transaction(async t => {
return db.AccountFollow.create({
accountId: fromAccount.id,
targetAccountId: targetAccount.id,
state: 'pending'
})
.then(() => sendFollow(fromAccount, targetAccount, t))
})
tasks.push(p)
}
await Promise.all(tasks)
return res.status(204).end()
}