From 7a7724e66e4533523083e7336cd0d0c747c4a33b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 13 Nov 2017 17:39:41 +0100 Subject: Handle follow/accept --- server/controllers/activitypub/client.ts | 8 ++--- server/controllers/activitypub/inbox.ts | 29 +++++++++++---- server/controllers/activitypub/index.ts | 2 +- server/controllers/activitypub/videos.ts | 61 -------------------------------- server/controllers/api/pods.ts | 33 +++++++++++++---- 5 files changed, 53 insertions(+), 80 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 28d08b3f4..5cfbc2f1d 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -16,12 +16,12 @@ activityPubClientRouter.get('/account/:name', executeIfActivityPub(asyncMiddleware(accountController)) ) -activityPubClientRouter.get('/account/:name/followers', +activityPubClientRouter.get('/account/:nameWithHost/followers', executeIfActivityPub(localAccountValidator), executeIfActivityPub(asyncMiddleware(accountFollowersController)) ) -activityPubClientRouter.get('/account/:name/following', +activityPubClientRouter.get('/account/:nameWithHost/following', executeIfActivityPub(localAccountValidator), executeIfActivityPub(asyncMiddleware(accountFollowingController)) ) @@ -46,7 +46,7 @@ async function accountFollowersController (req: express.Request, res: express.Re const page = req.params.page || 1 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) - const result = await db.Account.listFollowerUrlsForApi(account.name, start, count) + const result = await db.Account.listFollowerUrlsForApi(account.id, start, count) const activityPubResult = activityPubCollectionPagination(req.url, page, result) return res.json(activityPubResult) @@ -58,7 +58,7 @@ async function accountFollowingController (req: express.Request, res: express.Re const page = req.params.page || 1 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) - const result = await db.Account.listFollowingUrlsForApi(account.name, start, count) + const result = await db.Account.listFollowingUrlsForApi(account.id, start, count) const activityPubResult = activityPubCollectionPagination(req.url, page, result) return res.json(activityPubResult) diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts index eee217650..eedb518b9 100644 --- a/server/controllers/activitypub/inbox.ts +++ b/server/controllers/activitypub/inbox.ts @@ -3,26 +3,41 @@ import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, Activity import { logger } from '../../helpers' import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity' import { processCreateActivity, processFlagActivity, processUpdateActivity } from '../../lib' +import { processAcceptActivity } from '../../lib/activitypub/process-accept' import { processAddActivity } from '../../lib/activitypub/process-add' -import { asyncMiddleware, checkSignature, signatureValidator } from '../../middlewares' +import { processDeleteActivity } from '../../lib/activitypub/process-delete' +import { processFollowActivity } from '../../lib/activitypub/process-follow' +import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares' import { activityPubValidator } from '../../middlewares/validators/activitypub/activity' +import { AccountInstance } from '../../models/account/account-interface' -const processActivity: { [ P in ActivityType ]: (activity: Activity) => Promise } = { +const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise } = { Create: processCreateActivity, Add: processAddActivity, Update: processUpdateActivity, - Flag: processFlagActivity + Flag: processFlagActivity, + Delete: processDeleteActivity, + Follow: processFollowActivity, + Accept: processAcceptActivity } const inboxRouter = express.Router() -inboxRouter.post('/', +inboxRouter.post('/inbox', signatureValidator, asyncMiddleware(checkSignature), activityPubValidator, asyncMiddleware(inboxController) ) +inboxRouter.post('/:nameWithHost/inbox', + signatureValidator, + asyncMiddleware(checkSignature), + localAccountValidator, + activityPubValidator, + asyncMiddleware(inboxController) +) + // --------------------------------------------------------------------------- export { @@ -46,12 +61,12 @@ async function inboxController (req: express.Request, res: express.Response, nex // Only keep activities we are able to process activities = activities.filter(a => isActivityValid(a)) - await processActivities(activities) + await processActivities(activities, res.locals.account) res.status(204).end() } -async function processActivities (activities: Activity[]) { +async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) { for (const activity of activities) { const activityProcessor = processActivity[activity.type] if (activityProcessor === undefined) { @@ -59,6 +74,6 @@ async function processActivities (activities: Activity[]) { continue } - await activityProcessor(activity) + await activityProcessor(activity, inboxAccount) } } diff --git a/server/controllers/activitypub/index.ts b/server/controllers/activitypub/index.ts index 2b0e2a489..6c7bafc6e 100644 --- a/server/controllers/activitypub/index.ts +++ b/server/controllers/activitypub/index.ts @@ -6,7 +6,7 @@ import { activityPubClientRouter } from './client' const remoteRouter = express.Router() -remoteRouter.use('/inbox', inboxRouter) +remoteRouter.use('/', inboxRouter) remoteRouter.use('/', activityPubClientRouter) remoteRouter.use('/*', badRequest) diff --git a/server/controllers/activitypub/videos.ts b/server/controllers/activitypub/videos.ts index a9b31bf75..98894379f 100644 --- a/server/controllers/activitypub/videos.ts +++ b/server/controllers/activitypub/videos.ts @@ -224,67 +224,6 @@ // logger.info('Remote video with uuid %s quick and dirty updated', videoUUID) // } // -// async function removeRemoteVideoRetryWrapper (videoToRemoveData: RemoteVideoRemoveData, fromPod: PodInstance) { -// const options = { -// arguments: [ videoToRemoveData, fromPod ], -// errorMessage: 'Cannot remove the remote video channel with many retries.' -// } -// -// await retryTransactionWrapper(removeRemoteVideo, options) -// } -// -// async function removeRemoteVideo (videoToRemoveData: RemoteVideoRemoveData, fromPod: PodInstance) { -// logger.debug('Removing remote video "%s".', videoToRemoveData.uuid) -// -// await db.sequelize.transaction(async t => { -// // We need the instance because we have to remove some other stuffs (thumbnail etc) -// const videoInstance = await fetchVideoByHostAndUUID(fromPod.host, videoToRemoveData.uuid, t) -// await videoInstance.destroy({ transaction: t }) -// }) -// -// logger.info('Remote video with uuid %s removed.', videoToRemoveData.uuid) -// } -// -// async function removeRemoteVideoAccountRetryWrapper (accountAttributesToRemove: RemoteVideoAccountRemoveData, fromPod: PodInstance) { -// const options = { -// arguments: [ accountAttributesToRemove, fromPod ], -// errorMessage: 'Cannot remove the remote video account with many retries.' -// } -// -// await retryTransactionWrapper(removeRemoteVideoAccount, options) -// } -// -// async function removeRemoteVideoAccount (accountAttributesToRemove: RemoteVideoAccountRemoveData, fromPod: PodInstance) { -// logger.debug('Removing remote video account "%s".', accountAttributesToRemove.uuid) -// -// await db.sequelize.transaction(async t => { -// const videoAccount = await db.Account.loadAccountByPodAndUUID(accountAttributesToRemove.uuid, fromPod.id, t) -// await videoAccount.destroy({ transaction: t }) -// }) -// -// logger.info('Remote video account with uuid %s removed.', accountAttributesToRemove.uuid) -// } -// -// async function removeRemoteVideoChannelRetryWrapper (videoChannelAttributesToRemove: RemoteVideoChannelRemoveData, fromPod: PodInstance) { -// const options = { -// arguments: [ videoChannelAttributesToRemove, fromPod ], -// errorMessage: 'Cannot remove the remote video channel with many retries.' -// } -// -// await retryTransactionWrapper(removeRemoteVideoChannel, options) -// } -// -// async function removeRemoteVideoChannel (videoChannelAttributesToRemove: RemoteVideoChannelRemoveData, fromPod: PodInstance) { -// logger.debug('Removing remote video channel "%s".', videoChannelAttributesToRemove.uuid) -// -// await db.sequelize.transaction(async t => { -// const videoChannel = await fetchVideoChannelByHostAndUUID(fromPod.host, videoChannelAttributesToRemove.uuid, t) -// await videoChannel.destroy({ transaction: t }) -// }) -// -// logger.info('Remote video channel with uuid %s removed.', videoChannelAttributesToRemove.uuid) -// } -// // async function reportAbuseRemoteVideoRetryWrapper (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) { // const options = { // arguments: [ reportData, fromPod ], diff --git a/server/controllers/api/pods.ts b/server/controllers/api/pods.ts index 43df3f66f..aa07b17f6 100644 --- a/server/controllers/api/pods.ts +++ b/server/controllers/api/pods.ts @@ -1,16 +1,27 @@ import * as express from 'express' import { getFormattedObjects } from '../../helpers' +import { getApplicationAccount } from '../../helpers/utils' import { database as db } from '../../initializers/database' -import { asyncMiddleware, paginationValidator, podsSortValidator, setPagination, setPodsSort } from '../../middlewares' +import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../middlewares' +import { setFollowingSort } from '../../middlewares/sort' +import { followersSortValidator, followingSortValidator } from '../../middlewares/validators/sort' const podsRouter = express.Router() -podsRouter.get('/', +podsRouter.get('/following', paginationValidator, - podsSortValidator, - setPodsSort, + followingSortValidator, + setFollowingSort, setPagination, - asyncMiddleware(listPods) + asyncMiddleware(listFollowing) +) + +podsRouter.get('/followers', + paginationValidator, + followersSortValidator, + setFollowersSort, + setPagination, + asyncMiddleware(listFollowers) ) // --------------------------------------------------------------------------- @@ -21,8 +32,16 @@ export { // --------------------------------------------------------------------------- -async function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.Pod.listForApi(req.query.start, req.query.count, req.query.sort) +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) return res.json(getFormattedObjects(resultList.data, resultList.total)) } -- cgit v1.2.3