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