From 55d27a84b4b448f5db389ff88e060da8cd4c05f8 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 19 Nov 2017 12:44:58 +0000
Subject: [PATCH] Wipe username when deleting comments + delete entriely if no
 replies are detected

---
 Changelog.md                 |  1 +
 build/index.php              |  3 ++-
 module_index.json            |  2 +-
 modules/feature-comments.php | 12 ++++++++++--
 4 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 9c68ef0..7bba512 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -11,6 +11,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
  - Added new "« Parent Page" to subpages so that you can easily visit their parent pages
  - Users can now delete their own comments, and users logged in as a moderator or better can delete anyone's comments.
      - Added new `comment-delete` action
+     - Comments are deleted entirely if they have no replies - otherwise the username & message are wiped
  - The `history` action now supports `format=json` and `format=csv`
  - Added tags next to the names of pages in the search results
  - Added new `random_page_exclude` setting that allows you to exclude pages from the random action with a (PHP) regular expression
diff --git a/build/index.php b/build/index.php
index 0b73754..5ceaf3f 100644
--- a/build/index.php
+++ b/build/index.php
@@ -2853,6 +2853,7 @@ function delete_comment(&$comment_data, $target_id)
 	
 	for($i = 0; $i < $comment_count; $i++) {
 		if($comment_data[$i]->id == $target_id) {
+			unset($comment_data[$i]->username);
 			$comment_data[$i]->message = "_[Deleted]_";
 			return true;
 		}
@@ -2923,7 +2924,7 @@ function render_comments($comments_data, $depth = 0)
 		$comment = $comments_data[$i];
 		
 		$result .= "\t<div class='comment' id='comment-$comment->id' data-comment-id='$comment->id'>\n";
-		$result .= "\t<p class='comment-header'><span class='name'>" . page_renderer::render_username($comment->username) . "</span> said:</p>";
+		$result .= "\t<p class='comment-header'><span class='name'>" . page_renderer::render_username($comment->username ?? "<em>Unknown</em>") . "</span> said:</p>";
 		$result .= "\t<div class='comment-body'>\n";
 		$result .= "\t\t" . parse_page_source($comment->message);
 		$result .= "\t</div>\n";
diff --git a/module_index.json b/module_index.json
index 111d8d9..ee0ebfc 100755
--- a/module_index.json
+++ b/module_index.json
@@ -59,7 +59,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds threaded comments to the bottom of every page.",
         "id": "feature-comments",
-        "lastupdate": 1511092311,
+        "lastupdate": 1511092595,
         "optional": false
     },
     {
diff --git a/modules/feature-comments.php b/modules/feature-comments.php
index af4b9f1..c0b6e8e 100644
--- a/modules/feature-comments.php
+++ b/modules/feature-comments.php
@@ -338,7 +338,15 @@ function delete_comment(&$comment_data, $target_id)
 	
 	for($i = 0; $i < $comment_count; $i++) {
 		if($comment_data[$i]->id == $target_id) {
-			$comment_data[$i]->message = "_[Deleted]_";
+			if(count($comment_data[$i]->replies) == 0) {
+				unset($comment_data[$i]);
+				// Reindex the comment list before returning
+				$comment_data = array_values($comment_data);
+			}
+			else {
+				unset($comment_data[$i]->username);
+				$comment_data[$i]->message = "_[Deleted]_";
+			}
 			return true;
 		}
 		if(count($comment_data[$i]->replies) > 0 &&
@@ -408,7 +416,7 @@ function render_comments($comments_data, $depth = 0)
 		$comment = $comments_data[$i];
 		
 		$result .= "\t<div class='comment' id='comment-$comment->id' data-comment-id='$comment->id'>\n";
-		$result .= "\t<p class='comment-header'><span class='name'>" . page_renderer::render_username($comment->username) . "</span> said:</p>";
+		$result .= "\t<p class='comment-header'><span class='name'>" . page_renderer::render_username($comment->username ?? "<em>Unknown</em>") . "</span> said:</p>";
 		$result .= "\t<div class='comment-body'>\n";
 		$result .= "\t\t" . parse_page_source($comment->message);
 		$result .= "\t</div>\n";
