This commit is contained in:
octospacc 2023-05-01 00:08:01 +02:00
parent fd0695e133
commit 782f3ad500
6 changed files with 94 additions and 34 deletions

View File

@ -23,9 +23,11 @@ var TransSchemas = {
var ApiSchema = {
__All__: {
ServerSoftware: { //TODO: Handle this in JsonTransform
//Mastodon: {__Set__: "Mastodon"},
//Misskey: {__Set__: "Misskey"},
//ServerId: {
// __All__: "id",
//},
// NOTE: objects that embed cross-server data (e.g. a renote), might get this value assigned wrong for our needs (???)
ServerSoftware: {
__All__: {__EvalSet__: "TypeOld"},
},
},
@ -70,6 +72,9 @@ var ApiSchema = {
Mastodon: {__OldOr__: ["display_name", "title"]},
Misskey: "name",
},
//ServerUsername: {
// __All__: "username",
//},
Type: { // user, bot, group, channel:[normal, server]
Mastodon: {__EvalSet__: `
if (TreeOld.bot) 'Bot';
@ -90,12 +95,13 @@ var ApiSchema = {
var ApiEndpoints = {
FetchNotes: {
Mastodon(Profile) {
return `GET api/v1/accounts/${Profile.Id}/statuses`;
// Must actually get the id by calling GET api/v1/accounts/lookup?acct=USERNAME, the provided one is glitchy
return `GET api/v1/accounts/${Profile.__TreeOld__.id}/statuses`;
},
Misskey(Profile) {
return {
Method: "POST api/users/show",
Data: {"username": Profile.Id},
Method: "POST api/users/notes",
Data: {"userId": Profile.Id},
};
},
},
@ -112,12 +118,36 @@ var ApiEndpoints = {
},
};
var WebEndpoints = {
Note: {
Misskey(Note) {
//return `SERVER_URL/notes/${Note.ServerId}`;
return `SERVER_URL/notes/${Note.__TreeOld__.id}`;
},
},
Profile: {
Misskey(Profile) {
var Host = Profile.__TreeOld__.host;
//return `SERVER_URL/@${Profile.ServerUsername}`;
return `SERVER_URL/@${Profile.__TreeOld__.username}`;
},
},
};
function ApiTransform(Data, FromSource, DestType) {
var DataFinal = JsonTransformB(Data, ApiSchema, ApiSchema[DestType], FromSource);
LogDebug([Data, DestType, FromSource, DataFinal]);
return DataFinal;
};
function GetWebUrl(Data, Type) {
return (TryStr(Data.Url)
? Data.Url
: WebEndpoints[Type][Data.ServerSoftware](Data)
);
//return Data.Url || WebEndpoints[Type][Data.ServerSoftware](Data);
};
/*
var TransParsers = {
Mastodon: {

View File

@ -13,8 +13,8 @@ Present.__Set__ = function __Set__() {
};
var ApiCache = {
__Store__(Data, Path/*Key, Where*/) {
eval(`ApiCache.${Path} = Data;`);
__Store__(Data, Path) {
eval(`ApiCache${JPath(Path)} = Data;`);
},
__UrlStore__(Data) {
ApiCache.__Store__(Data, `Urls['${Data.Url}']`);
@ -92,7 +92,7 @@ function HtmlAssignPropper(El, Data) {
var Val = eval(`Data.${Toks[1]}`);
if (Val !== undefined) {
if (Key === 'src') {
Val = MkUrl(Val);
Val = MkReqUrl(Val);
};
El[Key] = Val;
};
@ -115,12 +115,11 @@ function TransNetCall(Data, FromSource, DestType, Proc) {
function DisplayProfile(Profile) {
Profile = UrlObj(Profile, DisplayProfile);
//if (Profile) {
if (Profile) {
var Window = MkWindow({className: "Profile"});
Window.innerHTML += Templating.ViewProfile(Profile);
// TODO: Handle fetching notes of non-standard profiles like servers timelines
DoAsync(FetchNotes, FillTimeline, Profile);
//};
};
};
function FetchNotes(Profile, Proc) {
@ -158,7 +157,8 @@ function FillTimeline(Notes) {
};
function DisplayThread(Note) {
var Window = MkWindow({className: "Thread"});
//Window.innerHTML += Templating.ViewNote(Note);
};
function FillHome() {
@ -171,13 +171,7 @@ function FillHome() {
ApiCache.Urls[Profile.Url] = Profile;
var Rnd = RndHtmlId();
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="${MkUrl(Profile.Banner)}"/>
<div>
<img class="Profile Icon" data-assign="src=Icon" src="${MkUrl(Profile.Icon)}"/>
<span data-assign="innerHTML=Name">${Profile.Url}</span>
</div>
</a>
${Templating.ViewProfile(Profile, {Name: Profile.Url})}
</li>`;
var Endp = ApiEndpoints.ServerInfo[Profile.ServerSoftware];
var Method = Endp.Method || Endp;

View File

@ -30,7 +30,7 @@ function NetCall(Data, Proc) {
// Req.open(Method, `${FriendicaUrl}/api/${Endpoint}.json`, true);
// Req.setRequestHeader('Authorization', `Basic ${btoa(FriendicaCredentials)}`);
//};
Req.open(Method, MkUrl(`${Data.Target}/${Endpoint}`), true);
Req.open(Method, MkReqUrl(`${Data.Target}/${Endpoint}`), true);
Req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
_.forOwn(_.merge({"Content-Type": "application/json"}, Data.Headers), function(Val, Key) {
Req.setRequestHeader(Key, Val);
@ -52,7 +52,7 @@ function IsHttpCodeGood(Code) {
};
};
function MkUrl(Url) {
function MkReqUrl(Url) {
if (Url && !Url.toLowerCase().startsWith(HttpProxy)) {
Url = HttpProxy + Url;
};

View File

@ -63,6 +63,10 @@ img, video {
margin: 12px;
}
.View.Profile {
display: inline-block;
}
.Profile.Banner {
width: 384px;
}

View File

@ -1,24 +1,40 @@
var Templating = {
ViewNote(Note) {
ViewNote(Note, Override) {
Override = Override || {};
//var Url = TryStr(Note.Url) || Note.ServerId;
var Url = Override.Url || GetWebUrl(Note, 'Note');
//var ProfileUrl = TryStr(Note.Profile.Url) || Note.Profile.ServerUsername;
var ProfileUrl = Override.ProfileUrl || GetWebUrl(Note.Profile, 'Profile');
//if (Note.Quoting) {
// //Note.Quoting.Profile.Url = TryStr(Note.Quoting.Profile.Url) || Note.Quoting.Profile.ServerUsername;
//};
return `<div class="View Note">
<a href="${Note.Profile.Url}" onclick="DisplayProfile('${Note.Profile.Url}'); return false;">
<img class="Profile Icon" src="${MkUrl(Note.Profile.Icon)}"/>
<a href="${ProfileUrl}" onclick="DisplayProfile('${ProfileUrl}'); return false;">
<img class="Profile Icon" src="${MkReqUrl(Note.Profile.Icon)}"/>
${Note.Profile.Name}
</a>
<div class="Note Content">
${Note.Content ? Note.Content : 'renoted:'}
${Note.Quoting ? Templating.ViewNote(Note.Quoting) : ''}
</div>
<a href="${Note.Url}" onclick="DisplayThread('${Note.Url}'); return false;">${Note.Time}</a>
<a href="${Url}" onclick="DisplayThread('${Url}'); return false;">
${Note.Time}
</a>
</div>`;
},
ViewProfile(Profile) {
return `<div class="View Profile" style="display: inline-block;">
<a href="${Profile.Url}">
<img class="Profile Banner" src="${MkUrl(Profile.Banner)}"/>
ViewProfile(Profile, Override) {
Override = Override || {};
//var Url = TryStr(Profile.Url) || Profile.ServerUsername;
var Url = Override.Url || GetWebUrl(Profile, 'Profile');
var Name = Override.Name || Profile.Name;
return `<div class="View Profile">
<a href="${Url}" onclick="DisplayProfile('${Url}'); return false;">
<img class="Profile Banner" data-assign="src=Banner" src="${Override.Banner || MkReqUrl(Profile.Banner)}"/>
<div>
<img class="Profile Icon" src="${MkUrl(Profile.Icon)}"/>
${Profile.Name}
<img class="Profile Icon" data-assign="src=Icon" src="${Override.Icon || MkReqUrl(Profile.Icon)}"/>
<span data-assign="innerHTML=Name">
${Name}
</span>
</div>
</a>
</div>`;

View File

@ -42,8 +42,16 @@ function LogDebug(Data, Status) {
};
};
function IsObj(Item) {
return typeof(Item) === 'object';
function TryStr(Val) {
if (typeof(Val) === 'string') {
return Val;
} else {
return false;
};
};
function IsObj(Val) {
return typeof(Val) === 'object';
};
function ExtrimObj(Obj) {
@ -74,6 +82,14 @@ function UnB64Obj(Obj) {
return JSON.parse(atob(Obj));
};
function JPath(Path) {
Path = Path.trim();
if (!(Path.startsWith('[') || Path.startsWith('.'))) {
Path = `.${Path}`;
};
return Path;
};
// https://stackoverflow.com/a/7616484
String.prototype.hashCode = function() {
var hash = 0, i, chr;