diff options
Diffstat (limited to 'server/models/account/account-blocklist.ts')
-rw-r--r-- | server/models/account/account-blocklist.ts | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts new file mode 100644 index 000000000..efd6ed59e --- /dev/null +++ b/server/models/account/account-blocklist.ts | |||
@@ -0,0 +1,142 @@ | |||
1 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' | ||
2 | import { AccountModel } from './account' | ||
3 | import { getSort } from '../utils' | ||
4 | import { AccountBlock } from '../../../shared/models/blocklist' | ||
5 | import { Op } from 'sequelize' | ||
6 | |||
7 | enum ScopeNames { | ||
8 | WITH_ACCOUNTS = 'WITH_ACCOUNTS' | ||
9 | } | ||
10 | |||
11 | @Scopes({ | ||
12 | [ScopeNames.WITH_ACCOUNTS]: { | ||
13 | include: [ | ||
14 | { | ||
15 | model: () => AccountModel, | ||
16 | required: true, | ||
17 | as: 'ByAccount' | ||
18 | }, | ||
19 | { | ||
20 | model: () => AccountModel, | ||
21 | required: true, | ||
22 | as: 'BlockedAccount' | ||
23 | } | ||
24 | ] | ||
25 | } | ||
26 | }) | ||
27 | |||
28 | @Table({ | ||
29 | tableName: 'accountBlocklist', | ||
30 | indexes: [ | ||
31 | { | ||
32 | fields: [ 'accountId', 'targetAccountId' ], | ||
33 | unique: true | ||
34 | }, | ||
35 | { | ||
36 | fields: [ 'targetAccountId' ] | ||
37 | } | ||
38 | ] | ||
39 | }) | ||
40 | export class AccountBlocklistModel extends Model<AccountBlocklistModel> { | ||
41 | |||
42 | @CreatedAt | ||
43 | createdAt: Date | ||
44 | |||
45 | @UpdatedAt | ||
46 | updatedAt: Date | ||
47 | |||
48 | @ForeignKey(() => AccountModel) | ||
49 | @Column | ||
50 | accountId: number | ||
51 | |||
52 | @BelongsTo(() => AccountModel, { | ||
53 | foreignKey: { | ||
54 | name: 'accountId', | ||
55 | allowNull: false | ||
56 | }, | ||
57 | as: 'ByAccount', | ||
58 | onDelete: 'CASCADE' | ||
59 | }) | ||
60 | ByAccount: AccountModel | ||
61 | |||
62 | @ForeignKey(() => AccountModel) | ||
63 | @Column | ||
64 | targetAccountId: number | ||
65 | |||
66 | @BelongsTo(() => AccountModel, { | ||
67 | foreignKey: { | ||
68 | name: 'targetAccountId', | ||
69 | allowNull: false | ||
70 | }, | ||
71 | as: 'BlockedAccount', | ||
72 | onDelete: 'CASCADE' | ||
73 | }) | ||
74 | BlockedAccount: AccountModel | ||
75 | |||
76 | static isAccountMutedBy (accountId: number, targetAccountId: number) { | ||
77 | return AccountBlocklistModel.isAccountMutedByMulti([ accountId ], targetAccountId) | ||
78 | .then(result => result[accountId]) | ||
79 | } | ||
80 | |||
81 | static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) { | ||
82 | const query = { | ||
83 | attributes: [ 'accountId', 'id' ], | ||
84 | where: { | ||
85 | accountId: { | ||
86 | [Op.any]: accountIds | ||
87 | }, | ||
88 | targetAccountId | ||
89 | }, | ||
90 | raw: true | ||
91 | } | ||
92 | |||
93 | return AccountBlocklistModel.unscoped() | ||
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 | }) | ||
104 | } | ||
105 | |||
106 | static loadByAccountAndTarget (accountId: number, targetAccountId: number) { | ||
107 | const query = { | ||
108 | where: { | ||
109 | accountId, | ||
110 | targetAccountId | ||
111 | } | ||
112 | } | ||
113 | |||
114 | return AccountBlocklistModel.findOne(query) | ||
115 | } | ||
116 | |||
117 | static listForApi (accountId: number, start: number, count: number, sort: string) { | ||
118 | const query = { | ||
119 | offset: start, | ||
120 | limit: count, | ||
121 | order: getSort(sort), | ||
122 | where: { | ||
123 | accountId | ||
124 | } | ||
125 | } | ||
126 | |||
127 | return AccountBlocklistModel | ||
128 | .scope([ ScopeNames.WITH_ACCOUNTS ]) | ||
129 | .findAndCountAll(query) | ||
130 | .then(({ rows, count }) => { | ||
131 | return { total: count, data: rows } | ||
132 | }) | ||
133 | } | ||
134 | |||
135 | toFormattedJSON (): AccountBlock { | ||
136 | return { | ||
137 | byAccount: this.ByAccount.toFormattedJSON(), | ||
138 | blockedAccount: this.BlockedAccount.toFormattedJSON(), | ||
139 | createdAt: this.createdAt | ||
140 | } | ||
141 | } | ||
142 | } | ||