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

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

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

  listBlacklist (pagination: RestPagination, sort: SortMeta, type?: VideoBlacklistType): Observable<ResultList<VideoBlacklist>> {
    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)

    if (type) {
      params = params.set('type', type.toString())
    }

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

  getAutoBlacklistedAsVideoList (videoPagination: ComponentPaginationLight): Observable<ResultList<Video>> {
    const pagination = this.restService.componentPaginationToRestPagination(videoPagination)

    // prioritize first created since waiting longest
    const AUTO_BLACKLIST_SORT = 'createdAt'

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

    params = params.set('type', VideoBlacklistType.AUTO_BEFORE_PUBLISHED.toString())

    return this.authHttp.get<ResultList<VideoBlacklist>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params })
              .pipe(
                map(res => {
                  return {
                    total: res.total,
                    data: res.data.map(videoBlacklist => new Video(videoBlacklist.video))
                  }
                }),
                catchError(res => this.restExtractor.handleError(res))
              )
  }

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

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

  blacklistVideo (videoId: number, reason: string, unfederate: boolean) {
    const body = {
      unfederate,
      reason
    }

    return this.authHttp.post(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist', body)
               .pipe(
                 map(this.restExtractor.extractDataBool),
                 catchError(res => this.restExtractor.handleError(res))
               )
  }
}