Friendiiverse/Source/Main.js

203 lines
5.0 KiB
JavaScript
Raw Normal View History

2023-04-19 12:18:14 +02:00
var CurrTasks = {};
2023-04-19 00:16:35 +02:00
function DoAsync(First, Then, Data) {
var Job = RndId();
CurrTasks[Job] = {
Remains: 0,
Return(Data) {
this.Remains -= 1;
this.Result = Data;
},
2023-04-19 00:16:35 +02:00
};
// Call all First functs
2023-04-19 12:18:14 +02:00
ForceList(First).forEach(function(Fun, Data){
var Task = RndId();
var Proc = [Job, Task];
CurrTasks[Job][Task] = {};
CurrTasks[Job].Remains += 1;
//Fun(Proc, Data);
Data ? Fun(Data, Proc) : Fun(Proc);
2023-04-19 12:18:14 +02:00
});
// Continuosly check when First functs completed
CurrTasks[Job].Interval = setInterval(function(Job, Then){
if (CurrTasks[Job].Remains <= 0) {
clearInterval(CurrTasks[Job].Interval);
// Call all Then functs
2023-04-19 12:18:14 +02:00
ForceList(Then).forEach(function(Fun){
Fun(CurrTasks[Job].Result);
2023-04-19 12:18:14 +02:00
});
2023-04-20 22:08:38 +02:00
delete CurrTasks[Job];
2023-04-19 12:18:14 +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;
};
function DisplayChannel(Channel) {
2023-04-22 10:06:31 +02:00
var Window = MakeWindow({className: "Channel"});
Window.innerHTML += `<div class="" style="display: inline-block;">
<a href="${Channel.Url}">
<div>
<img class="" src="${Channel.Banner}"/>
</div>
<div>
<img class="" src="${Channel.Icon}"/>
${Channel.Name}
</div>
</a>
</div>`;
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) {
var Notes = TransParsers.Mastodon.Status(Res.responseJson);
LogDebug(Notes, 'l');
CurrTasks[Res.Proc[0]].Return(Notes);
};
2023-04-19 00:16:35 +02:00
function FillTimeline(Notes) {
Notes.forEach(function(Note){
2023-04-20 22:08:38 +02:00
TimelineView.innerHTML += `<div class="View Note" data-data="${B64Obj(Note)}">
<a href="${Note.Author.Url}" onclick="DisplayChannel(UnB64Obj(this.parentNode.dataset.data).Author); return false;">
2023-04-20 18:13:47 +02:00
<img class="Author Icon" src="${Note.Author.Icon}"/>
${Note.Author.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) {
CurrTasks[Proc[0]].Return(FakeApi.Friendiiverse.Featured);
//} else {
//};
};
function FillFeatured(Categories) {
2023-04-20 22:08:38 +02:00
var Window = MakeWindow({className: "Gallery"});
2023-04-20 18:13:47 +02:00
Object.values(Categories).forEach(function(Channels){
Channels.forEach(function(Channel){
2023-04-20 22:08:38 +02:00
Window.innerHTML += `<div data-data="${B64Obj(Channel)}">
2023-04-22 10:06:31 +02:00
<a href="${Channel.Url}" onclick="DisplayChannel(UnB64Obj(this.parentNode.dataset.data)); return false;">
2023-04-20 22:08:38 +02:00
<div>
<img src="${Channel.Banner}"/>
</div>
<div>
<img src="${Channel.Icon}"/>
${Channel.Name}
</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>
<li onclick="DoAsync(FetchMastodon, FillTimeline);">
<img src=""/>
${MastodonUrl}
</li>
2023-04-19 00:16:35 +02:00
</ul>
</div>
<div>
<h3>Categories</h3>
<ul>
<li>Testing</li>
<li>#fediverse</li>
</ul>
</div>
`;
2023-04-20 22:08:38 +02:00
*/
2023-04-20 18:13:47 +02:00
DoAsync(FetchFeatured, FillFeatured);
2023-04-22 10:06:31 +02:00
CoverView.remove();