X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Fuser.ts;h=39654cfcf2f51326f71dc538913aa911942fdb44;hb=7ad9b9846c44d198a736183fb186c2039f5236b5;hp=bae683b128d03b9362ab619101a1bb40b5ebcb9f;hpb=8b60488020883c66d3831eacd030893ab184268f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/user.ts b/server/models/account/user.ts index bae683b12..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,12 +25,13 @@ import { isUserAutoPlayVideoValid, isUserBlockedReasonValid, isUserBlockedValid, + 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' @@ -38,6 +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 { clearCacheByUserId } from '../../lib/oauth-model' enum ScopeNames { WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' @@ -92,6 +96,12 @@ export class UserModel extends Model { @Column(DataType.STRING(400)) email: string + @AllowNull(true) + @Default(null) + @Is('UserEmailVerified', value => throwIfNotValid(value, isUserEmailVerifiedValid, 'email verified boolean')) + @Column + emailVerified: boolean + @AllowNull(false) @Is('UserNSFWPolicy', value => throwIfNotValid(value, isUserNSFWPolicyValid, 'NSFW policy')) @Column(DataType.ENUM(values(NSFW_POLICY_TYPES))) @@ -161,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: [ @@ -188,7 +222,8 @@ export class UserModel extends Model { }, offset: start, limit: count, - order: getSort(sort) + order: getSort(sort), + where } return UserModel.findAndCountAll(query) @@ -288,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) } @@ -304,6 +353,7 @@ export class UserModel extends Model { id: this.id, username: this.username, email: this.email, + emailVerified: this.emailVerified, nsfwPolicy: this.nsfwPolicy, autoPlayVideo: this.autoPlayVideo, role: this.role,