]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { ActorModel } from '@server/models/actor/actor' | |
3 | import { UserModel } from '@server/models/user/user' | |
4 | import { MUserDefault } from '@server/types/models' | |
5 | import { HttpStatusCode } from '@shared/models' | |
6 | ||
7 | function checkUserIdExist (idArg: number | string, res: express.Response, withStats = false) { | |
8 | const id = parseInt(idArg + '', 10) | |
9 | return checkUserExist(() => UserModel.loadByIdWithChannels(id, withStats), res) | |
10 | } | |
11 | ||
12 | function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) { | |
13 | return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse) | |
14 | } | |
15 | ||
16 | async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) { | |
17 | const user = await UserModel.loadByUsernameOrEmail(username, email) | |
18 | ||
19 | if (user) { | |
20 | res.fail({ | |
21 | status: HttpStatusCode.CONFLICT_409, | |
22 | message: 'User with this username or email already exists.' | |
23 | }) | |
24 | return false | |
25 | } | |
26 | ||
27 | const actor = await ActorModel.loadLocalByName(username) | |
28 | if (actor) { | |
29 | res.fail({ | |
30 | status: HttpStatusCode.CONFLICT_409, | |
31 | message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' | |
32 | }) | |
33 | return false | |
34 | } | |
35 | ||
36 | return true | |
37 | } | |
38 | ||
39 | async function checkUserExist (finder: () => Promise<MUserDefault>, res: express.Response, abortResponse = true) { | |
40 | const user = await finder() | |
41 | ||
42 | if (!user) { | |
43 | if (abortResponse === true) { | |
44 | res.fail({ | |
45 | status: HttpStatusCode.NOT_FOUND_404, | |
46 | message: 'User not found' | |
47 | }) | |
48 | } | |
49 | ||
50 | return false | |
51 | } | |
52 | ||
53 | res.locals.user = user | |
54 | return true | |
55 | } | |
56 | ||
57 | export { | |
58 | checkUserIdExist, | |
59 | checkUserEmailExist, | |
60 | checkUserNameOrEmailDoesNotAlreadyExist, | |
61 | checkUserExist | |
62 | } |