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