]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/CommentRepo.php
#47 - Fixes the issues with the test case.
[bookstack] / app / Repos / CommentRepo.php
index ba34617ed8fe0a53dcd3a21a4c0e6d49ec55268c..ce71b923498ac0ed376ac25ab154f42852ddfb2b 100644 (file)
@@ -10,7 +10,7 @@ use BookStack\Page;
 class CommentRepo {
     /**
      *
-     * @var Comment $comment 
+     * @var Comment $comment
      */
     protected $comment;
 
@@ -25,26 +25,81 @@ class CommentRepo {
         $comment->fill($data);
         // new comment
         $comment->page_id = $page->id;
-        $comment->created_by = $userId;        
+        $comment->created_by = $userId;
+        $comment->updated_at = null;
         $comment->save();
         return $comment;
     }
 
-    public function update($comment, $input) {
+    public function update($comment, $input, $activeOnly = true) {
         $userId = user()->id;
         $comment->updated_by = $userId;
         $comment->fill($input);
+
+        // only update active comments by default.
+        $whereClause = ['active' => 1];
+        if (!$activeOnly) {
+            $whereClause = [];
+        }
+        $comment->update($whereClause);
+        return $comment;
+    }
+
+    public function delete($comment) {
+        $comment->text = trans('entities.comment_deleted');
+        $comment->html = trans('entities.comment_deleted');
+        $comment->active = false;
+        $userId = user()->id;
+        $comment->updated_by = $userId;
         $comment->save();
         return $comment;
     }
-    
-    public function getCommentsForPage($pageId, $commentId, $count = 20) {        
-        // requesting parent comments
-        $query = $this->comment->getCommentsByPage($pageId, $commentId);
-        return $query->paginate($count);        
+
+    public function getPageComments($pageId) {
+        $comments = $this->comment->getAllPageComments($pageId);
+        $index = [];
+        $totalComments = count($comments);
+        $finalCommentList = [];
+
+        // normalizing the response.
+        for ($i = 0; $i < count($comments); ++$i) {
+            $comment = $this->normalizeComment($comments[$i]);
+            $parentId = $comment->parent_id;
+            if (empty($parentId)) {
+                $finalCommentList[] = $comment;
+                $index[$comment->id] = $comment;
+                continue;
+            }
+
+            if (empty($index[$parentId])) {
+                // weird condition should not happen.
+                continue;
+            }
+            if (empty($index[$parentId]->sub_comments)) {
+                $index[$parentId]->sub_comments = [];
+            }
+            array_push($index[$parentId]->sub_comments, $comment);
+            $index[$comment->id] = $comment;
+        }
+        return [
+            'comments' => $finalCommentList,
+            'total' => $totalComments
+        ];
+    }
+
+    public function getCommentById($commentId) {
+        return $this->normalizeComment($this->comment->getCommentById($commentId));
     }
-    
-    public function getCommentCount($pageId) {
-        return $this->comment->where('page_id', '=', $pageId)->count();
+
+    private function normalizeComment($comment) {
+        if (empty($comment)) {
+            return;
+        }
+        $comment->createdBy->avatar_url = $comment->createdBy->getAvatar(50);
+        $comment->createdBy->profile_url = $comment->createdBy->getProfileUrl();
+        if (!empty($comment->updatedBy)) {
+            $comment->updatedBy->profile_url = $comment->updatedBy->getProfileUrl();
+        }
+        return $comment;
     }
 }
\ No newline at end of file