aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+admin/moderation/abuse-list/abuse-list.component.ts
blob: 1ea61ed379fbe678a75e2a0e9aff2f0ee48e6666 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { SortMeta } from 'primeng/api'
import { buildVideoEmbed, buildVideoLink } from 'src/assets/player/utils'
import { environment } from 'src/environments/environment'
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
import { ActivatedRoute, Params, Router } from '@angular/router'
import { ConfirmService, MarkdownService, Notifier, RestPagination, RestTable } from '@app/core'
import { Account, Actor, DropdownAction, Video, VideoService } from '@app/shared/shared-main'
import { AbuseService, BlocklistService, VideoBlockService } from '@app/shared/shared-moderation'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { Abuse, AbuseState } from '@shared/models'
import { ModerationCommentModalComponent } from './moderation-comment-modal.component'
import truncate from 'lodash-es/truncate'

export type ProcessedAbuse = Abuse & {
  moderationCommentHtml?: string,
  reasonHtml?: string
  embedHtml?: SafeHtml
  updatedAt?: Date

  // override bare server-side definitions with rich client-side definitions
  reporterAccount?: Account
  flaggedAccount?: Account

  truncatedCommentHtml?: string
  commentHtml?: string

  video: Abuse['video'] & {
    channel: Abuse['video']['channel'] & {
      ownerAccount: Account
    }
  }
}

@Component({
  selector: 'my-abuse-list',
  templateUrl: './abuse-list.component.html',
  styleUrls: [ '../moderation.component.scss', './abuse-list.component.scss' ]
})
export class AbuseListComponent extends RestTable implements OnInit, AfterViewInit {
  @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent

  abuses: ProcessedAbuse[] = []
  totalRecords = 0
  sort: SortMeta = { field: 'createdAt', order: 1 }
  pagination: RestPagination = { count: this.rowsPerPage, start: 0 }

  abuseActions: DropdownAction<Abuse>[][] = []

