X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Fuser.ts;h=39654cfcf2f51326f71dc538913aa911942fdb44;hb=7ad9b9846c44d198a736183fb186c2039f5236b5;hp=4b13e47a0dc306d76a62acc3a61da8979e2cb2ee;hpb=74d63469355bad731cdd32defdc85913df3cbd5c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 4b13e47a0..39654cfcf 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -1,5 +1,7 @@ import * as Sequelize from 'sequelize' import { + AfterDelete, + AfterUpdate, AllowNull, BeforeCreate, BeforeUpdate, @@ -23,13 +25,13 @@ import { isUserAutoPlayVideoValid, isUserBlockedReasonValid, isUserBlockedValid, - isUserNSFWPolicyValid, isUserEmailVerifiedValid, + isUserNSFWPolicyValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, - isUserVideoQuotaValid, - isUserVideoQuotaDailyValid + isUserVideoQuotaDailyValid, + isUserVideoQuotaValid } from '../../helpers/custom-validators/users' import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' import { OAuthTokenModel } from '../oauth/oauth-token' @@ -39,7 +41,7 @@ import { AccountModel } from './account' import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' import { values } from 'lodash' import { NSFW_POLICY_TYPES } from '../../initializers' -import { VideoFileModel } from '../video/video-file' +import { clearCacheByUserId } from '../../lib/oauth-model' enum ScopeNames { WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' @@ -169,11 +171,35 @@ export class UserModel extends Model { } } + @AfterUpdate + @AfterDelete + static removeTokenCache (instance: UserModel) { + return clearCacheByUserId(instance.id) + } + static countTotal () { return this.count() } - static listForApi (start: number, count: number, sort: string) { + static listForApi (start: number, count: number, sort: string, search?: string) { + let where = undefined + if (search) { + where = { + [Sequelize.Op.or]: [ + { + email: { + [Sequelize.Op.iLike]: '%' + search + '%' + } + }, + { + username: { + [ Sequelize.Op.iLike ]: '%' + search + '%' + } + } + ] + } + } + const query = { attributes: { include: [ @@ -196,7 +222,8 @@ export class UserModel extends Model { }, offset: start, limit: count, - order: getSort(sort) + order: getSort(sort), + where } return UserModel.findAndCountAll(query) @@ -296,6 +323,20 @@ export class UserModel extends Model { } } + static autoComplete (search: string) { + const query = { + where: { + username: { + [ Sequelize.Op.like ]: `%${search}%` + } + }, + limit: 10 + } + + return UserModel.findAll(query) + .then(u => u.map(u => u.username)) + } + hasRight (right: UserRight) { return hasUserRight(this.role, right) } @@ -394,15 +435,4 @@ export class UserModel extends Model { return parseInt(total, 10) }) } - - static autocomplete (search: string) { - return UserModel.findAll({ - where: { - username: { - [Sequelize.Op.like]: `%${search}%` - } - } - }) - .then(u => u.map(u => u.username)) - } }