]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/server/server-blocklist.ts
Support transcoding options/encoders by plugins
[github/Chocobozzz/PeerTube.git] / server / models / server / server-blocklist.ts
1 import { Op } from 'sequelize'
2 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/types/models'
4 import { ServerBlock } from '@shared/models'
5 import { AccountModel } from '../account/account'
6 import { getSort, searchAttribute } from '../utils'
7 import { ServerModel } from './server'
8
9 enum ScopeNames {
10 WITH_ACCOUNT = 'WITH_ACCOUNT',
11 WITH_SERVER = 'WITH_SERVER'
12 }
13
14 @Scopes(() => ({
15 [ScopeNames.WITH_ACCOUNT]: {
16 include: [
17 {
18 model: AccountModel,
19 required: true
20 }
21 ]
22 },
23 [ScopeNames.WITH_SERVER]: {
24 include: [
25 {
26 model: ServerModel,
27 required: true
28 }
29 ]
30 }
31 }))
32
33 @Table({
34 tableName: 'serverBlocklist',
35 indexes: [
36 {
37 fields: [ 'accountId', 'targetServerId' ],
38 unique: true
39 },
40 {
41 fields: [ 'targetServerId' ]
42 }
43 ]
44 })
45 export class ServerBlocklistModel extends Model {
46
47 @CreatedAt
48 createdAt: Date
49
50 @UpdatedAt
51 updatedAt: Date
52
53 @ForeignKey(() => AccountModel)
54 @Column
55 accountId: number
56
57 @BelongsTo(() => AccountModel, {
58 foreignKey: {
59 name: 'accountId',
60 allowNull: false
61 },
62 onDelete: 'CASCADE'
63 })
64 ByAccount: AccountModel
65
66 @ForeignKey(() => ServerModel)
67 @Column
68 targetServerId: number
69
70 @BelongsTo(() => ServerModel, {
71 foreignKey: {
72 allowNull: false
73 },
74 onDelete: 'CASCADE'
75 })
76 BlockedServer: ServerModel
77
78 static isServerMutedByMulti (accountIds: number[], targetServerId: number) {
79 const query = {
80 attributes: [ 'accountId', 'id' ],
81 where: {
82 accountId: {
83 [Op.in]: accountIds
84 },
85 targetServerId
86 },
87 raw: true
88 }
89
90 return ServerBlocklistModel.unscoped()
91 .findAll(query)
92 .then(rows => {
93 const result: { [accountId: number]: boolean } = {}
94
95 for (const accountId of accountIds) {
96 result[accountId] = !!rows.find(r => r.accountId === accountId)
97 }
98
99 return result
100 })
101 }
102
103 static loadByAccountAndHost (accountId: number, host: string): Promise<MServerBlocklist> {
104 const query = {
105 where: {
106 accountId
107 },
108 include: [
109 {
110 model: ServerModel,
111 where: {
112 host
113 },
114 required: true
115 }
116 ]
117 }
118
119 return ServerBlocklistModel.findOne(query)
120 }
121
122 static listHostsBlockedBy (accountIds: number[]): Promise<string[]> {
123 const query = {
124 attributes: [ ],
125 where: {
126 accountId: {
127 [Op.in]: accountIds
128 }
129 },
130 include: [
131 {
132 attributes: [ 'host' ],
133 model: ServerModel.unscoped(),
134 required: true
135 }
136 ]
137 }
138
139 return ServerBlocklistModel.findAll(query)
140 .then(entries => entries.map(e => e.BlockedServer.host))
141 }
142
143 static listForApi (parameters: {
144 start: number
145 count: number
146 sort: string
147 search?: string
148 accountId: number
149 }) {
150 const { start, count, sort, search, accountId } = parameters
151
152 const query = {
153 offset: start,
154 limit: count,
155 order: getSort(sort),
156 where: {
157 accountId,
158 ...searchAttribute(search, '$BlockedServer.host$')
159 }
160 }
161
162 return ServerBlocklistModel
163 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
164 .findAndCountAll<MServerBlocklistAccountServer>(query)
165 .then(({ rows, count }) => {
166 return { total: count, data: rows }
167 })
168 }
169
170 toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
171 return {
172 byAccount: this.ByAccount.toFormattedJSON(),
173 blockedServer: this.BlockedServer.toFormattedJSON(),
174 createdAt: this.createdAt
175 }
176 }
177 }