import * as Sequelize from 'sequelize'
import {
+ AfterDestroy,
+ AfterUpdate,
AllowNull,
BeforeCreate,
BeforeUpdate,
isUserAutoPlayVideoValid,
isUserBlockedReasonValid,
isUserBlockedValid,
- isUserNSFWPolicyValid,
isUserEmailVerifiedValid,
+ isUserNSFWPolicyValid,
isUserPasswordValid,
isUserRoleValid,
isUserUsernameValid,
+ isUserVideoQuotaDailyValid,
isUserVideoQuotaValid,
- isUserVideoQuotaDailyValid
+ isUserWebTorrentEnabledValid,
+ isUserVideosHistoryEnabledValid
} from '../../helpers/custom-validators/users'
import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto'
import { OAuthTokenModel } from '../oauth/oauth-token'
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'
@Column(DataType.ENUM(values(NSFW_POLICY_TYPES)))
nsfwPolicy: NSFWPolicyType
+ @AllowNull(false)
+ @Default(true)
+ @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled'))
+ @Column
+ webTorrentEnabled: boolean
+
+ @AllowNull(false)
+ @Default(true)
+ @Is('UserVideosHistoryEnabled', value => throwIfNotValid(value, isUserVideosHistoryEnabledValid, 'Videos history enabled'))
+ @Column
+ videosHistoryEnabled: boolean
+
@AllowNull(false)
@Default(true)
@Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean'))
}
}
+ @AfterUpdate
+ @AfterDestroy
+ 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: [
},
offset: start,
limit: count,
- order: getSort(sort)
+ order: getSort(sort),
+ where
}
return UserModel.findAndCountAll(query)
}
}
+ 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)
}
email: this.email,
emailVerified: this.emailVerified,
nsfwPolicy: this.nsfwPolicy,
+ webTorrentEnabled: this.webTorrentEnabled,
autoPlayVideo: this.autoPlayVideo,
role: this.role,
roleLabel: USER_ROLE_LABELS[ this.role ],
return parseInt(total, 10)
})
}
-
- static autocomplete (search: string) {
- return UserModel.findAll({
- where: {
- username: {
- [Sequelize.Op.like]: `%${search}%`
- }
- }
- })
- .then(u => u.map(u => u.username))
- }
}