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