]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account-blocklist.ts
Add import finished and video published notifs
[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
6 enum ScopeNames {
7 WITH_ACCOUNTS = 'WITH_ACCOUNTS'
8 }
9
10 @Scopes({
11 [ScopeNames.WITH_ACCOUNTS]: {
12 include: [
13 {
14 model: () => AccountModel,
15 required: true,
16 as: 'ByAccount'
17 },
18 {
19 model: () => AccountModel,
20 required: true,
21 as: 'BlockedAccount'
22 }
23 ]
24 }
25 })
26
27 @Table({
28 tableName: 'accountBlocklist',
29 indexes: [
30 {
31 fields: [ 'accountId', 'targetAccountId' ],
32 unique: true
33 },
34 {
35 fields: [ 'targetAccountId' ]
36 }
37 ]
38 })
39 export class AccountBlocklistModel extends Model<AccountBlocklistModel> {
40
41 @CreatedAt
42 createdAt: Date
43
44 @UpdatedAt
45 updatedAt: Date
46
47 @ForeignKey(() => AccountModel)
48 @Column
49 accountId: number
50
51 @BelongsTo(() => AccountModel, {
52 foreignKey: {
53 name: 'accountId',
54 allowNull: false
55 },
56 as: 'ByAccount',
57 onDelete: 'CASCADE'
58 })
59 ByAccount: AccountModel
60
61 @ForeignKey(() => AccountModel)
62 @Column
63 targetAccountId: number
64
65 @BelongsTo(() => AccountModel, {
66 foreignKey: {
67 name: 'targetAccountId',
68 allowNull: false
69 },
70 as: 'BlockedAccount',
71 onDelete: 'CASCADE'
72 })
73 BlockedAccount: AccountModel
74
75 static isAccountMutedBy (accountId: number, targetAccountId: number) {
76 const query = {
77 attributes: [ 'id' ],
78 where: {
79 accountId,
80 targetAccountId
81 },
82 raw: true
83 }
84
85 return AccountBlocklistModel.unscoped()
86 .findOne(query)
87 .then(a => !!a)
88 }
89
90 static loadByAccountAndTarget (accountId: number, targetAccountId: number) {
91 const query = {
92 where: {
93 accountId,
94 targetAccountId
95 }
96 }
97
98 return AccountBlocklistModel.findOne(query)
99 }
100
101 static listForApi (accountId: number, start: number, count: number, sort: string) {
102 const query = {
103 offset: start,
104 limit: count,
105 order: getSort(sort),
106 where: {
107 accountId
108 }
109 }
110
111 return AccountBlocklistModel
112 .scope([ ScopeNames.WITH_ACCOUNTS ])
113 .findAndCountAll(query)
114 .then(({ rows, count }) => {
115 return { total: count, data: rows }
116 })
117 }
118
119 toFormattedJSON (): AccountBlock {
120 return {
121 byAccount: this.ByAccount.toFormattedJSON(),
122 blockedAccount: this.BlockedAccount.toFormattedJSON(),
123 createdAt: this.createdAt
124 }
125 }
126 }