Browse Source

more comments, some smaller fixes

max.mehl 5 months ago
parent
commit
517883f907
No account linked to committer's email address

+ 8
- 6
themes/hugo-mastodon-comments/static/comments/config.sample.php View File

@@ -1,9 +1,11 @@
1 1
 <?php
2 2
 $config = [
3
-    'mastodon-instance' => 'https://mastodon.social',
4
-    'user-id' => 379833,
5
-    'search-url' => 'https?://fsfe.org', // please use https?:// as schema
6
-    'cache_toots' => 300,
7
-    'cache_comments' => 300,
8
-    'debug' => true
3
+    'mastodon-instance' => 'https://mastodon.social',  // URL of your Mastodon instance
4
+    'user-id' => 379833,      // Your Mastodon-ID. A bit tricky to find out, the API might help
5
+    // the URL of your blog. All toots are searched for this string
6
+    // please use https?:// as schema
7
+    'search-url' => 'https?://fsfe.org',
8
+    'cache_toots' => 300,     // seconds to cache toots
9
+    'cache_comments' => 300,  // seconds to cache comments (per ID)
10
+    'debug' => false          // writes some debug messages in error_log
9 11
 ];

+ 1
- 1
themes/hugo-mastodon-comments/static/comments/getcomments.js View File

@@ -43,7 +43,7 @@ $(document).ready(function() {
43 43
             } else {
44 44
                 $("#comments").empty();
45 45
                 $("#statistics").empty();
46
-                $("#reference").append("Comments are handled by my <a href='" + MastodonUser + "'>Mastodon account</a>. Sadly this article wasn't published at Mastodon. Feel free to <a href='" + CommentsContact + "/'>send me a mail</a> if you want to share your thoughts regarding this topic.");
46
+                $("#reference").append("Comments are handled by my <a href='" + MastodonUser + "'>Mastodon account</a>. Sadly this article wasn't published at Mastodon. Feel free to <a href='" + CommentsContact + "'>send me a mail</a> if you want to share your thoughts regarding this topic.");
47 47
             }
48 48
 
49 49
         }

+ 8
- 4
themes/hugo-mastodon-comments/static/comments/getcomments.php View File

@@ -1,5 +1,6 @@
1 1
 <?php
2 2
 
3
+/* load config. You normally don't want to edit something here */
3 4
 require_once 'config.php';
4 5
 $instance = $config['mastodon-instance'];
5 6
 $uid = $config['user-id'];
@@ -78,20 +79,22 @@ function collectToots($instance, $uid, $min_id, $searchurl) {
78 79
 }
79 80
 /* Find out if a toot contains the searched URL */
80 81
 function analyzeToot($instance, $id, $searchurl) {
81
-  //debug("Searching for $searchurl in $id");
82
+  debug("Searching for $searchurl in $id");
82 83
   $raw = file_get_contents("$instance/api/v1/statuses/$id");
83 84
   $json = json_decode($raw, true);
84 85
 
86
+  // search for $searchurl inside of <a> tags, until (and excluding) a "
85 87
   preg_match("|$searchurl.+?(?=\")|i", $json['content'], $matches);
86 88
 
87 89
   if(!empty($matches)) {
88
-    return(strtolower($matches[0]));
90
+    return(strtolower($matches[0]));  // take first match inside toot
89 91
   } else {
90 92
     return("");
91 93
   }
92 94
 }
93 95
 /* of context, extract the interesting bits */
94 96
 function filterComments($descendants, $root, &$result) {
97
+  // go through each comment
95 98
   foreach ($descendants as $d) {
96 99
     $result['comments'][$d['id']] = [
97 100
       'author' => [
@@ -182,7 +185,7 @@ if ($cachebreak) {
182 185
   debug("Toots cache is up-to-date");
183 186
 }
184 187
 
185
-/* prepare $result array */
188
+/* prepare empty $result array with structure */
186 189
 $result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]];
187 190
 
188 191
 /* check if URL from $search exists in $toots */
@@ -190,7 +193,7 @@ $id = array_keys(array_column($toots, 'url'), strtolower($search));
190 193
 if (empty($id)) {
191 194
   debug("Blog URL \"$search\" has not been found");
192 195
 } else {
193
-  // if multiple exist, take the oldest one (highest array position)
196
+  // if multiple toots with the searched URL exist, take the oldest one (largest array index)
194 197
   $id = $toots[end($id)]['id'];
195 198
 
196 199
   /* read cached comments, or reload new comments if cached data too old */
@@ -219,6 +222,7 @@ header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
219 222
 // headers to tell that result is JSON
220 223
 header('Content-type: application/json');
221 224
 
225
+// actually output result as JSON, to be digested by getcomments.js
222 226
 echo json_encode($result);
223 227
 
224 228
 ?>

Loading…
Cancel
Save