2019-10-20 22:43:46 +02:00
/ *
SPDX - License - Identifier : AGPL - 3.0 - or - later
SPDX - FileCopyrightText : 2019 Max Mehl < mail @ mehl . mx >
2019-10-20 23:00:24 +02:00
SPDX - FileCopyrightText : 2019 Björn Schießle
2019-10-20 22:43:46 +02:00
* /
2026-02-23 17:38:01 +01:00
// Debug logging helper
var debugEnabled = false ;
function debugLog ( message , data ) {
if ( debugEnabled ) {
if ( data !== undefined ) {
console . log ( '[Mastodon Comments] ' + message , data ) ;
} else {
console . log ( '[Mastodon Comments] ' + message ) ;
}
}
}
2019-10-20 22:43:46 +02:00
$ ( document ) . ready ( function ( ) {
// check if we show a blog post or not. Regex is defined in site-wide config
var patt = new RegExp ( BlogRegex ) ;
var isArticle = patt . test ( RelPermalink ) ;
if ( isArticle === false ) {
console . log ( "Not a blog post, no need to search for comments" ) ;
return ;
}
2026-02-23 17:38:01 +01:00
debugLog ( 'Searching for comments for: ' + RelPermalink ) ;
2026-02-24 11:55:42 +01:00
// Check if a direct Mastodon toot URL is provided
var ajaxData = { search : RelPermalink } ;
if ( MastodonTootUrl && MastodonTootUrl !== "" ) {
// Pass the full URL to PHP so it can extract instance and ID
ajaxData . toot _url = MastodonTootUrl ;
debugLog ( 'Using predefined Mastodon toot URL: ' + MastodonTootUrl ) ;
}
2019-10-20 22:43:46 +02:00
$ . ajax ( {
url : "/comments/getcomments.php" ,
type : "get" ,
2026-02-24 11:55:42 +01:00
data : ajaxData ,
2019-10-20 22:43:46 +02:00
success : function ( data ) {
2026-02-23 17:38:01 +01:00
// Enable debug logging if backend has debug enabled
if ( data . debug === true ) {
debugEnabled = true ;
debugLog ( 'Debug mode enabled' ) ;
}
debugLog ( 'Received data from backend' , data ) ;
2019-10-20 22:43:46 +02:00
var stats = data . stats ;
var root = data . stats . root ;
2026-02-23 17:38:01 +01:00
debugLog ( 'Stats - Favs: ' + stats . favs + ', Reblogs: ' + stats . reblogs + ', Replies: ' + stats . replies ) ;
debugLog ( 'Root toot ID: ' + root ) ;
2019-10-20 22:43:46 +02:00
$ ( "#like-count-container" ) . append ( '<div id="mastodon-like-count"><a href="' + stats . url + '"><span title="Likes"><i class="fa fa-star"></i>' + stats . favs + '</span></a></div></div>' ) ;
$ ( "#reblog-count-container" ) . append ( '<div id="mastodon-reblog-count"><a href="' + stats . url + '"><span title="Reblogs"><i class="fa fa-retweet"></i>' + stats . reblogs + '</span></a></div></div>' ) ;
$ ( "#reply-count-container" ) . append ( '<div id="mastodon-reply-count"><a href="' + stats . url + '"><span title="Comments"><i class="fa fa-comments"></i>' + stats . replies + '</span></a></div></div>' ) ;
2026-02-23 17:38:01 +01:00
2019-10-20 22:43:46 +02:00
var comments = data . comments ;
2026-02-23 17:38:01 +01:00
var commentCount = Object . keys ( comments ) . length ;
debugLog ( 'Processing ' + commentCount + ' comments' ) ;
2019-10-20 22:43:46 +02:00
$ . each ( comments , function ( key , value ) {
2026-02-23 17:38:01 +01:00
debugLog ( 'Adding comment: ' + key , value ) ;
2019-10-20 22:43:46 +02:00
var timestamp = Date . parse ( value . date ) ;
var date = new Date ( timestamp ) ;
var comment = "<div class='comment' id='" + key + "'>" ;
comment += "<img class='avatar' src='" + value . author . avatar + "' />" ;
comment += "<div class='author'><a class='displayName' href='" + value . author . url + "'>" + value . author . display _name + "</a> wrote at " ;
comment += "<a class='date' href='" + value . url + "'>" + date . toDateString ( ) + ', ' + date . toLocaleTimeString ( ) + "</a></div>" ;
comment += "<div class='toot'>" + value . toot + "</div>" ;
comment += "</div>" ;
var parentComment = document . getElementById ( value . reply _to ) ;
if ( value . reply _to === root || parentComment === null ) {
$ ( "#comments" ) . append ( comment ) ;
2026-02-23 17:38:01 +01:00
debugLog ( ' → Added as top-level comment' ) ;
2019-10-20 22:43:46 +02:00
} else {
var selector = '#' + value . reply _to ;
$ ( selector ) . append ( comment ) ;
2026-02-23 17:38:01 +01:00
debugLog ( ' → Added as reply to: ' + value . reply _to ) ;
2019-10-20 22:43:46 +02:00
}
} ) ;
2026-02-23 17:38:01 +01:00
2019-10-20 22:43:46 +02:00
if ( parseInt ( root ) > 0 ) {
$ ( "#reference" ) . append ( "<a href='" + MastodonUser + "/statuses/" + root + "'>Join the discussion on Mastodon!</a>" ) ;
2026-02-23 17:38:01 +01:00
debugLog ( 'Added Mastodon discussion link for root: ' + root ) ;
2019-10-20 22:43:46 +02:00
} else {
$ ( "#comments" ) . 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." ) ;
2026-02-23 17:38:01 +01:00
debugLog ( 'No Mastodon toot found for this article' ) ;
2019-10-20 22:43:46 +02:00
}
2026-02-23 17:38:01 +01:00
} ,
error : function ( xhr , status , error ) {
console . error ( '[Mastodon Comments] AJAX Error:' , status , error ) ;
console . error ( '[Mastodon Comments] Response:' , xhr . responseText ) ;
2019-10-20 22:43:46 +02:00
}
} ) ;
} ) ;