  constructor (
    private notifier: Notifier,
    private abuseService: AbuseService,
    private blocklistService: BlocklistService,
    private videoService: VideoService,
    private videoBlocklistService: VideoBlockService,
    private confirmService: ConfirmService,
    private i18n: I18n,
    private markdownRenderer: MarkdownService,
    private sanitizer: DomSanitizer,
    private route: ActivatedRoute,
    private router: Router
  ) {
    super()

    this.abuseActions = [
      [
        {
          label: this.i18n('Internal actions'),
          isHeader: true
        },
        {
          label: this.i18n('Delete report'),
          handler: abuse => this.removeAbuse(abuse)
        },
        {
          label: this.i18n('Add note'),
          handler: abuse => this.openModerationCommentModal(abuse),
          isDisplayed: abuse => !abuse.moderationComment
        },
        {
          label: this.i18n('Update note'),
          handler: abuse => this.openModerationCommentModal(abuse),
          isDisplayed: abuse => !!abuse.moderationComment
        },
        {
          label: this.i18n('Mark as accepted'),
          handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED),
          isDisplayed: abuse => !this.isAbuseAccepted(abuse)
        },
        {
          label: this.i18n('Mark as rejected'),
          handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED),
          isDisplayed: abuse => !this.isAbuseRejected(abuse)
        }
      ],
      [
        {
          label: this.i18n('Actions for the video'),
          isHeader: true,
          isDisplayed: abuse => abuse.video && !abuse.video.deleted
        },
        {
          label: this.i18n('Block video'),
          isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted,
          handler: abuse => {
            this.videoBlocklistService.blockVideo(abuse.video.id, undefined, true)
              .subscribe(
                () => {
                  this.notifier.success(this.i18n('Video blocked.'))

                  this.updateAbuseState(abuse, AbuseState.ACCEPTED)
                },

                err => this.notifier.error(err.message)
              )
          }
        },
        {
          label: this.i18n('Unblock video'),
          isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted,
          handler: abuse => {
            this.videoBlocklistService.unblockVideo(abuse.video.id)
              .subscribe(
                () => {
                  this.notifier.success(this.i18n('Video unblocked.'))

                  this.updateAbuseState(abuse, AbuseState.ACCEPTED)
                },

                err => this.notifier.error(err.message)
              )
          }
        },
        {
          label: this.i18n('Delete video'),
          isDisplayed: abuse => abuse.video && !abuse.video.deleted,
          handler: async abuse => {
            const res = await this.confirmService.confirm(
              this.i18n('Do you really want to delete this video?'),
              this.i18n('Delete')
            )
            if (res === false) return

            this.videoService.removeVideo(abuse.video.id)
              .subscribe(
                () => {
                  this.notifier.success(this.i18n('Video deleted.'))

                  this.updateAbuseState(abuse, AbuseState.ACCEPTED)
                },

                err => this.notifier.error(err.message)
              )
          }
        }
      ],
      [
        {
          label: this.i18n('Actions for the reporter'),
          isHeader: true,
          isDisplayed: abuse => !!abuse.reporterAccount
        },
        {
          label: this.i18n('Mute reporter'),
          isDisplayed: abuse => !!abuse.reporterAccount,
          handler: async abuse => {
            const account = abuse.reporterAccount as Account

            this.blocklistService.blockAccountByInstance(account)
              .subscribe(
                () => {
                  this.notifier.success(
                    this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
                  )

                  account.mutedByInstance = true
                },

                err => this.notifier.error(err.message)
              )
          }
        },
        {
          label: this.i18n('Mute server'),
          isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId,
          handler: async abuse => {
            this.blocklistService.blockServerByInstance(abuse.reporterAccount.host)
              .subscribe(
                () => {
                  this.notifier.success(
                    this.i18n('Server {{host}} muted by the instance.', { host: abuse.reporterAccount.host })
                  )
                },

                err => this.notifier.error(err.message)
              )
          }
        }
      ]
    ]
  }

  ngOnInit () {
    this.initialize()

    this.route.queryParams
      .subscribe(params => {
        this.search = params.search || ''

        this.setTableFilter(this.search)
        this.loadData()
      })
  }

  ngAfterViewInit () {
    if (this.search) this.setTableFilter(this.search)
  }

  getIdentifier () {
    return 'AbuseListComponent'
  }

  openModerationCommentModal (abuse: Abuse) {
    this.moderationCommentModal.openModal(abuse)
  }

  onModerationCommentUpdated () {
    this.loadData()
  }

  /* Table filter functions */
  onAbuseSearch (event: Event) {
    this.onSearch(event)
    this.setQueryParams((event.target as HTMLInputElement).value)
  }

  setQueryParams (search: string) {
    const queryParams: Params = {}
    if (search) Object.assign(queryParams, { search })

    this.router.navigate([ '/admin/moderation/abuses/list' ], { queryParams })
  }

  resetTableFilter () {
    this.setTableFilter('')
    this.setQueryParams('')
    this.resetSearch()
  }
  /* END Table filter functions */

  isAbuseAccepted (abuse: Abuse) {
    return abuse.state.id === AbuseState.ACCEPTED
  }

  isAbuseRejected (abuse: Abuse) {
    return abuse.state.id === AbuseState.REJECTED
  }

  getVideoUrl (abuse: Abuse) {
    return Video.buildClientUrl(abuse.video.uuid)
  }

  getCommentUrl (abuse: Abuse) {
    return Video.buildClientUrl(abuse.comment.video.uuid) + ';threadId=' + abuse.comment.threadId
  }

  getVideoEmbed (abuse: Abuse) {
    return buildVideoEmbed(
      buildVideoLink({
        baseUrl: `${environment.embedUrl}/videos/embed/${abuse.video.uuid}`,
        title: false,
        warningTitle: false,
        startTime: abuse.startAt,
        stopTime: abuse.endAt
      })
    )
  }

  switchToDefaultAvatar ($event: Event) {
    ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
  }

  async removeAbuse (abuse: Abuse) {
    const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this abuse report?'), this.i18n('Delete'))
    if (res === false) return

    this.abuseService.removeAbuse(abuse).subscribe(
      () => {
        this.notifier.success(this.i18n('Abuse deleted.'))
        this.loadData()
      },

      err => this.notifier.error(err.message)
    )
  }

  updateAbuseState (abuse: Abuse, state: AbuseState) {
    this.abuseService.updateAbuse(abuse, { state })
      .subscribe(
        () => this.loadData(),

        err => this.notifier.error(err.message)
      )
  }

  protected loadData () {
    return this.abuseService.getAbuses({
      pagination: this.pagination,
      sort: this.sort,
      search: this.search
    }).subscribe(
        async resultList => {
          this.totalRecords = resultList.total

          this.abuses = []

          for (const a of resultList.data) {
            const abuse = a as ProcessedAbuse

            abuse.reasonHtml = await this.toHtml(abuse.reason)
            abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment)

            if (abuse.video) {
              abuse.embedHtml = this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))

              if (abuse.video.channel?.ownerAccount) {
                abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount)
              }
            }

            if (abuse.comment) {
              if (abuse.comment.deleted) {
                abuse.truncatedCommentHtml = abuse.commentHtml = this.i18n('Deleted comment')
              } else {
                const truncated = truncate(abuse.comment.text, { length: 100 })
                abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML(truncated, true)
                abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML(abuse.comment.text, true)
              }
            }

            if (abuse.reporterAccount) {
              abuse.reporterAccount = new Account(abuse.reporterAccount)
            }

            if (abuse.flaggedAccount) {
              abuse.flaggedAccount = new Account(abuse.flaggedAccount)
            }

            if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt

            this.abuses.push(abuse)
          }
        },

        err => this.notifier.error(err.message)
      )
  }

  private toHtml (text: string) {
    return this.markdownRenderer.textMarkdownToHTML(text)
  }
}