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