aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-moderation/blocklist.service.ts
blob: db2a8c5846ea920cfda02045344f5c6f8cd6aa3b (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { SortMeta } from 'primeng/api'
import { catchError, map } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor, RestPagination, RestService } from '@app/core'
import { AccountBlock as AccountBlockServer, ResultList, ServerBlock } from '@shared/models'
import { environment } from '../../../environments/environment'
import { Account } from '../shared-main'
import { AccountBlock } from './account-block.model'

export enum BlocklistComponentType { Account, Instance }

@Injectable()
export class BlocklistService {
  static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
  static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'

  constructor (
    private authHttp: HttpClient,
    private restExtractor: RestExtractor,
    private restService: RestService
  ) { }

  /** ********************* User -> Account blocklist ***********************/

  getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
    const { pagination, sort, search } = options

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)

    if (search) params = params.append('search', search)

    return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
               .pipe(
                 map(res => this.restExtractor.convertResultListDateToHuman(res)),
                 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  blockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
    const body = { accountName: account.nameWithHost }

    return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
    const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost

    return this.authHttp.delete(path)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  /** ********************* User -> Server blocklist ***********************/

  getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
    const { pagination, sort, search } = options

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)

    if (search) params = params.append('search', search)

    return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
               .pipe(
                 map(res => this.restExtractor.convertResultListDateToHuman(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  blockServerByUser (host: string) {
    const body = { host }

    return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  unblockServerByUser (host: string) {
    const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host

    return this.authHttp.delete(path)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  /** ********************* Instance -> Account blocklist ***********************/

  getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
    const { pagination, sort, search } = options

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)

    if (search) params = params.append('search', search)

    return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
               .pipe(
                 map(res => this.restExtractor.convertResultListDateToHuman(res)),
                 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  blockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
    const body = { accountName: account.nameWithHost }

    return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', body)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
    const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost

    return this.authHttp.delete(path)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  /** ********************* Instance -> Server blocklist ***********************/

  getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
    const { pagination, sort, search } = options

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)

    if (search) params = params.append('search', search)

    return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
               .pipe(
                 map(res => this.restExtractor.convertResultListDateToHuman(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  blockServerByInstance (host: string) {
    const body = { host }

    return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  unblockServerByInstance (host: string) {
    const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host

    return this.authHttp.delete(path)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  private formatAccountBlock (accountBlock: AccountBlockServer) {
    return new AccountBlock(accountBlock)
  }
}