]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account-blocklist.ts
bb537139515c0eeb7727c75436f7d14af9d9df5e
[github/Chocobozzz/PeerTube.git] / server / models / account / account-blocklist.ts
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'
5 import { Op } from 'sequelize'
6 import * as Bluebird from 'bluebird'
7 import { MAccountBlocklist, MAccountBlocklistAccounts } from '@server/typings/models'
8
9 enum ScopeNames {
10 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
11 }
12
13 @Scopes(() => ({
14 [ScopeNames.WITH_ACCOUNTS]: {
15 include: [
16 {
17 model: AccountModel,
18 required: true,
19 as: 'ByAccount'
20 },
21 {
22 model: AccountModel,
23 required: true,
24 as: 'BlockedAccount'
25 }
26 ]
27 }
28 }))
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 },
73 as: 'BlockedAccount',
74 onDelete: 'CASCADE'
75 })
76 BlockedAccount: AccountModel
77
78 static isAccountMutedBy (accountId: number, targetAccountId: number) {
79 return AccountBlocklistModel.isAccountMutedByMulti([ accountId ], targetAccountId)
80 .then(result => result[accountId])
81 }
82
83 static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) {
84 const query = {
85 attributes: [ 'accountId', 'id' ],
86 where: {
87 accountId: {
88 [Op.in]: accountIds // FIXME: sequelize ANY seems broken
89 },
90 targetAccountId
91 },
92 raw: true
93 }
94
95 return AccountBlocklistModel.unscoped()
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 })
106 }
107
108 static loadByAccountAndTarget (accountId: number, targetAccountId: number): Bluebird<MAccountBlocklist> {
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 ])
131 .findAndCountAll<MAccountBlocklistAccounts>(query)
132 .then(({ rows, count }) => {
133 return { total: count, data: rows }
134 })
135 }
136
137 toFormattedJSON (): AccountBlock {
138 return {
139 byAccount: this.ByAccount.toFormattedJSON(),
140 blockedAccount: this.BlockedAccount.toFormattedJSON(),
141 createdAt: this.createdAt
142 }
143 }
144 }