]>
Commit | Line | Data |
---|---|---|
74d63469 GR |
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' | |
5cf84858 | 17 | import { VideoChangeOwnershipStatus, VideoPrivacy, VideoState } from '../../../../shared/models/videos' |
74d63469 GR |
18 | import { VideoChannelModel } from '../../../models/video/video-channel' |
19 | import { getFormattedObjects } from '../../../helpers/utils' | |
5cf84858 C |
20 | import { changeVideoChannelShare } from '../../../lib/activitypub' |
21 | import { sendUpdateVideo } from '../../../lib/activitypub/send' | |
91411dba | 22 | import { UserModel } from '../../../models/account/user' |
74d63469 GR |
23 | |
24 | const ownershipVideoRouter = express.Router() | |
25 | ||
26 | ownershipVideoRouter.post('/:videoId/give-ownership', | |
27 | authenticate, | |
28 | asyncMiddleware(videosChangeOwnershipValidator), | |
29 | asyncRetryTransactionMiddleware(giveVideoOwnership) | |
30 | ) | |
31 | ||
32 | ownershipVideoRouter.get('/ownership', | |
33 | authenticate, | |
34 | paginationValidator, | |
35 | setDefaultPagination, | |
36 | asyncRetryTransactionMiddleware(listVideoOwnership) | |
37 | ) | |
38 | ||
39 | ownershipVideoRouter.post('/ownership/:id/accept', | |
40 | authenticate, | |
41 | asyncMiddleware(videosTerminateChangeOwnershipValidator), | |
42 | asyncMiddleware(videosAcceptChangeOwnershipValidator), | |
43 | asyncRetryTransactionMiddleware(acceptOwnership) | |
44 | ) | |
45 | ||
46 | ownershipVideoRouter.post('/ownership/:id/refuse', | |
47 | authenticate, | |
48 | asyncMiddleware(videosTerminateChangeOwnershipValidator), | |
49 | asyncRetryTransactionMiddleware(refuseOwnership) | |
50 | ) | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
54 | export { | |
55 | ownershipVideoRouter | |
56 | } | |
57 | ||
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | async function giveVideoOwnership (req: express.Request, res: express.Response) { | |
61 | const videoInstance = res.locals.video as VideoModel | |
91411dba | 62 | const initiatorAccountId = (res.locals.oauth.token.User as UserModel).Account.id |
74d63469 GR |
63 | const nextOwner = res.locals.nextOwner as AccountModel |
64 | ||
5cf84858 C |
65 | await sequelizeTypescript.transaction(t => { |
66 | return VideoChangeOwnershipModel.findOrCreate({ | |
74d63469 | 67 | where: { |
91411dba | 68 | initiatorAccountId, |
74d63469 GR |
69 | nextOwnerAccountId: nextOwner.id, |
70 | videoId: videoInstance.id, | |
71 | status: VideoChangeOwnershipStatus.WAITING | |
72 | }, | |
73 | defaults: { | |
91411dba | 74 | initiatorAccountId, |
74d63469 GR |
75 | nextOwnerAccountId: nextOwner.id, |
76 | videoId: videoInstance.id, | |
77 | status: VideoChangeOwnershipStatus.WAITING | |
5cf84858 C |
78 | }, |
79 | transaction: t | |
74d63469 | 80 | }) |
74d63469 | 81 | }) |
5cf84858 C |
82 | |
83 | logger.info('Ownership change for video %s created.', videoInstance.name) | |
84 | return res.type('json').status(204).end() | |
74d63469 GR |
85 | } |
86 | ||
87 | async function listVideoOwnership (req: express.Request, res: express.Response) { | |
91411dba C |
88 | const currentAccountId = (res.locals.oauth.token.User as UserModel).Account.id |
89 | ||
74d63469 | 90 | const resultList = await VideoChangeOwnershipModel.listForApi( |
91411dba | 91 | currentAccountId, |
74d63469 GR |
92 | req.query.start || 0, |
93 | req.query.count || 10, | |
94 | req.query.sort || 'createdAt' | |
95 | ) | |
96 | ||
97 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
98 | } | |
99 | ||
100 | async function acceptOwnership (req: express.Request, res: express.Response) { | |
101 | return sequelizeTypescript.transaction(async t => { | |
102 | const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel | |
103 | const targetVideo = videoChangeOwnership.Video | |
104 | const channel = res.locals.videoChannel as VideoChannelModel | |
105 | ||
5cf84858 C |
106 | const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId) |
107 | ||
74d63469 | 108 | targetVideo.set('channelId', channel.id) |
5cf84858 C |
109 | const targetVideoUpdated = await targetVideo.save({ transaction: t }) |
110 | targetVideoUpdated.VideoChannel = channel | |
111 | ||
112 | if (targetVideoUpdated.privacy !== VideoPrivacy.PRIVATE && targetVideoUpdated.state === VideoState.PUBLISHED) { | |
113 | await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t) | |
114 | await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor) | |
115 | } | |
74d63469 | 116 | |
74d63469 | 117 | videoChangeOwnership.set('status', VideoChangeOwnershipStatus.ACCEPTED) |
5cf84858 | 118 | await videoChangeOwnership.save({ transaction: t }) |
74d63469 GR |
119 | |
120 | return res.sendStatus(204) | |
121 | }) | |
122 | } | |
123 | ||
124 | async function refuseOwnership (req: express.Request, res: express.Response) { | |
125 | return sequelizeTypescript.transaction(async t => { | |
126 | const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel | |
5cf84858 | 127 | |
74d63469 | 128 | videoChangeOwnership.set('status', VideoChangeOwnershipStatus.REFUSED) |
5cf84858 C |
129 | await videoChangeOwnership.save({ transaction: t }) |
130 | ||
74d63469 GR |
131 | return res.sendStatus(204) |
132 | }) | |
133 | } |