aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/account-blocklist.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/models/account/account-blocklist.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/models/account/account-blocklist.ts')
-rw-r--r--server/models/account/account-blocklist.ts236
1 files changed, 0 insertions, 236 deletions
diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts
deleted file mode 100644
index f6212ff6e..000000000
--- a/server/models/account/account-blocklist.ts
+++ /dev/null
@@ -1,236 +0,0 @@
1import { FindOptions, Op, QueryTypes } from 'sequelize'
2import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { handlesToNameAndHost } from '@server/helpers/actors'
4import { MAccountBlocklist, MAccountBlocklistFormattable } from '@server/types/models'
5import { AttributesOnly } from '@shared/typescript-utils'
6import { AccountBlock } from '../../../shared/models'
7import { ActorModel } from '../actor/actor'
8import { ServerModel } from '../server/server'
9import { createSafeIn, getSort, searchAttribute } from '../shared'
10import { AccountModel } from './account'
11
12@Table({
13 tableName: 'accountBlocklist',
14 indexes: [
15 {
16 fields: [ 'accountId', 'targetAccountId' ],
17 unique: true
18 },
19 {
20 fields: [ 'targetAccountId' ]
21 }
22 ]
23})
24export class AccountBlocklistModel extends Model<Partial<AttributesOnly<AccountBlocklistModel>>> {
25
26 @CreatedAt
27 createdAt: Date
28
29 @UpdatedAt
30 updatedAt: Date
31
32 @ForeignKey(() => AccountModel)
33 @Column
34 accountId: number
35
36 @BelongsTo(() => AccountModel, {
37 foreignKey: {
38 name: 'accountId',
39 allowNull: false
40 },
41 as: 'ByAccount',
42 onDelete: 'CASCADE'
43 })
44 ByAccount: AccountModel
45
46 @ForeignKey(() => AccountModel)
47 @Column
48 targetAccountId: number
49
50 @BelongsTo(() => AccountModel, {
51 foreignKey: {
52 name: 'targetAccountId',
53 allowNull: false
54 },
55 as: 'BlockedAccount',
56 onDelete: 'CASCADE'
57 })
58 BlockedAccount: AccountModel
59
60 static isAccountMutedByAccounts (accountIds: number[], targetAccountId: number) {
61 const query = {
62 attributes: [ 'accountId', 'id' ],
63 where: {
64 accountId: {
65 [Op.in]: accountIds
66 },
67 targetAccountId
68 },
69 raw: true
70 }
71
72 return AccountBlocklistModel.unscoped()
73 .findAll(query)
74 .then(rows => {
75 const result: { [accountId: number]: boolean } = {}
76
77 for (const accountId of accountIds) {
78 result[accountId] = !!rows.find(r => r.accountId === accountId)
79 }
80
81 return result
82 })
83 }
84
85 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> {
86 const query = {
87 where: {
88 accountId,
89 targetAccountId
90 }
91 }
92
93 return AccountBlocklistModel.findOne(query)
94 }
95
96 static listForApi (parameters: {
97 start: number
98 count: number
99 sort: string
100 search?: string
101 accountId: number
102 }) {
103 const { start, count, sort, search, accountId } = parameters
104
105 const getQuery = (forCount: boolean) => {
106 const query: FindOptions = {
107 offset: start,
108 limit: count,
109 order: getSort(sort),
110 where: { accountId }
111 }
112
113 if (search) {
114 Object.assign(query.where, {
115 [Op.or]: [
116 searchAttribute(search, '$BlockedAccount.name$'),
117 searchAttribute(search, '$BlockedAccount.Actor.url$')
118 ]
119 })
120 }
121
122 if (forCount !== true) {
123 query.include = [
124 {
125 model: AccountModel,
126 required: true,
127 as: 'ByAccount'
128 },
129 {
130 model: AccountModel,
131 required: true,
132 as: 'BlockedAccount'
133 }
134 ]
135 } else if (search) { // We need some joins when counting with search
136 query.include = [
137 {
138 model: AccountModel.unscoped(),
139 required: true,
140 as: 'BlockedAccount',
141 include: [
142 {
143 model: ActorModel.unscoped(),
144 required: true
145 }
146 ]
147 }
148 ]
149 }
150
151 return query
152 }
153
154 return Promise.all([
155 AccountBlocklistModel.count(getQuery(true)),
156 AccountBlocklistModel.findAll(getQuery(false))
157 ]).then(([ total, data ]) => ({ total, data }))
158 }
159
160 static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> {
161 const query = {
162 attributes: [ 'id' ],
163 where: {
164 accountId: {
165 [Op.in]: accountIds
166 }
167 },
168 include: [
169 {
170 attributes: [ 'id' ],
171 model: AccountModel.unscoped(),
172 required: true,
173 as: 'BlockedAccount',
174 include: [
175 {
176 attributes: [ 'preferredUsername' ],
177 model: ActorModel.unscoped(),
178 required: true,
179 include: [
180 {
181 attributes: [ 'host' ],
182 model: ServerModel.unscoped(),
183 required: true
184 }
185 ]
186 }
187 ]
188 }
189 ]
190 }
191
192 return AccountBlocklistModel.findAll(query)
193 .then(entries => entries.map(e => `${e.BlockedAccount.Actor.preferredUsername}@${e.BlockedAccount.Actor.Server.host}`))
194 }
195
196 static getBlockStatus (byAccountIds: number[], handles: string[]): Promise<{ name: string, host: string, accountId: number }[]> {
197 const sanitizedHandles = handlesToNameAndHost(handles)
198
199 const localHandles = sanitizedHandles.filter(h => !h.host)
200 .map(h => h.name)
201
202 const remoteHandles = sanitizedHandles.filter(h => !!h.host)
203 .map(h => ([ h.name, h.host ]))
204
205 const handlesWhere: string[] = []
206
207 if (localHandles.length !== 0) {
208 handlesWhere.push(`("actor"."preferredUsername" IN (:localHandles) AND "server"."id" IS NULL)`)
209 }
210
211 if (remoteHandles.length !== 0) {
212 handlesWhere.push(`(("actor"."preferredUsername", "server"."host") IN (:remoteHandles))`)
213 }
214
215 const rawQuery = `SELECT "accountBlocklist"."accountId", "actor"."preferredUsername" AS "name", "server"."host" ` +
216 `FROM "accountBlocklist" ` +
217 `INNER JOIN "account" ON "account"."id" = "accountBlocklist"."targetAccountId" ` +
218 `INNER JOIN "actor" ON "actor"."id" = "account"."actorId" ` +
219 `LEFT JOIN "server" ON "server"."id" = "actor"."serverId" ` +
220 `WHERE "accountBlocklist"."accountId" IN (${createSafeIn(AccountBlocklistModel.sequelize, byAccountIds)}) ` +
221 `AND (${handlesWhere.join(' OR ')})`
222
223 return AccountBlocklistModel.sequelize.query(rawQuery, {
224 type: QueryTypes.SELECT as QueryTypes.SELECT,
225 replacements: { byAccountIds, localHandles, remoteHandles }
226 })
227 }
228
229 toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
230 return {
231 byAccount: this.ByAccount.toFormattedJSON(),
232 blockedAccount: this.BlockedAccount.toFormattedJSON(),
233 createdAt: this.createdAt
234 }
235 }
236}