aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/blocklist.ts
blob: dee12b108cdc8f7a441f056770a9f16f9b7df30f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import express from 'express'
import { handleToNameAndHost } from '@server/helpers/actors'
import { logger } from '@server/helpers/logger'
import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
import { getServerActor } from '@server/models/application/application'
import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
import { MActorAccountId, MUserAccountId } from '@server/types/models'
import { BlockStatus } from '@shared/models'
import { apiRateLimiter, asyncMiddleware, blocklistStatusValidator, optionalAuthenticate } from '../../middlewares'

const blocklistRouter = express.Router()

blocklistRouter.use(apiRateLimiter)

blocklistRouter.get('/status',
  optionalAuthenticate,
  blocklistStatusValidator,
  asyncMiddleware(getBlocklistStatus)
)

// ---------------------------------------------------------------------------

export {
  blocklistRouter
}

// ---------------------------------------------------------------------------

async function getBlocklistStatus (req: express.Request, res: express.Response) {
  const hosts = req.query.hosts as string[]
  const accounts = req.query.accounts as string[]
  const user = res.locals.oauth?.token.User

  const serverActor = await getServerActor()

  const byAccountIds = [ serverActor.Account.id ]
  if (user) byAccountIds.push(user.Account.id)

  const status: BlockStatus = {
    accounts: {},
    hosts: {}
  }

  const baseOptions = {
    byAccountIds,
    user,
    serverActor,
    status
  }

  await Promise.all([
    populateServerBlocklistStatus({ ...baseOptions, hosts }),
    populateAccountBlocklistStatus({ ...baseOptions, accounts })
  ])

  return res.json(status)
}

async function populateServerBlocklistStatus (options: {
  byAccountIds: number[]
  user?: MUserAccountId
  serverActor: MActorAccountId
  hosts: string[]
  status: BlockStatus
}) {
  const { byAccountIds, user, serverActor, hosts, status } = options

  if (!hosts || hosts.length === 0) return

  const serverBlocklistStatus = await ServerBlocklistModel.getBlockStatus(byAccountIds, hosts)

  logger.debug('Got server blocklist status.', { serverBlocklistStatus, byAccountIds, hosts })

  for (const host of hosts) {
    const block = serverBlocklistStatus.find(b => b.host === host)

    status.hosts[host] = getStatus(block, serverActor, user)
  }
}

async function populateAccountBlocklistStatus (options: {
  byAccountIds: number[]
  user?: MUserAccountId
  serverActor: MActorAccountId
  accounts: string[]
  status: BlockStatus
}) {
  const { byAccountIds, user, serverActor, accounts, status } = options

  if (!accounts || accounts.length === 0) return

  const accountBlocklistStatus = await AccountBlocklistModel.getBlockStatus(byAccountIds, accounts)

  logger.debug('Got account blocklist status.', { accountBlocklistStatus, byAccountIds, accounts })

  for (const account of accounts) {
    const sanitizedHandle = handleToNameAndHost(account)

    const block = accountBlocklistStatus.find(b => b.name === sanitizedHandle.name && b.host === sanitizedHandle.host)

    status.accounts[sanitizedHandle.handle] = getStatus(block, serverActor, user)
  }
}

function getStatus (block: { accountId: number }, serverActor: MActorAccountId, user?: MUserAccountId) {
  return {
    blockedByServer: !!(block && block.accountId === serverActor.Account.id),
    blockedByUser: !!(block && user && block.accountId === user.Account.id)
  }
}