]>
Commit | Line | Data |
---|---|---|
e02643f3 | 1 | import * as Sequelize from 'sequelize' |
3fd3ab2d | 2 | import { |
50d6de9c C |
3 | AllowNull, BeforeCreate, BeforeUpdate, Column, CreatedAt, DataType, Default, DefaultScope, HasMany, HasOne, Is, IsEmail, Model, |
4 | Scopes, Table, UpdatedAt | |
3fd3ab2d | 5 | } from 'sequelize-typescript' |
e34c85e5 | 6 | import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' |
50d6de9c | 7 | import { comparePassword, cryptPassword } from '../../helpers' |
65fcc311 | 8 | import { |
50d6de9c C |
9 | isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, |
10 | isUserVideoQuotaValid | |
3fd3ab2d C |
11 | } from '../../helpers/custom-validators/users' |
12 | import { OAuthTokenModel } from '../oauth/oauth-token' | |
13 | import { getSort, throwIfNotValid } from '../utils' | |
14 | import { VideoChannelModel } from '../video/video-channel' | |
15 | import { AccountModel } from './account' | |
16 | ||
d48ff09d C |
17 | @DefaultScope({ |
18 | include: [ | |
19 | { | |
20 | model: () => AccountModel, | |
21 | required: true | |
22 | } | |
23 | ] | |
24 | }) | |
25 | @Scopes({ | |
26 | withVideoChannel: { | |
27 | include: [ | |
28 | { | |
29 | model: () => AccountModel, | |
30 | required: true, | |
31 | include: [ () => VideoChannelModel ] | |
32 | } | |
33 | ] | |
34 | } | |
35 | }) | |
3fd3ab2d C |
36 | @Table({ |
37 | tableName: 'user', | |
38 | indexes: [ | |
feb4bdfd | 39 | { |
3fd3ab2d C |
40 | fields: [ 'username' ], |
41 | unique: true | |
feb4bdfd C |
42 | }, |
43 | { | |
3fd3ab2d C |
44 | fields: [ 'email' ], |
45 | unique: true | |
feb4bdfd | 46 | } |
e02643f3 | 47 | ] |
3fd3ab2d C |
48 | }) |
49 | export class UserModel extends Model<UserModel> { | |
50 | ||
51 | @AllowNull(false) | |
52 | @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password')) | |
53 | @Column | |
54 | password: string | |
55 | ||
56 | @AllowNull(false) | |
57 | @Is('UserPassword', value => throwIfNotValid(value, isUserUsernameValid, 'user name')) | |
58 | @Column | |
59 | username: string | |
60 | ||
61 | @AllowNull(false) | |
62 | @IsEmail | |
63 | @Column(DataType.STRING(400)) | |
64 | email: string | |
65 | ||
66 | @AllowNull(false) | |
67 | @Default(false) | |
68 | @Is('UserDisplayNSFW', value => throwIfNotValid(value, isUserDisplayNSFWValid, 'display NSFW boolean')) | |
69 | @Column | |
70 | displayNSFW: boolean | |
71 | ||
7efe153b AL |
72 | @AllowNull(false) |
73 | @Default(true) | |
74 | @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean')) | |
75 | @Column | |
76 | autoPlayVideo: boolean | |
77 | ||
3fd3ab2d C |
78 | @AllowNull(false) |
79 | @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role')) | |
80 | @Column | |
81 | role: number | |
82 | ||
83 | @AllowNull(false) | |
84 | @Is('UserVideoQuota', value => throwIfNotValid(value, isUserVideoQuotaValid, 'video quota')) | |
85 | @Column(DataType.BIGINT) | |
86 | videoQuota: number | |
87 | ||
88 | @CreatedAt | |
89 | createdAt: Date | |
90 | ||
91 | @UpdatedAt | |
92 | updatedAt: Date | |
93 | ||
94 | @HasOne(() => AccountModel, { | |
95 | foreignKey: 'userId', | |
96 | onDelete: 'cascade' | |
97 | }) | |
98 | Account: AccountModel | |
69b0a27c | 99 | |
3fd3ab2d C |
100 | @HasMany(() => OAuthTokenModel, { |
101 | foreignKey: 'userId', | |
102 | onDelete: 'cascade' | |
103 | }) | |
104 | OAuthTokens: OAuthTokenModel[] | |
105 | ||
106 | @BeforeCreate | |
107 | @BeforeUpdate | |
108 | static cryptPasswordIfNeeded (instance: UserModel) { | |
109 | if (instance.changed('password')) { | |
110 | return cryptPassword(instance.password) | |
111 | .then(hash => { | |
112 | instance.password = hash | |
113 | return undefined | |
114 | }) | |
115 | } | |
59557c46 | 116 | } |
26d7d31b | 117 | |
3fd3ab2d C |
118 | static countTotal () { |
119 | return this.count() | |
120 | } | |
954605a8 | 121 | |
3fd3ab2d C |
122 | static getByUsername (username: string) { |
123 | const query = { | |
124 | where: { | |
125 | username: username | |
126 | }, | |
127 | include: [ { model: AccountModel, required: true } ] | |
128 | } | |
26d7d31b | 129 | |
3fd3ab2d | 130 | return UserModel.findOne(query) |
26d7d31b | 131 | } |
72c7248b | 132 | |
3fd3ab2d C |
133 | static listForApi (start: number, count: number, sort: string) { |
134 | const query = { | |
135 | offset: start, | |
136 | limit: count, | |
d48ff09d | 137 | order: [ getSort(sort) ] |
3fd3ab2d | 138 | } |
72c7248b | 139 | |
3fd3ab2d C |
140 | return UserModel.findAndCountAll(query) |
141 | .then(({ rows, count }) => { | |
142 | return { | |
143 | data: rows, | |
144 | total: count | |
145 | } | |
72c7248b | 146 | }) |
72c7248b C |
147 | } |
148 | ||
3fd3ab2d | 149 | static loadById (id: number) { |
d48ff09d | 150 | return UserModel.findById(id) |
3fd3ab2d | 151 | } |
feb4bdfd | 152 | |
3fd3ab2d C |
153 | static loadByUsername (username: string) { |
154 | const query = { | |
155 | where: { | |
156 | username | |
d48ff09d | 157 | } |
3fd3ab2d | 158 | } |
089ff2f2 | 159 | |
3fd3ab2d | 160 | return UserModel.findOne(query) |
feb4bdfd C |
161 | } |
162 | ||
3fd3ab2d C |
163 | static loadByUsernameAndPopulateChannels (username: string) { |
164 | const query = { | |
165 | where: { | |
166 | username | |
d48ff09d | 167 | } |
3fd3ab2d | 168 | } |
9bd26629 | 169 | |
d48ff09d | 170 | return UserModel.scope('withVideoChannel').findOne(query) |
feb4bdfd C |
171 | } |
172 | ||
3fd3ab2d C |
173 | static loadByUsernameOrEmail (username: string, email: string) { |
174 | const query = { | |
3fd3ab2d C |
175 | where: { |
176 | [ Sequelize.Op.or ]: [ { username }, { email } ] | |
177 | } | |
6fcd19ba | 178 | } |
69b0a27c | 179 | |
d48ff09d | 180 | return UserModel.findOne(query) |
72c7248b C |
181 | } |
182 | ||
3fd3ab2d C |
183 | private static getOriginalVideoFileTotalFromUser (user: UserModel) { |
184 | // Don't use sequelize because we need to use a sub query | |
185 | const query = 'SELECT SUM("size") AS "total" FROM ' + | |
186 | '(SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + | |
187 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | |
188 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
189 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | |
190 | 'INNER JOIN "user" ON "account"."userId" = "user"."id" ' + | |
191 | 'WHERE "user"."id" = $userId GROUP BY "video"."id") t' | |
192 | ||
193 | const options = { | |
194 | bind: { userId: user.id }, | |
195 | type: Sequelize.QueryTypes.SELECT | |
196 | } | |
197 | return UserModel.sequelize.query(query, options) | |
198 | .then(([ { total } ]) => { | |
199 | if (total === null) return 0 | |
68a3b9f2 | 200 | |
3fd3ab2d C |
201 | return parseInt(total, 10) |
202 | }) | |
72c7248b C |
203 | } |
204 | ||
3fd3ab2d C |
205 | hasRight (right: UserRight) { |
206 | return hasUserRight(this.role, right) | |
207 | } | |
72c7248b | 208 | |
3fd3ab2d C |
209 | isPasswordMatch (password: string) { |
210 | return comparePassword(password, this.password) | |
feb4bdfd C |
211 | } |
212 | ||
3fd3ab2d C |
213 | toFormattedJSON () { |
214 | const json = { | |
215 | id: this.id, | |
216 | username: this.username, | |
217 | email: this.email, | |
218 | displayNSFW: this.displayNSFW, | |
7efe153b | 219 | autoPlayVideo: this.autoPlayVideo, |
3fd3ab2d C |
220 | role: this.role, |
221 | roleLabel: USER_ROLE_LABELS[ this.role ], | |
222 | videoQuota: this.videoQuota, | |
223 | createdAt: this.createdAt, | |
224 | account: this.Account.toFormattedJSON() | |
225 | } | |
226 | ||
227 | if (Array.isArray(this.Account.VideoChannels) === true) { | |
228 | json['videoChannels'] = this.Account.VideoChannels | |
229 | .map(c => c.toFormattedJSON()) | |
230 | .sort((v1, v2) => { | |
231 | if (v1.createdAt < v2.createdAt) return -1 | |
232 | if (v1.createdAt === v2.createdAt) return 0 | |
ad4a8a1c | 233 | |
3fd3ab2d C |
234 | return 1 |
235 | }) | |
ad4a8a1c | 236 | } |
3fd3ab2d C |
237 | |
238 | return json | |
ad4a8a1c C |
239 | } |
240 | ||
3fd3ab2d C |
241 | isAbleToUploadVideo (videoFile: Express.Multer.File) { |
242 | if (this.videoQuota === -1) return Promise.resolve(true) | |
b0f9f39e | 243 | |
3fd3ab2d C |
244 | return UserModel.getOriginalVideoFileTotalFromUser(this) |
245 | .then(totalBytes => { | |
246 | return (videoFile.size + totalBytes) < this.videoQuota | |
247 | }) | |
b0f9f39e | 248 | } |
b0f9f39e | 249 | } |