Add scores to follows and remove bad ones
[oweals/peertube.git] / client / src / app / shared / misc / utils.ts
1 // Thanks: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
2
3 import { environment } from '../../../environments/environment'
4 import { AuthService } from '../../core/auth'
5
6 function getParameterByName (name: string, url: string) {
7   if (!url) url = window.location.href
8   name = name.replace(/[\[\]]/g, '\\$&')
9
10   const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
11   const results = regex.exec(url)
12
13   if (!results) return null
14   if (!results[2]) return ''
15
16   return decodeURIComponent(results[2].replace(/\+/g, ' '))
17 }
18
19 function viewportHeight () {
20   return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
21 }
22
23 function populateAsyncUserVideoChannels (authService: AuthService, channel: any[]) {
24   return new Promise(res => {
25     authService.userInformationLoaded
26       .subscribe(
27         () => {
28           const user = authService.getUser()
29           if (!user) return
30
31           const videoChannels = user.videoChannels
32           if (Array.isArray(videoChannels) === false) return
33
34           videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName }))
35
36           return res()
37         }
38       )
39   })
40 }
41
42 function getAbsoluteAPIUrl () {
43   let absoluteAPIUrl = environment.apiUrl
44   if (!absoluteAPIUrl) {
45     // The API is on the same domain
46     absoluteAPIUrl = window.location.origin
47   }
48
49   return absoluteAPIUrl
50 }
51
52 export {
53   viewportHeight,
54   getParameterByName,
55   populateAsyncUserVideoChannels,
56   getAbsoluteAPIUrl
57 }