X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fserver%2Ffollows.ts;h=e78361c9a94f43473bcc029b4ca315d2a9a10aae;hb=0030284b0df2983914291d6fe83675e2aa892e6a;hp=520d4d858536fa534e13dfcf5c2fd5c0fcb134c8;hpb=4610bc5b12eaa4bfd64fe3fd70c65e5b722aa22d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index 520d4d858..e78361c9a 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts @@ -1,43 +1,52 @@ import * as express from 'express' -import { UserRight } from '../../../../shared/models/users/user-right.enum' -import { getFormattedObjects } from '../../../helpers' +import { UserRight } from '../../../../shared/models/users' import { logger } from '../../../helpers/logger' -import { getServerAccount } from '../../../helpers/utils' -import { getAccountFromWebfinger } from '../../../helpers/webfinger' -import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants' -import { database as db } from '../../../initializers/database' -import { sendFollow } from '../../../lib/activitypub/send-request' -import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../../middlewares' -import { authenticate } from '../../../middlewares/oauth' -import { setBodyHostsPort } from '../../../middlewares/servers' -import { setFollowingSort } from '../../../middlewares/sort' -import { ensureUserHasRight } from '../../../middlewares/user-right' -import { followValidator } from '../../../middlewares/validators/servers' -import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort' +import { getFormattedObjects, getServerActor } from '../../../helpers/utils' +import { sequelizeTypescript } from '../../../initializers' +import { sendUndoFollow } from '../../../lib/activitypub/send' +import { + asyncMiddleware, + authenticate, + ensureUserHasRight, + paginationValidator, + removeFollowingValidator, + setBodyHostsPort, + setDefaultPagination, + setDefaultSort +} from '../../../middlewares' +import { followersSortValidator, followingSortValidator, followValidator } from '../../../middlewares/validators' +import { ActorFollowModel } from '../../../models/activitypub/actor-follow' +import { JobQueue } from '../../../lib/job-queue' const serverFollowsRouter = express.Router() - serverFollowsRouter.get('/following', paginationValidator, followingSortValidator, - setFollowingSort, - setPagination, + setDefaultSort, + setDefaultPagination, asyncMiddleware(listFollowing) ) -serverFollowsRouter.post('/follow', +serverFollowsRouter.post('/following', authenticate, ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), followValidator, setBodyHostsPort, - asyncMiddleware(follow) + asyncMiddleware(followInstance) +) + +serverFollowsRouter.delete('/following/:host', + authenticate, + ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), + asyncMiddleware(removeFollowingValidator), + asyncMiddleware(removeFollow) ) serverFollowsRouter.get('/followers', paginationValidator, followersSortValidator, - setFollowersSort, - setPagination, + setDefaultSort, + setDefaultPagination, asyncMiddleware(listFollowers) ) @@ -50,82 +59,44 @@ export { // --------------------------------------------------------------------------- async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) { - const serverAccount = await getServerAccount() - const resultList = await db.AccountFollow.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) + const serverActor = await getServerActor() + const resultList = await ActorFollowModel.listFollowingForApi(serverActor.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 serverAccount = await getServerAccount() - const resultList = await db.AccountFollow.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) + const serverActor = await getServerActor() + const resultList = await ActorFollowModel.listFollowersForApi(serverActor.id, req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } -async function follow (req: express.Request, res: express.Response, next: express.NextFunction) { +async function followInstance (req: express.Request, res: express.Response, next: express.NextFunction) { const hosts = req.body.hosts as string[] - const fromAccount = await getServerAccount() - - const tasks: Promise[] = [] - const accountName = SERVER_ACCOUNT_NAME 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 - 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) - } - }) - }) - .catch(err => logger.warn('Cannot follow server %s.', `${accountName}@${host}`, err)) - - tasks.push(p) + JobQueue.Instance.createJob({ type: 'activitypub-follow', payload: { host } }) + .catch(err => logger.error('Cannot create follow job for %s.', host, err)) } - // Don't make the client wait the tasks - Promise.all(tasks) - .catch(err => { - logger.error('Error in follow.', err) - }) - return res.status(204).end() } -async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { - let loadedFromDB = true - let account = await db.Account.loadByNameAndHost(name, host) +async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) { + const follow: ActorFollowModel = res.locals.follow - if (!account) { - const nameWithDomain = name + '@' + host - account = await getAccountFromWebfinger(nameWithDomain) - loadedFromDB = false - } + await sequelizeTypescript.transaction(async t => { + if (follow.state === 'accepted') await sendUndoFollow(follow, t) - return { account, loadedFromDB } + await follow.destroy({ transaction: t }) + }) + + // Destroy the actor that will destroy video channels, videos and video files too + // This could be long so don't wait this task + const following = follow.ActorFollowing + following.destroy() + .catch(err => logger.error('Cannot destroy actor that we do not follow anymore %s.', following.url, { err })) + + return res.status(204).end() }