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