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