]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core' | |
2 | import { Account } from '@app/shared/account/account.model' | |
3 | import { Notifier } from '@app/core' | |
4 | import { SortMeta } from 'primeng/api' | |
5 | import { VideoAbuse, VideoAbuseState } from '../../../../../../shared' | |
6 | import { RestPagination, RestTable, VideoAbuseService, VideoBlockService } from '../../../shared' | |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
8 | import { DropdownAction } from '../../../shared/buttons/action-dropdown.component' | |
9 | import { ConfirmService } from '../../../core/index' | |
10 | import { ModerationCommentModalComponent } from './moderation-comment-modal.component' | |
11 | import { Video } from '../../../shared/video/video.model' | |
12 | import { MarkdownService } from '@app/shared/renderer' | |
13 | import { Actor } from '@app/shared/actor/actor.model' | |
14 | import { buildVideoLink, buildVideoEmbed } from 'src/assets/player/utils' | |
15 | import { getAbsoluteAPIUrl } from '@app/shared/misc/utils' | |
16 | import { DomSanitizer } from '@angular/platform-browser' | |
17 | import { BlocklistService } from '@app/shared/blocklist' | |
18 | import { VideoService } from '@app/shared/video/video.service' | |
19 | import { ActivatedRoute, Params, Router } from '@angular/router' | |
20 | import { filter } from 'rxjs/operators' | |
21 | ||
22 | export type ProcessedVideoAbuse = VideoAbuse & { | |
23 | moderationCommentHtml?: string, | |
24 | reasonHtml?: string | |
25 | embedHtml?: string | |
26 | updatedAt?: Date | |
27 | // override bare server-side definitions with rich client-side definitions | |
28 | reporterAccount: Account | |
29 | video: VideoAbuse['video'] & { | |
30 | channel: VideoAbuse['video']['channel'] & { | |
31 | ownerAccount: Account | |
32 | } | |
33 | } | |
34 | } | |
35 | ||
36 | @Component({ | |
37 | selector: 'my-video-abuse-list', | |
38 | templateUrl: './video-abuse-list.component.html', | |
39 | styleUrls: [ '../moderation.component.scss', './video-abuse-list.component.scss' ] | |
40 | }) | |
41 | export class VideoAbuseListComponent extends RestTable implements OnInit, AfterViewInit { | |
42 | @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent | |
43 | ||
44 | videoAbuses: ProcessedVideoAbuse[] = [] | |
45 | totalRecords = 0 | |
46 | sort: SortMeta = { field: 'createdAt', order: 1 } | |
47 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } | |
48 | ||
49 | videoAbuseActions: DropdownAction<VideoAbuse>[][] = [] | |
50 | ||
51 | constructor ( | |
52 | private notifier: Notifier, | |
53 | private videoAbuseService: VideoAbuseService, | |
54 | private blocklistService: BlocklistService, | |
55 | private videoService: VideoService, | |
56 | private videoBlocklistService: VideoBlockService, | |
57 | private confirmService: ConfirmService, | |
58 | private i18n: I18n, | |
59 | private markdownRenderer: MarkdownService, | |
60 | private sanitizer: DomSanitizer, | |
61 | private route: ActivatedRoute, | |
62 | private router: Router | |
63 | ) { | |
64 | super() | |
65 | ||
66 | this.videoAbuseActions = [ | |
67 | [ | |
68 | { | |
69 | label: this.i18n('Internal actions'), | |
70 | isHeader: true | |
71 | }, | |
72 | { | |
73 | label: this.i18n('Delete report'), | |
74 | handler: videoAbuse => this.removeVideoAbuse(videoAbuse) | |
75 | }, | |
76 | { | |
77 | label: this.i18n('Add note'), | |
78 | handler: videoAbuse => this.openModerationCommentModal(videoAbuse), | |
79 | isDisplayed: videoAbuse => !videoAbuse.moderationComment | |
80 | }, | |
81 | { | |
82 | label: this.i18n('Update note'), | |
83 | handler: videoAbuse => this.openModerationCommentModal(videoAbuse), | |
84 | isDisplayed: videoAbuse => !!videoAbuse.moderationComment | |
85 | }, | |
86 | { | |
87 | label: this.i18n('Mark as accepted'), | |
88 | handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED), | |
89 | isDisplayed: videoAbuse => !this.isVideoAbuseAccepted(videoAbuse) | |
90 | }, | |
91 | { | |
92 | label: this.i18n('Mark as rejected'), | |
93 | handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.REJECTED), | |
94 | isDisplayed: videoAbuse => !this.isVideoAbuseRejected(videoAbuse) | |
95 | } | |
96 | ], | |
97 | [ | |
98 | { | |
99 | label: this.i18n('Actions for the video'), | |
100 | isHeader: true, | |
101 | isDisplayed: videoAbuse => !videoAbuse.video.deleted | |
102 | }, | |
103 | { | |
104 | label: this.i18n('Block video'), | |
105 | isDisplayed: videoAbuse => !videoAbuse.video.deleted && !videoAbuse.video.blacklisted, | |
106 | handler: videoAbuse => { | |
107 | this.videoBlocklistService.blockVideo(videoAbuse.video.id, undefined, true) | |
108 | .subscribe( | |
109 | () => { | |
110 | this.notifier.success(this.i18n('Video blocklisted.')) | |
111 | ||
112 | this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED) | |
113 | }, | |
114 | ||
115 | err => this.notifier.error(err.message) | |
116 | ) | |
117 | } | |
118 | }, | |
119 | { | |
120 | label: this.i18n('Unblock video'), | |
121 | isDisplayed: videoAbuse => !videoAbuse.video.deleted && videoAbuse.video.blacklisted, | |
122 | handler: videoAbuse => { | |
123 | this.videoBlocklistService.unblockVideo(videoAbuse.video.id) | |
124 | .subscribe( | |
125 | () => { | |
126 | this.notifier.success(this.i18n('Video unblocklisted.')) | |
127 | ||
128 | this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED) | |
129 | }, | |
130 | ||
131 | err => this.notifier.error(err.message) | |
132 | ) | |
133 | } | |
134 | }, | |
135 | { | |
136 | label: this.i18n('Delete video'), | |
137 | isDisplayed: videoAbuse => !videoAbuse.video.deleted, | |
138 | handler: async videoAbuse => { | |
139 | const res = await this.confirmService.confirm( | |
140 | this.i18n('Do you really want to delete this video?'), | |
141 | this.i18n('Delete') | |
142 | ) | |
143 | if (res === false) return | |
144 | ||
145 | this.videoService.removeVideo(videoAbuse.video.id) | |
146 | .subscribe( | |
147 | () => { | |
148 | this.notifier.success(this.i18n('Video deleted.')) | |
149 | ||
150 | this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED) | |
151 | }, | |
152 | ||
153 | err => this.notifier.error(err.message) | |
154 | ) | |
155 | } | |
156 | } | |
157 | ], | |
158 | [ | |
159 | { | |
160 | label: this.i18n('Actions for the reporter'), | |
161 | isHeader: true | |
162 | }, | |
163 | { | |
164 | label: this.i18n('Mute reporter'), | |
165 | handler: async videoAbuse => { | |
166 | const account = videoAbuse.reporterAccount as Account | |
167 | ||
168 | this.blocklistService.blockAccountByInstance(account) | |
169 | .subscribe( | |
170 | () => { | |
171 | this.notifier.success( | |
172 | this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost }) | |
173 | ) | |
174 | ||
175 | account.mutedByInstance = true | |
176 | }, | |
177 | ||
178 | err => this.notifier.error(err.message) | |
179 | ) | |
180 | } | |
181 | }, | |
182 | { | |
183 | label: this.i18n('Mute server'), | |
184 | isDisplayed: videoAbuse => !videoAbuse.reporterAccount.userId, | |
185 | handler: async videoAbuse => { | |
186 | this.blocklistService.blockServerByInstance(videoAbuse.reporterAccount.host) | |
187 | .subscribe( | |
188 | () => { | |
189 | this.notifier.success( | |
190 | this.i18n('Server {{host}} muted by the instance.', { host: videoAbuse.reporterAccount.host }) | |
191 | ) | |
192 | }, | |
193 | ||
194 | err => this.notifier.error(err.message) | |
195 | ) | |
196 | } | |
197 | } | |
198 | ] | |
199 | ] | |
200 | } | |
201 | ||
202 | ngOnInit () { | |
203 | this.initialize() | |
204 | ||
205 | this.route.queryParams | |
206 | .pipe(filter(params => params.search !== undefined && params.search !== null)) | |
207 | .subscribe(params => { | |
208 | this.search = params.search | |
209 | this.setTableFilter(params.search) | |
210 | this.loadData() | |
211 | }) | |
212 | } | |
213 | ||
214 | ngAfterViewInit () { | |
215 | if (this.search) this.setTableFilter(this.search) | |
216 | } | |
217 | ||
218 | getIdentifier () { | |
219 | return 'VideoAbuseListComponent' | |
220 | } | |
221 | ||
222 | openModerationCommentModal (videoAbuse: VideoAbuse) { | |
223 | this.moderationCommentModal.openModal(videoAbuse) | |
224 | } | |
225 | ||
226 | onModerationCommentUpdated () { | |
227 | this.loadData() | |
228 | } | |
229 | ||
230 | /* Table filter functions */ | |
231 | onAbuseSearch (event: Event) { | |
232 | this.onSearch(event) | |
233 | this.setQueryParams((event.target as HTMLInputElement).value) | |
234 | } | |
235 | ||
236 | setQueryParams (search: string) { | |
237 | const queryParams: Params = {} | |
238 | if (search) Object.assign(queryParams, { search }) | |
239 | this.router.navigate([ '/admin/moderation/video-abuses/list' ], { queryParams }) | |
240 | } | |
241 | ||
242 | resetTableFilter () { | |
243 | this.setTableFilter('') | |
244 | this.setQueryParams('') | |
245 | this.resetSearch() | |
246 | } | |
247 | /* END Table filter functions */ | |
248 | ||
249 | isVideoAbuseAccepted (videoAbuse: VideoAbuse) { | |
250 | return videoAbuse.state.id === VideoAbuseState.ACCEPTED | |
251 | } | |
252 | ||
253 | isVideoAbuseRejected (videoAbuse: VideoAbuse) { | |
254 | return videoAbuse.state.id === VideoAbuseState.REJECTED | |
255 | } | |
256 | ||
257 | getVideoUrl (videoAbuse: VideoAbuse) { | |
258 | return Video.buildClientUrl(videoAbuse.video.uuid) | |
259 | } | |
260 | ||
261 | getVideoEmbed (videoAbuse: VideoAbuse) { | |
262 | const absoluteAPIUrl = getAbsoluteAPIUrl() | |
263 | const embedUrl = buildVideoLink({ | |
264 | baseUrl: absoluteAPIUrl + '/videos/embed/' + videoAbuse.video.uuid, | |
265 | warningTitle: false | |
266 | }) | |
267 | return buildVideoEmbed(embedUrl) | |
268 | } | |
269 | ||
270 | switchToDefaultAvatar ($event: Event) { | |
271 | ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL() | |
272 | } | |
273 | ||
274 | async removeVideoAbuse (videoAbuse: VideoAbuse) { | |
275 | const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this abuse report?'), this.i18n('Delete')) | |
276 | if (res === false) return | |
277 | ||
278 | this.videoAbuseService.removeVideoAbuse(videoAbuse).subscribe( | |
279 | () => { | |
280 | this.notifier.success(this.i18n('Abuse deleted.')) | |
281 | this.loadData() | |
282 | }, | |
283 | ||
284 | err => this.notifier.error(err.message) | |
285 | ) | |
286 | } | |
287 | ||
288 | updateVideoAbuseState (videoAbuse: VideoAbuse, state: VideoAbuseState) { | |
289 | this.videoAbuseService.updateVideoAbuse(videoAbuse, { state }) | |
290 | .subscribe( | |
291 | () => this.loadData(), | |
292 | ||
293 | err => this.notifier.error(err.message) | |
294 | ) | |
295 | } | |
296 | ||
297 | protected loadData () { | |
298 | return this.videoAbuseService.getVideoAbuses({ | |
299 | pagination: this.pagination, | |
300 | sort: this.sort, | |
301 | search: this.search | |
302 | }).subscribe( | |
303 | async resultList => { | |
304 | this.totalRecords = resultList.total | |
305 | const videoAbuses = [] | |
306 | ||
307 | for (const abuse of resultList.data) { | |
308 | Object.assign(abuse, { | |
309 | reasonHtml: await this.toHtml(abuse.reason), | |
310 | moderationCommentHtml: await this.toHtml(abuse.moderationComment), | |
311 | embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse)), | |
312 | reporterAccount: new Account(abuse.reporterAccount) | |
313 | }) | |
314 | ||
315 | if (abuse.video.channel?.ownerAccount) abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount) | |
316 | if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt | |
317 | ||
318 | videoAbuses.push(abuse as ProcessedVideoAbuse) | |
319 | } | |
320 | ||
321 | this.videoAbuses = videoAbuses | |
322 | }, | |
323 | ||
324 | err => this.notifier.error(err.message) | |
325 | ) | |
326 | } | |
327 | ||
328 | private toHtml (text: string) { | |
329 | return this.markdownRenderer.textMarkdownToHTML(text) | |
330 | } | |
331 | } |