]>
Commit | Line | Data |
---|---|---|
80badf49 | 1 | import { Op, QueryTypes } from 'sequelize' |
d95d1559 | 2 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
80badf49 | 3 | import { handlesToNameAndHost } from '@server/helpers/actors' |
26d6bf65 | 4 | import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/types/models' |
6b5f72be | 5 | import { AttributesOnly } from '@shared/typescript-utils' |
d95d1559 | 6 | import { AccountBlock } from '../../../shared/models' |
7d9ba5c0 | 7 | import { ActorModel } from '../actor/actor' |
5fb2e288 | 8 | import { ServerModel } from '../server/server' |
80badf49 | 9 | import { createSafeIn, getSort, searchAttribute } from '../utils' |
d95d1559 | 10 | import { AccountModel } from './account' |
7ad9b984 C |
11 | |
12 | enum ScopeNames { | |
13 | WITH_ACCOUNTS = 'WITH_ACCOUNTS' | |
14 | } | |
15 | ||
3acc5084 | 16 | @Scopes(() => ({ |
7ad9b984 C |
17 | [ScopeNames.WITH_ACCOUNTS]: { |
18 | include: [ | |
19 | { | |
3acc5084 | 20 | model: AccountModel, |
7ad9b984 C |
21 | required: true, |
22 | as: 'ByAccount' | |
23 | }, | |
24 | { | |
3acc5084 | 25 | model: AccountModel, |
7ad9b984 | 26 | required: true, |
af5767ff | 27 | as: 'BlockedAccount' |
7ad9b984 C |
28 | } |
29 | ] | |
30 | } | |
3acc5084 | 31 | })) |
7ad9b984 C |
32 | |
33 | @Table({ | |
34 | tableName: 'accountBlocklist', | |
35 | indexes: [ | |
36 | { | |
37 | fields: [ 'accountId', 'targetAccountId' ], | |
38 | unique: true | |
39 | }, | |
40 | { | |
41 | fields: [ 'targetAccountId' ] | |
42 | } | |
43 | ] | |
44 | }) | |
16c016e8 | 45 | export class AccountBlocklistModel extends Model<Partial<AttributesOnly<AccountBlocklistModel>>> { |
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 | as: 'ByAccount', | |
63 | onDelete: 'CASCADE' | |
64 | }) | |
65 | ByAccount: AccountModel | |
66 | ||
67 | @ForeignKey(() => AccountModel) | |
68 | @Column | |
69 | targetAccountId: number | |
70 | ||
71 | @BelongsTo(() => AccountModel, { | |
72 | foreignKey: { | |
73 | name: 'targetAccountId', | |
74 | allowNull: false | |
75 | }, | |
af5767ff | 76 | as: 'BlockedAccount', |
7ad9b984 C |
77 | onDelete: 'CASCADE' |
78 | }) | |
af5767ff | 79 | BlockedAccount: AccountModel |
7ad9b984 | 80 | |
80badf49 | 81 | static isAccountMutedByAccounts (accountIds: number[], targetAccountId: number) { |
dc133480 | 82 | const query = { |
f7cc67b4 | 83 | attributes: [ 'accountId', 'id' ], |
dc133480 | 84 | where: { |
f7cc67b4 | 85 | accountId: { |
0374b6b5 | 86 | [Op.in]: accountIds |
f7cc67b4 | 87 | }, |
dc133480 C |
88 | targetAccountId |
89 | }, | |
90 | raw: true | |
91 | } | |
92 | ||
93 | return AccountBlocklistModel.unscoped() | |
f7cc67b4 C |
94 | .findAll(query) |
95 | .then(rows => { | |
96 | const result: { [accountId: number]: boolean } = {} | |
97 | ||
98 | for (const accountId of accountIds) { | |
99 | result[accountId] = !!rows.find(r => r.accountId === accountId) | |
100 | } | |
101 | ||
102 | return result | |
103 | }) | |
dc133480 C |
104 | } |
105 | ||
b49f22d8 | 106 | static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> { |
7ad9b984 C |
107 | const query = { |
108 | where: { | |
109 | accountId, | |
110 | targetAccountId | |
111 | } | |
112 | } | |
113 | ||
114 | return AccountBlocklistModel.findOne(query) | |
115 | } | |
116 | ||
e0a92917 RK |
117 | static listForApi (parameters: { |
118 | start: number | |
119 | count: number | |
120 | sort: string | |
121 | search?: string | |
122 | accountId: number | |
123 | }) { | |
124 | const { start, count, sort, search, accountId } = parameters | |
125 | ||
7ad9b984 C |
126 | const query = { |
127 | offset: start, | |
128 | limit: count, | |
e0a92917 RK |
129 | order: getSort(sort) |
130 | } | |
131 | ||
132 | const where = { | |
133 | accountId | |
7ad9b984 C |
134 | } |
135 | ||
e0a92917 RK |
136 | if (search) { |
137 | Object.assign(where, { | |
138 | [Op.or]: [ | |
0251197e RK |
139 | searchAttribute(search, '$BlockedAccount.name$'), |
140 | searchAttribute(search, '$BlockedAccount.Actor.url$') | |
e0a92917 RK |
141 | ] |
142 | }) | |
143 | } | |
144 | ||
145 | Object.assign(query, { where }) | |
146 | ||
7ad9b984 C |
147 | return AccountBlocklistModel |
148 | .scope([ ScopeNames.WITH_ACCOUNTS ]) | |
453e83ea | 149 | .findAndCountAll<MAccountBlocklistAccounts>(query) |
7ad9b984 C |
150 | .then(({ rows, count }) => { |
151 | return { total: count, data: rows } | |
152 | }) | |
153 | } | |
154 | ||
b49f22d8 | 155 | static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> { |
5fb2e288 | 156 | const query = { |
b49f22d8 | 157 | attributes: [ 'id' ], |
5fb2e288 C |
158 | where: { |
159 | accountId: { | |
160 | [Op.in]: accountIds | |
161 | } | |
162 | }, | |
163 | include: [ | |
164 | { | |
165 | attributes: [ 'id' ], | |
166 | model: AccountModel.unscoped(), | |
167 | required: true, | |
168 | as: 'BlockedAccount', | |
169 | include: [ | |
170 | { | |
171 | attributes: [ 'preferredUsername' ], | |
172 | model: ActorModel.unscoped(), | |
173 | required: true, | |
174 | include: [ | |
175 | { | |
176 | attributes: [ 'host' ], | |
177 | model: ServerModel.unscoped(), | |
178 | required: true | |
179 | } | |
180 | ] | |
181 | } | |
182 | ] | |
183 | } | |
184 | ] | |
185 | } | |
186 | ||
187 | return AccountBlocklistModel.findAll(query) | |
188 | .then(entries => entries.map(e => `${e.BlockedAccount.Actor.preferredUsername}@${e.BlockedAccount.Actor.Server.host}`)) | |
189 | } | |
190 | ||
80badf49 C |
191 | static getBlockStatus (byAccountIds: number[], handles: string[]): Promise<{ name: string, host: string, accountId: number }[]> { |
192 | const sanitizedHandles = handlesToNameAndHost(handles) | |
193 | ||
194 | const localHandles = sanitizedHandles.filter(h => !h.host) | |
195 | .map(h => h.name) | |
196 | ||
197 | const remoteHandles = sanitizedHandles.filter(h => !!h.host) | |
198 | .map(h => ([ h.name, h.host ])) | |
199 | ||
200 | const handlesWhere: string[] = [] | |
201 | ||
202 | if (localHandles.length !== 0) { | |
203 | handlesWhere.push(`("actor"."preferredUsername" IN (:localHandles) AND "server"."id" IS NULL)`) | |
204 | } | |
205 | ||
206 | if (remoteHandles.length !== 0) { | |
207 | handlesWhere.push(`(("actor"."preferredUsername", "server"."host") IN (:remoteHandles))`) | |
208 | } | |
209 | ||
210 | const rawQuery = `SELECT "accountBlocklist"."accountId", "actor"."preferredUsername" AS "name", "server"."host" ` + | |
211 | `FROM "accountBlocklist" ` + | |
212 | `INNER JOIN "account" ON "account"."id" = "accountBlocklist"."targetAccountId" ` + | |
213 | `INNER JOIN "actor" ON "actor"."id" = "account"."actorId" ` + | |
214 | `LEFT JOIN "server" ON "server"."id" = "actor"."serverId" ` + | |
215 | `WHERE "accountBlocklist"."accountId" IN (${createSafeIn(AccountBlocklistModel.sequelize, byAccountIds)}) ` + | |
216 | `AND (${handlesWhere.join(' OR ')})` | |
217 | ||
218 | return AccountBlocklistModel.sequelize.query(rawQuery, { | |
219 | type: QueryTypes.SELECT as QueryTypes.SELECT, | |
220 | replacements: { byAccountIds, localHandles, remoteHandles } | |
221 | }) | |
222 | } | |
223 | ||
1ca9f7c3 | 224 | toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock { |
7ad9b984 C |
225 | return { |
226 | byAccount: this.ByAccount.toFormattedJSON(), | |
af5767ff | 227 | blockedAccount: this.BlockedAccount.toFormattedJSON(), |
7ad9b984 C |
228 | createdAt: this.createdAt |
229 | } | |
230 | } | |
231 | } |