diff options
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r-- | server/helpers/custom-validators/video-ownership.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/video-ownership.ts b/server/helpers/custom-validators/video-ownership.ts new file mode 100644 index 000000000..aaa0c736b --- /dev/null +++ b/server/helpers/custom-validators/video-ownership.ts | |||
@@ -0,0 +1,42 @@ | |||
1 | import { Response } from 'express' | ||
2 | import * as validator from 'validator' | ||
3 | import { VideoChangeOwnershipModel } from '../../models/video/video-change-ownership' | ||
4 | import { UserModel } from '../../models/account/user' | ||
5 | |||
6 | export async function doesChangeVideoOwnershipExist (id: string, res: Response): Promise<boolean> { | ||
7 | const videoChangeOwnership = await loadVideoChangeOwnership(id) | ||
8 | |||
9 | if (!videoChangeOwnership) { | ||
10 | res.status(404) | ||
11 | .json({ error: 'Video change ownership not found' }) | ||
12 | .end() | ||
13 | |||
14 | return false | ||
15 | } | ||
16 | |||
17 | res.locals.videoChangeOwnership = videoChangeOwnership | ||
18 | return true | ||
19 | } | ||
20 | |||
21 | async function loadVideoChangeOwnership (id: string): Promise<VideoChangeOwnershipModel | undefined> { | ||
22 | if (validator.isInt(id)) { | ||
23 | return VideoChangeOwnershipModel.load(parseInt(id, 10)) | ||
24 | } | ||
25 | |||
26 | return undefined | ||
27 | } | ||
28 | |||
29 | export function checkUserCanTerminateOwnershipChange ( | ||
30 | user: UserModel, | ||
31 | videoChangeOwnership: VideoChangeOwnershipModel, | ||
32 | res: Response | ||
33 | ): boolean { | ||
34 | if (videoChangeOwnership.NextOwner.userId === user.Account.userId) { | ||
35 | return true | ||
36 | } | ||
37 | |||
38 | res.status(403) | ||
39 | .json({ error: 'Cannot terminate an ownership change of another user' }) | ||
40 | .end() | ||
41 | return false | ||
42 | } | ||