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