]>
Commit | Line | Data |
---|---|---|
3e753302 C |
1 | import { Response } from 'express' |
2 | import { fetchVideo, VideoFetchType } from '../video' | |
3e753302 C |
3 | import { UserRight } from '../../../shared/models/users' |
4 | import { VideoChannelModel } from '../../models/video/video-channel' | |
7eba5e1f C |
5 | import { |
6 | MUser, | |
7 | MUserAccountId, | |
8 | MVideoAccountLight, | |
9 | MVideoFullLight, | |
10 | MVideoIdThumbnail, | |
11 | MVideoImmutable, | |
12 | MVideoThumbnail, | |
13 | MVideoWithRights | |
26d6bf65 | 14 | } from '@server/types/models' |
8319d6ae | 15 | import { VideoFileModel } from '@server/models/video/video-file' |
3e753302 C |
16 | |
17 | async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { | |
18 | const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined | |
19 | ||
20 | const video = await fetchVideo(id, fetchType, userId) | |
21 | ||
22 | if (video === null) { | |
23 | res.status(404) | |
24 | .json({ error: 'Video not found' }) | |
25 | .end() | |
26 | ||
27 | return false | |
28 | } | |
29 | ||
453e83ea C |
30 | switch (fetchType) { |
31 | case 'all': | |
32 | res.locals.videoAll = video as MVideoFullLight | |
33 | break | |
34 | ||
7eba5e1f C |
35 | case 'only-immutable-attributes': |
36 | res.locals.onlyImmutableVideo = video as MVideoImmutable | |
37 | break | |
38 | ||
453e83ea | 39 | case 'id': |
7eba5e1f | 40 | res.locals.videoId = video as MVideoIdThumbnail |
453e83ea C |
41 | break |
42 | ||
43 | case 'only-video': | |
0283eaac | 44 | res.locals.onlyVideo = video as MVideoThumbnail |
453e83ea C |
45 | break |
46 | ||
47 | case 'only-video-with-rights': | |
48 | res.locals.onlyVideoWithRights = video as MVideoWithRights | |
49 | break | |
50 | } | |
51 | ||
3e753302 C |
52 | return true |
53 | } | |
54 | ||
8319d6ae RK |
55 | async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) { |
56 | if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) { | |
57 | res.status(404) | |
58 | .json({ error: 'VideoFile matching Video not found' }) | |
59 | .end() | |
60 | ||
61 | return false | |
62 | } | |
63 | ||
64 | return true | |
65 | } | |
66 | ||
453e83ea | 67 | async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) { |
3e753302 C |
68 | if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { |
69 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) | |
70 | if (videoChannel === null) { | |
71 | res.status(400) | |
72 | .json({ error: 'Unknown video `video channel` on this instance.' }) | |
73 | .end() | |
74 | ||
75 | return false | |
76 | } | |
77 | ||
78 | res.locals.videoChannel = videoChannel | |
79 | return true | |
80 | } | |
81 | ||
82 | const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) | |
83 | if (videoChannel === null) { | |
84 | res.status(400) | |
85 | .json({ error: 'Unknown video `video channel` for this account.' }) | |
86 | .end() | |
87 | ||
88 | return false | |
89 | } | |
90 | ||
91 | res.locals.videoChannel = videoChannel | |
92 | return true | |
93 | } | |
94 | ||
af4ae64f | 95 | function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { |
3e753302 | 96 | // Retrieve the user who did the request |
af4ae64f | 97 | if (onlyOwned && video.isOwned() === false) { |
3e753302 C |
98 | res.status(403) |
99 | .json({ error: 'Cannot manage a video of another server.' }) | |
100 | .end() | |
101 | return false | |
102 | } | |
103 | ||
104 | // Check if the user can delete the video | |
105 | // The user can delete it if he has the right | |
106 | // Or if s/he is the video's account | |
107 | const account = video.VideoChannel.Account | |
108 | if (user.hasRight(right) === false && account.userId !== user.id) { | |
109 | res.status(403) | |
110 | .json({ error: 'Cannot manage a video of another user.' }) | |
111 | .end() | |
112 | return false | |
113 | } | |
114 | ||
115 | return true | |
116 | } | |
117 | ||
118 | // --------------------------------------------------------------------------- | |
119 | ||
120 | export { | |
121 | doesVideoChannelOfAccountExist, | |
122 | doesVideoExist, | |
8319d6ae | 123 | doesVideoFileOfVideoExist, |
3e753302 C |
124 | checkUserCanManageVideo |
125 | } |