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: {
Mastodon: "avatar",
Misskey: "avatarUrl",
Misskey: {__OldOr__: ["avatarUrl", "iconUrl"]},
},
Name: {
Mastodon: {__OldOr__: ["display_name", "title"]},
@ -82,14 +82,18 @@ var ApiSchema = {
var ApiEndpoints = {
FetchNotes: {
Mastodon(Profile) {
return `GET accounts/${Profile.Id}/statuses`;
return `GET api/v1/accounts/${Profile.Id}/statuses`;
},
},
ServerInfo: {
Mastodon: "GET instance",
Mastodon: "GET api/v1/instance",
Misskey: {
Method: "POST api/meta",
Data: {"detail": true},
},
},
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) {
Profile = UrlObj(Profile);
var Window = MkWindow({className: "Profile"});
@ -124,7 +139,7 @@ function DisplayMastodonTimeline(Data) {
function FetchNotes(Profile, Proc) {
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');
LogDebug(Notes, 'l');
Tasker[Res.Proc[0]].Return(Notes);
@ -135,7 +150,7 @@ function FetchMastodon(Proc) {
//if (UseFakeApi) {
// ResFetchMastodon({responseJson: [FakeApi.Mastodon.Status], Proc: Proc});
//} else {
NetApiCall({Target: "Mastodon", Method: "GET timelines/public", CallFine: ResFetchMastodon}, Proc);
NetCall({Target: "Mastodon", Method: "GET timelines/public", CallFine: ResFetchMastodon}, Proc);
//};
};
function ResFetchMastodon(Res) {
@ -162,26 +177,25 @@ function FillHome() {
var Window = MkWindow({className: "Gallery"});
var Categories = ApiStatic.Featured;
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){
ApiCache.Urls[Profile.Url] = Profile;
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;">
<img class="Profile Banner" data-assign="src:Banner" src="${Profile.Banner}"/>
<div>
<img data-assign="src:Banner" src="${Profile.Banner}"/>
</div>
<div>
<img data-assign="src:Icon" src="${Profile.Icon}"/>
<img class="Profile Icon" data-assign="src:Icon" src="${Profile.Icon}"/>
<span data-assign="innerHTML:Name">${Profile.Url}</span>
</div>
</a>
</div>`;
NetApiCall({Target: Profile.Url, Method: ApiEndpoints.ServerInfo[Profile.ServerSoftware], CallFine: function(Res){
var Data = ApiTransform(Res.responseJson, Profile.ServerSoftware, 'Profile');
HtmlAssign(Rnd, Data);
_.merge(ApiCache.Urls[Profile.Url], Data);
}});
</li>`;
var Endp = ApiEndpoints.ServerInfo[Profile.ServerSoftware];
var Method = Endp.Method || Endp;
TransNetCall({Target: Profile.Url, Method: Method, Data: Endp.Data, CallFine: function(Res){
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 Endpoint = Data.Method.split(' ').slice(1).join(' ');
var Req = new XMLHttpRequest();
@ -6,6 +6,7 @@ function NetApiCall(Data, Proc) {
Req.onloadend = function(){
try {
this.responseJson = JSON.parse(this.responseText);
this.response = this.responseJson;
this.responseLog = this.responseJson;
} catch(Ex) {
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') {
// Req.open(Method, `${FriendicaUrl}/api/${Endpoint}.json`, true);
// Req.setRequestHeader('Authorization', `Basic ${btoa(FriendicaCredentials)}`);
//};
Req.open(Method, `${Data.Target}/api/v1/${Endpoint}`, true);
Req.send();
Req.open(Method, `${Data.Target}/${Endpoint}`, true);
_.forOwn(_.merge({"Content-Type": "application/json"}, Data.Headers), function(Val, Key) {
Req.setRequestHeader(Key, Val);
});
Req.send(JSON.stringify(Data.Data));
};
function IsHttpCodeGood(Code) {

View File

@ -63,10 +63,19 @@ img, video {
margin: 12px;
}
.View.Note .Profile.Icon {
.Profile.Banner {
width: 384px;
}
/*.View.Note*/ .Profile.Icon {
width: 64px;
}
.Gallery > div {
display: inline-block;
.Window.Gallery > ul {
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];
};
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) {
if (Debug) {
if (!Status) {
@ -32,8 +45,7 @@ function LogDebug(Data, Status) {
Data[i] = JSON.parse(Data[i]);
} catch(_){};
};
console[{l: "log", e: "error"}[Status.toLowerCase()]](LogDebug.caller.toString().hashCode()
/*Issue trying to get function name on Eruda... .split(' ')[1].split('(')[0]*/, Data);
console[{l: "log", e: "error"}[Status.toLowerCase()]](FuncName(LogDebug.caller), Data);
};
};