PeerTube/server/models/server/server-blocklist.ts

149 lines
3.4 KiB
TypeScript
Raw Normal View History

import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
import { AccountModel } from '../account/account'
import { ServerModel } from './server'
import { ServerBlock } from '../../../shared/models/blocklist'
import { getSort } from '../utils'
2019-08-15 04:53:26 -05:00
import * as Bluebird from 'bluebird'
2019-08-20 12:05:31 -05:00
import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/typings/models'
2019-12-19 03:35:47 -06:00
import { Op } from 'sequelize'
enum ScopeNames {
WITH_ACCOUNT = 'WITH_ACCOUNT',
WITH_SERVER = 'WITH_SERVER'
}
2019-04-23 02:50:57 -05:00
@Scopes(() => ({
[ScopeNames.WITH_ACCOUNT]: {
include: [
{
2019-04-23 02:50:57 -05:00
model: AccountModel,
required: true
}
]
},
[ScopeNames.WITH_SERVER]: {
include: [
{
2019-04-23 02:50:57 -05:00
model: ServerModel,
required: true
}
]
}
2019-04-23 02:50:57 -05:00
}))
@Table({
tableName: 'serverBlocklist',
indexes: [
{
fields: [ 'accountId', 'targetServerId' ],
unique: true
},
{
fields: [ 'targetServerId' ]
}
]
})
export class ServerBlocklistModel extends Model<ServerBlocklistModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'accountId',
allowNull: false
},
onDelete: 'CASCADE'
})
ByAccount: AccountModel
@ForeignKey(() => ServerModel)
@Column
targetServerId: number
@BelongsTo(() => ServerModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
BlockedServer: ServerModel
2019-12-19 03:35:47 -06:00
static isServerMutedByMulti (accountIds: number[], targetServerId: number) {
const query = {
attributes: [ 'accountId', 'id' ],
where: {
accountId: {
[Op.in]: accountIds // FIXME: sequelize ANY seems broken
},
targetServerId
},
raw: true
}
return ServerBlocklistModel.unscoped()
.findAll(query)
.then(rows => {
const result: { [accountId: number]: boolean } = {}
for (const accountId of accountIds) {
result[accountId] = !!rows.find(r => r.accountId === accountId)
}
return result
})
}
2019-08-15 04:53:26 -05:00
static loadByAccountAndHost (accountId: number, host: string): Bluebird<MServerBlocklist> {
const query = {
where: {
accountId
},
include: [
{
model: ServerModel,
where: {
host
},
required: true
}
]
}
return ServerBlocklistModel.findOne(query)
}
static listForApi (accountId: number, start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
order: getSort(sort),
where: {
accountId
}
}
return ServerBlocklistModel
.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ])
2019-08-15 04:53:26 -05:00
.findAndCountAll<MServerBlocklistAccountServer>(query)
.then(({ rows, count }) => {
return { total: count, data: rows }
})
}
2019-08-20 12:05:31 -05:00
toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
return {
byAccount: this.ByAccount.toFormattedJSON(),
blockedServer: this.BlockedServer.toFormattedJSON(),
createdAt: this.createdAt
}
}
}