]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-blocklist.ts
Add filter inputs for blacklisted videos and muted accounts/servers
[github/Chocobozzz/PeerTube.git] / server / models / account / account-blocklist.ts
CommitLineData
7ad9b984
C
1import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2import { AccountModel } from './account'
e0a92917 3import { getSort, searchAttribute } from '../utils'
7ad9b984 4import { AccountBlock } from '../../../shared/models/blocklist'
f7cc67b4 5import { Op } from 'sequelize'
453e83ea 6import * as Bluebird from 'bluebird'
1ca9f7c3 7import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/typings/models'
7ad9b984
C
8
9enum 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})
42export 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
f7cc67b4 78 static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
dc133480 79 const query = {
f7cc67b4 80 attributes: [ 'accountId', 'id' ],
dc133480 81 where: {
f7cc67b4 82 accountId: {
0374b6b5 83 [Op.in]: accountIds
f7cc67b4 84 },
dc133480
C
85 targetAccountId
86 },
87 raw: true
88 }
89
90 return AccountBlocklistModel.unscoped()
f7cc67b4
C
91 .findAll(query)
92 .then(rows => {
93 const result: { [accountId: number]: boolean } = {}
94
95 for (const accountId of accountIds) {
96 result[accountId] = !!rows.find(r => r.accountId === accountId)
97 }
98
99 return result
100 })
dc133480
C
101 }
102
453e83ea 103 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Bluebird<MAccountBlocklist> {
7ad9b984
C
104 const query = {
105 where: {
106 accountId,
107 targetAccountId
108 }
109 }
110
111 return AccountBlocklistModel.findOne(query)
112 }
113
e0a92917
RK
114 static listForApi (parameters: {
115 start: number
116 count: number
117 sort: string
118 search?: string
119 accountId: number
120 }) {
121 const { start, count, sort, search, accountId } = parameters
122
7ad9b984
C
123 const query = {
124 offset: start,
125 limit: count,
e0a92917
RK
126 order: getSort(sort)
127 }
128
129 const where = {
130 accountId
7ad9b984
C
131 }
132
e0a92917
RK
133 if (search) {
134 Object.assign(where, {
135 [Op.or]: [
136 { ...searchAttribute(search, '$BlockedAccount.name$') },
137 { ...searchAttribute(search, '$BlockedAccount.Actor.url$') }
138 ]
139 })
140 }
141
142 Object.assign(query, { where })
143
7ad9b984
C
144 return AccountBlocklistModel
145 .scope([ ScopeNames.WITH_ACCOUNTS ])
453e83ea 146 .findAndCountAll<MAccountBlocklistAccounts>(query)
7ad9b984
C
147 .then(({ rows, count }) => {
148 return { total: count, data: rows }
149 })
150 }
151
1ca9f7c3 152 toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
7ad9b984
C
153 return {
154 byAccount: this.ByAccount.toFormattedJSON(),
af5767ff 155 blockedAccount: this.BlockedAccount.toFormattedJSON(),
7ad9b984
C
156 createdAt: this.createdAt
157 }
158 }
159}