]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/user.ts
add webtorrent opt-out settings
[github/Chocobozzz/PeerTube.git] / server / models / account / user.ts
index 4b13e47a0dc306d76a62acc3a61da8979e2cb2ee..5fe7d7e7d94aa00864ed6d3a954d02cc623fd8e0 100644 (file)
@@ -1,5 +1,7 @@
 import * as Sequelize from 'sequelize'
 import {
+  AfterDelete,
+  AfterUpdate,
   AllowNull,
   BeforeCreate,
   BeforeUpdate,
@@ -23,13 +25,14 @@ import {
   isUserAutoPlayVideoValid,
   isUserBlockedReasonValid,
   isUserBlockedValid,
-  isUserNSFWPolicyValid,
   isUserEmailVerifiedValid,
+  isUserNSFWPolicyValid,
   isUserPasswordValid,
   isUserRoleValid,
   isUserUsernameValid,
+  isUserVideoQuotaDailyValid,
   isUserVideoQuotaValid,
-  isUserVideoQuotaDailyValid
+  isUserWebTorrentPolicyValid
 } from '../../helpers/custom-validators/users'
 import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto'
 import { OAuthTokenModel } from '../oauth/oauth-token'
@@ -37,9 +40,10 @@ import { getSort, throwIfNotValid } from '../utils'
 import { VideoChannelModel } from '../video/video-channel'
 import { AccountModel } from './account'
 import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type'
+import { WebTorrentPolicyType } from '../../../shared/models/users/user-webtorrent-policy.type'
 import { values } from 'lodash'
-import { NSFW_POLICY_TYPES } from '../../initializers'
-import { VideoFileModel } from '../video/video-file'
+import { NSFW_POLICY_TYPES, WEBTORRENT_POLICY_TYPES } from '../../initializers'
+import { clearCacheByUserId } from '../../lib/oauth-model'
 
 enum ScopeNames {
   WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL'
@@ -105,6 +109,11 @@ export class UserModel extends Model<UserModel> {
   @Column(DataType.ENUM(values(NSFW_POLICY_TYPES)))
   nsfwPolicy: NSFWPolicyType
 
+  @AllowNull(false)
+  @Is('UserWebTorrentPolicy', value => throwIfNotValid(value, isUserWebTorrentPolicyValid, 'WebTorrent policy'))
+  @Column(DataType.ENUM(values(WEBTORRENT_POLICY_TYPES)))
+  webTorrentPolicy: WebTorrentPolicyType
+
   @AllowNull(false)
   @Default(true)
   @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean'))
@@ -169,11 +178,35 @@ export class UserModel extends Model<UserModel> {
     }
   }
 
+  @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 +229,8 @@ export class UserModel extends Model<UserModel> {
       },
       offset: start,
       limit: count,
-      order: getSort(sort)
+      order: getSort(sort),
+      where
     }
 
     return UserModel.findAndCountAll(query)
@@ -296,6 +330,20 @@ export class UserModel extends Model<UserModel> {
     }
   }
 
+  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)
   }
@@ -314,6 +362,7 @@ export class UserModel extends Model<UserModel> {
       email: this.email,
       emailVerified: this.emailVerified,
       nsfwPolicy: this.nsfwPolicy,
+      webTorrentPolicy: this.webTorrentPolicy,
       autoPlayVideo: this.autoPlayVideo,
       role: this.role,
       roleLabel: USER_ROLE_LABELS[ this.role ],
@@ -394,15 +443,4 @@ export class UserModel extends Model<UserModel> {
                       return parseInt(total, 10)
                     })
   }
-
-  static autocomplete (search: string) {
-    return UserModel.findAll({
-      where: {
-        username: {
-          [Sequelize.Op.like]: `%${search}%`
-        }
-      }
-    })
-      .then(u => u.map(u => u.username))
-  }
 }