aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/users/blocklist-command.ts
blob: 14491a1aee596c744b8b88f8f21f538f7903ebcc (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { AccountBlock, HttpStatusCode, ResultList, ServerBlock } from '@shared/models'
import { AbstractCommand, OverrideCommandOptions } from '../shared'

type ListBlocklistOptions = OverrideCommandOptions & {
  start: number
  count: number
  sort: string // default -createdAt
}

export class BlocklistCommand extends AbstractCommand {

  listMyAccountBlocklist (options: ListBlocklistOptions) {
    const path = '/api/v1/users/me/blocklist/accounts'

    return this.listBlocklist<AccountBlock>(options, path)
  }

  listMyServerBlocklist (options: ListBlocklistOptions) {
    const path = '/api/v1/users/me/blocklist/servers'

    return this.listBlocklist<ServerBlock>(options, path)
  }

  listServerAccountBlocklist (options: ListBlocklistOptions) {
    const path = '/api/v1/server/blocklist/accounts'

    return this.listBlocklist<AccountBlock>(options, path)
  }

  listServerServerBlocklist (options: ListBlocklistOptions) {
    const path = '/api/v1/server/blocklist/servers'

    return this.listBlocklist<ServerBlock>(options, path)
  }

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

  addToMyBlocklist (options: OverrideCommandOptions & {
    account?: string
    server?: string
  }) {
    const { account, server } = options

    const path = account
      ? '/api/v1/users/me/blocklist/accounts'
      : '/api/v1/users/me/blocklist/servers'

    return this.postBodyRequest({
      ...options,

      path,
      fields: {
        accountName: account,
        host: server
      },
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }

  addToServerBlocklist (options: OverrideCommandOptions & {
    account?: string
    server?: string
  }) {
    const { account, server } = options

    const path = account
      ? '/api/v1/server/blocklist/accounts'
      : '/api/v1/server/blocklist/servers'

    return this.postBodyRequest({
      ...options,

      path,
      fields: {
        accountName: account,
        host: server
      },
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }

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

  removeFromMyBlocklist (options: OverrideCommandOptions & {
    account?: string
    server?: string
  }) {
    const { account, server } = options

    const path = account
      ? '/api/v1/users/me/blocklist/accounts/' + account
      : '/api/v1/users/me/blocklist/servers/' + server

    return this.deleteRequest({
      ...options,

      path,
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }

  removeFromServerBlocklist (options: OverrideCommandOptions & {
    account?: string
    server?: string
  }) {
    const { account, server } = options

    const path = account
      ? '/api/v1/server/blocklist/accounts/' + account
      : '/api/v1/server/blocklist/servers/' + server

    return this.deleteRequest({
      ...options,

      path,
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }

  private listBlocklist <T> (options: ListBlocklistOptions, path: string) {
    const { start, count, sort = '-createdAt' } = options

    return this.getRequestBody<ResultList<T>>({
      ...options,

      path,
      query: { start, count, sort },
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.OK_200
    })
  }

}