]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/helpers/utils.ts
Fix button icon in admin plugins
[github/Chocobozzz/PeerTube.git] / client / src / app / helpers / utils.ts
index b4e26d792e07ea3c17acb5d6ea1f459be814137c..d6ac5b9b4a4c4de8561e15c226a9d429e6254c3a 100644 (file)
@@ -1,10 +1,11 @@
+import { map } from 'rxjs/operators'
+import { SelectChannelItem } from 'src/types/select-options-item.model'
 import { DatePipe } from '@angular/common'
 import { HttpErrorResponse } from '@angular/common/http'
 import { Notifier } from '@app/core'
-import { SelectChannelItem } from '@app/shared/shared-forms'
+import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
 import { environment } from '../../environments/environment'
 import { AuthService } from '../core/auth'
-import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
 
 // Thanks: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
 function getParameterByName (name: string, url: string) {
@@ -20,31 +21,28 @@ function getParameterByName (name: string, url: string) {
   return decodeURIComponent(results[2].replace(/\+/g, ' '))
 }
 
-function populateAsyncUserVideoChannels (
-  authService: AuthService,
-  channel: SelectChannelItem[]
-) {
-  return new Promise<void>(res => {
-    authService.userInformationLoaded
-      .subscribe(
-        () => {
-          const user = authService.getUser()
-          if (!user) return
-
-          const videoChannels = user.videoChannels
-          if (Array.isArray(videoChannels) === false) return
-
-          videoChannels.forEach(c => channel.push({
-            id: c.id,
-            label: c.displayName,
-            support: c.support,
-            avatarPath: c.avatar?.path
-          }))
-
-          return res()
-        }
-      )
-  })
+function listUserChannels (authService: AuthService) {
+  return authService.userInformationLoaded
+    .pipe(map(() => {
+      const user = authService.getUser()
+      if (!user) return undefined
+
+      const videoChannels = user.videoChannels
+      if (Array.isArray(videoChannels) === false) return undefined
+
+      return videoChannels
+        .sort((a, b) => {
+          if (a.updatedAt < b.updatedAt) return 1
+          if (a.updatedAt > b.updatedAt) return -1
+          return 0
+        })
+        .map(c => ({
+          id: c.id,
+          label: c.displayName,
+          support: c.support,
+          avatarPath: c.avatar?.path
+        }) as SelectChannelItem)
+    }))
 }
 
 function getAbsoluteAPIUrl () {
@@ -175,8 +173,8 @@ function isXPercentInViewport (el: HTMLElement, percentVisible: number) {
   )
 }
 
-function uploadErrorHandler (parameters: {
-  err: HttpErrorResponse
+function genericUploadErrorHandler (parameters: {
+  err: Pick<HttpErrorResponse, 'message' | 'status' | 'headers'>
   name: string
   notifier: Notifier
   sticky?: boolean
@@ -188,6 +186,9 @@ function uploadErrorHandler (parameters: {
   if (err instanceof ErrorEvent) { // network error
     message = $localize`The connection was interrupted`
     notifier.error(message, title, null, sticky)
+  } else if (err.status === HttpStatusCode.INTERNAL_SERVER_ERROR_500) {
+    message = $localize`The server encountered an error`
+    notifier.error(message, title, null, sticky)
   } else if (err.status === HttpStatusCode.REQUEST_TIMEOUT_408) {
     message = $localize`Your ${name} file couldn't be transferred before the set timeout (usually 10min)`
     notifier.error(message, title, null, sticky)
@@ -207,7 +208,6 @@ export {
   durationToString,
   lineFeedToHtml,
   getParameterByName,
-  populateAsyncUserVideoChannels,
   getAbsoluteAPIUrl,
   dateToHuman,
   immutableAssign,
@@ -218,5 +218,6 @@ export {
   scrollToTop,
   isInViewport,
   isXPercentInViewport,
-  uploadErrorHandler
+  listUserChannels,
+  genericUploadErrorHandler
 }