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