2023-04-22 17:09:59 +02:00
|
|
|
var Tasker = {};
|
|
|
|
var ApiCache = {Notes: {}, Profiles: {},};
|
2023-04-19 00:16:35 +02:00
|
|
|
|
2023-04-19 18:06:08 +02:00
|
|
|
function DoAsync(First, Then, Data) {
|
|
|
|
var Job = RndId();
|
2023-04-22 17:09:59 +02:00
|
|
|
Tasker[Job] = {
|
2023-04-19 18:06:08 +02:00
|
|
|
Remains: 0,
|
|
|
|
Return(Data) {
|
|
|
|
this.Remains -= 1;
|
|
|
|
this.Result = Data;
|
|
|
|
},
|
2023-04-19 00:16:35 +02:00
|
|
|
};
|
2023-04-19 18:06:08 +02:00
|
|
|
// Call all First functs
|
2023-04-19 12:18:14 +02:00
|
|
|
ForceList(First).forEach(function(Fun, Data){
|
|
|
|
var Task = RndId();
|
2023-04-19 18:06:08 +02:00
|
|
|
var Proc = [Job, Task];
|
2023-04-22 17:09:59 +02:00
|
|
|
Tasker[Job][Task] = {};
|
|
|
|
Tasker[Job].Remains += 1;
|
2023-04-19 18:06:08 +02:00
|
|
|
//Fun(Proc, Data);
|
|
|
|
Data ? Fun(Data, Proc) : Fun(Proc);
|
2023-04-19 12:18:14 +02:00
|
|
|
});
|
2023-04-19 18:06:08 +02:00
|
|
|
// Continuosly check when First functs completed
|
2023-04-22 17:09:59 +02:00
|
|
|
Tasker[Job].Interval = setInterval(function(Job, Then){
|
|
|
|
if (Tasker[Job].Remains <= 0) {
|
|
|
|
clearInterval(Tasker[Job].Interval);
|
2023-04-19 18:06:08 +02:00
|
|
|
// Call all Then functs
|
2023-04-19 12:18:14 +02:00
|
|
|
ForceList(Then).forEach(function(Fun){
|
2023-04-22 17:09:59 +02:00
|
|
|
Fun(Tasker[Job].Result);
|
2023-04-19 12:18:14 +02:00
|
|
|
});
|
2023-04-22 17:09:59 +02:00
|
|
|
delete Tasker[Job];
|
2023-04-19 12:18:14 +02:00
|
|
|
};
|
2023-04-19 18:06:08 +02:00
|
|
|
}, 50, Job, Then);
|
|
|
|
return Job;
|
2023-04-19 12:18:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function HttpCodeGood(Code) {
|
|
|
|
var Unit = String(Code)[0];
|
|
|
|
if (['1', '2', '3'].includes(Unit)) {
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
if (['4', '5'].includes(Unit)) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function ApiCall(Data, Proc) {
|
2023-04-19 00:16:35 +02:00
|
|
|
// Data = {Target: "Friendica", Method: "...", Data: {}, Call: (), CallFine: (), CallFail: ()}
|
|
|
|
var Req = new XMLHttpRequest();
|
2023-04-19 12:18:14 +02:00
|
|
|
Req.Proc = Proc;
|
2023-04-19 00:16:35 +02:00
|
|
|
Req.onloadend = function(){
|
2023-04-20 15:17:00 +02:00
|
|
|
try {
|
|
|
|
this.responseJson = JSON.parse(this.responseText);
|
|
|
|
this.responseLog = this.responseJson;
|
|
|
|
} catch(Ex) {
|
|
|
|
this.responseLog = this.responseText;
|
|
|
|
};
|
2023-04-19 12:18:14 +02:00
|
|
|
if (Data.Call) {
|
|
|
|
Data.Call(this);
|
2023-04-20 00:19:19 +02:00
|
|
|
};
|
2023-04-19 12:18:14 +02:00
|
|
|
if (HttpCodeGood(this.status)) {
|
2023-04-20 15:17:00 +02:00
|
|
|
LogDebug([this.status, this.responseLog], 'l');
|
2023-04-19 12:18:14 +02:00
|
|
|
if (Data.CallFine) {
|
|
|
|
Data.CallFine(this);
|
|
|
|
};
|
|
|
|
} else {
|
2023-04-20 15:17:00 +02:00
|
|
|
LogDebug([this.status, this.responseLog], 'e');
|
2023-04-19 12:18:14 +02:00
|
|
|
if (Data.CallFail) {
|
|
|
|
Data.CallFail(this);
|
2023-04-19 00:16:35 +02:00
|
|
|
};
|
2023-04-19 12:18:14 +02:00
|
|
|
};
|
2023-04-19 00:16:35 +02:00
|
|
|
};
|
|
|
|
if (Data.Target == 'Mastodon') {
|
|
|
|
Req.open('GET', `${MastodonUrl}/api/v1/${Data.Method}`, true);
|
2023-04-20 00:19:19 +02:00
|
|
|
} else
|
2023-04-19 00:16:35 +02:00
|
|
|
if (Data.Target == 'Friendica') {
|
|
|
|
Req.open('GET', `${FriendicaUrl}/api/${Data.Method}.json`, true);
|
|
|
|
Req.setRequestHeader('Authorization', `Basic ${btoa(FriendicaCredentials)}`);
|
|
|
|
};
|
|
|
|
Req.send();
|
|
|
|
};
|
|
|
|
|
2023-04-20 22:08:38 +02:00
|
|
|
/*
|
2023-04-19 00:16:35 +02:00
|
|
|
function DisplayFriendicaTimeline(Timeline) {
|
|
|
|
ApiCall({Target: "Friendica", Method: Timeline, CallFine: function(Res){
|
|
|
|
DataView.innerHTML = Res.responseText;
|
|
|
|
JSON.parse(DataView.innerHTML).forEach(function(Item){
|
|
|
|
var Title = Item.friendica_title ? `<h2>${Item.friendica_title}</h2>` : '';
|
|
|
|
TimelineView.innerHTML += `<div class=PostView>
|
|
|
|
<a href="${Item.external_url}">${Item.created_at}</a>
|
|
|
|
${Item.friendica_author.url}
|
|
|
|
${Title}
|
|
|
|
${Item.friendica_html}
|
|
|
|
</div>`;
|
|
|
|
});
|
|
|
|
}});
|
|
|
|
};
|
2023-04-20 22:08:38 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
function MakeWindow(Attrs) {
|
|
|
|
var Window = document.createElement('div');
|
|
|
|
if (Attrs) {
|
|
|
|
Object.keys(Attrs).forEach(function(Attr){
|
|
|
|
Window[Attr] = Attrs[Attr];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Window.className += ' Window';
|
|
|
|
Root.appendChild(Window);
|
|
|
|
return Window;
|
|
|
|
};
|
|
|
|
|
2023-04-22 17:09:59 +02:00
|
|
|
function DisplayProfile(Profile) {
|
|
|
|
var Window = MakeWindow({className: "Profile"});
|
2023-04-22 10:06:31 +02:00
|
|
|
Window.innerHTML += `<div class="" style="display: inline-block;">
|
2023-04-22 17:09:59 +02:00
|
|
|
<a href="${Profile.Url}">
|
2023-04-22 10:06:31 +02:00
|
|
|
<div>
|
2023-04-22 17:09:59 +02:00
|
|
|
<img class="" src="${Profile.Banner}"/>
|
2023-04-22 10:06:31 +02:00
|
|
|
</div>
|
|
|
|
<div>
|
2023-04-22 17:09:59 +02:00
|
|
|
<img class="" src="${Profile.Icon}"/>
|
|
|
|
${Profile.Name}
|
2023-04-22 10:06:31 +02:00
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
</div>`;
|
2023-04-22 17:09:59 +02:00
|
|
|
DoAsync(FetchMastodon, FillTimeline);
|
2023-04-20 22:08:38 +02:00
|
|
|
};
|
2023-04-19 00:16:35 +02:00
|
|
|
|
2023-04-19 12:18:14 +02:00
|
|
|
function FetchMastodon(Proc) {
|
2023-04-20 15:17:00 +02:00
|
|
|
if (UseFakeApi) {
|
|
|
|
ResFetchMastodon({responseJson: [FakeApi.Mastodon.Status], Proc: Proc});
|
|
|
|
} else {
|
|
|
|
ApiCall({Target: "Mastodon", Method: "timelines/public", CallFine: ResFetchMastodon}, Proc);
|
|
|
|
};
|
2023-04-19 00:16:35 +02:00
|
|
|
};
|
2023-04-20 18:13:47 +02:00
|
|
|
function ResFetchMastodon(Res) {
|
2023-04-23 00:48:14 +02:00
|
|
|
var Notes = ApiTransform(Res.responseJson, 'Mastodon', 'Note');
|
2023-04-20 18:13:47 +02:00
|
|
|
LogDebug(Notes, 'l');
|
2023-04-22 17:09:59 +02:00
|
|
|
Tasker[Res.Proc[0]].Return(Notes);
|
2023-04-20 18:13:47 +02:00
|
|
|
};
|
2023-04-19 00:16:35 +02:00
|
|
|
|
|
|
|
function FillTimeline(Notes) {
|
|
|
|
Notes.forEach(function(Note){
|
2023-04-22 17:09:59 +02:00
|
|
|
Root.lastChild.innerHTML += `<div class="View Note">
|
|
|
|
<a href="${Note.Profile.Url}" onclick="DisplayProfile(ApiCache.Profiles['${Note.Profile.Url}']); return false;">
|
|
|
|
<img class="Profile Icon" src="${Note.Profile.Icon}"/>
|
|
|
|
${Note.Profile.Name}
|
2023-04-20 00:19:19 +02:00
|
|
|
</a>
|
2023-04-19 00:16:35 +02:00
|
|
|
${Note.Content}
|
2023-04-20 15:17:00 +02:00
|
|
|
<a href="${Note.Url}">${Note.Time}</a>
|
2023-04-19 00:16:35 +02:00
|
|
|
</div>`;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-20 18:13:47 +02:00
|
|
|
function FetchFeatured(Proc) {
|
|
|
|
//if (UseFakeApi) {
|
2023-04-22 17:09:59 +02:00
|
|
|
var Featured = FakeApi.Friendiiverse.Featured;
|
|
|
|
Object.values(Featured).forEach(function(Profiles){
|
|
|
|
Profiles.forEach(function(Profile){
|
|
|
|
ApiCache.Profiles[Profile.Url] = Profile;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
Tasker[Proc[0]].Return(Featured);
|
2023-04-20 18:13:47 +02:00
|
|
|
//} else {
|
|
|
|
|
|
|
|
//};
|
|
|
|
};
|
|
|
|
|
|
|
|
function FillFeatured(Categories) {
|
2023-04-20 22:08:38 +02:00
|
|
|
var Window = MakeWindow({className: "Gallery"});
|
2023-04-22 17:09:59 +02:00
|
|
|
Object.values(Categories).forEach(function(Profiles){
|
|
|
|
Profiles.forEach(function(Profile){
|
|
|
|
Window.innerHTML += `<div>
|
|
|
|
<a href="${Profile.Url}" onclick="DisplayProfile(ApiCache.Profiles['${Profile.Url}']); return false;">
|
2023-04-20 22:08:38 +02:00
|
|
|
<div>
|
2023-04-22 17:09:59 +02:00
|
|
|
<img src="${Profile.Banner}"/>
|
2023-04-20 22:08:38 +02:00
|
|
|
</div>
|
|
|
|
<div>
|
2023-04-22 17:09:59 +02:00
|
|
|
<img src="${Profile.Icon}"/>
|
|
|
|
${Profile.Name}
|
2023-04-20 22:08:38 +02:00
|
|
|
</div>
|
2023-04-20 18:13:47 +02:00
|
|
|
</a>
|
|
|
|
</div>`;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-20 22:08:38 +02:00
|
|
|
/*
|
2023-04-19 00:16:35 +02:00
|
|
|
PlazasView.innerHTML = `
|
|
|
|
<div>
|
|
|
|
<h3>Featured</h3>
|
|
|
|
<ul>
|
|
|
|
<li onclick="DisplayFriendicaTimeline('statuses/networkpublic_timeline');">Federation</li>
|
|
|
|
<li onclick="DisplayFriendicaTimeline('statuses/public_timeline');">${FriendicaUrl}</li>
|
2023-04-19 18:06:08 +02:00
|
|
|
<li onclick="DoAsync(FetchMastodon, FillTimeline);">
|
|
|
|
<img src=""/>
|
|
|
|
${MastodonUrl}
|
|
|
|
</li>
|
2023-04-19 00:16:35 +02:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
`;
|
2023-04-20 22:08:38 +02:00
|
|
|
*/
|
2023-04-20 18:13:47 +02:00
|
|
|
|
2023-04-23 00:48:14 +02:00
|
|
|
function ComposeNote() {
|
|
|
|
var Window = MakeWindow();
|
|
|
|
Window.innerHTML += `
|
|
|
|
<h2>Compose</h2>
|
|
|
|
<textarea style="width: 100%; height: 20em;"></textarea>
|
|
|
|
<p>
|
|
|
|
<button onclick="PostNote(this.parentNode.parentNode.querySelector('textarea').value);">Note!</button>
|
|
|
|
</p>
|
|
|
|
`;
|
|
|
|
};
|
|
|
|
function PostNote(Text) {
|
|
|
|
Obj = ExtrimObj(ApiSchema.Note);
|
|
|
|
};
|
|
|
|
|
2023-04-20 18:13:47 +02:00
|
|
|
DoAsync(FetchFeatured, FillFeatured);
|
2023-04-22 10:06:31 +02:00
|
|
|
CoverView.remove();
|