]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/server-blocklist.ts
Save
[github/Chocobozzz/PeerTube.git] / server / models / server / server-blocklist.ts
index 450f27152069449057dc9be0c795876699f53b24..68cd72ee791c1dd3e6b575c3994569779d12589d 100644 (file)
@@ -1,19 +1,22 @@
+import * as Bluebird from 'bluebird'
+import { Op } from 'sequelize'
 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
+import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/types/models'
+import { ServerBlock } from '@shared/models'
 import { AccountModel } from '../account/account'
+import { getSort, searchAttribute } from '../utils'
 import { ServerModel } from './server'
-import { ServerBlock } from '../../../shared/models/blocklist'
-import { getSort } from '../utils'
 
 enum ScopeNames {
   WITH_ACCOUNT = 'WITH_ACCOUNT',
   WITH_SERVER = 'WITH_SERVER'
 }
 
-@Scopes({
+@Scopes(() => ({
   [ScopeNames.WITH_ACCOUNT]: {
     include: [
       {
-        model: () => AccountModel,
+        model: AccountModel,
         required: true
       }
     ]
@@ -21,12 +24,12 @@ enum ScopeNames {
   [ScopeNames.WITH_SERVER]: {
     include: [
       {
-        model: () => ServerModel,
+        model: ServerModel,
         required: true
       }
     ]
   }
-})
+}))
 
 @Table({
   tableName: 'serverBlocklist',
@@ -67,14 +70,38 @@ export class ServerBlocklistModel extends Model<ServerBlocklistModel> {
 
   @BelongsTo(() => ServerModel, {
     foreignKey: {
-      name: 'targetServerId',
       allowNull: false
     },
     onDelete: 'CASCADE'
   })
   BlockedServer: ServerModel
 
-  static loadByAccountAndHost (accountId: number, host: string) {
+  static isServerMutedByMulti (accountIds: number[], targetServerId: number) {
+    const query = {
+      attributes: [ 'accountId', 'id' ],
+      where: {
+        accountId: {
+          [Op.in]: accountIds
+        },
+        targetServerId
+      },
+      raw: true
+    }
+
+    return ServerBlocklistModel.unscoped()
+                                .findAll(query)
+                                .then(rows => {
+                                  const result: { [accountId: number]: boolean } = {}
+
+                                  for (const accountId of accountIds) {
+                                    result[accountId] = !!rows.find(r => r.accountId === accountId)
+                                  }
+
+                                  return result
+                                })
+  }
+
+  static loadByAccountAndHost (accountId: number, host: string): Bluebird<MServerBlocklist> {
     const query = {
       where: {
         accountId
@@ -93,25 +120,55 @@ export class ServerBlocklistModel extends Model<ServerBlocklistModel> {
     return ServerBlocklistModel.findOne(query)
   }
 
-  static listForApi (accountId: number, start: number, count: number, sort: string) {
+  static listHostsBlockedBy (accountIds: number[]): Bluebird<string[]> {
+    const query = {
+      attributes: [ ],
+      where: {
+        accountId: {
+          [Op.in]: accountIds
+        }
+      },
+      include: [
+        {
+          attributes: [ 'host' ],
+          model: ServerModel.unscoped(),
+          required: true
+        }
+      ]
+    }
+
+    return ServerBlocklistModel.findAll(query)
+      .then(entries => entries.map(e => e.BlockedServer.host))
+  }
+
+  static listForApi (parameters: {
+    start: number
+    count: number
+    sort: string
+    search?: string
+    accountId: number
+  }) {
+    const { start, count, sort, search, accountId } = parameters
+
     const query = {
       offset: start,
       limit: count,
       order: getSort(sort),
       where: {
-        accountId
+        accountId,
+        ...searchAttribute(search, '$BlockedServer.host$')
       }
     }
 
     return ServerBlocklistModel
       .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
-      .findAndCountAll(query)
+      .findAndCountAll<MServerBlocklistAccountServer>(query)
       .then(({ rows, count }) => {
         return { total: count, data: rows }
       })
   }
 
-  toFormattedJSON (): ServerBlock {
+  toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
     return {
       byAccount: this.ByAccount.toFormattedJSON(),
       blockedServer: this.BlockedServer.toFormattedJSON(),