]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/user.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / account / user.ts
index 8bd0397dd50e5a776510a83344a9d42593710b19..4a9acd703a94e2615229fe1cb8ee0fbf1d65d164 100644 (file)
@@ -1,4 +1,4 @@
-import * as Sequelize from 'sequelize'
+import { FindOptions, literal, Op, QueryTypes } from 'sequelize'
 import {
   AfterDestroy,
   AfterUpdate,
@@ -56,33 +56,33 @@ enum ScopeNames {
   WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL'
 }
 
-@DefaultScope({
+@DefaultScope(() => ({
   include: [
     {
-      model: () => AccountModel,
+      model: AccountModel,
       required: true
     },
     {
-      model: () => UserNotificationSettingModel,
+      model: UserNotificationSettingModel,
       required: true
     }
   ]
-})
-@Scopes({
+}))
+@Scopes(() => ({
   [ScopeNames.WITH_VIDEO_CHANNEL]: {
     include: [
       {
-        model: () => AccountModel,
+        model: AccountModel,
         required: true,
-        include: [ () => VideoChannelModel ]
+        include: [ VideoChannelModel ]
       },
       {
-        model: () => UserNotificationSettingModel,
+        model: UserNotificationSettingModel,
         required: true
       }
-    ] as any // FIXME: sequelize typings
+    ]
   }
-})
+}))
 @Table({
   tableName: 'user',
   indexes: [
@@ -233,26 +233,26 @@ export class UserModel extends Model<UserModel> {
     let where = undefined
     if (search) {
       where = {
-        [Sequelize.Op.or]: [
+        [Op.or]: [
           {
             email: {
-              [Sequelize.Op.iLike]: '%' + search + '%'
+              [Op.iLike]: '%' + search + '%'
             }
           },
           {
             username: {
-              [ Sequelize.Op.iLike ]: '%' + search + '%'
+              [ Op.iLike ]: '%' + search + '%'
             }
           }
         ]
       }
     }
 
-    const query = {
+    const query: FindOptions = {
       attributes: {
         include: [
           [
-            Sequelize.literal(
+            literal(
               '(' +
                 'SELECT COALESCE(SUM("size"), 0) ' +
                 'FROM (' +
@@ -265,7 +265,7 @@ export class UserModel extends Model<UserModel> {
               ')'
             ),
             'videoQuotaUsed'
-          ] as any // FIXME: typings
+          ]
         ]
       },
       offset: start,
@@ -291,7 +291,7 @@ export class UserModel extends Model<UserModel> {
     const query = {
       where: {
         role: {
-          [Sequelize.Op.in]: roles
+          [Op.in]: roles
         }
       }
     }
@@ -387,7 +387,7 @@ export class UserModel extends Model<UserModel> {
 
     const query = {
       where: {
-        [ Sequelize.Op.or ]: [ { username }, { email } ]
+        [ Op.or ]: [ { username }, { email } ]
       }
     }
 
@@ -510,7 +510,7 @@ export class UserModel extends Model<UserModel> {
     const query = {
       where: {
         username: {
-          [ Sequelize.Op.like ]: `%${search}%`
+          [ Op.like ]: `%${search}%`
         }
       },
       limit: 10
@@ -591,15 +591,11 @@ export class UserModel extends Model<UserModel> {
 
     const uploadedTotal = videoFile.size + totalBytes
     const uploadedDaily = videoFile.size + totalBytesDaily
-    if (this.videoQuotaDaily === -1) {
-      return uploadedTotal < this.videoQuota
-    }
-    if (this.videoQuota === -1) {
-      return uploadedDaily < this.videoQuotaDaily
-    }
 
-    return (uploadedTotal < this.videoQuota) &&
-        (uploadedDaily < this.videoQuotaDaily)
+    if (this.videoQuotaDaily === -1) return uploadedTotal < this.videoQuota
+    if (this.videoQuota === -1) return uploadedDaily < this.videoQuotaDaily
+
+    return uploadedTotal < this.videoQuota && uploadedDaily < this.videoQuotaDaily
   }
 
   private static generateUserQuotaBaseSQL (where?: string) {
@@ -619,14 +615,14 @@ export class UserModel extends Model<UserModel> {
   private static getTotalRawQuery (query: string, userId: number) {
     const options = {
       bind: { userId },
-      type: Sequelize.QueryTypes.SELECT as Sequelize.QueryTypes.SELECT
+      type: QueryTypes.SELECT as QueryTypes.SELECT
     }
 
-    return UserModel.sequelize.query<{ total: number }>(query, options)
+    return UserModel.sequelize.query<{ total: string }>(query, options)
                     .then(([ { total } ]) => {
                       if (total === null) return 0
 
-                      return parseInt(total + '', 10)
+                      return parseInt(total, 10)
                     })
   }
 }