2022-11-19 08:56:09 -07:00
|
|
|
/*
|
|
|
|
Dies ist eine erste Implementierung, da ist noch vieeeel zu tun :)
|
|
|
|
*/
|
|
|
|
|
2022-11-19 12:01:33 -07:00
|
|
|
let ownProfilePic;
|
2022-11-19 08:56:09 -07:00
|
|
|
let userInfo;
|
|
|
|
let connection_list = {};
|
2022-11-19 12:01:33 -07:00
|
|
|
let requestCounter = 1;
|
2022-11-20 05:03:18 -07:00
|
|
|
// Request Limit
|
|
|
|
let request_limit;
|
2022-11-19 08:56:09 -07:00
|
|
|
|
|
|
|
// The main function called by the button-click
|
|
|
|
function circle_main() {
|
2022-11-19 09:53:53 -07:00
|
|
|
// Make Button invisible to prevent clicking
|
|
|
|
document.getElementById("btn_create").style.display = "none";
|
2022-11-19 12:52:03 -07:00
|
|
|
// Reset all global variables
|
2022-11-20 05:03:18 -07:00
|
|
|
[ownProfilePic, userInfo, connection_list, requestCounter, request_limit] = [null, null, {}, 1, 50];
|
2022-11-19 08:56:09 -07:00
|
|
|
// Get handle from Textfield
|
|
|
|
let mastodon_handle = document.getElementById("txt_mastodon_handle").value;
|
|
|
|
userInfo = formatedUserHandle(mastodon_handle);
|
2022-11-20 05:03:18 -07:00
|
|
|
// Do all the Magic for creating circle
|
2022-11-19 08:56:09 -07:00
|
|
|
getStatuses();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the Mastodon Handle to an array: [username, userID, instance.tld]
|
|
|
|
function formatedUserHandle(mastodon_handle) {
|
|
|
|
// Remove leading @
|
|
|
|
if (mastodon_handle.charAt(0) === '@') mastodon_handle = mastodon_handle.substr(1);
|
2022-11-20 06:51:58 -07:00
|
|
|
// Remove Spaces
|
|
|
|
mastodon_handle = mastodon_handle.replaceAll(" ","");
|
2022-11-20 05:03:18 -07:00
|
|
|
// Split handle into name and instance
|
2022-11-19 08:56:09 -07:00
|
|
|
mastodon_handle = mastodon_handle.split("@");
|
2022-11-20 05:03:18 -07:00
|
|
|
// Return the array (fetch user ID with getIdFromName)
|
2022-11-19 08:56:09 -07:00
|
|
|
return [mastodon_handle[0], getIdFromName(mastodon_handle[0], mastodon_handle[1]), mastodon_handle[1]];
|
|
|
|
}
|
|
|
|
|
2022-11-20 05:03:18 -07:00
|
|
|
// Get the user ID from the handle (synchronous request! :( )
|
2022-11-19 08:56:09 -07:00
|
|
|
function getIdFromName(name, server) {
|
|
|
|
var xmlHttp = new XMLHttpRequest();
|
|
|
|
let url = "https://"+server+"/api/v1/accounts/lookup?acct="+name;
|
2022-11-20 05:03:18 -07:00
|
|
|
xmlHttp.open( "GET", url, false );
|
2022-11-19 08:56:09 -07:00
|
|
|
xmlHttp.send( null );
|
2022-11-19 12:01:33 -07:00
|
|
|
let response = JSON.parse(xmlHttp.responseText);
|
|
|
|
ownProfilePic = response["avatar"];
|
|
|
|
return response["id"];
|
2022-11-19 08:56:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a JSON String with all the posted statuses from the account and call processStatuses()
|
2022-11-19 12:01:33 -07:00
|
|
|
async function getStatuses() {
|
2022-11-19 08:56:09 -07:00
|
|
|
// Build the URL
|
2022-11-20 05:03:18 -07:00
|
|
|
let url = "https://"+userInfo[2]+"/api/v1/accounts/"+userInfo[1]+"/statuses?exclude_replies=true&exclude_reblogs=true";
|
2022-11-19 08:56:09 -07:00
|
|
|
// Do the async http request and call processStatuses()
|
|
|
|
httpRequest(url, processStatuses);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the JSON String into an array
|
|
|
|
function processStatuses(statuses) {
|
|
|
|
jsonStat = JSON.parse(statuses);
|
|
|
|
|
2022-11-19 09:53:53 -07:00
|
|
|
for (var i=0; i<jsonStat.length; i++) {
|
2022-11-20 05:03:18 -07:00
|
|
|
evaluateStatus(jsonStat[i]["id"], (jsonStat[i]["favourites_count"]>0), (jsonStat[i]["reblogs_count"]>0));
|
|
|
|
request_limit--;
|
|
|
|
if (request_limit<0) return;
|
2022-11-19 08:56:09 -07:00
|
|
|
}
|
2022-11-20 05:03:18 -07:00
|
|
|
|
|
|
|
// Do another API request to fetch older Posts?
|
2022-11-19 08:56:09 -07:00
|
|
|
}
|
|
|
|
|
2022-11-19 09:53:53 -07:00
|
|
|
// Get all Reblogs and Favs for a status update
|
2022-11-19 08:56:09 -07:00
|
|
|
function evaluateStatus(id, faved, rebloged) {
|
2022-11-19 12:01:33 -07:00
|
|
|
requestCounter += faved+rebloged+1;
|
2022-11-19 08:56:09 -07:00
|
|
|
// Build the URL
|
|
|
|
let url1 = "https://"+userInfo[2]+"/api/v1/statuses/"+id+"/reblogged_by";
|
|
|
|
// Do the async http request
|
2022-11-19 09:53:53 -07:00
|
|
|
if (rebloged) httpRequest(url1, evalStatusInteractions, 1.3);
|
|
|
|
|
|
|
|
// Build the URL
|
|
|
|
let url2 = "https://"+userInfo[2]+"/api/v1/statuses/"+id+"/context";
|
|
|
|
// Do the async http request
|
2022-11-19 12:01:33 -07:00
|
|
|
httpRequest(url2, evalReplies, 1.1);
|
2022-11-19 08:56:09 -07:00
|
|
|
|
|
|
|
// Build the URL
|
2022-11-19 09:53:53 -07:00
|
|
|
let url3 = "https://"+userInfo[2]+"/api/v1/statuses/"+id+"/favourited_by";
|
2022-11-19 08:56:09 -07:00
|
|
|
// Do the async http request
|
2022-11-19 09:53:53 -07:00
|
|
|
if (faved) httpRequest(url3, evalStatusInteractions, 1.0);
|
2022-11-19 08:56:09 -07:00
|
|
|
}
|
|
|
|
|
2022-11-19 09:53:53 -07:00
|
|
|
// Evaluate the direct replies to tweets (no trees yet :( )
|
|
|
|
function evalReplies(jsonString, plus) {
|
|
|
|
let jsonArray = JSON.parse(jsonString)["descendants"];
|
|
|
|
|
|
|
|
for (var i=0; i<jsonArray.length; i++) {
|
|
|
|
incConnectionValue(jsonArray[i]["account"], plus);
|
|
|
|
}
|
2022-11-19 12:01:33 -07:00
|
|
|
|
|
|
|
if (requestCounter<=0) showConnections();
|
2022-11-19 09:53:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate the Favs and Reposts
|
|
|
|
function evalStatusInteractions(jsonString, plus) {
|
2022-11-19 08:56:09 -07:00
|
|
|
let jsonArray = JSON.parse(jsonString);
|
|
|
|
|
|
|
|
for (var i=0; i<jsonArray.length; i++) {
|
|
|
|
incConnectionValue(jsonArray[i], plus);
|
|
|
|
}
|
2022-11-19 12:01:33 -07:00
|
|
|
|
|
|
|
if (requestCounter<=0) showConnections();
|
2022-11-19 08:56:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// increment the relationship value by the integer "plus" (3 for reblogs, 1 for likes)
|
|
|
|
function incConnectionValue(conJSON, plus) {
|
|
|
|
let id = conJSON["id"];
|
|
|
|
// Test if a connection was already discovered
|
|
|
|
if (!(id in connection_list)) {
|
|
|
|
// NO? call addNewConnection and create the connection!
|
|
|
|
addNewConnection(conJSON)
|
|
|
|
}
|
|
|
|
// Increment the connection strength
|
|
|
|
connection_list[id]["conStrength"] = connection_list[id]["conStrength"] + plus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new node in the connection_list dictionary
|
|
|
|
function addNewConnection(jsonArray) {
|
|
|
|
connection_list[jsonArray["id"]] = {};
|
|
|
|
connection_list[jsonArray["id"]]["conStrength"] = 0;
|
|
|
|
connection_list[jsonArray["id"]]["acct"] = jsonArray["acct"];
|
|
|
|
connection_list[jsonArray["id"]]["pic"] = jsonArray["avatar"];
|
|
|
|
connection_list[jsonArray["id"]]["name"] = jsonArray["display_name"]
|
|
|
|
connection_list[jsonArray["id"]]["bot"] = jsonArray["bot"];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function showConnections() {
|
2022-11-19 09:53:53 -07:00
|
|
|
// Remove own User from Dict
|
|
|
|
if (userInfo[1] in connection_list) delete connection_list[userInfo[1]];
|
|
|
|
|
|
|
|
// Sort dict into Array items
|
2022-11-19 08:56:09 -07:00
|
|
|
var items = Object.keys(connection_list).map(
|
|
|
|
(key) => { return [key, connection_list[key]] });
|
|
|
|
items.sort(
|
|
|
|
(first, second) => { return second[1]["conStrength"] - first[1]["conStrength"] }
|
|
|
|
);
|
2022-11-19 09:53:53 -07:00
|
|
|
|
2022-11-20 06:51:58 -07:00
|
|
|
// Also export the Username List
|
2022-11-20 05:03:18 -07:00
|
|
|
let userDataExport = {};
|
2022-11-20 06:51:58 -07:00
|
|
|
let usersDivs = [document.getElementById("ud1"), document.getElementById("ud2"), document.getElementById("ud3")];
|
2022-11-20 05:03:18 -07:00
|
|
|
for (var i=0; i<items.length; i++) {
|
2022-11-20 06:51:58 -07:00
|
|
|
// Create a new html Element
|
|
|
|
let newUser = document.createElement("p");
|
|
|
|
newUser.innerText = items[i][1]["acct"];
|
|
|
|
|
|
|
|
// Determine the column for the data
|
|
|
|
let udNum = 0;
|
|
|
|
if (i > numb[0]) udNum = 1;
|
|
|
|
if (i > numb[0]+numb[1]) udNum = 2;
|
|
|
|
usersDivs[udNum].appendChild(newUser);
|
|
|
|
|
|
|
|
// Belongs to the hidden Export - Maybe for further Projects
|
|
|
|
// userDataExport[items[i][0]] = items[i][1]["conStrength"].toFixed(1);
|
2022-11-20 05:03:18 -07:00
|
|
|
}
|
2022-11-20 06:51:58 -07:00
|
|
|
//document.getElementById("outDiv").innerText = JSON.stringify(userDataExport);
|
2022-11-20 05:03:18 -07:00
|
|
|
|
2022-11-19 09:53:53 -07:00
|
|
|
// Render the Objects
|
2022-11-19 12:52:03 -07:00
|
|
|
document.getElementById("btn_download").style.display = "inline";
|
2022-11-19 12:01:33 -07:00
|
|
|
render(items);
|
2022-11-19 08:56:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function createUserObj(usr) {
|
|
|
|
let usrElement = document.createElement("div");
|
2022-11-19 12:01:33 -07:00
|
|
|
usrElement.innerHTML = "<img src=\""+usr["pic"]+"\" width=\"20px\"> <b>"+usr["name"]+"</b> "+usr["acct"];
|
2022-11-19 08:56:09 -07:00
|
|
|
document.getElementById("outDiv").appendChild(usrElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Function for the http request
|
|
|
|
function httpRequest(url, callback, callbackVal=null)
|
|
|
|
{
|
|
|
|
var xmlHttp = new XMLHttpRequest();
|
|
|
|
xmlHttp.onreadystatechange = function() {
|
2022-11-19 12:01:33 -07:00
|
|
|
if (xmlHttp.readyState == 4) {
|
|
|
|
requestCounter--;
|
|
|
|
if (xmlHttp.status == 200) {
|
|
|
|
callback(xmlHttp.responseText, callbackVal);
|
|
|
|
} else
|
|
|
|
callback("[]", callbackVal);
|
|
|
|
}
|
2022-11-19 08:56:09 -07:00
|
|
|
}
|
|
|
|
xmlHttp.open("GET", url, true);
|
|
|
|
xmlHttp.send(null);
|
2022-11-19 12:52:03 -07:00
|
|
|
}
|
|
|
|
|
2022-11-20 06:51:58 -07:00
|
|
|
function downloadImage(){
|
|
|
|
var link = document.createElement('a');
|
|
|
|
link.download = 'mastodon-circle.png';
|
|
|
|
link.href = document.getElementById('canvas').toDataURL()
|
|
|
|
link.click();
|
|
|
|
}
|