]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/overview/comments/video-comment-list.component.ts
Merge branch 'release/4.2.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 }
60 ]
61
62 get authUser () {
63 return this.auth.getUser()
64 }
65
66 constructor (
67 protected router: Router,
68 protected route: ActivatedRoute,
69 private auth: AuthService,
70 private notifier: Notifier,
71 private confirmService: ConfirmService,
72 private videoCommentService: VideoCommentService,
73 private markdownRenderer: MarkdownService,
74 private bulkService: BulkService
75 ) {
76 super()
77
78 this.videoCommentActions = [
79 [
80 {
81 label: $localize`Delete this comment`,
82 handler: comment => this.deleteComment(comment),
83 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
84 },
85
86 {
87 label: $localize`Delete all comments of this account`,
88 description: $localize`Comments are deleted after a few minutes`,
89 handler: comment => this.deleteUserComments(comment),
90 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
91 }
92 ]
93 ]
94 }
95
96 ngOnInit () {
97 this.initialize()
98
99 this.bulkCommentActions = [
100 {
101 label: $localize`Delete`,
102 handler: comments => this.removeComments(comments),
103 isDisplayed: () => this.authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT),
104 iconName: 'delete'
105 }
106 ]
107 }
108
109 getIdentifier () {
110 return 'VideoCommentListComponent'
111 }
112
113 toHtml (text: string) {
114 return this.markdownRenderer.textMarkdownToHTML(text, true, true)
115 }
116
117 isInSelectionMode () {
118 return this.selectedComments.length !== 0
119 }
120
121 reloadData () {
122 this.videoCommentService.getAdminVideoComments({
123 pagination: this.pagination,
124 sort: this.sort,
125 search: this.search
126 }).subscribe({
127 next: async resultList => {
128 this.totalRecords = resultList.total
129
130 this.comments = []
131
132 for (const c of resultList.data) {
133 this.comments.push(
134 new VideoCommentAdmin(c, await this.toHtml(c.text))
135 )
136 }
137 },
138
139 error: err => this.notifier.error(err.message)
140 })
141 }
142
143 private removeComments (comments: VideoCommentAdmin[]) {
144 const commentArgs = comments.map(c => ({ videoId: c.video.id, commentId: c.id }))
145
146 this.videoCommentService.deleteVideoComments(commentArgs)
147 .subscribe({
148 next: () => {
149 this.notifier.success(
150 prepareIcu($localize`{count, plural, =1 {1 comment deleted.} other {{count} comments deleted.}}`)(
151 { count: commentArgs.length },
152 $localize`${commentArgs.length} comment(s) deleted.`
153 )
154 )
155
156 this.reloadData()
157 },
158
159 error: err => this.notifier.error(err.message),
160
161 complete: () => this.selectedComments = []
162 })
163 }
164
165 private deleteComment (comment: VideoCommentAdmin) {
166 this.videoCommentService.deleteVideoComment(comment.video.id, comment.id)
167 .subscribe({
168 next: () => this.reloadData(),
169
170 error: err => this.notifier.error(err.message)
171 })
172 }
173
174 private async deleteUserComments (comment: VideoCommentAdmin) {
175 const message = $localize`Do you really want to delete all comments of ${comment.by}?`
176 const res = await this.confirmService.confirm(message, $localize`Delete`)
177 if (res === false) return
178
179 const options = {
180 accountName: comment.by,
181 scope: 'instance' as 'instance'
182 }
183
184 this.bulkService.removeCommentsOf(options)
185 .subscribe({
186 next: () => {
187 this.notifier.success($localize`Comments of ${options.accountName} will be deleted in a few minutes`)
188 },
189
190 error: err => this.notifier.error(err.message)
191 })
192 }
193 }