PeerTube/client/src/app/videos/+video-watch/comment/video-comment.component.ts

99 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-02-19 10:38:24 +01:00
import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
2018-01-04 17:50:30 +01:00
import * as sanitizeHtml from 'sanitize-html'
2018-01-03 17:25:47 +01:00
import { Account as AccountInterface } from '../../../../../../shared/models/actors'
2018-01-04 11:19:16 +01:00
import { UserRight } from '../../../../../../shared/models/users'
2017-12-27 16:11:53 +01:00
import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
import { AuthService } from '../../../core/auth'
2018-01-03 17:25:47 +01:00
import { Account } from '../../../shared/account/account.model'
2017-12-27 16:11:53 +01:00
import { Video } from '../../../shared/video/video.model'
import { VideoComment } from './video-comment.model'
@Component({
selector: 'my-video-comment',
templateUrl: './video-comment.component.html',
styleUrls: ['./video-comment.component.scss']
})
2018-02-19 10:38:24 +01:00
export class VideoCommentComponent implements OnInit, OnChanges {
2017-12-27 16:11:53 +01:00
@Input() video: Video
@Input() comment: VideoComment
2018-01-05 11:19:25 +01:00
@Input() parentComments: VideoComment[] = []
2017-12-27 16:11:53 +01:00
@Input() commentTree: VideoCommentThreadTree
@Input() inReplyToCommentId: number
2018-02-19 10:38:24 +01:00
@Input() highlightedComment = false
2017-12-27 16:11:53 +01:00
2018-01-04 11:19:16 +01:00
@Output() wantedToDelete = new EventEmitter<VideoComment>()
2017-12-27 16:11:53 +01:00
@Output() wantedToReply = new EventEmitter<VideoComment>()
2018-01-04 11:19:16 +01:00
@Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
2017-12-27 16:11:53 +01:00
@Output() resetReply = new EventEmitter()
2018-01-04 17:50:30 +01:00
sanitizedCommentHTML = ''
2018-01-05 11:19:25 +01:00
newParentComments = []
2018-01-04 17:50:30 +01:00
2018-01-03 17:25:47 +01:00
constructor (private authService: AuthService) {}
get user () {
return this.authService.getUser()
2017-12-27 16:11:53 +01:00
}
2018-01-04 17:50:30 +01:00
ngOnInit () {
2018-02-19 10:38:24 +01:00
this.init()
}
2018-01-05 11:19:25 +01:00
2018-02-19 10:38:24 +01:00
ngOnChanges () {
this.init()
2018-01-04 17:50:30 +01:00
}
2017-12-27 16:28:15 +01:00
onCommentReplyCreated (createdComment: VideoComment) {
if (!this.commentTree) {
this.commentTree = {
comment: this.comment,
children: []
}
2018-01-04 11:19:16 +01:00
this.threadCreated.emit(this.commentTree)
2017-12-27 16:28:15 +01:00
}
this.commentTree.children.push({
comment: createdComment,
children: []
})
this.resetReply.emit()
2017-12-27 16:11:53 +01:00
}
2018-01-04 11:19:16 +01:00
onWantToReply (comment?: VideoComment) {
this.wantedToReply.emit(comment || this.comment)
2017-12-27 16:11:53 +01:00
}
2018-01-04 11:19:16 +01:00
onWantToDelete (comment?: VideoComment) {
this.wantedToDelete.emit(comment || this.comment)
2017-12-27 16:11:53 +01:00
}
2018-01-04 11:19:16 +01:00
isUserLoggedIn () {
return this.authService.isLoggedIn()
2017-12-27 16:11:53 +01:00
}
onResetReply () {
this.resetReply.emit()
}
2018-01-03 17:25:47 +01:00
getAvatarUrl (account: AccountInterface) {
return Account.GET_ACCOUNT_AVATAR_URL(account)
}
2018-01-04 11:19:16 +01:00
isRemovableByUser () {
return this.isUserLoggedIn() &&
(
this.user.account.id === this.comment.account.id ||
this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
)
}
2018-02-19 10:38:24 +01:00
private init () {
this.sanitizedCommentHTML = sanitizeHtml(this.comment.text, {
2018-02-20 10:41:11 +01:00
allowedTags: [ 'p', 'span', 'br' ]
2018-02-19 10:38:24 +01:00
})
this.newParentComments = this.parentComments.concat([ this.comment ])
}
2017-12-27 16:11:53 +01:00
}