web/src/js/live-polemic.js
changeset 1571 4a1e6952afe5
parent 1558 761ba7426984
child 1575 ce1d5b0d1479
--- a/web/src/js/live-polemic.js	Tue Oct 22 10:01:37 2024 +0200
+++ b/web/src/js/live-polemic.js	Thu Nov 07 22:38:14 2024 +0100
@@ -54,13 +54,22 @@
     return typeof(global[varkey]) == "undefined" ? defaultValue : global[varkey];
 }
 
+function getSocialGroupUri(social_group) {
+    const groupParts = social_group.replace(/^\@+/,"").split('@');
+    return `https://${groupParts[1]}/u/${groupParts[0]}`
+}
+
+
 //var i10n = getGlobal('i10n', { "rechercher" : "Rechercher" });
 
-var suggested_keywords = getGlobal('suggested_keywords',[]);
+const suggested_keywords = getGlobal('suggested_keywords',[]);
+
+const max_pages = getGlobal('max_pages', 5);
 
-var max_pages = getGlobal('max_pages', 5);
-
-var social_network = getGlobal('social_network', 'Twitter');
+const social_network = getGlobal('social_network', 'Twitter');
+const social_login_domain = getGlobal('social_login_domain');
+const social_access_token = getGlobal('social_access_token');
+const social_group = getGlobal('social_group');
 
 tracking_keywords = _(getGlobal('tracking_keywords', [])).map(function(_w) {
     return _w.toLowerCase();
@@ -111,6 +120,29 @@
             ]
     }
 
+function getSocialData() {
+    return $.ajax({
+        beforeSend: function(request) {
+            request.setRequestHeader('Authorization', `Bearer ${social_access_token}`);
+        },
+        type: "GET",
+        dataType: "json",
+        url: `https://${social_login_domain}/api/v1/accounts/verify_credentials`,
+    });
+}
+
+function getFollowing(user_id) {
+    return $.ajax({
+        beforeSend: function(request) {
+            request.setRequestHeader('Authorization', `Bearer ${social_access_token}`);
+        },
+        type: "GET",
+        dataType: "json",
+        url: `https://${social_login_domain}/api/v1/accounts/${user_id}/following`,
+    });
+}
+
+
 function getTweets(options) {
     
     function getTweetUrl(url) {
@@ -283,9 +315,13 @@
     	tweet.retweeted_status_id = tweet.retweeted_status.id_str;
         backRef( tweet.id, tweet.retweeted_status_id, "retweet" );
     }
-    
-    
-    var tab = tweet.full_text.replace(twCx.urlRegExp,'').match(twCx.wordRegExp);
+
+    // clean full text from html
+    const temp_div_element = document.createElement("div");
+    temp_div_element.innerHTML = tweet.full_text;
+    const full_text = temp_div_element.textContent || temp_div_element.innerText || tweet.full_text;
+
+    var tab = full_text.replace(twCx.urlRegExp,'').match(twCx.wordRegExp);
     _(tab).each(function(w) {
         var word = w.toLowerCase();
         if (_(twCx.stopWords).indexOf(word) == -1 && _(tracking_keywords).indexOf(word) == -1 && word[0] != '@') {
@@ -607,11 +643,11 @@
         txt += highlight( full_text.substring(lastend) );
         html += '<p class="tweet_text"><b>' + a_user + '<span title="'+tweet.user.screen_name+ '">' +highlight('@' + tweet.user.name) + '</span></a>' + ( className == 'full' ? ' (' + tweet.user.name + ')</b><br />' : '</b> : ') + txt + '</p>';
         if (className == 'full' && el == 'li') {
-            html += '<div class="tweet_actions"><a href="' + htmlAdapter.getMsgUrl() + '" onclick="tweetPopup(this.href); return false;" target="_blank">afficher tweet</a>';
+            html += '<div class="tweet_actions"><a href="' + htmlAdapter.getMsgUrl() + '" onclick="tweetPopup(this.href); return false;" target="_blank">afficher message</a>';
             const replyUrl = htmlAdapter.getMsgReplyUrl();
             if (replyUrl) { html += '<a href="' + replyUrl + '" onclick="tweetPopup(this.href); return false;" target="_blank">répondre</a> · '; }
             const retweetUrl = htmlAdapter.getMsgRetweetUrl();
-            if (retweetUrl) { html += '<a href="' + retweetUrl + '" onclick="tweetPopup(this.href); return false;" target="_blank">retweeter</a> · '; }
+            if (retweetUrl) { html += '<a href="' + retweetUrl + '" onclick="tweetPopup(this.href); return false;" target="_blank">retransmettre</a> · '; }
             const favoriteUrl = htmlAdapter.getMsgFavoriteUrl();
             if (favoriteUrl) { html += '<a href="' + favoriteUrl + '" onclick="tweetPopup(this.href); return false;" target="_blank">favori</a>'; }
             html += '</div>';
@@ -1161,7 +1197,50 @@
     document.location.href = "data:text/json;base64," + btoa(_buf);
 }
 
-$(document).ready(function() {
+function followSocialGroup() {
+    $.ajax({
+        beforeSend: function(request) {
+            request.setRequestHeader('Authorization', `Bearer ${social_access_token}`);
+        },
+        type: "POST",
+        dataType: "json",
+        url: `https://${social_login_domain}/api/v1/accounts/`,
+    });
+}
+
+function buildFollowLink() {
+    const sgroup = social_group.replace(/^\@+/,"")
+    $("#twwWrap").prepend(`<div id="socialGroupFollow" ><a target=”_blank” href="https://${social_login_domain}/authorize_interaction?uri=${sgroup}">Suivez le groupe ${social_group}</a> et recharger cette page</div>`)
+}
+
+function checkSocialGroup() {
+    getSocialData().then( (data) => {
+        getFollowing(data.id).then((following_data) => {
+            let is_following_group = false;
+            for(const user_def of following_data) {
+                const group_uri = getSocialGroupUri(social_group)
+                if (user_def.uri == group_uri) {
+                    is_following_group = true;
+                }
+            }
+
+            $("#socialGroupFollow").remove();
+            if(!is_following_group) {
+                buildFollowLink();
+                setTimeout(function() {
+                    checkSocialGroup();
+                }, 5000);
+            }
+        });
+    });
+}
+
+$(function() {
+    //twwWrap
+    if(social_network == "Mastodon") {
+        checkSocialGroup();
+    }
+
     twCx.tlWidth = $("#timeline").width();
     twCx.tlHeight = $("#timeline").height();
     twCx.tlPaper = Raphael("timeline", twCx.tlWidth, twCx.tlHeight);
@@ -1338,7 +1417,6 @@
             })
         },
         "cbEnd" : function() {
-            console.log(this.tweets);
             loadTweets(this.tweets);
             setInterval(function() {
                 getTweets({
@@ -1360,5 +1438,7 @@
     rolloverTweet,
     selectTweet,
     filtrerTexte,
-    tweetPopup
+    tweetPopup,
+    getSocialData,
+    getFollowing
 }
\ No newline at end of file