]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/overview/comments/video-comment-list.component.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / overview / comments / video-comment-list.component.ts
1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { AuthService, ConfirmService, MarkdownService, Notifier, RestPagination, RestTable } from '@app/core'
5 import { AdvancedInputFilter } from '@app/shared/shared-forms'
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 import { prepareIcu } from '@app/helpers'
11
12 @Component({
13 selector: 'my-video-comment-list',
14 templateUrl: './video-comment-list.component.html',
15 styleUrls: [ '../../../shared/shared-moderation/moderation.scss', './video-comment-list.component.scss' ]
16 })
17 export class VideoCommentListComponent extends RestTable implements OnInit {
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
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
43 selectedComments: VideoCommentAdmin[] = []
44 bulkCommentActions: DropdownAction<VideoCommentAdmin[]>[] = []
45
46 inputFilters: AdvancedInputFilter[] = [
47 {
48 title: $localize`Advanced filters`,
49 children: [
50 {
51 value: 'local:true',
52 label: $localize`Local comments`
53 },
54 {
55 value: 'local:false',
56 label: $localize`Remote comments`
57 },
58 {
59 value: 'localVideo:true',
60 label: $localize`Comments on local videos`
61 }
62 ]
63 }
64 ]
65
66 get authUser () {
67 return this.auth.getUser()
68 }
69
70 constructor (
71 protected router: Router,
72 protected route: ActivatedRoute,
73 private auth: AuthService,
74 private notifier: Notifier,
75 private confirmService: ConfirmService,
76 private videoCommentService: VideoCommentService,
77 private markdownRenderer: MarkdownService,
78 private bulkService: BulkService
79 ) {
80 super()
81
82 this.videoCommentActions = [
83 [
84 {
85 label: $localize`Delete this comment`,
86 handler: comment => this.deleteComment(comment),
87 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
88 },
89
90 {
91 label: $localize`Delete all comments of this account`,
92 description: $localize`Comments are deleted after a few minutes`,
93 handler: comment => this.deleteUserComments(comment),
94 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
95 }
96 ]
97 ]
98 }
99
100 ngOnInit () {
101 this.initialize()
102
103 this.bulkCommentActions = [
104 {
105 label: $localize`Delete`,
106 handler: comments => this.removeComments(comments),
107 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT),
108 iconName: 'delete'
109 }
110 ]
111 }
112
113 getIdentifier () {
114 return 'VideoCommentListComponent'
115 }
116
117 toHtml (text: string) {
118 return this.markdownRenderer.textMarkdownToHTML({ markdown: text, withHtml: true, withEmoji: true })
119 }
120
121 isInSelectionMode () {
122 return this.selectedComments.length !== 0
123 }
124
125 reloadData () {
126 this.videoCommentService.getAdminVideoComments({
127 pagination: this.pagination,
128 sort: this.sort,
129 search: this.search
130 }).subscribe({
131 next: async resultList => {
132 this.totalRecords = resultList.total
133
134 this.comments = []
135
136 for (const c of resultList.data) {
137 this.comments.push(
138 new VideoCommentAdmin(c, await this.toHtml(c.text))
139 )
140 }
141 },
142
143 error: err => this.notifier.error(err.message)
144 })
145 }
146
147 private removeComments (comments: VideoCommentAdmin[]) {
148 const commentArgs = comments.map(c => ({ videoId: c.video.id, commentId: c.id }))
149
150 this.videoCommentService.deleteVideoComments(commentArgs)
151 .subscribe({
152 next: () => {
153 this.notifier.success(
154 prepareIcu($localize`{count, plural, =1 {1 comment deleted.} other {{count} comments deleted.}}`)(
155 { count: commentArgs.length },
156 $localize`${commentArgs.length} comment(s) deleted.`
157 )
158 )
159
160 this.reloadData()
161 },
162
163 error: err => this.notifier.error(err.message),
164
165 complete: () => this.selectedComments = []
166 })
167 }
168
169 private deleteComment (comment: VideoCommentAdmin) {
170 this.videoCommentService.deleteVideoComment(comment.video.id, comment.id)
171 .subscribe({
172 next: () => this.reloadData(),
173
174 error: err => this.notifier.error(err.message)
175 })
176 }
177
178 private async deleteUserComments (comment: VideoCommentAdmin) {
179 const message = $localize`Do you really want to delete all comments of ${comment.by}?`
180 const res = await this.confirmService.confirm(message, $localize`Delete`)
181 if (res === false) return
182
183 const options = {
184 accountName: comment.by,
185 scope: 'instance' as 'instance'
186 }
187
188 this.bulkService.removeCommentsOf(options)
189 .subscribe({
190 next: () => {
191 this.notifier.success($localize`Comments of ${options.accountName} will be deleted in a few minutes`)
192 },
193
194 error: err => this.notifier.error(err.message)
195 })
196 }
197 }