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