]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account-blocklist.ts
Cleanup models directory organization
[github/Chocobozzz/PeerTube.git] / server / models / account / account-blocklist.ts
1 import { Op } from 'sequelize'
2 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/types/models'
4 import { AccountBlock } from '../../../shared/models'
5 import { ActorModel } from '../actor/actor'
6 import { ServerModel } from '../server/server'
7 import { getSort, searchAttribute } from '../utils'
8 import { AccountModel } from './account'
9
10 enum ScopeNames {
11 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
12 }
13
14 @Scopes(() => ({
15 [ScopeNames.WITH_ACCOUNTS]: {
16 include: [
17 {
18 model: AccountModel,
19 required: true,
20 as: 'ByAccount'
21 },
22 {
23 model: AccountModel,
24 required: true,
25 as: 'BlockedAccount'
26 }
27 ]
28 }
29 }))
30
31 @Table({
32 tableName: 'accountBlocklist',
33 indexes: [
34 {
35 fields: [ 'accountId', 'targetAccountId' ],
36 unique: true
37 },
38 {
39 fields: [ 'targetAccountId' ]
40 }
41 ]
42 })
43 export class AccountBlocklistModel extends Model {
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 },
74 as: 'BlockedAccount',
75 onDelete: 'CASCADE'
76 })
77 BlockedAccount: AccountModel
78
79 static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
80 const query = {
81 attributes: [ 'accountId', 'id' ],
82 where: {
83 accountId: {
84 [Op.in]: accountIds
85 },
86 targetAccountId
87 },
88 raw: true
89 }
90
91 return AccountBlocklistModel.unscoped()
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 })
102 }
103
104 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> {
105 const query = {
106 where: {
107 accountId,
108 targetAccountId
109 }
110 }
111
112 return AccountBlocklistModel.findOne(query)
113 }
114
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
124 const query = {
125 offset: start,
126 limit: count,
127 order: getSort(sort)
128 }
129
130 const where = {
131 accountId
132 }
133
134 if (search) {
135 Object.assign(where, {
136 [Op.or]: [
137 searchAttribute(search, '$BlockedAccount.name$'),
138 searchAttribute(search, '$BlockedAccount.Actor.url$')
139 ]
140 })
141 }
142
143 Object.assign(query, { where })
144
145 return AccountBlocklistModel
146 .scope([ ScopeNames.WITH_ACCOUNTS ])
147 .findAndCountAll<MAccountBlocklistAccounts>(query)
148 .then(({ rows, count }) => {
149 return { total: count, data: rows }
150 })
151 }
152
153 static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> {
154 const query = {
155 attributes: [ 'id' ],
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
189 toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
190 return {
191 byAccount: this.ByAccount.toFormattedJSON(),
192 blockedAccount: this.BlockedAccount.toFormattedJSON(),
193 createdAt: this.createdAt
194 }
195 }
196 }