]>
Commit | Line | Data |
---|---|---|
7ad9b984 C |
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' | |
f7cc67b4 | 5 | import { Op } from 'sequelize' |
453e83ea | 6 | import * as Bluebird from 'bluebird' |
1ca9f7c3 | 7 | import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/typings/models' |
7ad9b984 C |
8 | |
9 | enum ScopeNames { | |
10 | WITH_ACCOUNTS = 'WITH_ACCOUNTS' | |
11 | } | |
12 | ||
3acc5084 | 13 | @Scopes(() => ({ |
7ad9b984 C |
14 | [ScopeNames.WITH_ACCOUNTS]: { |
15 | include: [ | |
16 | { | |
3acc5084 | 17 | model: AccountModel, |
7ad9b984 C |
18 | required: true, |
19 | as: 'ByAccount' | |
20 | }, | |
21 | { | |
3acc5084 | 22 | model: AccountModel, |
7ad9b984 | 23 | required: true, |
af5767ff | 24 | as: 'BlockedAccount' |
7ad9b984 C |
25 | } |
26 | ] | |
27 | } | |
3acc5084 | 28 | })) |
7ad9b984 C |
29 | |
30 | @Table({ | |
31 | tableName: 'accountBlocklist', | |
32 | indexes: [ | |
33 | { | |
34 | fields: [ 'accountId', 'targetAccountId' ], | |
35 | unique: true | |
36 | }, | |
37 | { | |
38 | fields: [ 'targetAccountId' ] | |
39 | } | |
40 | ] | |
41 | }) | |
42 | export class AccountBlocklistModel extends Model<AccountBlocklistModel> { | |
43 | ||
44 | @CreatedAt | |
45 | createdAt: Date | |
46 | ||
47 | @UpdatedAt | |
48 | updatedAt: Date | |
49 | ||
50 | @ForeignKey(() => AccountModel) | |
51 | @Column | |
52 | accountId: number | |
53 | ||
54 | @BelongsTo(() => AccountModel, { | |
55 | foreignKey: { | |
56 | name: 'accountId', | |
57 | allowNull: false | |
58 | }, | |
59 | as: 'ByAccount', | |
60 | onDelete: 'CASCADE' | |
61 | }) | |
62 | ByAccount: AccountModel | |
63 | ||
64 | @ForeignKey(() => AccountModel) | |
65 | @Column | |
66 | targetAccountId: number | |
67 | ||
68 | @BelongsTo(() => AccountModel, { | |
69 | foreignKey: { | |
70 | name: 'targetAccountId', | |
71 | allowNull: false | |
72 | }, | |
af5767ff | 73 | as: 'BlockedAccount', |
7ad9b984 C |
74 | onDelete: 'CASCADE' |
75 | }) | |
af5767ff | 76 | BlockedAccount: AccountModel |
7ad9b984 | 77 | |
dc133480 | 78 | static isAccountMutedBy (accountId: number, targetAccountId: number) { |
f7cc67b4 C |
79 | return AccountBlocklistModel.isAccountMutedByMulti([ accountId ], targetAccountId) |
80 | .then(result => result[accountId]) | |
81 | } | |
82 | ||
83 | static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) { | |
dc133480 | 84 | const query = { |
f7cc67b4 | 85 | attributes: [ 'accountId', 'id' ], |
dc133480 | 86 | where: { |
f7cc67b4 | 87 | accountId: { |
3acc5084 | 88 | [Op.in]: accountIds // FIXME: sequelize ANY seems broken |
f7cc67b4 | 89 | }, |
dc133480 C |
90 | targetAccountId |
91 | }, | |
92 | raw: true | |
93 | } | |
94 | ||
95 | return AccountBlocklistModel.unscoped() | |
f7cc67b4 C |
96 | .findAll(query) |
97 | .then(rows => { | |
98 | const result: { [accountId: number]: boolean } = {} | |
99 | ||
100 | for (const accountId of accountIds) { | |
101 | result[accountId] = !!rows.find(r => r.accountId === accountId) | |
102 | } | |
103 | ||
104 | return result | |
105 | }) | |
dc133480 C |
106 | } |
107 | ||
453e83ea | 108 | static loadByAccountAndTarget (accountId: number, targetAccountId: number): Bluebird<MAccountBlocklist> { |
7ad9b984 C |
109 | const query = { |
110 | where: { | |
111 | accountId, | |
112 | targetAccountId | |
113 | } | |
114 | } | |
115 | ||
116 | return AccountBlocklistModel.findOne(query) | |
117 | } | |
118 | ||
119 | static listForApi (accountId: number, start: number, count: number, sort: string) { | |
120 | const query = { | |
121 | offset: start, | |
122 | limit: count, | |
123 | order: getSort(sort), | |
124 | where: { | |
125 | accountId | |
126 | } | |
127 | } | |
128 | ||
129 | return AccountBlocklistModel | |
130 | .scope([ ScopeNames.WITH_ACCOUNTS ]) | |
453e83ea | 131 | .findAndCountAll<MAccountBlocklistAccounts>(query) |
7ad9b984 C |
132 | .then(({ rows, count }) => { |
133 | return { total: count, data: rows } | |
134 | }) | |
135 | } | |
136 | ||
1ca9f7c3 | 137 | toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock { |
7ad9b984 C |
138 | return { |
139 | byAccount: this.ByAccount.toFormattedJSON(), | |
af5767ff | 140 | blockedAccount: this.BlockedAccount.toFormattedJSON(), |
7ad9b984 C |
141 | createdAt: this.createdAt |
142 | } | |
143 | } | |
144 | } |