]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/moderation/video-comment-list/video-comment-list.component.ts
Refactor rest table search filter
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / moderation / video-comment-list / video-comment-list.component.ts
1 import { SortMeta } from 'primeng/api'
2 import { filter } from 'rxjs/operators'
3 import { AfterViewInit, Component, OnInit } from '@angular/core'
4 import { ActivatedRoute, Params, Router } from '@angular/router'
5 import { AuthService, ConfirmService, MarkdownService, Notifier, RestPagination, RestTable } from '@app/core'
6 import { DropdownAction } from '@app/shared/shared-main'
7 import { BulkService } from '@app/shared/shared-moderation'
8 import { VideoCommentAdmin, VideoCommentService } from '@app/shared/shared-video-comment'
9 import { FeedFormat, UserRight } from '@shared/models'
10
11 @Component({
12 selector: 'my-video-comment-list',
13 templateUrl: './video-comment-list.component.html',
14 styleUrls: [ '../../../shared/shared-moderation/moderation.scss', './video-comment-list.component.scss' ]
15 })
16 export class VideoCommentListComponent extends RestTable implements OnInit, AfterViewInit {
17 baseRoute = '/admin/moderation/video-comments/list'
18
19 comments: VideoCommentAdmin[]
20 totalRecords = 0
21 sort: SortMeta = { field: 'createdAt', order: -1 }
22 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
23
24 videoCommentActions: DropdownAction<VideoCommentAdmin>[][] = []
25
26 syndicationItems = [
27 {
28 format: FeedFormat.RSS,
29 label: 'media rss 2.0',
30 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
31 },
32 {
33 format: FeedFormat.ATOM,
34 label: 'atom 1.0',
35 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
36 },
37 {
38 format: FeedFormat.JSON,
39 label: 'json 1.0',
40 url: VideoCommentService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
41 }
42 ]
43
44 get authUser () {
45 return this.auth.getUser()
46 }
47
48 constructor (
49 protected router: Router,
50 protected route: ActivatedRoute,
51 private auth: AuthService,
52 private notifier: Notifier,
53 private confirmService: ConfirmService,
54 private videoCommentService: VideoCommentService,
55 private markdownRenderer: MarkdownService,
56 private bulkService: BulkService
57 ) {
58 super()
59
60 this.videoCommentActions = [
61 [
62 {
63 label: $localize`Delete this comment`,
64 handler: comment => this.deleteComment(comment),
65 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
66 },
67
68 {
69 label: $localize`Delete all comments of this account`,
70 description: $localize`Comments are deleted after a few minutes`,
71 handler: comment => this.deleteUserComments(comment),
72 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
73 }
74 ]
75 ]
76 }
77
78 ngOnInit () {
79 this.initialize()
80 this.listenToSearchChange()
81 }
82
83 ngAfterViewInit () {
84 if (this.search) this.setTableFilter(this.search)
85 }
86
87 getIdentifier () {
88 return 'VideoCommentListComponent'
89 }
90
91 toHtml (text: string) {
92 return this.markdownRenderer.textMarkdownToHTML(text, true, true)
93 }
94
95 protected loadData () {
96 this.videoCommentService.getAdminVideoComments({
97 pagination: this.pagination,
98 sort: this.sort,
99 search: this.search
100 }).subscribe(
101 async resultList => {
102 this.totalRecords = resultList.total
103
104 this.comments = []
105
106 for (const c of resultList.data) {
107 this.comments.push(
108 new VideoCommentAdmin(c, await this.toHtml(c.text))
109 )
110 }
111 },
112
113 err => this.notifier.error(err.message)
114 )
115 }
116
117 private deleteComment (comment: VideoCommentAdmin) {
118 this.videoCommentService.deleteVideoComment(comment.video.id, comment.id)
119 .subscribe(
120 () => this.loadData(),
121
122 err => this.notifier.error(err.message)
123 )
124 }
125
126 private async deleteUserComments (comment: VideoCommentAdmin) {
127 const message = $localize`Do you really want to delete all comments of ${comment.by}?`
128 const res = await this.confirmService.confirm(message, $localize`Delete`)
129 if (res === false) return
130
131 const options = {
132 accountName: comment.by,
133 scope: 'instance' as 'instance'
134 }
135
136 this.bulkService.removeCommentsOf(options)
137 .subscribe(
138 () => {
139 this.notifier.success($localize`Comments of ${options.accountName} will be deleted in a few minutes`)
140 },
141
142 err => this.notifier.error(err.message)
143 )
144 }
145 }