aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+remote-interaction/remote-interaction.component.ts
blob: 6ddf5b58da6c45ab47acef765c35102697745b29 (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
import { forkJoin } from 'rxjs'
import { Component, OnInit } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { VideoChannel } from '@app/shared/shared-main'
import { SearchService } from '@app/shared/shared-search'

@Component({
  selector: 'my-remote-interaction',
  templateUrl: './remote-interaction.component.html',
  styleUrls: ['./remote-interaction.component.scss']
})
export class RemoteInteractionComponent implements OnInit {
  error = ''

  constructor (
    private route: ActivatedRoute,
    private router: Router,
    private search: SearchService
  ) { }

  ngOnInit () {
    const uri = this.route.snapshot.queryParams['uri']

    if (!uri) {
      this.error = $localize`URL parameter is missing in URL parameters`
      return
    }

    this.loadUrl(uri)
  }

  private loadUrl (uri: string) {
    forkJoin([
      this.search.searchVideos({ search: uri }),
      this.search.searchVideoChannels({ search: uri })
    ]).subscribe(([ videoResult, channelResult ]) => {
      let redirectUrl: string

      if (videoResult.data.length !== 0) {
        const video = videoResult.data[0]

        redirectUrl = '/w/' + video.uuid
      } else if (channelResult.data.length !== 0) {
        const channel = new VideoChannel(channelResult.data[0])

        redirectUrl = '/c/' + channel.nameWithHost
      } else {
        this.error = $localize`Cannot access to the remote resource`
        return
      }

      this.router.navigateByUrl(redirectUrl)
    })
  }

}