]>
Commit | Line | Data |
---|---|---|
8ca56654 C |
1 | |
2 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core' | |
67ed6552 | 3 | import { MarkdownService, Notifier, UserService } from '@app/core' |
edf1a4e5 | 4 | import { AuthService } from '@app/core/auth' |
8ca56654 | 5 | import { Account, Actor, DropdownAction, Video } from '@app/shared/shared-main' |
cfde28ba C |
6 | import { CommentReportComponent } from '@app/shared/shared-moderation/report-modals/comment-report.component' |
7 | import { VideoComment, VideoCommentThreadTree } from '@app/shared/shared-video-comment' | |
8ca56654 | 8 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 | 9 | import { User, UserRight } from '@shared/models' |
4635f59d C |
10 | |
11 | @Component({ | |
12 | selector: 'my-video-comment', | |
13 | templateUrl: './video-comment.component.html', | |
14 | styleUrls: ['./video-comment.component.scss'] | |
15 | }) | |
1263fc4e | 16 | export class VideoCommentComponent implements OnInit, OnChanges { |
8ca56654 C |
17 | @ViewChild('commentReportModal') commentReportModal: CommentReportComponent |
18 | ||
4635f59d C |
19 | @Input() video: Video |
20 | @Input() comment: VideoComment | |
d7e70384 | 21 | @Input() parentComments: VideoComment[] = [] |
4635f59d C |
22 | @Input() commentTree: VideoCommentThreadTree |
23 | @Input() inReplyToCommentId: number | |
1263fc4e | 24 | @Input() highlightedComment = false |
c62a34d3 | 25 | @Input() firstInThread = false |
4635f59d | 26 | |
4cb6d457 | 27 | @Output() wantedToDelete = new EventEmitter<VideoComment>() |
4635f59d | 28 | @Output() wantedToReply = new EventEmitter<VideoComment>() |
4cb6d457 | 29 | @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>() |
4635f59d | 30 | @Output() resetReply = new EventEmitter() |
b29bf61d | 31 | @Output() timestampClicked = new EventEmitter<number>() |
4635f59d | 32 | |
8ca56654 C |
33 | prependModerationActions: DropdownAction<any>[] |
34 | ||
2890b615 | 35 | sanitizedCommentHTML = '' |
c199c427 | 36 | newParentComments: VideoComment[] = [] |
2890b615 | 37 | |
edf1a4e5 RK |
38 | commentAccount: Account |
39 | commentUser: User | |
40 | ||
3d9eaae3 | 41 | constructor ( |
8ca56654 | 42 | private i18n: I18n, |
1aa75434 | 43 | private markdownService: MarkdownService, |
edf1a4e5 | 44 | private authService: AuthService, |
edf1a4e5 RK |
45 | private userService: UserService, |
46 | private notifier: Notifier | |
3d9eaae3 | 47 | ) {} |
cf117aaa C |
48 | |
49 | get user () { | |
50 | return this.authService.getUser() | |
4635f59d C |
51 | } |
52 | ||
2890b615 | 53 | ngOnInit () { |
1263fc4e C |
54 | this.init() |
55 | } | |
d7e70384 | 56 | |
1263fc4e C |
57 | ngOnChanges () { |
58 | this.init() | |
2890b615 C |
59 | } |
60 | ||
ae45f988 C |
61 | onCommentReplyCreated (createdComment: VideoComment) { |
62 | if (!this.commentTree) { | |
63 | this.commentTree = { | |
64 | comment: this.comment, | |
65 | children: [] | |
66 | } | |
4cb6d457 C |
67 | |
68 | this.threadCreated.emit(this.commentTree) | |
ae45f988 C |
69 | } |
70 | ||
86ec3e53 | 71 | this.commentTree.children.unshift({ |
ae45f988 C |
72 | comment: createdComment, |
73 | children: [] | |
74 | }) | |
75 | this.resetReply.emit() | |
4635f59d C |
76 | } |
77 | ||
4cb6d457 C |
78 | onWantToReply (comment?: VideoComment) { |
79 | this.wantedToReply.emit(comment || this.comment) | |
4635f59d C |
80 | } |
81 | ||
4cb6d457 C |
82 | onWantToDelete (comment?: VideoComment) { |
83 | this.wantedToDelete.emit(comment || this.comment) | |
4635f59d C |
84 | } |
85 | ||
4cb6d457 C |
86 | isUserLoggedIn () { |
87 | return this.authService.isLoggedIn() | |
4635f59d C |
88 | } |
89 | ||
90 | onResetReply () { | |
91 | this.resetReply.emit() | |
92 | } | |
cf117aaa | 93 | |
b29bf61d RK |
94 | handleTimestampClicked (timestamp: number) { |
95 | this.timestampClicked.emit(timestamp) | |
96 | } | |
97 | ||
4cb6d457 | 98 | isRemovableByUser () { |
b29bf61d | 99 | return this.comment.account && this.isUserLoggedIn() && |
4cb6d457 C |
100 | ( |
101 | this.user.account.id === this.comment.account.id || | |
fde37dc9 | 102 | this.user.account.id === this.video.account.id || |
4cb6d457 C |
103 | this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) |
104 | ) | |
105 | } | |
1263fc4e | 106 | |
c511c3f0 RK |
107 | switchToDefaultAvatar ($event: Event) { |
108 | ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL() | |
109 | } | |
110 | ||
edf1a4e5 RK |
111 | private getUserIfNeeded (account: Account) { |
112 | if (!account.userId) return | |
113 | if (!this.authService.isLoggedIn()) return | |
114 | ||
115 | const user = this.authService.getUser() | |
116 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
218b0874 | 117 | this.userService.getUserWithCache(account.userId) |
edf1a4e5 RK |
118 | .subscribe( |
119 | user => this.commentUser = user, | |
120 | ||
121 | err => this.notifier.error(err.message) | |
122 | ) | |
123 | } | |
124 | } | |
125 | ||
41d71344 | 126 | private async init () { |
b29bf61d RK |
127 | const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true) |
128 | this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html) | |
1263fc4e | 129 | this.newParentComments = this.parentComments.concat([ this.comment ]) |
6cb55644 C |
130 | |
131 | if (this.comment.account) { | |
132 | this.commentAccount = new Account(this.comment.account) | |
133 | this.getUserIfNeeded(this.commentAccount) | |
134 | } else { | |
135 | this.comment.account = null | |
136 | } | |
8ca56654 | 137 | |
cfde28ba | 138 | if (this.isUserLoggedIn() && this.authService.getUser().account.id !== this.comment.account.id) { |
8ca56654 C |
139 | this.prependModerationActions = [ |
140 | { | |
141 | label: this.i18n('Report comment'), | |
142 | handler: () => this.showReportModal() | |
143 | } | |
144 | ] | |
145 | } else { | |
146 | this.prependModerationActions = undefined | |
147 | } | |
148 | } | |
149 | ||
150 | private showReportModal () { | |
151 | this.commentReportModal.show() | |
1263fc4e | 152 | } |
4635f59d | 153 | } |