]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+admin/moderation/video-abuse-list/video-abuse-list.component.ts
Fix rowsPerPage change, add filter clear button, update video-abuse-list search query...
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / moderation / video-abuse-list / video-abuse-list.component.ts
index cc5014ae8596bbd8b211320608d5cf16ecb864a6..f54e3dccd4578cfb5a9f12b4c5b06228503c55ef 100644 (file)
@@ -1,4 +1,4 @@
-import { Component, OnInit, ViewChild } from '@angular/core'
+import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core'
 import { Account } from '@app/shared/account/account.model'
 import { Notifier } from '@app/core'
 import { SortMeta } from 'primeng/api'
@@ -16,19 +16,33 @@ import { getAbsoluteAPIUrl } from '@app/shared/misc/utils'
 import { DomSanitizer } from '@angular/platform-browser'
 import { BlocklistService } from '@app/shared/blocklist'
 import { VideoService } from '@app/shared/video/video.service'
+import { ActivatedRoute, Params, Router } from '@angular/router'
+import { filter } from 'rxjs/operators'
+
+export type ProcessedVideoAbuse = VideoAbuse & {
+  moderationCommentHtml?: string,
+  reasonHtml?: string
+  embedHtml?: string
+  updatedAt?: Date
+  // override bare server-side definitions with rich client-side definitions
+  reporterAccount: Account
+  video: VideoAbuse['video'] & {
+    channel: VideoAbuse['video']['channel'] & {
+      ownerAccount: Account
+    }
+  }
+}
 
 @Component({
   selector: 'my-video-abuse-list',
   templateUrl: './video-abuse-list.component.html',
   styleUrls: [ '../moderation.component.scss', './video-abuse-list.component.scss' ]
 })
-export class VideoAbuseListComponent extends RestTable implements OnInit {
+export class VideoAbuseListComponent extends RestTable implements OnInit, AfterViewInit {
   @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent
 
-  videoAbuses: (VideoAbuse & { moderationCommentHtml?: string, reasonHtml?: string })[] = []
+  videoAbuses: ProcessedVideoAbuse[] = []
   totalRecords = 0
-  rowsPerPageOptions = [ 20, 50, 100 ]
-  rowsPerPage = this.rowsPerPageOptions[0]
   sort: SortMeta = { field: 'createdAt', order: 1 }
   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
 
@@ -43,7 +57,9 @@ export class VideoAbuseListComponent extends RestTable implements OnInit {
     private confirmService: ConfirmService,
     private i18n: I18n,
     private markdownRenderer: MarkdownService,
-    private sanitizer: DomSanitizer
+    private sanitizer: DomSanitizer,
+    private route: ActivatedRoute,
+    private router: Router
   ) {
     super()
 
@@ -185,6 +201,18 @@ export class VideoAbuseListComponent extends RestTable implements OnInit {
 
   ngOnInit () {
     this.initialize()
+
+    this.route.queryParams
+      .pipe(filter(params => params.search !== undefined && params.search !== null))
+      .subscribe(params => {
+        this.search = params.search
+        this.setTableFilter(params.search)
+        this.loadData()
+      })
+  }
+
+  ngAfterViewInit () {
+    if (this.search) this.setTableFilter(this.search)
   }
 
   getIdentifier () {
@@ -199,10 +227,25 @@ export class VideoAbuseListComponent extends RestTable implements OnInit {
     this.loadData()
   }
 
-  createByString (account: Account) {
-    return Account.CREATE_BY_STRING(account.name, account.host)
+  /* Table filter functions */
+  onAbuseSearch (event: Event) {
+    this.onSearch(event)
+    this.setQueryParams((event.target as HTMLInputElement).value)
+  }
+
+  setQueryParams (search: string) {
+    const queryParams: Params = {}
+    if (search) Object.assign(queryParams, { search })
+    this.router.navigate([ '/admin/moderation/video-abuses/list' ], { queryParams })
   }
 
+  resetTableFilter () {
+    this.setTableFilter('')
+    this.setQueryParams('')
+    this.resetSearch()
+  }
+  /* END Table filter functions */
+
   isVideoAbuseAccepted (videoAbuse: VideoAbuse) {
     return videoAbuse.state.id === VideoAbuseState.ACCEPTED
   }
@@ -216,7 +259,7 @@ export class VideoAbuseListComponent extends RestTable implements OnInit {
   }
 
   getVideoEmbed (videoAbuse: VideoAbuse) {
-    const absoluteAPIUrl = 'http://localhost:9000' || getAbsoluteAPIUrl()
+    const absoluteAPIUrl = getAbsoluteAPIUrl()
     const embedUrl = buildVideoLink({
       baseUrl: absoluteAPIUrl + '/videos/embed/' + videoAbuse.video.uuid,
       warningTitle: false
@@ -253,26 +296,33 @@ export class VideoAbuseListComponent extends RestTable implements OnInit {
   }
 
   protected loadData () {
-    return this.videoAbuseService.getVideoAbuses(this.pagination, this.sort)
-               .subscribe(
-                 async resultList => {
-                   this.totalRecords = resultList.total
-
-                   this.videoAbuses = resultList.data
-
-                   for (const abuse of this.videoAbuses) {
-                     Object.assign(abuse, {
-                       reasonHtml: await this.toHtml(abuse.reason),
-                       moderationCommentHtml: await this.toHtml(abuse.moderationComment),
-                       embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse)),
-                       reporterAccount: new Account(abuse.reporterAccount)
-                     })
-                   }
-
-                 },
-
-                 err => this.notifier.error(err.message)
-               )
+    return this.videoAbuseService.getVideoAbuses({
+      pagination: this.pagination,
+      sort: this.sort,
+      search: this.search
+    }).subscribe(
+        async resultList => {
+          this.totalRecords = resultList.total
+          this.videoAbuses = []
+
+          for (const abuse of resultList.data) {
+            Object.assign(abuse, {
+              reasonHtml: await this.toHtml(abuse.reason),
+              moderationCommentHtml: await this.toHtml(abuse.moderationComment),
+              embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse)),
+              reporterAccount: new Account(abuse.reporterAccount)
+            })
+
+            if (abuse.video.channel?.ownerAccount) abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
+            if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt
+
+            this.videoAbuses.push(abuse as ProcessedVideoAbuse)
+          }
+
+        },
+
+        err => this.notifier.error(err.message)
+      )
   }
 
   private toHtml (text: string) {