]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-blocklist.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / account / account-blocklist.ts
CommitLineData
d95d1559
C
1import { Op } from 'sequelize'
2import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
26d6bf65 3import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/types/models'
16c016e8 4import { AttributesOnly } from '@shared/core-utils'
d95d1559 5import { AccountBlock } from '../../../shared/models'
7d9ba5c0 6import { ActorModel } from '../actor/actor'
5fb2e288 7import { ServerModel } from '../server/server'
d95d1559
C
8import { getSort, searchAttribute } from '../utils'
9import { AccountModel } from './account'
7ad9b984
C
10
11enum 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})
16c016e8 44export class AccountBlocklistModel extends Model<Partial<AttributesOnly<AccountBlocklistModel>>> {
7ad9b984
C
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
b49f22d8 105 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<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
b49f22d8 154 static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> {
5fb2e288 155 const query = {
b49f22d8 156 attributes: [ 'id' ],
5fb2e288
C
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}