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