net call with transformations, misc utils

This commit is contained in:
octospacc 2023-04-27 00:13:31 +02:00
parent 7b0c4f3cb5
commit 7585f3fe14
5 changed files with 69 additions and 29 deletions

View File

@ -60,7 +60,7 @@ var ApiSchema = {
}, },
Icon: { Icon: {
Mastodon: "avatar", Mastodon: "avatar",
Misskey: "avatarUrl", Misskey: {__OldOr__: ["avatarUrl", "iconUrl"]},
}, },
Name: { Name: {
Mastodon: {__OldOr__: ["display_name", "title"]}, Mastodon: {__OldOr__: ["display_name", "title"]},
@ -82,14 +82,18 @@ var ApiSchema = {
var ApiEndpoints = { var ApiEndpoints = {
FetchNotes: { FetchNotes: {
Mastodon(Profile) { Mastodon(Profile) {
return `GET accounts/${Profile.Id}/statuses`; return `GET api/v1/accounts/${Profile.Id}/statuses`;
}, },
}, },
ServerInfo: { ServerInfo: {
Mastodon: "GET instance", Mastodon: "GET api/v1/instance",
Misskey: {
Method: "POST api/meta",
Data: {"detail": true},
},
}, },
ServerTimeline: { ServerTimeline: {
Mastodon: "GET timelines/public", Mastodon: "GET api/v1/timelines/public",
}, },
}; };

View File

@ -88,6 +88,21 @@ function HtmlAssignPropper(El, Data) {
}); });
}; };
function TransNetCall(Data, FromSource, DestType, Proc) {
Data.CallOld = Data.Call;
Data.CallFineOld = Data.CallFine;
Data.CallFailOld = Data.CallFail;
NetCall(_.merge(Data, {
//Call: function(Res){ CallFun(Data.Call, Res); },
CallFine: function(Res){
Res.responseJsonOld = Res.responseJson;
Res.responseJson = ApiTransform(Res.responseJson, FromSource, DestType);
Res.response = Res.responseJson;
CallFun(Data.CallFineOld, Res);
}, //CallFail: function(Res){ CallFun(Data.CallFail, Res); },
}), Proc);
};
function DisplayProfile(Profile) { function DisplayProfile(Profile) {
Profile = UrlObj(Profile); Profile = UrlObj(Profile);
var Window = MkWindow({className: "Profile"}); var Window = MkWindow({className: "Profile"});
@ -124,7 +139,7 @@ function DisplayMastodonTimeline(Data) {
function FetchNotes(Profile, Proc) { function FetchNotes(Profile, Proc) {
var Soft = Profile.__Software__; var Soft = Profile.__Software__;
NetApiCall({Target: UrlBase(Profile.Url), Method: ApiEndpoints.FetchNotes['Mastodon'](Profile), CallFine: function(Res){ NetCall({Target: UrlBase(Profile.Url), Method: ApiEndpoints.FetchNotes['Mastodon'](Profile), CallFine: function(Res){
var Notes = ApiTransform(Res.responseJson, Soft, 'Note'); var Notes = ApiTransform(Res.responseJson, Soft, 'Note');
LogDebug(Notes, 'l'); LogDebug(Notes, 'l');
Tasker[Res.Proc[0]].Return(Notes); Tasker[Res.Proc[0]].Return(Notes);
@ -135,7 +150,7 @@ function FetchMastodon(Proc) {
//if (UseFakeApi) { //if (UseFakeApi) {
// ResFetchMastodon({responseJson: [FakeApi.Mastodon.Status], Proc: Proc}); // ResFetchMastodon({responseJson: [FakeApi.Mastodon.Status], Proc: Proc});
//} else { //} else {
NetApiCall({Target: "Mastodon", Method: "GET timelines/public", CallFine: ResFetchMastodon}, Proc); NetCall({Target: "Mastodon", Method: "GET timelines/public", CallFine: ResFetchMastodon}, Proc);
//}; //};
}; };
function ResFetchMastodon(Res) { function ResFetchMastodon(Res) {
@ -162,26 +177,25 @@ function FillHome() {
var Window = MkWindow({className: "Gallery"}); var Window = MkWindow({className: "Gallery"});
var Categories = ApiStatic.Featured; var Categories = ApiStatic.Featured;
Object.keys(Categories).forEach(function(Category){ Object.keys(Categories).forEach(function(Category){
Window.innerHTML += `<h2>Featured ${Category}</h2>`; Window.innerHTML += `<h2>Featured ${Category}</h2><ul></ul>`;
Categories[Category].forEach(function(Profile){ Categories[Category].forEach(function(Profile){
ApiCache.Urls[Profile.Url] = Profile; ApiCache.Urls[Profile.Url] = Profile;
var Rnd = RndHtmlId(); var Rnd = RndHtmlId();
Window.innerHTML += `<div id="${Rnd}"> Window.querySelector('ul').innerHTML += `<li id="${Rnd}">
<a href="${Profile.Url}" onclick="DisplayProfile('${Profile.Url}'); return false;"> <a href="${Profile.Url}" onclick="DisplayProfile('${Profile.Url}'); return false;">
<img class="Profile Banner" data-assign="src:Banner" src="${Profile.Banner}"/>
<div> <div>
<img data-assign="src:Banner" src="${Profile.Banner}"/> <img class="Profile Icon" data-assign="src:Icon" src="${Profile.Icon}"/>
</div>
<div>
<img data-assign="src:Icon" src="${Profile.Icon}"/>
<span data-assign="innerHTML:Name">${Profile.Url}</span> <span data-assign="innerHTML:Name">${Profile.Url}</span>
</div> </div>
</a> </a>
</div>`; </li>`;
NetApiCall({Target: Profile.Url, Method: ApiEndpoints.ServerInfo[Profile.ServerSoftware], CallFine: function(Res){ var Endp = ApiEndpoints.ServerInfo[Profile.ServerSoftware];
var Data = ApiTransform(Res.responseJson, Profile.ServerSoftware, 'Profile'); var Method = Endp.Method || Endp;
HtmlAssign(Rnd, Data); TransNetCall({Target: Profile.Url, Method: Method, Data: Endp.Data, CallFine: function(Res){
_.merge(ApiCache.Urls[Profile.Url], Data); HtmlAssign(Rnd, Res.responseJson);
}}); _.merge(ApiCache.Urls[Profile.Url], Res.responseJson);
}}, Profile.ServerSoftware, 'Profile');
}); });
}); });
}; };

View File

@ -1,4 +1,4 @@
function NetApiCall(Data, Proc) { function NetCall(Data, Proc) {
var Method = Data.Method.split(' ')[0]; var Method = Data.Method.split(' ')[0];
var Endpoint = Data.Method.split(' ').slice(1).join(' '); var Endpoint = Data.Method.split(' ').slice(1).join(' ');
var Req = new XMLHttpRequest(); var Req = new XMLHttpRequest();
@ -6,6 +6,7 @@ function NetApiCall(Data, Proc) {
Req.onloadend = function(){ Req.onloadend = function(){
try { try {
this.responseJson = JSON.parse(this.responseText); this.responseJson = JSON.parse(this.responseText);
this.response = this.responseJson;
this.responseLog = this.responseJson; this.responseLog = this.responseJson;
} catch(Ex) { } catch(Ex) {
this.responseLog = this.responseText; this.responseLog = this.responseText;
@ -25,15 +26,15 @@ function NetApiCall(Data, Proc) {
}; };
}; };
}; };
//if (Data.Target == 'Mastodon') {
// Req.open(Method, `${MastodonUrl}/api/v1/${Endpoint}`, true);
//} else
//if (Data.Target == 'Friendica') { //if (Data.Target == 'Friendica') {
// Req.open(Method, `${FriendicaUrl}/api/${Endpoint}.json`, true); // Req.open(Method, `${FriendicaUrl}/api/${Endpoint}.json`, true);
// Req.setRequestHeader('Authorization', `Basic ${btoa(FriendicaCredentials)}`); // Req.setRequestHeader('Authorization', `Basic ${btoa(FriendicaCredentials)}`);
//}; //};
Req.open(Method, `${Data.Target}/api/v1/${Endpoint}`, true); Req.open(Method, `${Data.Target}/${Endpoint}`, true);
Req.send(); _.forOwn(_.merge({"Content-Type": "application/json"}, Data.Headers), function(Val, Key) {
Req.setRequestHeader(Key, Val);
});
Req.send(JSON.stringify(Data.Data));
}; };
function IsHttpCodeGood(Code) { function IsHttpCodeGood(Code) {

View File

@ -63,10 +63,19 @@ img, video {
margin: 12px; margin: 12px;
} }
.View.Note .Profile.Icon { .Profile.Banner {
width: 384px;
}
/*.View.Note*/ .Profile.Icon {
width: 64px; width: 64px;
} }
.Gallery > div { .Window.Gallery > ul {
display: inline-block; padding: 0;
}
.Window.Gallery > ul > li {
display: inline-block;
width: 384px;
} }

View File

@ -22,6 +22,19 @@ function UrlDomain(Url) {
return Url.split('//')[1].split('/')[0]; return Url.split('//')[1].split('/')[0];
}; };
function FuncName(Fun) {
Fun = Fun.toString();
var Name = Fun.split(' ')[1].split('(')[0].split('{')[0];
var Hash = Fun.hashCode();
return `f:${Name} (${Fun.hashCode()})`;
};
function CallFun(f, a, b, c, d) {
if (typeof(f) === 'function') {
f(a, b, c, d);
};
};
function LogDebug(Data, Status) { function LogDebug(Data, Status) {
if (Debug) { if (Debug) {
if (!Status) { if (!Status) {
@ -32,8 +45,7 @@ function LogDebug(Data, Status) {
Data[i] = JSON.parse(Data[i]); Data[i] = JSON.parse(Data[i]);
} catch(_){}; } catch(_){};
}; };
console[{l: "log", e: "error"}[Status.toLowerCase()]](LogDebug.caller.toString().hashCode() console[{l: "log", e: "error"}[Status.toLowerCase()]](FuncName(LogDebug.caller), Data);
/*Issue trying to get function name on Eruda... .split(' ')[1].split('(')[0]*/, Data);
}; };
}; };