First implem global search
[oweals/peertube.git] / client / src / app / search / channel-lazy-load.resolver.ts
1 import { map } from 'rxjs/operators'
2 import { Injectable } from '@angular/core'
3 import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router'
4 import { SearchService } from './search.service'
5 import { RedirectService } from '@app/core'
6
7 @Injectable()
8 export class ChannelLazyLoadResolver implements Resolve<any> {
9   constructor (
10     private router: Router,
11     private searchService: SearchService,
12     private redirectService: RedirectService
13   ) { }
14
15   resolve (route: ActivatedRouteSnapshot) {
16     const url = route.params.url
17     const externalRedirect = route.params.externalRedirect
18     const fromPath = route.params.fromPath
19
20     if (!url) {
21       console.error('Could not find url param.', { params: route.params })
22       return this.router.navigateByUrl('/404')
23     }
24
25     if (externalRedirect === 'true') {
26       window.open(url)
27       this.router.navigateByUrl(fromPath)
28       return
29     }
30
31     return this.searchService.searchVideoChannels({ search: url })
32       .pipe(
33         map(result => {
34           if (result.data.length !== 1) {
35             console.error('Cannot find result for this URL')
36             return this.router.navigateByUrl('/404')
37           }
38
39           const channel = result.data[0]
40
41           return this.router.navigateByUrl('/video-channels/' + channel.nameWithHost)
42         })
43       )
44   }
45 }