]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/server/server-blocklist.ts
Support transcoding options/encoders by plugins
[github/Chocobozzz/PeerTube.git] / server / models / server / server-blocklist.ts
CommitLineData
d95d1559 1import { Op } from 'sequelize'
7ad9b984 2import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
d95d1559
C
3import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/types/models'
4import { ServerBlock } from '@shared/models'
7ad9b984 5import { AccountModel } from '../account/account'
e0a92917 6import { getSort, searchAttribute } from '../utils'
d95d1559 7import { ServerModel } from './server'
7ad9b984
C
8
9enum ScopeNames {
10 WITH_ACCOUNT = 'WITH_ACCOUNT',
11 WITH_SERVER = 'WITH_SERVER'
12}
13
3acc5084 14@Scopes(() => ({
7ad9b984
C
15 [ScopeNames.WITH_ACCOUNT]: {
16 include: [
17 {
3acc5084 18 model: AccountModel,
7ad9b984
C
19 required: true
20 }
21 ]
22 },
23 [ScopeNames.WITH_SERVER]: {
24 include: [
25 {
3acc5084 26 model: ServerModel,
7ad9b984
C
27 required: true
28 }
29 ]
30 }
3acc5084 31}))
7ad9b984
C
32
33@Table({
34 tableName: 'serverBlocklist',
35 indexes: [
36 {
37 fields: [ 'accountId', 'targetServerId' ],
38 unique: true
39 },
40 {
41 fields: [ 'targetServerId' ]
42 }
43 ]
44})
b49f22d8 45export class ServerBlocklistModel extends Model {
7ad9b984
C
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: {
7ad9b984
C
72 allowNull: false
73 },
74 onDelete: 'CASCADE'
75 })
af5767ff 76 BlockedServer: ServerModel
7ad9b984 77
dddc8b1f
C
78 static isServerMutedByMulti (accountIds: number[], targetServerId: number) {
79 const query = {
80 attributes: [ 'accountId', 'id' ],
81 where: {
82 accountId: {
0374b6b5 83 [Op.in]: accountIds
dddc8b1f
C
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
b49f22d8 103 static loadByAccountAndHost (accountId: number, host: string): Promise<MServerBlocklist> {
7ad9b984
C
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
b49f22d8 122 static listHostsBlockedBy (accountIds: number[]): Promise<string[]> {
5fb2e288
C
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
e0a92917
RK
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
7ad9b984
C
152 const query = {
153 offset: start,
154 limit: count,
155 order: getSort(sort),
156 where: {
e0a92917
RK
157 accountId,
158 ...searchAttribute(search, '$BlockedServer.host$')
7ad9b984
C
159 }
160 }
161
162 return ServerBlocklistModel
163 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
453e83ea 164 .findAndCountAll<MServerBlocklistAccountServer>(query)
7ad9b984
C
165 .then(({ rows, count }) => {
166 return { total: count, data: rows }
167 })
168 }
169
1ca9f7c3 170 toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
7ad9b984
C
171 return {
172 byAccount: this.ByAccount.toFormattedJSON(),
af5767ff 173 blockedServer: this.BlockedServer.toFormattedJSON(),
7ad9b984
C
174 createdAt: this.createdAt
175 }
176 }
177}