From 54141398354e6e7b94aa3065a705a1251390111c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 20 Nov 2017 09:43:39 +0100 Subject: Refractor activity pub lib/helpers --- server/controllers/api/server/follows.ts | 30 ++++++++++++++++++++++++++---- server/controllers/api/videos/channel.ts | 6 +++--- server/controllers/api/videos/index.ts | 11 ++++++----- 3 files changed, 35 insertions(+), 12 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index 3d184ec1f..8fc70f34f 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts @@ -6,14 +6,16 @@ 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 { asyncMiddleware, paginationValidator, removeFollowingValidator, 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 { followValidator } from '../../../middlewares/validators/follows' import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort' +import { AccountFollowInstance } from '../../../models/index' +import { sendFollow } from '../../../lib/index' +import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo' const serverFollowsRouter = express.Router() @@ -33,6 +35,13 @@ serverFollowsRouter.post('/following', asyncMiddleware(follow) ) +serverFollowsRouter.delete('/following/:accountId', + authenticate, + ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), + removeFollowingValidator, + asyncMiddleware(removeFollow) +) + serverFollowsRouter.get('/followers', paginationValidator, followersSortValidator, @@ -96,10 +105,12 @@ async function follow (req: express.Request, res: express.Response, next: expres }, transaction: t }) + accountFollow.AccountFollowing = targetAccount + accountFollow.AccountFollower = fromAccount // Send a notification to remote server if (accountFollow.state === 'pending') { - await sendFollow(fromAccount, targetAccount, t) + await sendFollow(accountFollow, t) } }) }) @@ -117,6 +128,17 @@ async function follow (req: express.Request, res: express.Response, next: expres return res.status(204).end() } +async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) { + const following: AccountFollowInstance = res.locals.following + + await db.sequelize.transaction(async t => { + await sendUndoFollow(following, t) + await following.destroy({ transaction: t }) + }) + + return res.status(204).end() +} + async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { let loadedFromDB = true let account = await db.Account.loadByNameAndHost(name, host) diff --git a/server/controllers/api/videos/channel.ts b/server/controllers/api/videos/channel.ts index 8f3df2550..ce2656e71 100644 --- a/server/controllers/api/videos/channel.ts +++ b/server/controllers/api/videos/channel.ts @@ -17,7 +17,7 @@ import { videoChannelsUpdateValidator } from '../../../middlewares' import { AccountInstance, VideoChannelInstance } from '../../../models' -import { sendUpdateVideoChannel } from '../../../lib/activitypub/send-request' +import { sendUpdateVideoChannel } from '../../../lib/activitypub/send/send-update' const videoChannelRouter = express.Router() @@ -128,9 +128,9 @@ async function updateVideoChannel (req: express.Request, res: express.Response) if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name) if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description) - await videoChannelInstance.save(sequelizeOptions) + const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions) - await sendUpdateVideoChannel(videoChannelInstance, t) + await sendUpdateVideoChannel(videoChannelInstanceUpdated, t) }) logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.uuid) diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 22a88620a..8c9b0aa50 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -12,10 +12,11 @@ import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' -import { getActivityPubUrl, shareVideoByServer } from '../../../helpers/activitypub' +import { getVideoActivityPubUrl, shareVideoByServer } from '../../../helpers/activitypub' import { CONFIG, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES } from '../../../initializers' import { database as db } from '../../../initializers/database' -import { sendAddVideo, sendUpdateVideo } from '../../../lib/activitypub/send-request' +import { sendAddVideo } from '../../../lib/activitypub/send/send-add' +import { sendUpdateVideo } from '../../../lib/activitypub/send/send-update' import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler' import { asyncMiddleware, @@ -175,7 +176,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi channelId: res.locals.videoChannel.id } const video = db.Video.build(videoData) - video.url = getActivityPubUrl('video', video.uuid) + video.url = getVideoActivityPubUrl(video) const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) const videoFileHeight = await getVideoFileHeight(videoFilePath) @@ -274,7 +275,7 @@ async function updateVideo (req: express.Request, res: express.Response) { if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', videoInfoToUpdate.privacy) if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) - await videoInstance.save(sequelizeOptions) + const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) if (videoInfoToUpdate.tags) { const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t) @@ -285,7 +286,7 @@ async function updateVideo (req: express.Request, res: express.Response) { // Now we'll update the video's meta data to our friends if (wasPrivateVideo === false) { - await sendUpdateVideo(videoInstance, t) + await sendUpdateVideo(videoInstanceUpdated, t) } // Video is not private anymore, send a create action to remote servers -- cgit v1.2.3