]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/server/server-blocklist.ts
Fix margin-content and miniature thumbnail width on mobile, fix media queries for...
[github/Chocobozzz/PeerTube.git] / server / models / server / server-blocklist.ts
CommitLineData
7ad9b984
C
1import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2import { AccountModel } from '../account/account'
3import { ServerModel } from './server'
4import { ServerBlock } from '../../../shared/models/blocklist'
e0a92917 5import { getSort, searchAttribute } from '../utils'
453e83ea 6import * as Bluebird from 'bluebird'
1ca9f7c3 7import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/typings/models'
dddc8b1f 8import { Op } from 'sequelize'
7ad9b984
C
9
10enum ScopeNames {
11 WITH_ACCOUNT = 'WITH_ACCOUNT',
12 WITH_SERVER = 'WITH_SERVER'
13}
14
3acc5084 15@Scopes(() => ({
7ad9b984
C
16 [ScopeNames.WITH_ACCOUNT]: {
17 include: [
18 {
3acc5084 19 model: AccountModel,
7ad9b984
C
20 required: true
21 }
22 ]
23 },
24 [ScopeNames.WITH_SERVER]: {
25 include: [
26 {
3acc5084 27 model: ServerModel,
7ad9b984
C
28 required: true
29 }
30 ]
31 }
3acc5084 32}))
7ad9b984
C
33
34@Table({
35 tableName: 'serverBlocklist',
36 indexes: [
37 {
38 fields: [ 'accountId', 'targetServerId' ],
39 unique: true
40 },
41 {
42 fields: [ 'targetServerId' ]
43 }
44 ]
45})
46export class ServerBlocklistModel extends Model<ServerBlocklistModel> {
47
48 @CreatedAt
49 createdAt: Date
50
51 @UpdatedAt
52 updatedAt: Date
53
54 @ForeignKey(() => AccountModel)
55 @Column
56 accountId: number
57
58 @BelongsTo(() => AccountModel, {
59 foreignKey: {
60 name: 'accountId',
61 allowNull: false
62 },
63 onDelete: 'CASCADE'
64 })
65 ByAccount: AccountModel
66
67 @ForeignKey(() => ServerModel)
68 @Column
69 targetServerId: number
70
71 @BelongsTo(() => ServerModel, {
72 foreignKey: {
7ad9b984
C
73 allowNull: false
74 },
75 onDelete: 'CASCADE'
76 })
af5767ff 77 BlockedServer: ServerModel
7ad9b984 78
dddc8b1f
C
79 static isServerMutedByMulti (accountIds: number[], targetServerId: number) {
80 const query = {
81 attributes: [ 'accountId', 'id' ],
82 where: {
83 accountId: {
0374b6b5 84 [Op.in]: accountIds
dddc8b1f
C
85 },
86 targetServerId
87 },
88 raw: true
89 }
90
91 return ServerBlocklistModel.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
453e83ea 104 static loadByAccountAndHost (accountId: number, host: string): Bluebird<MServerBlocklist> {
7ad9b984
C
105 const query = {
106 where: {
107 accountId
108 },
109 include: [
110 {
111 model: ServerModel,
112 where: {
113 host
114 },
115 required: true
116 }
117 ]
118 }
119
120 return ServerBlocklistModel.findOne(query)
121 }
122
e0a92917
RK
123 static listForApi (parameters: {
124 start: number
125 count: number
126 sort: string
127 search?: string
128 accountId: number
129 }) {
130 const { start, count, sort, search, accountId } = parameters
131
7ad9b984
C
132 const query = {
133 offset: start,
134 limit: count,
135 order: getSort(sort),
136 where: {
e0a92917
RK
137 accountId,
138 ...searchAttribute(search, '$BlockedServer.host$')
7ad9b984
C
139 }
140 }
141
142 return ServerBlocklistModel
143 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
453e83ea 144 .findAndCountAll<MServerBlocklistAccountServer>(query)
7ad9b984
C
145 .then(({ rows, count }) => {
146 return { total: count, data: rows }
147 })
148 }
149
1ca9f7c3 150 toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
7ad9b984
C
151 return {
152 byAccount: this.ByAccount.toFormattedJSON(),
af5767ff 153 blockedServer: this.BlockedServer.toFormattedJSON(),
7ad9b984
C
154 createdAt: this.createdAt
155 }
156 }
157}