]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comment.component.ts
Fix mark all as read notifications
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.component.ts
CommitLineData
1263fc4e 1import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
edf1a4e5 2import { User, UserRight } from '../../../../../../shared/models/users'
4635f59d 3import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
edf1a4e5
RK
4import { AuthService } from '@app/core/auth'
5import { AccountService } from '@app/shared/account/account.service'
6import { Video } from '@app/shared/video/video.model'
4635f59d 7import { VideoComment } from './video-comment.model'
b29bf61d 8import { MarkdownService } from '@app/shared/renderer'
edf1a4e5
RK
9import { Account } from '@app/shared/account/account.model'
10import { Notifier } from '@app/core'
11import { UserService } from '@app/shared'
4635f59d
C
12
13@Component({
14 selector: 'my-video-comment',
15 templateUrl: './video-comment.component.html',
16 styleUrls: ['./video-comment.component.scss']
17})
1263fc4e 18export class VideoCommentComponent implements OnInit, OnChanges {
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
2890b615 33 sanitizedCommentHTML = ''
c199c427 34 newParentComments: VideoComment[] = []
2890b615 35
edf1a4e5
RK
36 commentAccount: Account
37 commentUser: User
38
3d9eaae3 39 constructor (
1aa75434 40 private markdownService: MarkdownService,
edf1a4e5
RK
41 private authService: AuthService,
42 private accountService: AccountService,
43 private userService: UserService,
44 private notifier: Notifier
3d9eaae3 45 ) {}
cf117aaa
C
46
47 get user () {
48 return this.authService.getUser()
4635f59d
C
49 }
50
2890b615 51 ngOnInit () {
1263fc4e
C
52 this.init()
53 }
d7e70384 54
1263fc4e
C
55 ngOnChanges () {
56 this.init()
2890b615
C
57 }
58
ae45f988
C
59 onCommentReplyCreated (createdComment: VideoComment) {
60 if (!this.commentTree) {
61 this.commentTree = {
62 comment: this.comment,
63 children: []
64 }
4cb6d457
C
65
66 this.threadCreated.emit(this.commentTree)
ae45f988
C
67 }
68
86ec3e53 69 this.commentTree.children.unshift({
ae45f988
C
70 comment: createdComment,
71 children: []
72 })
73 this.resetReply.emit()
4635f59d
C
74 }
75
4cb6d457
C
76 onWantToReply (comment?: VideoComment) {
77 this.wantedToReply.emit(comment || this.comment)
4635f59d
C
78 }
79
4cb6d457
C
80 onWantToDelete (comment?: VideoComment) {
81 this.wantedToDelete.emit(comment || this.comment)
4635f59d
C
82 }
83
4cb6d457
C
84 isUserLoggedIn () {
85 return this.authService.isLoggedIn()
4635f59d
C
86 }
87
88 onResetReply () {
89 this.resetReply.emit()
90 }
cf117aaa 91
b29bf61d
RK
92 handleTimestampClicked (timestamp: number) {
93 this.timestampClicked.emit(timestamp)
94 }
95
4cb6d457 96 isRemovableByUser () {
b29bf61d 97 return this.comment.account && this.isUserLoggedIn() &&
4cb6d457
C
98 (
99 this.user.account.id === this.comment.account.id ||
100 this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
101 )
102 }
1263fc4e 103
edf1a4e5
RK
104 private getUserIfNeeded (account: Account) {
105 if (!account.userId) return
106 if (!this.authService.isLoggedIn()) return
107
108 const user = this.authService.getUser()
109 if (user.hasRight(UserRight.MANAGE_USERS)) {
110 this.userService.getUser(account.userId)
111 .subscribe(
112 user => this.commentUser = user,
113
114 err => this.notifier.error(err.message)
115 )
116 }
117 }
118
41d71344 119 private async init () {
b29bf61d
RK
120 const html = await this.markdownService.textMarkdownToHTML(this.comment.text, true)
121 this.sanitizedCommentHTML = await this.markdownService.processVideoTimestamps(html)
1263fc4e 122 this.newParentComments = this.parentComments.concat([ this.comment ])
edf1a4e5
RK
123 this.commentAccount = new Account(this.comment.account)
124 this.getUserIfNeeded(this.commentAccount)
1263fc4e 125 }
4635f59d 126}