]>
Commit | Line | Data |
---|---|---|
d0800f76 | 1 | import { FindOptions, Op, QueryTypes } from 'sequelize' |
2 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
80badf49 | 3 | import { handlesToNameAndHost } from '@server/helpers/actors' |
d0800f76 | 4 | import { MAccountBlocklist, 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 | 11 | |
7ad9b984 C |
12 | @Table({ |
13 | tableName: 'accountBlocklist', | |
14 | indexes: [ | |
15 | { | |
16 | fields: [ 'accountId', 'targetAccountId' ], | |
17 | unique: true | |
18 | }, | |
19 | { | |
20 | fields: [ 'targetAccountId' ] | |
21 | } | |
22 | ] | |
23 | }) | |
16c016e8 | 24 | export class AccountBlocklistModel extends Model<Partial<AttributesOnly<AccountBlocklistModel>>> { |
7ad9b984 C |
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 | }, | |
af5767ff | 55 | as: 'BlockedAccount', |
7ad9b984 C |
56 | onDelete: 'CASCADE' |
57 | }) | |
af5767ff | 58 | BlockedAccount: AccountModel |
7ad9b984 | 59 | |
80badf49 | 60 | static isAccountMutedByAccounts (accountIds: number[], targetAccountId: number) { |
dc133480 | 61 | const query = { |
f7cc67b4 | 62 | attributes: [ 'accountId', 'id' ], |
dc133480 | 63 | where: { |
f7cc67b4 | 64 | accountId: { |
0374b6b5 | 65 | [Op.in]: accountIds |
f7cc67b4 | 66 | }, |
dc133480 C |
67 | targetAccountId |
68 | }, | |
69 | raw: true | |
70 | } | |
71 | ||
72 | return AccountBlocklistModel.unscoped() | |
f7cc67b4 C |
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 | }) | |
dc133480 C |
83 | } |
84 | ||
b49f22d8 | 85 | static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> { |
7ad9b984 C |
86 | const query = { |
87 | where: { | |
88 | accountId, | |
89 | targetAccountId | |
90 | } | |
91 | } | |
92 | ||
93 | return AccountBlocklistModel.findOne(query) | |
94 | } | |
95 | ||
e0a92917 RK |
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 | ||
d0800f76 | 105 | const getQuery = (forCount: boolean) => { |
106 | const query: FindOptions = { | |
107 | offset: start, | |
108 | limit: count, | |
109 | order: getSort(sort), | |
110 | where: { accountId } | |
111 | } | |
e0a92917 | 112 | |
d0800f76 | 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 | } | |
7ad9b984 | 121 | |
d0800f76 | 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 | } | |
e0a92917 | 134 | ] |
d0800f76 | 135 | } |
e0a92917 | 136 | |
d0800f76 | 137 | return query |
138 | } | |
e0a92917 | 139 | |
d0800f76 | 140 | return Promise.all([ |
141 | AccountBlocklistModel.count(getQuery(true)), | |
142 | AccountBlocklistModel.findAll(getQuery(false)) | |
143 | ]).then(([ total, data ]) => ({ total, data })) | |
7ad9b984 C |
144 | } |
145 | ||
b49f22d8 | 146 | static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> { |
5fb2e288 | 147 | const query = { |
b49f22d8 | 148 | attributes: [ 'id' ], |
5fb2e288 C |
149 | where: { |
150 | accountId: { | |
151 | [Op.in]: accountIds | |
152 | } | |
153 | }, | |
154 | include: [ | |
155 | { | |
156 | attributes: [ 'id' ], | |
157 | model: AccountModel.unscoped(), | |
158 | required: true, | |
159 | as: 'BlockedAccount', | |
160 | include: [ | |
161 | { | |
162 | attributes: [ 'preferredUsername' ], | |
163 | model: ActorModel.unscoped(), | |
164 | required: true, | |
165 | include: [ | |
166 | { | |
167 | attributes: [ 'host' ], | |
168 | model: ServerModel.unscoped(), | |
169 | required: true | |
170 | } | |
171 | ] | |
172 | } | |
173 | ] | |
174 | } | |
175 | ] | |
176 | } | |
177 | ||
178 | return AccountBlocklistModel.findAll(query) | |
179 | .then(entries => entries.map(e => `${e.BlockedAccount.Actor.preferredUsername}@${e.BlockedAccount.Actor.Server.host}`)) | |
180 | } | |
181 | ||
80badf49 C |
182 | static getBlockStatus (byAccountIds: number[], handles: string[]): Promise<{ name: string, host: string, accountId: number }[]> { |
183 | const sanitizedHandles = handlesToNameAndHost(handles) | |
184 | ||
185 | const localHandles = sanitizedHandles.filter(h => !h.host) | |
186 | .map(h => h.name) | |
187 | ||
188 | const remoteHandles = sanitizedHandles.filter(h => !!h.host) | |
189 | .map(h => ([ h.name, h.host ])) | |
190 | ||
191 | const handlesWhere: string[] = [] | |
192 | ||
193 | if (localHandles.length !== 0) { | |
194 | handlesWhere.push(`("actor"."preferredUsername" IN (:localHandles) AND "server"."id" IS NULL)`) | |
195 | } | |
196 | ||
197 | if (remoteHandles.length !== 0) { | |
198 | handlesWhere.push(`(("actor"."preferredUsername", "server"."host") IN (:remoteHandles))`) | |
199 | } | |
200 | ||
201 | const rawQuery = `SELECT "accountBlocklist"."accountId", "actor"."preferredUsername" AS "name", "server"."host" ` + | |
202 | `FROM "accountBlocklist" ` + | |
203 | `INNER JOIN "account" ON "account"."id" = "accountBlocklist"."targetAccountId" ` + | |
204 | `INNER JOIN "actor" ON "actor"."id" = "account"."actorId" ` + | |
205 | `LEFT JOIN "server" ON "server"."id" = "actor"."serverId" ` + | |
206 | `WHERE "accountBlocklist"."accountId" IN (${createSafeIn(AccountBlocklistModel.sequelize, byAccountIds)}) ` + | |
207 | `AND (${handlesWhere.join(' OR ')})` | |
208 | ||
209 | return AccountBlocklistModel.sequelize.query(rawQuery, { | |
210 | type: QueryTypes.SELECT as QueryTypes.SELECT, | |
211 | replacements: { byAccountIds, localHandles, remoteHandles } | |
212 | }) | |
213 | } | |
214 | ||
1ca9f7c3 | 215 | toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock { |
7ad9b984 C |
216 | return { |
217 | byAccount: this.ByAccount.toFormattedJSON(), | |
af5767ff | 218 | blockedAccount: this.BlockedAccount.toFormattedJSON(), |
7ad9b984 C |
219 | createdAt: this.createdAt |
220 | } | |
221 | } | |
222 | } |