aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-moderation/video-block.service.ts
blob: 5dfb0d7d451d3574ab8bb034a877b176aadb8d8a (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
import { SortMeta } from 'primeng/api'
import { from as observableFrom, Observable } from 'rxjs'
import { catchError, concatMap, map, toArray } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor, RestPagination, RestService } from '@app/core'
import { ResultList, VideoBlacklist, VideoBlacklistType } from '@shared/models'
import { environment } from '../../../environments/environment'

@Injectable()
export class VideoBlockService {
  private static BASE_VIDEOS_URL = environment.apiUrl + '/api/v1/videos/'

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

  listBlocks (options: {
    pagination: RestPagination
    sort: SortMeta
    search?: string
    type?: VideoBlacklistType
  }): Observable<ResultList<VideoBlacklist>> {
    const { pagination, sort, search, type } = options

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

    if (search) {
      const filters = this.restService.parseQueryStringFilter(search, {
        type: {
          prefix: 'type:',
          handler: v => {
            if (v === 'manual') return VideoBlacklistType.MANUAL
            if (v === 'auto') return VideoBlacklistType.AUTO_BEFORE_PUBLISHED

            return undefined
          }
        }
      })

      params = this.restService.addObjectParams(params, filters)
    }
    if (type) params = params.append('type', type.toString())

    return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlockService.BASE_VIDEOS_URL + 'blacklist', { params })
               .pipe(
                 map(res => this.restExtractor.convertResultListDateToHuman(res)),
                 catchError(res => this.restExtractor.handleError(res))
               )
  }

  unblockVideo (videoIdArgs: number | number[]) {
    const videoIds = Array.isArray(videoIdArgs) ? videoIdArgs : [ videoIdArgs ]

    return observableFrom(videoIds)
      .pipe(
        concatMap(id => this.authHttp.delete(VideoBlockService.BASE_VIDEOS_URL + id + '/blacklist')),
        toArray(),
        catchError(err => this.restExtractor.handleError(err))
      )
  }

  blockVideo (options: {
    videoId: number
    reason?: string
    unfederate: boolean
  }[]) {
    return observableFrom(options)
      .pipe(
        concatMap(({ videoId, unfederate, reason }) => {
          const body = { unfederate, reason }

          return this.authHttp.post(VideoBlockService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
        }),
        toArray(),
        catchError(res => this.restExtractor.handleError(res))
      )
  }
}