more comments, some smaller fixes

This commit is contained in:
2019-10-17 01:29:00 +02:00
parent cdd5d40236
commit 517883f907
3 changed files with 17 additions and 11 deletions

View File

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

View File

@@ -43,7 +43,7 @@ $(document).ready(function() {
} else { } else {
$("#comments").empty(); $("#comments").empty();
$("#statistics").empty(); $("#statistics").empty();
$("#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."); $("#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.");
} }
} }

View File

@@ -1,5 +1,6 @@
<?php <?php
/* load config. You normally don't want to edit something here */
require_once 'config.php'; require_once 'config.php';
$instance = $config['mastodon-instance']; $instance = $config['mastodon-instance'];
$uid = $config['user-id']; $uid = $config['user-id'];
@@ -78,20 +79,22 @@ function collectToots($instance, $uid, $min_id, $searchurl) {
} }
/* Find out if a toot contains the searched URL */ /* Find out if a toot contains the searched URL */
function analyzeToot($instance, $id, $searchurl) { function analyzeToot($instance, $id, $searchurl) {
//debug("Searching for $searchurl in $id"); debug("Searching for $searchurl in $id");
$raw = file_get_contents("$instance/api/v1/statuses/$id"); $raw = file_get_contents("$instance/api/v1/statuses/$id");
$json = json_decode($raw, true); $json = json_decode($raw, true);
// search for $searchurl inside of <a> tags, until (and excluding) a "
preg_match("|$searchurl.+?(?=\")|i", $json['content'], $matches); preg_match("|$searchurl.+?(?=\")|i", $json['content'], $matches);
if(!empty($matches)) { if(!empty($matches)) {
return(strtolower($matches[0])); return(strtolower($matches[0])); // take first match inside toot
} else { } else {
return(""); return("");
} }
} }
/* of context, extract the interesting bits */ /* of context, extract the interesting bits */
function filterComments($descendants, $root, &$result) { function filterComments($descendants, $root, &$result) {
// go through each comment
foreach ($descendants as $d) { foreach ($descendants as $d) {
$result['comments'][$d['id']] = [ $result['comments'][$d['id']] = [
'author' => [ 'author' => [
@@ -182,7 +185,7 @@ if ($cachebreak) {
debug("Toots cache is up-to-date"); debug("Toots cache is up-to-date");
} }
/* prepare $result array */ /* prepare empty $result array with structure */
$result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]]; $result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]];
/* check if URL from $search exists in $toots */ /* check if URL from $search exists in $toots */
@@ -190,7 +193,7 @@ $id = array_keys(array_column($toots, 'url'), strtolower($search));
if (empty($id)) { if (empty($id)) {
debug("Blog URL \"$search\" has not been found"); debug("Blog URL \"$search\" has not been found");
} else { } else {
// if multiple exist, take the oldest one (highest array position) // if multiple toots with the searched URL exist, take the oldest one (largest array index)
$id = $toots[end($id)]['id']; $id = $toots[end($id)]['id'];
/* read cached comments, or reload new comments if cached data too old */ /* 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');
// headers to tell that result is JSON // headers to tell that result is JSON
header('Content-type: application/json'); header('Content-type: application/json');
// actually output result as JSON, to be digested by getcomments.js
echo json_encode($result); echo json_encode($result);
?> ?>