aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/users/index.ts12
-rw-r--r--server/controllers/api/videos/index.ts2
-rw-r--r--server/controllers/api/videos/ownership.ts117
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({
51const usersRouter = express.Router() 52const usersRouter = express.Router()
52usersRouter.use('/', meRouter) 53usersRouter.use('/', meRouter)
53 54
55usersRouter.get('/autocomplete',
56 userAutocompleteValidator,
57 asyncMiddleware(autocompleteUsers)
58)
59
54usersRouter.get('/', 60usersRouter.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
231async 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
225async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { 237async 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'
49import { blacklistRouter } from './blacklist' 49import { blacklistRouter } from './blacklist'
50import { videoCommentRouter } from './comment' 50import { videoCommentRouter } from './comment'
51import { rateVideoRouter } from './rate' 51import { rateVideoRouter } from './rate'
52import { ownershipVideoRouter } from './ownership'
52import { VideoFilter } from '../../../../shared/models/videos/video-query.type' 53import { VideoFilter } from '../../../../shared/models/videos/video-query.type'
53import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils' 54import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils'
54import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' 55import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
@@ -84,6 +85,7 @@ videosRouter.use('/', rateVideoRouter)
84videosRouter.use('/', videoCommentRouter) 85videosRouter.use('/', videoCommentRouter)
85videosRouter.use('/', videoCaptionsRouter) 86videosRouter.use('/', videoCaptionsRouter)
86videosRouter.use('/', videoImportsRouter) 87videosRouter.use('/', videoImportsRouter)
88videosRouter.use('/', ownershipVideoRouter)
87 89
88videosRouter.get('/categories', listVideoCategories) 90videosRouter.get('/categories', listVideoCategories)
89videosRouter.get('/licences', listVideoLicences) 91videosRouter.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 @@
1import * as express from 'express'
2import { logger } from '../../../helpers/logger'
3import { sequelizeTypescript } from '../../../initializers'
4import {
5 asyncMiddleware,
6 asyncRetryTransactionMiddleware,
7 authenticate,
8 paginationValidator,
9 setDefaultPagination,
10 videosAcceptChangeOwnershipValidator,
11 videosChangeOwnershipValidator,
12 videosTerminateChangeOwnershipValidator
13} from '../../../middlewares'
14import { AccountModel } from '../../../models/account/account'
15import { VideoModel } from '../../../models/video/video'
16import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership'
17import { VideoChangeOwnershipStatus } from '../../../../shared/models/videos'
18import { VideoChannelModel } from '../../../models/video/video-channel'
19import { getFormattedObjects } from '../../../helpers/utils'
20
21const ownershipVideoRouter = express.Router()
22
23ownershipVideoRouter.post('/:videoId/give-ownership',
24 authenticate,
25 asyncMiddleware(videosChangeOwnershipValidator),
26 asyncRetryTransactionMiddleware(giveVideoOwnership)
27)
28
29ownershipVideoRouter.get('/ownership',
30 authenticate,
31 paginationValidator,
32 setDefaultPagination,
33 asyncRetryTransactionMiddleware(listVideoOwnership)
34)
35
36ownershipVideoRouter.post('/ownership/:id/accept',
37 authenticate,
38 asyncMiddleware(videosTerminateChangeOwnershipValidator),
39 asyncMiddleware(videosAcceptChangeOwnershipValidator),
40 asyncRetryTransactionMiddleware(acceptOwnership)
41)
42
43ownershipVideoRouter.post('/ownership/:id/refuse',
44 authenticate,
45 asyncMiddleware(videosTerminateChangeOwnershipValidator),
46 asyncRetryTransactionMiddleware(refuseOwnership)
47)
48
49// ---------------------------------------------------------------------------
50
51export {
52 ownershipVideoRouter
53}
54
55// ---------------------------------------------------------------------------
56
57async 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
82async 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
94async 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
110async 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}