]>
Commit | Line | Data |
---|---|---|
d95d1559 C |
1 | import { Op } from 'sequelize' |
2 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' | |
26d6bf65 | 3 | import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/types/models' |
d95d1559 | 4 | import { AccountBlock } from '../../../shared/models' |
7d9ba5c0 | 5 | import { ActorModel } from '../actor/actor' |
5fb2e288 | 6 | import { ServerModel } from '../server/server' |
d95d1559 C |
7 | import { getSort, searchAttribute } from '../utils' |
8 | import { AccountModel } from './account' | |
7ad9b984 C |
9 | |
10 | enum ScopeNames { | |
11 | WITH_ACCOUNTS = 'WITH_ACCOUNTS' | |
12 | } | |
13 | ||
3acc5084 | 14 | @Scopes(() => ({ |
7ad9b984 C |
15 | [ScopeNames.WITH_ACCOUNTS]: { |
16 | include: [ | |
17 | { | |
3acc5084 | 18 | model: AccountModel, |
7ad9b984 C |
19 | required: true, |
20 | as: 'ByAccount' | |
21 | }, | |
22 | { | |
3acc5084 | 23 | model: AccountModel, |
7ad9b984 | 24 | required: true, |
af5767ff | 25 | as: 'BlockedAccount' |
7ad9b984 C |
26 | } |
27 | ] | |
28 | } | |
3acc5084 | 29 | })) |
7ad9b984 C |
30 | |
31 | @Table({ | |
32 | tableName: 'accountBlocklist', | |
33 | indexes: [ | |
34 | { | |
35 | fields: [ 'accountId', 'targetAccountId' ], | |
36 | unique: true | |
37 | }, | |
38 | { | |
39 | fields: [ 'targetAccountId' ] | |
40 | } | |
41 | ] | |
42 | }) | |
b49f22d8 | 43 | export class AccountBlocklistModel extends Model { |
7ad9b984 C |
44 | |
45 | @CreatedAt | |
46 | createdAt: Date | |
47 | ||
48 | @UpdatedAt | |
49 | updatedAt: Date | |
50 | ||
51 | @ForeignKey(() => AccountModel) | |
52 | @Column | |
53 | accountId: number | |
54 | ||
55 | @BelongsTo(() => AccountModel, { | |
56 | foreignKey: { | |
57 | name: 'accountId', | |
58 | allowNull: false | |
59 | }, | |
60 | as: 'ByAccount', | |
61 | onDelete: 'CASCADE' | |
62 | }) | |
63 | ByAccount: AccountModel | |
64 | ||
65 | @ForeignKey(() => AccountModel) | |
66 | @Column | |
67 | targetAccountId: number | |
68 | ||
69 | @BelongsTo(() => AccountModel, { | |
70 | foreignKey: { | |
71 | name: 'targetAccountId', | |
72 | allowNull: false | |
73 | }, | |
af5767ff | 74 | as: 'BlockedAccount', |
7ad9b984 C |
75 | onDelete: 'CASCADE' |
76 | }) | |
af5767ff | 77 | BlockedAccount: AccountModel |
7ad9b984 | 78 | |
f7cc67b4 | 79 | static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) { |
dc133480 | 80 | const query = { |
f7cc67b4 | 81 | attributes: [ 'accountId', 'id' ], |
dc133480 | 82 | where: { |
f7cc67b4 | 83 | accountId: { |
0374b6b5 | 84 | [Op.in]: accountIds |
f7cc67b4 | 85 | }, |
dc133480 C |
86 | targetAccountId |
87 | }, | |
88 | raw: true | |
89 | } | |
90 | ||
91 | return AccountBlocklistModel.unscoped() | |
f7cc67b4 C |
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 | }) | |
dc133480 C |
102 | } |
103 | ||
b49f22d8 | 104 | static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> { |
7ad9b984 C |
105 | const query = { |
106 | where: { | |
107 | accountId, | |
108 | targetAccountId | |
109 | } | |
110 | } | |
111 | ||
112 | return AccountBlocklistModel.findOne(query) | |
113 | } | |
114 | ||
e0a92917 RK |
115 | static listForApi (parameters: { |
116 | start: number | |
117 | count: number | |
118 | sort: string | |
119 | search?: string | |
120 | accountId: number | |
121 | }) { | |
122 | const { start, count, sort, search, accountId } = parameters | |
123 | ||
7ad9b984 C |
124 | const query = { |
125 | offset: start, | |
126 | limit: count, | |
e0a92917 RK |
127 | order: getSort(sort) |
128 | } | |
129 | ||
130 | const where = { | |
131 | accountId | |
7ad9b984 C |
132 | } |
133 | ||
e0a92917 RK |
134 | if (search) { |
135 | Object.assign(where, { | |
136 | [Op.or]: [ | |
0251197e RK |
137 | searchAttribute(search, '$BlockedAccount.name$'), |
138 | searchAttribute(search, '$BlockedAccount.Actor.url$') | |
e0a92917 RK |
139 | ] |
140 | }) | |
141 | } | |
142 | ||
143 | Object.assign(query, { where }) | |
144 | ||
7ad9b984 C |
145 | return AccountBlocklistModel |
146 | .scope([ ScopeNames.WITH_ACCOUNTS ]) | |
453e83ea | 147 | .findAndCountAll<MAccountBlocklistAccounts>(query) |
7ad9b984 C |
148 | .then(({ rows, count }) => { |
149 | return { total: count, data: rows } | |
150 | }) | |
151 | } | |
152 | ||
b49f22d8 | 153 | static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> { |
5fb2e288 | 154 | const query = { |
b49f22d8 | 155 | attributes: [ 'id' ], |
5fb2e288 C |
156 | where: { |
157 | accountId: { | |
158 | [Op.in]: accountIds | |
159 | } | |
160 | }, | |
161 | include: [ | |
162 | { | |
163 | attributes: [ 'id' ], | |
164 | model: AccountModel.unscoped(), | |
165 | required: true, | |
166 | as: 'BlockedAccount', | |
167 | include: [ | |
168 | { | |
169 | attributes: [ 'preferredUsername' ], | |
170 | model: ActorModel.unscoped(), | |
171 | required: true, | |
172 | include: [ | |
173 | { | |
174 | attributes: [ 'host' ], | |
175 | model: ServerModel.unscoped(), | |
176 | required: true | |
177 | } | |
178 | ] | |
179 | } | |
180 | ] | |
181 | } | |
182 | ] | |
183 | } | |
184 | ||
185 | return AccountBlocklistModel.findAll(query) | |
186 | .then(entries => entries.map(e => `${e.BlockedAccount.Actor.preferredUsername}@${e.BlockedAccount.Actor.Server.host}`)) | |
187 | } | |
188 | ||
1ca9f7c3 | 189 | toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock { |
7ad9b984 C |
190 | return { |
191 | byAccount: this.ByAccount.toFormattedJSON(), | |
af5767ff | 192 | blockedAccount: this.BlockedAccount.toFormattedJSON(), |
7ad9b984 C |
193 | createdAt: this.createdAt |
194 | } | |
195 | } | |
196 | } |