]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/videos/+video-watch/comment/video-comment.component.ts
Add ability to delete comments
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment.component.ts
index b8e2acd52f11316f9ca37a68e68df539fcf208ed..9bc9c8844c68261c3c6cd1e1d92ca694d0e8ad8a 100644 (file)
@@ -1,11 +1,11 @@
 import { Component, EventEmitter, Input, Output } from '@angular/core'
-import { NotificationsService } from 'angular2-notifications'
+import { Account as AccountInterface } from '../../../../../../shared/models/actors'
+import { UserRight } from '../../../../../../shared/models/users'
 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
 import { AuthService } from '../../../core/auth'
-import { User } from '../../../shared/users'
+import { Account } from '../../../shared/account/account.model'
 import { Video } from '../../../shared/video/video.model'
 import { VideoComment } from './video-comment.model'
-import { VideoCommentService } from './video-comment.service'
 
 @Component({
   selector: 'my-video-comment',
@@ -18,50 +18,59 @@ export class VideoCommentComponent {
   @Input() commentTree: VideoCommentThreadTree
   @Input() inReplyToCommentId: number
 
+  @Output() wantedToDelete = new EventEmitter<VideoComment>()
   @Output() wantedToReply = new EventEmitter<VideoComment>()
+  @Output() threadCreated = new EventEmitter<VideoCommentThreadTree>()
   @Output() resetReply = new EventEmitter()
 
-  constructor (private authService: AuthService,
-               private notificationsService: NotificationsService,
-               private videoCommentService: VideoCommentService) {
+  constructor (private authService: AuthService) {}
+
+  get user () {
+    return this.authService.getUser()
   }
 
-  onCommentReplyCreated (comment: VideoComment) {
-    this.videoCommentService.addCommentReply(this.video.id, this.comment.id, comment)
-      .subscribe(
-        createdComment => {
-          if (!this.commentTree) {
-            this.commentTree = {
-              comment: this.comment,
-              children: []
-            }
-          }
+  onCommentReplyCreated (createdComment: VideoComment) {
+    if (!this.commentTree) {
+      this.commentTree = {
+        comment: this.comment,
+        children: []
+      }
 
-          this.commentTree.children.push({
-            comment: createdComment,
-            children: []
-          })
-          this.resetReply.emit()
-        },
+      this.threadCreated.emit(this.commentTree)
+    }
 
-        err => this.notificationsService.error('Error', err.message)
-      )
+    this.commentTree.children.push({
+      comment: createdComment,
+      children: []
+    })
+    this.resetReply.emit()
   }
 
-  onWantToReply () {
-    this.wantedToReply.emit(this.comment)
+  onWantToReply (comment?: VideoComment) {
+    this.wantedToReply.emit(comment || this.comment)
   }
 
-  isUserLoggedIn () {
-    return this.authService.isLoggedIn()
+  onWantToDelete (comment?: VideoComment) {
+    this.wantedToDelete.emit(comment || this.comment)
   }
 
-  // Event from child comment
-  onWantedToReply (comment: VideoComment) {
-    this.wantedToReply.emit(comment)
+  isUserLoggedIn () {
+    return this.authService.isLoggedIn()
   }
 
   onResetReply () {
     this.resetReply.emit()
   }
+
+  getAvatarUrl (account: AccountInterface) {
+    return Account.GET_ACCOUNT_AVATAR_URL(account)
+  }
+
+  isRemovableByUser () {
+    return this.isUserLoggedIn() &&
+      (
+        this.user.account.id === this.comment.account.id ||
+        this.user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)
+      )
+  }
 }