New JSON transformations; Upd FakeApi

This commit is contained in:
octospacc 2023-04-20 15:17:00 +02:00
parent 76865b605d
commit f6e2dff546
7 changed files with 97 additions and 32 deletions

View File

@ -17,6 +17,7 @@ pages:
stage: deploy
script: |
python3 ./Build.py
mv ./Dist ./public
artifacts:
paths:
- public

View File

@ -5,20 +5,25 @@ from pathlib import Path
os.chdir(os.path.dirname(os.path.abspath(__file__)))
os.makedirs('./Dist', exist_ok=True)
with open('./Friendiiverse.html', 'r') as Base:
os.chdir('./Source')
with open(f'./Friendiiverse.html', 'r') as Base:
Base = Base.read()
def FragReplace(Find, Replace, Pattern='*.*', Folder='./Source/'):
def FragReplace(Find, Replace, Pattern='*.*'):
global Base
for File in Path('./').rglob(Pattern):
File = str(File)
with open(File, 'r') as Frag:
Frag = Replace[0] + Frag.read() + Replace[1]
Frag = Replace.format(File=File, Frag=Frag.read())
for Prefix in ('', './'):
Base = Base.replace(Find[0] + Prefix + str(File) + Find[1], Frag)
File = Prefix + File
Base = Base.replace(Find.format(File=File), Frag)
FragReplace(('<link rel="stylesheet" href="', '"/>'), ('<style>', '</style>'), '*.css')
FragReplace(('<script src="', '"></script>'), ('<script>', '</script>'), '*.js')
FragReplace('<link rel="stylesheet" href="{File}"/>', '<style data-source="{File}">{Frag}</style>', '*.css')
FragReplace('<script src="{File}"></script>', '<script data-source="{File}">{Frag}</script>', '*.js')
os.chdir('..')
with open('./Dist/Friendiiverse.html', 'w') as Build:
Build.write(Base)

View File

@ -19,15 +19,29 @@ var TransSchemas = {
},
};
var TransSchemas_ = {
"Note": {
"Author": {
"Mastodon": "Status.account"
"Author": {
"Banner": {
"Mastodon": "header"
},
"Content": {
"Mastodon": "Status.content"
"Picture": {
"Mastodon": "avatar"
},
"Url": {
"Mastodon": "Status.url"
"Mastodon": "url"
},
},
"Note": {
"Author": {
"Mastodon": "account"
},
"Content": {
"Mastodon": "content"
},
"Time": {
"Mastodon": "created_at"
},
"Url": {
"Mastodon": "url"
},
},
};
@ -42,7 +56,7 @@ var TransParsers = {
},
Status(Data) {
return JsonTransformA(Data, TransSchemas.Mastodon.Status, TransSchemas.Mastodon);
// return JsonTransformB({Status: Data}, TransSchemas_, TransSchemas_.Note, 'Mastodon');
//return JsonTransformB(Data, TransSchemas_, TransSchemas_.Note, 'Mastodon');
},
},
};

View File

@ -4,3 +4,15 @@ var FakeApi = {
//"timelines/public":
},
};
FakeApi.Mastodon.Account = {
"avatar": "https://picsum.photos/64",
"url": "https://mastodon.example.com/@Tester",
};
FakeApi.Mastodon.Status = {
"account": FakeApi.Mastodon.Account,
"content": "<p>Lorem ipsum dolor sit amet...</p>",
"created_at": "2023-01-01T13:00:00.123Z",
"url": "https://mastodon.example.com/@Tester/1234567890",
};

View File

@ -46,17 +46,22 @@ function ApiCall(Data, Proc) {
var Req = new XMLHttpRequest();
Req.Proc = Proc;
Req.onloadend = function(){
var Status = String(this.status);
try {
this.responseJson = JSON.parse(this.responseText);
this.responseLog = this.responseJson;
} catch(Ex) {
this.responseLog = this.responseText;
};
if (Data.Call) {
Data.Call(this);
};
if (HttpCodeGood(this.status)) {
LogDebug([this.status, this.responseText], 'l');
LogDebug([this.status, this.responseLog], 'l');
if (Data.CallFine) {
Data.CallFine(this);
};
} else {
LogDebug([this.status, this.responseText], 'e');
LogDebug([this.status, this.responseLog], 'e');
if (Data.CallFail) {
Data.CallFail(this);
};
@ -87,23 +92,29 @@ function DisplayFriendicaTimeline(Timeline) {
}});
};
function ResFetchMastodon(Res) {
var Notes = TransParsers.Mastodon.Status(Res.responseJson);
LogDebug(Notes, 'l');
CurrTasks[Res.Proc[0]].Return(Notes);
};
function FetchMastodon(Proc) {
ApiCall({Target: "Mastodon", Method: "timelines/public", CallFine: function(Res){
var Notes = TransParsers.Mastodon.Status(JSON.parse(Res.responseText));
LogDebug(Notes, 'l');
CurrTasks[Proc[0]].Return(Notes);
}}, Proc);
if (UseFakeApi) {
ResFetchMastodon({responseJson: [FakeApi.Mastodon.Status], Proc: Proc});
} else {
ApiCall({Target: "Mastodon", Method: "timelines/public", CallFine: ResFetchMastodon}, Proc);
};
};
function FillTimeline(Notes) {
Notes.forEach(function(Note){
TimelineView.innerHTML += `<div class="Note">
TimelineView.innerHTML += `<div class="View Note">
<a href="${Note.Author.Url}">
<img class="Author Picture" src="${Note.Author.Picture}"/>
${Note.Author.Url}
</a>
<a href="${Note.Url}">${Note.Time}</a>
${Note.Content}
<a href="${Note.Url}">${Note.Time}</a>
</div>`;
});
};

View File

@ -20,11 +20,11 @@ footer {
white-space: break-spaces;
}
.Note {
.View.Note {
border: 4px solid purple;
margin: 12px;
}
.Note > .Author.Picture {
.View.Note .Author.Picture {
width: 64px;
}

View File

@ -66,13 +66,35 @@ function JsonTransformCycleA(TreeOld, SchemaCurr, SchemaRoot) {
});
return TreeNew;
};
function JsonTransformB(TreesOld, Schema, Node, Source) {
function JsonTransformB(TreesOld, SchemaNew, NodeNew, TypeOld) {
if (Array.isArray(TreesOld)) {
var ListNew = [];
ForceList(TreesOld).forEach(function(TreeOld){
ListNew.push(JsonTransformCycleB(TreeOld, SchemaNew, NodeNew, TypeOld));
});
return ListNew;
} else {
return JsonTransformCycleB(TreesOld, SchemaNew, NodeNew, TypeOld);
};
};
function JsonTransformCycleB(TreeOld, Schema, Node, Source) {
var TreeNew = {};
Object.keys(Node).forEach(function(KeyOld){
console.log(KeyOld)
function JsonTransformCycleB(TreeOld, SchemaNew, NodeNew, TypeOld) {
var TreeNew = NodeNew;
Object.keys(NodeNew).forEach(function(KeyNew){
if (TypeOld in NodeNew[KeyNew]) {
var KeyOld = NodeNew[KeyNew][TypeOld];
var ObjOld = TreeOld[KeyOld];
if (typeof(KeyOld) == 'object') {
// Deep nested children in TreeOld
} else {
// Direct children in TreeOld
if (typeof(ObjOld) == 'object') {
TreeNew[KeyNew] = JsonTransformB(ObjOld, SchemaNew, SchemaNew[KeyNew], TypeOld);
} else {
TreeNew[KeyNew] = ObjOld;
};
};
};
});
return TreeNew;
};