diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/users/index.ts | 12 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/videos/ownership.ts | 117 |
3 files changed, 131 insertions, 0 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index 01ee73a53..faba7e208 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts | |||
@@ -18,6 +18,7 @@ import { | |||
18 | setDefaultPagination, | 18 | setDefaultPagination, |
19 | setDefaultSort, | 19 | setDefaultSort, |
20 | token, | 20 | token, |
21 | userAutocompleteValidator, | ||
21 | usersAddValidator, | 22 | usersAddValidator, |
22 | usersGetValidator, | 23 | usersGetValidator, |
23 | usersRegisterValidator, | 24 | usersRegisterValidator, |
@@ -51,6 +52,11 @@ const askSendEmailLimiter = new RateLimit({ | |||
51 | const usersRouter = express.Router() | 52 | const usersRouter = express.Router() |
52 | usersRouter.use('/', meRouter) | 53 | usersRouter.use('/', meRouter) |
53 | 54 | ||
55 | usersRouter.get('/autocomplete', | ||
56 | userAutocompleteValidator, | ||
57 | asyncMiddleware(autocompleteUsers) | ||
58 | ) | ||
59 | |||
54 | usersRouter.get('/', | 60 | usersRouter.get('/', |
55 | authenticate, | 61 | authenticate, |
56 | ensureUserHasRight(UserRight.MANAGE_USERS), | 62 | ensureUserHasRight(UserRight.MANAGE_USERS), |
@@ -222,6 +228,12 @@ function getUser (req: express.Request, res: express.Response, next: express.Nex | |||
222 | return res.json((res.locals.user as UserModel).toFormattedJSON()) | 228 | return res.json((res.locals.user as UserModel).toFormattedJSON()) |
223 | } | 229 | } |
224 | 230 | ||
231 | async function autocompleteUsers (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
232 | const resultList = await UserModel.autocomplete(req.query.search as string) | ||
233 | |||
234 | return res.json(resultList) | ||
235 | } | ||
236 | |||
225 | async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { | 237 | async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { |
226 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) | 238 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) |
227 | 239 | ||
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index be803490b..0c9e6c2d1 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -49,6 +49,7 @@ import { abuseVideoRouter } from './abuse' | |||
49 | import { blacklistRouter } from './blacklist' | 49 | import { blacklistRouter } from './blacklist' |
50 | import { videoCommentRouter } from './comment' | 50 | import { videoCommentRouter } from './comment' |
51 | import { rateVideoRouter } from './rate' | 51 | import { rateVideoRouter } from './rate' |
52 | import { ownershipVideoRouter } from './ownership' | ||
52 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' | 53 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' |
53 | import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils' | 54 | import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils' |
54 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' | 55 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' |
@@ -84,6 +85,7 @@ videosRouter.use('/', rateVideoRouter) | |||
84 | videosRouter.use('/', videoCommentRouter) | 85 | videosRouter.use('/', videoCommentRouter) |
85 | videosRouter.use('/', videoCaptionsRouter) | 86 | videosRouter.use('/', videoCaptionsRouter) |
86 | videosRouter.use('/', videoImportsRouter) | 87 | videosRouter.use('/', videoImportsRouter) |
88 | videosRouter.use('/', ownershipVideoRouter) | ||
87 | 89 | ||
88 | videosRouter.get('/categories', listVideoCategories) | 90 | videosRouter.get('/categories', listVideoCategories) |
89 | videosRouter.get('/licences', listVideoLicences) | 91 | videosRouter.get('/licences', listVideoLicences) |
diff --git a/server/controllers/api/videos/ownership.ts b/server/controllers/api/videos/ownership.ts new file mode 100644 index 000000000..fc42f5fff --- /dev/null +++ b/server/controllers/api/videos/ownership.ts | |||
@@ -0,0 +1,117 @@ | |||
1 | import * as express from 'express' | ||
2 | import { logger } from '../../../helpers/logger' | ||
3 | import { sequelizeTypescript } from '../../../initializers' | ||
4 | import { | ||
5 | asyncMiddleware, | ||
6 | asyncRetryTransactionMiddleware, | ||
7 | authenticate, | ||
8 | paginationValidator, | ||
9 | setDefaultPagination, | ||
10 | videosAcceptChangeOwnershipValidator, | ||
11 | videosChangeOwnershipValidator, | ||
12 | videosTerminateChangeOwnershipValidator | ||
13 | } from '../../../middlewares' | ||
14 | import { AccountModel } from '../../../models/account/account' | ||
15 | import { VideoModel } from '../../../models/video/video' | ||
16 | import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership' | ||
17 | import { VideoChangeOwnershipStatus } from '../../../../shared/models/videos' | ||
18 | import { VideoChannelModel } from '../../../models/video/video-channel' | ||
19 | import { getFormattedObjects } from '../../../helpers/utils' | ||
20 | |||
21 | const ownershipVideoRouter = express.Router() | ||
22 | |||
23 | ownershipVideoRouter.post('/:videoId/give-ownership', | ||
24 | authenticate, | ||
25 | asyncMiddleware(videosChangeOwnershipValidator), | ||
26 | asyncRetryTransactionMiddleware(giveVideoOwnership) | ||
27 | ) | ||
28 | |||
29 | ownershipVideoRouter.get('/ownership', | ||
30 | authenticate, | ||
31 | paginationValidator, | ||
32 | setDefaultPagination, | ||
33 | asyncRetryTransactionMiddleware(listVideoOwnership) | ||
34 | ) | ||
35 | |||
36 | ownershipVideoRouter.post('/ownership/:id/accept', | ||
37 | authenticate, | ||
38 | asyncMiddleware(videosTerminateChangeOwnershipValidator), | ||
39 | asyncMiddleware(videosAcceptChangeOwnershipValidator), | ||
40 | asyncRetryTransactionMiddleware(acceptOwnership) | ||
41 | ) | ||
42 | |||
43 | ownershipVideoRouter.post('/ownership/:id/refuse', | ||
44 | authenticate, | ||
45 | asyncMiddleware(videosTerminateChangeOwnershipValidator), | ||
46 | asyncRetryTransactionMiddleware(refuseOwnership) | ||
47 | ) | ||
48 | |||
49 | // --------------------------------------------------------------------------- | ||
50 | |||
51 | export { | ||
52 | ownershipVideoRouter | ||
53 | } | ||
54 | |||
55 | // --------------------------------------------------------------------------- | ||
56 | |||
57 | async function giveVideoOwnership (req: express.Request, res: express.Response) { | ||
58 | const videoInstance = res.locals.video as VideoModel | ||
59 | const initiatorAccount = res.locals.oauth.token.User.Account as AccountModel | ||
60 | const nextOwner = res.locals.nextOwner as AccountModel | ||
61 | |||
62 | await sequelizeTypescript.transaction(async t => { | ||
63 | await VideoChangeOwnershipModel.findOrCreate({ | ||
64 | where: { | ||
65 | initiatorAccountId: initiatorAccount.id, | ||
66 | nextOwnerAccountId: nextOwner.id, | ||
67 | videoId: videoInstance.id, | ||
68 | status: VideoChangeOwnershipStatus.WAITING | ||
69 | }, | ||
70 | defaults: { | ||
71 | initiatorAccountId: initiatorAccount.id, | ||
72 | nextOwnerAccountId: nextOwner.id, | ||
73 | videoId: videoInstance.id, | ||
74 | status: VideoChangeOwnershipStatus.WAITING | ||
75 | } | ||
76 | }) | ||
77 | logger.info('Ownership change for video %s created.', videoInstance.name) | ||
78 | return res.type('json').status(204).end() | ||
79 | }) | ||
80 | } | ||
81 | |||
82 | async function listVideoOwnership (req: express.Request, res: express.Response) { | ||
83 | const currentAccount = res.locals.oauth.token.User.Account as AccountModel | ||
84 | const resultList = await VideoChangeOwnershipModel.listForApi( | ||
85 | currentAccount.id, | ||
86 | req.query.start || 0, | ||
87 | req.query.count || 10, | ||
88 | req.query.sort || 'createdAt' | ||
89 | ) | ||
90 | |||
91 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
92 | } | ||
93 | |||
94 | async function acceptOwnership (req: express.Request, res: express.Response) { | ||
95 | return sequelizeTypescript.transaction(async t => { | ||
96 | const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel | ||
97 | const targetVideo = videoChangeOwnership.Video | ||
98 | const channel = res.locals.videoChannel as VideoChannelModel | ||
99 | |||
100 | targetVideo.set('channelId', channel.id) | ||
101 | |||
102 | await targetVideo.save() | ||
103 | videoChangeOwnership.set('status', VideoChangeOwnershipStatus.ACCEPTED) | ||
104 | await videoChangeOwnership.save() | ||
105 | |||
106 | return res.sendStatus(204) | ||
107 | }) | ||
108 | } | ||
109 | |||
110 | async function refuseOwnership (req: express.Request, res: express.Response) { | ||
111 | return sequelizeTypescript.transaction(async t => { | ||
112 | const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel | ||
113 | videoChangeOwnership.set('status', VideoChangeOwnershipStatus.REFUSED) | ||
114 | await videoChangeOwnership.save() | ||
115 | return res.sendStatus(204) | ||
116 | }) | ||
117 | } | ||