]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-blocklist.ts
Upgrade sequelize
[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'
3import { getSort } from '../utils'
4import { AccountBlock } from '../../../shared/models/blocklist'
f7cc67b4 5import { Op } from 'sequelize'
7ad9b984
C
6
7enum ScopeNames {
8 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
9}
10
3acc5084 11@Scopes(() => ({
7ad9b984
C
12 [ScopeNames.WITH_ACCOUNTS]: {
13 include: [
14 {
3acc5084 15 model: AccountModel,
7ad9b984
C
16 required: true,
17 as: 'ByAccount'
18 },
19 {
3acc5084 20 model: AccountModel,
7ad9b984 21 required: true,
af5767ff 22 as: 'BlockedAccount'
7ad9b984
C
23 }
24 ]
25 }
3acc5084 26}))
7ad9b984
C
27
28@Table({
29 tableName: 'accountBlocklist',
30 indexes: [
31 {
32 fields: [ 'accountId', 'targetAccountId' ],
33 unique: true
34 },
35 {
36 fields: [ 'targetAccountId' ]
37 }
38 ]
39})
40export 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 },
af5767ff 71 as: 'BlockedAccount',
7ad9b984
C
72 onDelete: 'CASCADE'
73 })
af5767ff 74 BlockedAccount: AccountModel
7ad9b984 75
dc133480 76 static isAccountMutedBy (accountId: number, targetAccountId: number) {
f7cc67b4
C
77 return AccountBlocklistModel.isAccountMutedByMulti([ accountId ], targetAccountId)
78 .then(result => result[accountId])
79 }
80
81 static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
dc133480 82 const query = {
f7cc67b4 83 attributes: [ 'accountId', 'id' ],
dc133480 84 where: {
f7cc67b4 85 accountId: {
3acc5084 86 [Op.in]: accountIds // FIXME: sequelize ANY seems broken
f7cc67b4 87 },
dc133480
C
88 targetAccountId
89 },
90 raw: true
91 }
92
93 return AccountBlocklistModel.unscoped()
f7cc67b4
C
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 })
dc133480
C
104 }
105
7ad9b984
C
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(),
af5767ff 138 blockedAccount: this.BlockedAccount.toFormattedJSON(),
7ad9b984
C
139 createdAt: this.createdAt
140 }
141 }
142}