X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Fuser.ts;h=777f0966680094e86e2b24613d6e5e6b523625fb;hb=3f87a46f1dd01485e4a28422f74c3a1b0f56729f;hp=27262af422999ba1335e551133af8f59f8500b61;hpb=ac0868bcc0259d4ff14265d9ae403e10869a13aa;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 27262af42..777f09666 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -1,4 +1,4 @@ -import { FindOptions, literal, Op, QueryTypes } from 'sequelize' +import { FindOptions, literal, Op, QueryTypes, where, fn, col, WhereOptions } from 'sequelize' import { AfterDestroy, AfterUpdate, @@ -49,7 +49,7 @@ import { VideoPlaylistModel } from '../video/video-playlist' import { AccountModel } from './account' import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' import { values } from 'lodash' -import { DEFAULT_THEME_NAME, DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants' +import { DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants' import { clearCacheByUserId } from '../../lib/oauth-model' import { UserNotificationSettingModel } from './user-notification-setting' import { VideoModel } from '../video/video' @@ -101,7 +101,7 @@ enum ScopeNames { required: true, where: { type: { - [ Op.ne ]: VideoPlaylistType.REGULAR + [Op.ne]: VideoPlaylistType.REGULAR } } } @@ -186,7 +186,10 @@ export class UserModel extends Model { @AllowNull(false) @Default(true) - @Is('UserAutoPlayNextVideoPlaylist', value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean')) + @Is( + 'UserAutoPlayNextVideoPlaylist', + value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean') + ) @Column autoPlayNextVideoPlaylist: boolean @@ -230,7 +233,7 @@ export class UserModel extends Model { videoQuotaDaily: number @AllowNull(false) - @Default(DEFAULT_THEME_NAME) + @Default(DEFAULT_USER_THEME_NAME) @Is('UserTheme', value => throwIfNotValid(value, isThemeNameValid, 'theme')) @Column theme: string @@ -308,7 +311,8 @@ export class UserModel extends Model { } static listForApi (start: number, count: number, sort: string, search?: string) { - let where = undefined + let where: WhereOptions + if (search) { where = { [Op.or]: [ @@ -319,7 +323,7 @@ export class UserModel extends Model { }, { username: { - [ Op.iLike ]: '%' + search + '%' + [Op.iLike]: '%' + search + '%' } } ] @@ -332,14 +336,14 @@ export class UserModel extends Model { [ literal( '(' + - 'SELECT COALESCE(SUM("size"), 0) ' + - 'FROM (' + - 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + - 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + - 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + - 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + - 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + - ') t' + + 'SELECT COALESCE(SUM("size"), 0) ' + + 'FROM (' + + 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + + 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + + 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + + ') t' + ')' ), 'videoQuotaUsed' @@ -353,18 +357,18 @@ export class UserModel extends Model { } return UserModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) + .then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) } static listWithRight (right: UserRight): Bluebird { const roles = Object.keys(USER_ROLE_LABELS) - .map(k => parseInt(k, 10) as UserRole) - .filter(role => hasUserRight(role, right)) + .map(k => parseInt(k, 10) as UserRole) + .filter(role => hasUserRight(role, right)) const query = { where: { @@ -390,7 +394,7 @@ export class UserModel extends Model { required: true, include: [ { - attributes: [ ], + attributes: [], model: ActorModel.unscoped(), required: true, where: { @@ -398,7 +402,7 @@ export class UserModel extends Model { }, include: [ { - attributes: [ ], + attributes: [], as: 'ActorFollowings', model: ActorFollowModel.unscoped(), required: true, @@ -433,7 +437,7 @@ export class UserModel extends Model { static loadByUsername (username: string): Bluebird { const query = { where: { - username: { [ Op.iLike ]: username } + username: { [Op.iLike]: username } } } @@ -443,7 +447,7 @@ export class UserModel extends Model { static loadForMeAPI (username: string): Bluebird { const query = { where: { - username: { [ Op.iLike ]: username } + username: { [Op.iLike]: username } } } @@ -465,7 +469,11 @@ export class UserModel extends Model { const query = { where: { - [ Op.or ]: [ { username: { [ Op.iLike ]: username } }, { email } ] + [Op.or]: [ + where(fn('lower', col('username')), fn('lower', username)), + + { email } + ] } } @@ -588,7 +596,7 @@ export class UserModel extends Model { const query = { where: { username: { - [ Op.like ]: `%${search}%` + [Op.like]: `%${search}%` } }, limit: 10 @@ -648,7 +656,7 @@ export class UserModel extends Model { videoLanguages: this.videoLanguages, role: this.role, - roleLabel: USER_ROLE_LABELS[ this.role ], + roleLabel: USER_ROLE_LABELS[this.role], videoQuota: this.videoQuota, videoQuotaDaily: this.videoQuotaDaily, @@ -682,13 +690,13 @@ export class UserModel extends Model { if (Array.isArray(this.Account.VideoChannels) === true) { json.videoChannels = this.Account.VideoChannels - .map(c => c.toFormattedJSON()) - .sort((v1, v2) => { - if (v1.createdAt < v2.createdAt) return -1 - if (v1.createdAt === v2.createdAt) return 0 + .map(c => c.toFormattedJSON()) + .sort((v1, v2) => { + if (v1.createdAt < v2.createdAt) return -1 + if (v1.createdAt === v2.createdAt) return 0 - return 1 - }) + return 1 + }) } return json @@ -698,7 +706,7 @@ export class UserModel extends Model { const formatted = this.toFormattedJSON() const specialPlaylists = this.Account.VideoPlaylists - .map(p => ({ id: p.id, name: p.name, type: p.type })) + .map(p => ({ id: p.id, name: p.name, type: p.type })) return Object.assign(formatted, { specialPlaylists }) } @@ -725,12 +733,12 @@ export class UserModel extends Model { return 'SELECT SUM("size") AS "total" ' + 'FROM (' + - 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + - 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + - 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + - 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + - 'WHERE "account"."userId" = $userId ' + andWhere + - 'GROUP BY "video"."id"' + + 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + + 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + + 'WHERE "account"."userId" = $userId ' + andWhere + + 'GROUP BY "video"."id"' + ') t' }