diff options
Diffstat (limited to 'server/models/user')
-rw-r--r-- | server/models/user/user-interface.ts | 8 | ||||
-rw-r--r-- | server/models/user/user.ts | 69 |
2 files changed, 60 insertions, 17 deletions
diff --git a/server/models/user/user-interface.ts b/server/models/user/user-interface.ts index 8974a9a97..1b5233eaf 100644 --- a/server/models/user/user-interface.ts +++ b/server/models/user/user-interface.ts | |||
@@ -5,6 +5,7 @@ import * as Promise from 'bluebird' | |||
5 | import { User as FormattedUser } from '../../../shared/models/users/user.model' | 5 | import { User as FormattedUser } from '../../../shared/models/users/user.model' |
6 | import { UserRole } from '../../../shared/models/users/user-role.type' | 6 | import { UserRole } from '../../../shared/models/users/user-role.type' |
7 | import { ResultList } from '../../../shared/models/result-list.model' | 7 | import { ResultList } from '../../../shared/models/result-list.model' |
8 | import { AuthorInstance } from '../video/author-interface' | ||
8 | 9 | ||
9 | export namespace UserMethods { | 10 | export namespace UserMethods { |
10 | export type IsPasswordMatch = (this: UserInstance, password: string) => Promise<boolean> | 11 | export type IsPasswordMatch = (this: UserInstance, password: string) => Promise<boolean> |
@@ -17,13 +18,12 @@ export namespace UserMethods { | |||
17 | 18 | ||
18 | export type GetByUsername = (username: string) => Promise<UserInstance> | 19 | export type GetByUsername = (username: string) => Promise<UserInstance> |
19 | 20 | ||
20 | export type List = () => Promise<UserInstance[]> | ||
21 | |||
22 | export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<UserInstance> > | 21 | export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<UserInstance> > |
23 | 22 | ||
24 | export type LoadById = (id: number) => Promise<UserInstance> | 23 | export type LoadById = (id: number) => Promise<UserInstance> |
25 | 24 | ||
26 | export type LoadByUsername = (username: string) => Promise<UserInstance> | 25 | export type LoadByUsername = (username: string) => Promise<UserInstance> |
26 | export type LoadByUsernameAndPopulateChannels = (username: string) => Promise<UserInstance> | ||
27 | 27 | ||
28 | export type LoadByUsernameOrEmail = (username: string, email: string) => Promise<UserInstance> | 28 | export type LoadByUsernameOrEmail = (username: string, email: string) => Promise<UserInstance> |
29 | } | 29 | } |
@@ -36,10 +36,10 @@ export interface UserClass { | |||
36 | 36 | ||
37 | countTotal: UserMethods.CountTotal, | 37 | countTotal: UserMethods.CountTotal, |
38 | getByUsername: UserMethods.GetByUsername, | 38 | getByUsername: UserMethods.GetByUsername, |
39 | list: UserMethods.List, | ||
40 | listForApi: UserMethods.ListForApi, | 39 | listForApi: UserMethods.ListForApi, |
41 | loadById: UserMethods.LoadById, | 40 | loadById: UserMethods.LoadById, |
42 | loadByUsername: UserMethods.LoadByUsername, | 41 | loadByUsername: UserMethods.LoadByUsername, |
42 | loadByUsernameAndPopulateChannels: UserMethods.LoadByUsernameAndPopulateChannels, | ||
43 | loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail | 43 | loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail |
44 | } | 44 | } |
45 | 45 | ||
@@ -51,6 +51,8 @@ export interface UserAttributes { | |||
51 | displayNSFW?: boolean | 51 | displayNSFW?: boolean |
52 | role: UserRole | 52 | role: UserRole |
53 | videoQuota: number | 53 | videoQuota: number |
54 | |||
55 | Author?: AuthorInstance | ||
54 | } | 56 | } |
55 | 57 | ||
56 | export interface UserInstance extends UserClass, UserAttributes, Sequelize.Instance<UserAttributes> { | 58 | export interface UserInstance extends UserClass, UserAttributes, Sequelize.Instance<UserAttributes> { |
diff --git a/server/models/user/user.ts b/server/models/user/user.ts index 0dc52d3cf..f8598c40f 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts | |||
@@ -27,10 +27,10 @@ let toFormattedJSON: UserMethods.ToFormattedJSON | |||
27 | let isAdmin: UserMethods.IsAdmin | 27 | let isAdmin: UserMethods.IsAdmin |
28 | let countTotal: UserMethods.CountTotal | 28 | let countTotal: UserMethods.CountTotal |
29 | let getByUsername: UserMethods.GetByUsername | 29 | let getByUsername: UserMethods.GetByUsername |
30 | let list: UserMethods.List | ||
31 | let listForApi: UserMethods.ListForApi | 30 | let listForApi: UserMethods.ListForApi |
32 | let loadById: UserMethods.LoadById | 31 | let loadById: UserMethods.LoadById |
33 | let loadByUsername: UserMethods.LoadByUsername | 32 | let loadByUsername: UserMethods.LoadByUsername |
33 | let loadByUsernameAndPopulateChannels: UserMethods.LoadByUsernameAndPopulateChannels | ||
34 | let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail | 34 | let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail |
35 | let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo | 35 | let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo |
36 | 36 | ||
@@ -113,10 +113,10 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
113 | 113 | ||
114 | countTotal, | 114 | countTotal, |
115 | getByUsername, | 115 | getByUsername, |
116 | list, | ||
117 | listForApi, | 116 | listForApi, |
118 | loadById, | 117 | loadById, |
119 | loadByUsername, | 118 | loadByUsername, |
119 | loadByUsernameAndPopulateChannels, | ||
120 | loadByUsernameOrEmail | 120 | loadByUsernameOrEmail |
121 | ] | 121 | ] |
122 | const instanceMethods = [ | 122 | const instanceMethods = [ |
@@ -144,15 +144,34 @@ isPasswordMatch = function (this: UserInstance, password: string) { | |||
144 | } | 144 | } |
145 | 145 | ||
146 | toFormattedJSON = function (this: UserInstance) { | 146 | toFormattedJSON = function (this: UserInstance) { |
147 | return { | 147 | const json = { |
148 | id: this.id, | 148 | id: this.id, |
149 | username: this.username, | 149 | username: this.username, |
150 | email: this.email, | 150 | email: this.email, |
151 | displayNSFW: this.displayNSFW, | 151 | displayNSFW: this.displayNSFW, |
152 | role: this.role, | 152 | role: this.role, |
153 | videoQuota: this.videoQuota, | 153 | videoQuota: this.videoQuota, |
154 | createdAt: this.createdAt | 154 | createdAt: this.createdAt, |
155 | author: { | ||
156 | id: this.Author.id, | ||
157 | uuid: this.Author.uuid | ||
158 | } | ||
155 | } | 159 | } |
160 | |||
161 | if (Array.isArray(this.Author.VideoChannels) === true) { | ||
162 | const videoChannels = this.Author.VideoChannels | ||
163 | .map(c => c.toFormattedJSON()) | ||
164 | .sort((v1, v2) => { | ||
165 | if (v1.createdAt < v2.createdAt) return -1 | ||
166 | if (v1.createdAt === v2.createdAt) return 0 | ||
167 | |||
168 | return 1 | ||
169 | }) | ||
170 | |||
171 | json['videoChannels'] = videoChannels | ||
172 | } | ||
173 | |||
174 | return json | ||
156 | } | 175 | } |
157 | 176 | ||
158 | isAdmin = function (this: UserInstance) { | 177 | isAdmin = function (this: UserInstance) { |
@@ -189,21 +208,19 @@ getByUsername = function (username: string) { | |||
189 | const query = { | 208 | const query = { |
190 | where: { | 209 | where: { |
191 | username: username | 210 | username: username |
192 | } | 211 | }, |
212 | include: [ { model: User['sequelize'].models.Author, required: true } ] | ||
193 | } | 213 | } |
194 | 214 | ||
195 | return User.findOne(query) | 215 | return User.findOne(query) |
196 | } | 216 | } |
197 | 217 | ||
198 | list = function () { | ||
199 | return User.findAll() | ||
200 | } | ||
201 | |||
202 | listForApi = function (start: number, count: number, sort: string) { | 218 | listForApi = function (start: number, count: number, sort: string) { |
203 | const query = { | 219 | const query = { |
204 | offset: start, | 220 | offset: start, |
205 | limit: count, | 221 | limit: count, |
206 | order: [ getSort(sort) ] | 222 | order: [ getSort(sort) ], |
223 | include: [ { model: User['sequelize'].models.Author, required: true } ] | ||
207 | } | 224 | } |
208 | 225 | ||
209 | return User.findAndCountAll(query).then(({ rows, count }) => { | 226 | return User.findAndCountAll(query).then(({ rows, count }) => { |
@@ -215,14 +232,36 @@ listForApi = function (start: number, count: number, sort: string) { | |||
215 | } | 232 | } |
216 | 233 | ||
217 | loadById = function (id: number) { | 234 | loadById = function (id: number) { |
218 | return User.findById(id) | 235 | const options = { |
236 | include: [ { model: User['sequelize'].models.Author, required: true } ] | ||
237 | } | ||
238 | |||
239 | return User.findById(id, options) | ||
219 | } | 240 | } |
220 | 241 | ||
221 | loadByUsername = function (username: string) { | 242 | loadByUsername = function (username: string) { |
222 | const query = { | 243 | const query = { |
223 | where: { | 244 | where: { |
224 | username | 245 | username |
225 | } | 246 | }, |
247 | include: [ { model: User['sequelize'].models.Author, required: true } ] | ||
248 | } | ||
249 | |||
250 | return User.findOne(query) | ||
251 | } | ||
252 | |||
253 | loadByUsernameAndPopulateChannels = function (username: string) { | ||
254 | const query = { | ||
255 | where: { | ||
256 | username | ||
257 | }, | ||
258 | include: [ | ||
259 | { | ||
260 | model: User['sequelize'].models.Author, | ||
261 | required: true, | ||
262 | include: [ User['sequelize'].models.VideoChannel ] | ||
263 | } | ||
264 | ] | ||
226 | } | 265 | } |
227 | 266 | ||
228 | return User.findOne(query) | 267 | return User.findOne(query) |
@@ -230,6 +269,7 @@ loadByUsername = function (username: string) { | |||
230 | 269 | ||
231 | loadByUsernameOrEmail = function (username: string, email: string) { | 270 | loadByUsernameOrEmail = function (username: string, email: string) { |
232 | const query = { | 271 | const query = { |
272 | include: [ { model: User['sequelize'].models.Author, required: true } ], | ||
233 | where: { | 273 | where: { |
234 | $or: [ { username }, { email } ] | 274 | $or: [ { username }, { email } ] |
235 | } | 275 | } |
@@ -242,11 +282,12 @@ loadByUsernameOrEmail = function (username: string, email: string) { | |||
242 | // --------------------------------------------------------------------------- | 282 | // --------------------------------------------------------------------------- |
243 | 283 | ||
244 | function getOriginalVideoFileTotalFromUser (user: UserInstance) { | 284 | function getOriginalVideoFileTotalFromUser (user: UserInstance) { |
245 | // Don't use sequelize because we need to use a subquery | 285 | // Don't use sequelize because we need to use a sub query |
246 | const query = 'SELECT SUM("size") AS "total" FROM ' + | 286 | const query = 'SELECT SUM("size") AS "total" FROM ' + |
247 | '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' + | 287 | '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' + |
248 | 'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' + | 288 | 'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' + |
249 | 'INNER JOIN "Authors" ON "Videos"."authorId" = "Authors"."id" ' + | 289 | 'INNER JOIN "VideoChannels" ON "VideoChannels"."id" = "Videos"."channelId" ' + |
290 | 'INNER JOIN "Authors" ON "VideoChannels"."authorId" = "Authors"."id" ' + | ||
250 | 'INNER JOIN "Users" ON "Authors"."userId" = "Users"."id" ' + | 291 | 'INNER JOIN "Users" ON "Authors"."userId" = "Users"."id" ' + |
251 | 'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t' | 292 | 'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t' |
252 | 293 | ||