]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-video-miniature/abstract-video-list.ts
Fix scroll check in infinite scroller directive
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / abstract-video-list.ts
index e18002b74199d637b96bb5890f39edeb55f7da5f..c55e85afefc8e97c4f7f7b99cc74691dfa0d7e2b 100644 (file)
@@ -14,9 +14,8 @@ import {
 } from '@app/core'
 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
 import { GlobalIconName } from '@app/shared/shared-icons'
-import { I18n } from '@ngx-translate/i18n-polyfill'
-import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date'
-import { ServerConfig, VideoSortField } from '@shared/models'
+import { isLastMonth, isLastWeek, isThisMonth, isToday, isYesterday } from '@shared/core-utils/miscs/date'
+import { ServerConfig, UserRight, VideoFilter, VideoSortField } from '@shared/models'
 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
 import { Syndication, Video } from '../shared-main'
 import { MiniatureDisplayOptions, OwnerDisplayType } from './video-miniature.component'
@@ -25,9 +24,10 @@ enum GroupDate {
   UNKNOWN = 0,
   TODAY = 1,
   YESTERDAY = 2,
-  LAST_WEEK = 3,
-  LAST_MONTH = 4,
-  OLDER = 5
+  THIS_WEEK = 3,
+  THIS_MONTH = 4,
+  LAST_MONTH = 5,
+  OLDER = 6
 }
 
 @Directive()
@@ -70,9 +70,12 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
   }
 
   actions: {
-    routerLink: string
     iconName: GlobalIconName
     label: string
+    justIcon?: boolean
+    routerLink?: string
+    click?: Function
+    clipboard?: string
   }[] = []
 
   onDataSubject = new Subject<any[]>()
@@ -89,7 +92,6 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
   protected abstract screenService: ScreenService
   protected abstract storageService: LocalStorageService
   protected abstract router: Router
-  protected abstract i18n: I18n
   abstract titlePage: string
 
   private resizeSubscription: Subscription
@@ -111,11 +113,12 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
 
     this.groupedDateLabels = {
       [GroupDate.UNKNOWN]: null,
-      [GroupDate.TODAY]: this.i18n('Today'),
-      [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
-      [GroupDate.LAST_WEEK]: this.i18n('Last week'),
-      [GroupDate.LAST_MONTH]: this.i18n('Last month'),
-      [GroupDate.OLDER]: this.i18n('Older')
+      [GroupDate.TODAY]: $localize`Today`,
+      [GroupDate.YESTERDAY]: $localize`Yesterday`,
+      [GroupDate.THIS_WEEK]: $localize`This week`,
+      [GroupDate.THIS_MONTH]: $localize`This month`,
+      [GroupDate.LAST_MONTH]: $localize`Last month`,
+      [GroupDate.OLDER]: $localize`Older`
     }
 
     // Subscribe to route changes
@@ -192,7 +195,7 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
       },
 
       error => {
-        const message = this.i18n('Cannot load more videos. Try again later.')
+        const message = $localize`Cannot load more videos. Try again later.`
 
         console.error(message, { error })
         this.notifier.error(message)
@@ -205,10 +208,6 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
     this.loadMoreVideos(true)
   }
 
-  toggleModerationDisplay () {
-    throw new Error('toggleModerationDisplay is not implemented')
-  }
-
   removeVideoFromArray (video: Video) {
     this.videos = this.videos.filter(v => v.id !== video.id)
   }
@@ -216,46 +215,48 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
   buildGroupedDateLabels () {
     let currentGroupedDate: GroupDate = GroupDate.UNKNOWN
 
-    for (const video of this.videos) {
-      const publishedDate = video.publishedAt
-
-      if (currentGroupedDate <= GroupDate.TODAY && isToday(publishedDate)) {
-        if (currentGroupedDate === GroupDate.TODAY) continue
-
-        currentGroupedDate = GroupDate.TODAY
-        this.groupedDates[ video.id ] = currentGroupedDate
-        continue
-      }
-
-      if (currentGroupedDate <= GroupDate.YESTERDAY && isYesterday(publishedDate)) {
-        if (currentGroupedDate === GroupDate.YESTERDAY) continue
-
-        currentGroupedDate = GroupDate.YESTERDAY
-        this.groupedDates[ video.id ] = currentGroupedDate
-        continue
+    const periods = [
+      {
+        value: GroupDate.TODAY,
+        validator: (d: Date) => isToday(d)
+      },
+      {
+        value: GroupDate.YESTERDAY,
+        validator: (d: Date) => isYesterday(d)
+      },
+      {
+        value: GroupDate.THIS_WEEK,
+        validator: (d: Date) => isLastWeek(d)
+      },
+      {
+        value: GroupDate.THIS_MONTH,
+        validator: (d: Date) => isThisMonth(d)
+      },
+      {
+        value: GroupDate.LAST_MONTH,
+        validator: (d: Date) => isLastMonth(d)
+      },
+      {
+        value: GroupDate.OLDER,
+        validator: () => true
       }
+    ]
 
-      if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
-        if (currentGroupedDate === GroupDate.LAST_WEEK) continue
-
-        currentGroupedDate = GroupDate.LAST_WEEK
-        this.groupedDates[ video.id ] = currentGroupedDate
-        continue
-      }
+    for (const video of this.videos) {
+      const publishedDate = video.publishedAt
 
-      if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
-        if (currentGroupedDate === GroupDate.LAST_MONTH) continue
+      for (let i = 0; i < periods.length; i++) {
+        const period = periods[i]
 
-        currentGroupedDate = GroupDate.LAST_MONTH
-        this.groupedDates[ video.id ] = currentGroupedDate
-        continue
-      }
+        if (currentGroupedDate <= period.value && period.validator(publishedDate)) {
 
-      if (currentGroupedDate <= GroupDate.OLDER) {
-        if (currentGroupedDate === GroupDate.OLDER) continue
+          if (currentGroupedDate !== period.value) {
+            currentGroupedDate = period.value
+            this.groupedDates[ video.id ] = currentGroupedDate
+          }
 
-        currentGroupedDate = GroupDate.OLDER
-        this.groupedDates[ video.id ] = currentGroupedDate
+          break
+        }
       }
     }
   }
@@ -266,6 +267,10 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
     return this.groupedDateLabels[this.groupedDates[video.id]]
   }
 
+  toggleModerationDisplay () {
+    throw new Error('toggleModerationDisplay is not implemented')
+  }
+
   // On videos hook for children that want to do something
   protected onMoreVideos () { /* empty */ }
 
@@ -275,6 +280,28 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
     this.angularState = routeParams[ 'a-state' ]
   }
 
+  protected buildLocalFilter (existing: VideoFilter, base: VideoFilter) {
+    if (base === 'local') {
+      return existing === 'local'
+        ? 'all-local' as 'all-local'
+        : 'local' as 'local'
+    }
+
+    return existing === 'all'
+      ? null
+      : 'all'
+  }
+
+  protected enableAllFilterIfPossible () {
+    if (!this.authService.isLoggedIn()) return
+
+    this.authService.userInformationLoaded
+      .subscribe(() => {
+        const user = this.authService.getUser()
+        this.displayModerationBlock = user.hasRight(UserRight.SEE_ALL_VIDEOS)
+      })
+  }
+
   private calcPageSizes () {
     if (this.screenService.isInMobileView()) {
       this.pagination.itemsPerPage = 5