]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { MVideoFullLight } from '@server/types/models' | |
3 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | |
4 | import { VideoChangeOwnershipStatus, VideoState } from '../../../../shared/models/videos' | |
5 | import { logger } from '../../../helpers/logger' | |
6 | import { getFormattedObjects } from '../../../helpers/utils' | |
7 | import { sequelizeTypescript } from '../../../initializers/database' | |
8 | import { sendUpdateVideo } from '../../../lib/activitypub/send' | |
9 | import { changeVideoChannelShare } from '../../../lib/activitypub/share' | |
10 | import { | |
11 | asyncMiddleware, | |
12 | asyncRetryTransactionMiddleware, | |
13 | authenticate, | |
14 | paginationValidator, | |
15 | setDefaultPagination, | |
16 | videosAcceptChangeOwnershipValidator, | |
17 | videosChangeOwnershipValidator, | |
18 | videosTerminateChangeOwnershipValidator | |
19 | } from '../../../middlewares' | |
20 | import { VideoModel } from '../../../models/video/video' | |
21 | import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership' | |
22 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
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.videoAll | |
62 | const initiatorAccountId = res.locals.oauth.token.User.Account.id | |
63 | const nextOwner = res.locals.nextOwner | |
64 | ||
65 | await sequelizeTypescript.transaction(t => { | |
66 | return VideoChangeOwnershipModel.findOrCreate({ | |
67 | where: { | |
68 | initiatorAccountId, | |
69 | nextOwnerAccountId: nextOwner.id, | |
70 | videoId: videoInstance.id, | |
71 | status: VideoChangeOwnershipStatus.WAITING | |
72 | }, | |
73 | defaults: { | |
74 | initiatorAccountId, | |
75 | nextOwnerAccountId: nextOwner.id, | |
76 | videoId: videoInstance.id, | |
77 | status: VideoChangeOwnershipStatus.WAITING | |
78 | }, | |
79 | transaction: t | |
80 | }) | |
81 | }) | |
82 | ||
83 | logger.info('Ownership change for video %s created.', videoInstance.name) | |
84 | return res.type('json') | |
85 | .status(HttpStatusCode.NO_CONTENT_204) | |
86 | .end() | |
87 | } | |
88 | ||
89 | async function listVideoOwnership (req: express.Request, res: express.Response) { | |
90 | const currentAccountId = res.locals.oauth.token.User.Account.id | |
91 | ||
92 | const resultList = await VideoChangeOwnershipModel.listForApi( | |
93 | currentAccountId, | |
94 | req.query.start || 0, | |
95 | req.query.count || 10, | |
96 | req.query.sort || 'createdAt' | |
97 | ) | |
98 | ||
99 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
100 | } | |
101 | ||
102 | function acceptOwnership (req: express.Request, res: express.Response) { | |
103 | return sequelizeTypescript.transaction(async t => { | |
104 | const videoChangeOwnership = res.locals.videoChangeOwnership | |
105 | const channel = res.locals.videoChannel | |
106 | ||
107 | // We need more attributes for federation | |
108 | const targetVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoChangeOwnership.Video.id, t) | |
109 | ||
110 | const oldVideoChannel = await VideoChannelModel.loadAndPopulateAccount(targetVideo.channelId, t) | |
111 | ||
112 | targetVideo.channelId = channel.id | |
113 | ||
114 | const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFullLight | |
115 | targetVideoUpdated.VideoChannel = channel | |
116 | ||
117 | if (targetVideoUpdated.hasPrivacyForFederation() && targetVideoUpdated.state === VideoState.PUBLISHED) { | |
118 | await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t) | |
119 | await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor) | |
120 | } | |
121 | ||
122 | videoChangeOwnership.status = VideoChangeOwnershipStatus.ACCEPTED | |
123 | await videoChangeOwnership.save({ transaction: t }) | |
124 | ||
125 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
126 | }) | |
127 | } | |
128 | ||
129 | function refuseOwnership (req: express.Request, res: express.Response) { | |
130 | return sequelizeTypescript.transaction(async t => { | |
131 | const videoChangeOwnership = res.locals.videoChangeOwnership | |
132 | ||
133 | videoChangeOwnership.status = VideoChangeOwnershipStatus.REFUSED | |
134 | await videoChangeOwnership.save({ transaction: t }) | |
135 | ||
136 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
137 | }) | |
138 | } |