1
0
Fork 0
peertube/server/lib/blocklist.ts
Chocobozzz 696d83fd13 Block comments from muted accounts/servers
Add better control for users of comments displayed on their videos:

 * Do not forward comments from muted remote accounts/servers (muted by the current server or by the video owner)
 * Do not list threads and hide replies (with their children) of accounts/servers muted by the video owner
 * Hide from RSS comments of muted accounts/servers by video owners

Use case:
  * Try to limit spam propagation in the federation
  * Add ability for users to automatically hide comments on their videos from undesirable accounts/servers (the comment section belongs to videomakers, so they choose what's posted there)
2020-05-29 09:32:20 +02:00

62 lines
2.2 KiB
TypeScript

import { sequelizeTypescript } from '@server/initializers/database'
import { getServerActor } from '@server/models/application/application'
import { MAccountBlocklist, MAccountId, MAccountServer, MServerBlocklist } from '@server/typings/models'
import { AccountBlocklistModel } from '../models/account/account-blocklist'
import { ServerBlocklistModel } from '../models/server/server-blocklist'
function addAccountInBlocklist (byAccountId: number, targetAccountId: number) {
return sequelizeTypescript.transaction(async t => {
return AccountBlocklistModel.upsert({
accountId: byAccountId,
targetAccountId: targetAccountId
}, { transaction: t })
})
}
function addServerInBlocklist (byAccountId: number, targetServerId: number) {
return sequelizeTypescript.transaction(async t => {
return ServerBlocklistModel.upsert({
accountId: byAccountId,
targetServerId
}, { transaction: t })
})
}
function removeAccountFromBlocklist (accountBlock: MAccountBlocklist) {
return sequelizeTypescript.transaction(async t => {
return accountBlock.destroy({ transaction: t })
})
}
function removeServerFromBlocklist (serverBlock: MServerBlocklist) {
return sequelizeTypescript.transaction(async t => {
return serverBlock.destroy({ transaction: t })
})
}
async function isBlockedByServerOrAccount (targetAccount: MAccountServer, userAccount?: MAccountId) {
const serverAccountId = (await getServerActor()).Account.id
const sourceAccounts = [ serverAccountId ]
if (userAccount) sourceAccounts.push(userAccount.id)
const accountMutedHash = await AccountBlocklistModel.isAccountMutedByMulti(sourceAccounts, targetAccount.id)
if (accountMutedHash[serverAccountId] || (userAccount && accountMutedHash[userAccount.id])) {
return true
}
const instanceMutedHash = await ServerBlocklistModel.isServerMutedByMulti(sourceAccounts, targetAccount.Actor.serverId)
if (instanceMutedHash[serverAccountId] || (userAccount && instanceMutedHash[userAccount.id])) {
return true
}
return false
}
export {
addAccountInBlocklist,
addServerInBlocklist,
removeAccountFromBlocklist,
removeServerFromBlocklist,
isBlockedByServerOrAccount
}