diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-20 09:43:39 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:52 +0100 |
commit | 54141398354e6e7b94aa3065a705a1251390111c (patch) | |
tree | 8d30d1b9ea8acbe04f6d404125b04fc0c9897b70 /server/controllers/api | |
parent | eb8b27c93e61a896a08923dc1ca3c87ba8cf4948 (diff) | |
download | PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.gz PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.zst PeerTube-54141398354e6e7b94aa3065a705a1251390111c.zip |
Refractor activity pub lib/helpers
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/server/follows.ts | 30 | ||||
-rw-r--r-- | server/controllers/api/videos/channel.ts | 6 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 11 |
3 files changed, 35 insertions, 12 deletions
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' | |||
6 | import { getAccountFromWebfinger } from '../../../helpers/webfinger' | 6 | import { getAccountFromWebfinger } from '../../../helpers/webfinger' |
7 | import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants' | 7 | import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants' |
8 | import { database as db } from '../../../initializers/database' | 8 | import { database as db } from '../../../initializers/database' |
9 | import { sendFollow } from '../../../lib/activitypub/send-request' | 9 | import { asyncMiddleware, paginationValidator, removeFollowingValidator, setFollowersSort, setPagination } from '../../../middlewares' |
10 | import { asyncMiddleware, paginationValidator, setFollowersSort, setPagination } from '../../../middlewares' | ||
11 | import { authenticate } from '../../../middlewares/oauth' | 10 | import { authenticate } from '../../../middlewares/oauth' |
12 | import { setBodyHostsPort } from '../../../middlewares/servers' | 11 | import { setBodyHostsPort } from '../../../middlewares/servers' |
13 | import { setFollowingSort } from '../../../middlewares/sort' | 12 | import { setFollowingSort } from '../../../middlewares/sort' |
14 | import { ensureUserHasRight } from '../../../middlewares/user-right' | 13 | import { ensureUserHasRight } from '../../../middlewares/user-right' |
15 | import { followValidator } from '../../../middlewares/validators/servers' | 14 | import { followValidator } from '../../../middlewares/validators/follows' |
16 | import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort' | 15 | import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort' |
16 | import { AccountFollowInstance } from '../../../models/index' | ||
17 | import { sendFollow } from '../../../lib/index' | ||
18 | import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo' | ||
17 | 19 | ||
18 | const serverFollowsRouter = express.Router() | 20 | const serverFollowsRouter = express.Router() |
19 | 21 | ||
@@ -33,6 +35,13 @@ serverFollowsRouter.post('/following', | |||
33 | asyncMiddleware(follow) | 35 | asyncMiddleware(follow) |
34 | ) | 36 | ) |
35 | 37 | ||
38 | serverFollowsRouter.delete('/following/:accountId', | ||
39 | authenticate, | ||
40 | ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), | ||
41 | removeFollowingValidator, | ||
42 | asyncMiddleware(removeFollow) | ||
43 | ) | ||
44 | |||
36 | serverFollowsRouter.get('/followers', | 45 | serverFollowsRouter.get('/followers', |
37 | paginationValidator, | 46 | paginationValidator, |
38 | followersSortValidator, | 47 | followersSortValidator, |
@@ -96,10 +105,12 @@ async function follow (req: express.Request, res: express.Response, next: expres | |||
96 | }, | 105 | }, |
97 | transaction: t | 106 | transaction: t |
98 | }) | 107 | }) |
108 | accountFollow.AccountFollowing = targetAccount | ||
109 | accountFollow.AccountFollower = fromAccount | ||
99 | 110 | ||
100 | // Send a notification to remote server | 111 | // Send a notification to remote server |
101 | if (accountFollow.state === 'pending') { | 112 | if (accountFollow.state === 'pending') { |
102 | await sendFollow(fromAccount, targetAccount, t) | 113 | await sendFollow(accountFollow, t) |
103 | } | 114 | } |
104 | }) | 115 | }) |
105 | }) | 116 | }) |
@@ -117,6 +128,17 @@ async function follow (req: express.Request, res: express.Response, next: expres | |||
117 | return res.status(204).end() | 128 | return res.status(204).end() |
118 | } | 129 | } |
119 | 130 | ||
131 | async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
132 | const following: AccountFollowInstance = res.locals.following | ||
133 | |||
134 | await db.sequelize.transaction(async t => { | ||
135 | await sendUndoFollow(following, t) | ||
136 | await following.destroy({ transaction: t }) | ||
137 | }) | ||
138 | |||
139 | return res.status(204).end() | ||
140 | } | ||
141 | |||
120 | async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { | 142 | async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { |
121 | let loadedFromDB = true | 143 | let loadedFromDB = true |
122 | let account = await db.Account.loadByNameAndHost(name, host) | 144 | 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 { | |||
17 | videoChannelsUpdateValidator | 17 | videoChannelsUpdateValidator |
18 | } from '../../../middlewares' | 18 | } from '../../../middlewares' |
19 | import { AccountInstance, VideoChannelInstance } from '../../../models' | 19 | import { AccountInstance, VideoChannelInstance } from '../../../models' |
20 | import { sendUpdateVideoChannel } from '../../../lib/activitypub/send-request' | 20 | import { sendUpdateVideoChannel } from '../../../lib/activitypub/send/send-update' |
21 | 21 | ||
22 | const videoChannelRouter = express.Router() | 22 | const videoChannelRouter = express.Router() |
23 | 23 | ||
@@ -128,9 +128,9 @@ async function updateVideoChannel (req: express.Request, res: express.Response) | |||
128 | if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name) | 128 | if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name) |
129 | if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description) | 129 | if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description) |
130 | 130 | ||
131 | await videoChannelInstance.save(sequelizeOptions) | 131 | const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions) |
132 | 132 | ||
133 | await sendUpdateVideoChannel(videoChannelInstance, t) | 133 | await sendUpdateVideoChannel(videoChannelInstanceUpdated, t) |
134 | }) | 134 | }) |
135 | 135 | ||
136 | logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.uuid) | 136 | 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 { | |||
12 | resetSequelizeInstance, | 12 | resetSequelizeInstance, |
13 | retryTransactionWrapper | 13 | retryTransactionWrapper |
14 | } from '../../../helpers' | 14 | } from '../../../helpers' |
15 | import { getActivityPubUrl, shareVideoByServer } from '../../../helpers/activitypub' | 15 | import { getVideoActivityPubUrl, shareVideoByServer } from '../../../helpers/activitypub' |
16 | import { CONFIG, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES } from '../../../initializers' | 16 | import { CONFIG, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES } from '../../../initializers' |
17 | import { database as db } from '../../../initializers/database' | 17 | import { database as db } from '../../../initializers/database' |
18 | import { sendAddVideo, sendUpdateVideo } from '../../../lib/activitypub/send-request' | 18 | import { sendAddVideo } from '../../../lib/activitypub/send/send-add' |
19 | import { sendUpdateVideo } from '../../../lib/activitypub/send/send-update' | ||
19 | import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler' | 20 | import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler' |
20 | import { | 21 | import { |
21 | asyncMiddleware, | 22 | asyncMiddleware, |
@@ -175,7 +176,7 @@ async function addVideo (req: express.Request, res: express.Response, videoPhysi | |||
175 | channelId: res.locals.videoChannel.id | 176 | channelId: res.locals.videoChannel.id |
176 | } | 177 | } |
177 | const video = db.Video.build(videoData) | 178 | const video = db.Video.build(videoData) |
178 | video.url = getActivityPubUrl('video', video.uuid) | 179 | video.url = getVideoActivityPubUrl(video) |
179 | 180 | ||
180 | const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) | 181 | const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) |
181 | const videoFileHeight = await getVideoFileHeight(videoFilePath) | 182 | const videoFileHeight = await getVideoFileHeight(videoFilePath) |
@@ -274,7 +275,7 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
274 | if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', videoInfoToUpdate.privacy) | 275 | if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', videoInfoToUpdate.privacy) |
275 | if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) | 276 | if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) |
276 | 277 | ||
277 | await videoInstance.save(sequelizeOptions) | 278 | const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) |
278 | 279 | ||
279 | if (videoInfoToUpdate.tags) { | 280 | if (videoInfoToUpdate.tags) { |
280 | const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t) | 281 | const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t) |
@@ -285,7 +286,7 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
285 | 286 | ||
286 | // Now we'll update the video's meta data to our friends | 287 | // Now we'll update the video's meta data to our friends |
287 | if (wasPrivateVideo === false) { | 288 | if (wasPrivateVideo === false) { |
288 | await sendUpdateVideo(videoInstance, t) | 289 | await sendUpdateVideo(videoInstanceUpdated, t) |
289 | } | 290 | } |
290 | 291 | ||
291 | // Video is not private anymore, send a create action to remote servers | 292 | // Video is not private anymore, send a create action to remote servers |