PeerTube/server/controllers/api/pods.ts

128 lines
4.3 KiB
TypeScript
Raw Normal View History

2017-06-05 21:53:49 +02:00
import * as express from 'express'
2017-11-14 17:31:26 +01:00
import { UserRight } from '../../../shared/models/users/user-right.enum'
2017-11-10 17:27:49 +01:00
import { getFormattedObjects } from '../../helpers'
2017-11-14 17:31:26 +01:00
import { logger } from '../../helpers/logger'
2017-11-13 17:39:41 +01:00
import { getApplicationAccount } from '../../helpers/utils'
2017-11-14 17:31:26 +01:00
import { getAccountFromWebfinger } from '../../helpers/webfinger'
import { SERVER_ACCOUNT_NAME } from '../../initializers/constants'
2017-05-22 20:58:25 +02:00
import { database as db } from '../../initializers/database'
2017-11-14 17:31:26 +01:00
import { sendFollow } from '../../lib/activitypub/send-request'
2017-11-13 17:39:41 +01:00
import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares'
2017-11-14 17:31:26 +01:00
import { authenticate } from '../../middlewares/oauth'
2017-11-13 18:48:28 +01:00
import { setBodyHostsPort } from '../../middlewares/pods'
2017-11-13 17:39:41 +01:00
import { setFollowingSort } from '../../middlewares/sort'
2017-11-14 17:31:26 +01:00
import { ensureUserHasRight } from '../../middlewares/user-right'
2017-11-13 18:48:28 +01:00
import { followValidator } from '../../middlewares/validators/pods'
2017-11-13 17:39:41 +01:00
import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort'
2017-05-15 22:22:03 +02:00
const podsRouter = express.Router()
2017-11-13 17:39:41 +01:00
podsRouter.get('/following',
paginationValidator,
2017-11-13 17:39:41 +01:00
followingSortValidator,
setFollowingSort,
setPagination,
2017-11-13 17:39:41 +01:00
asyncMiddleware(listFollowing)
)
2017-11-13 18:48:28 +01:00
podsRouter.post('/follow',
2017-11-14 09:11:43 +01:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_PEERTUBE_FOLLOW),
2017-11-13 18:48:28 +01:00
followValidator,
setBodyHostsPort,
asyncMiddleware(follow)
)
2017-11-13 17:39:41 +01:00
podsRouter.get('/followers',
paginationValidator,
followersSortValidator,
setFollowersSort,
setPagination,
asyncMiddleware(listFollowers)
2017-05-15 22:22:03 +02:00
)
// ---------------------------------------------------------------------------
export {
podsRouter
}
// ---------------------------------------------------------------------------
2017-11-13 17:39:41 +01: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 11:55:06 +02:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
2017-05-15 22:22:03 +02:00
}
2017-11-13 18:48:28 +01:00
async function follow (req: express.Request, res: express.Response, next: express.NextFunction) {
const hosts = req.body.hosts as string[]
const fromAccount = await getApplicationAccount()
2017-11-14 17:31:26 +01:00
const tasks: Promise<any>[] = []
const accountName = SERVER_ACCOUNT_NAME
2017-11-13 18:48:28 +01:00
for (const host of hosts) {
// 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
2017-11-14 17:31:26 +01:00
const p = loadLocalOrGetAccountFromWebfinger(accountName, host)
.then(accountResult => {
let targetAccount = accountResult.account
return db.sequelize.transaction(async t => {
if (accountResult.loadedFromDB === false) {
targetAccount = await targetAccount.save({ transaction: t })
}
const [ accountFollow ] = await db.AccountFollow.findOrCreate({
where: {
accountId: fromAccount.id,
targetAccountId: targetAccount.id
},
defaults: {
state: 'pending',
accountId: fromAccount.id,
targetAccountId: targetAccount.id
},
transaction: t
})
// Send a notification to remote server
if (accountFollow.state === 'pending') {
await sendFollow(fromAccount, targetAccount, t)
}
})
2017-11-13 18:48:28 +01:00
})
2017-11-14 17:31:26 +01:00
.catch(err => logger.warn('Cannot follow server %s.', `${accountName}@${host}`, err))
2017-11-13 18:48:28 +01:00
tasks.push(p)
}
await Promise.all(tasks)
return res.status(204).end()
}
2017-11-14 17:31:26 +01:00
async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) {
let loadedFromDB = true
let account = await db.Account.loadByNameAndHost(name, host)
if (!account) {
const nameWithDomain = name + '@' + host
account = await getAccountFromWebfinger(nameWithDomain)
loadedFromDB = false
}
return { account, loadedFromDB }
}