From 279691e9a19bf7cb4fb45303b9a8e6bc7c1e3acd Mon Sep 17 00:00:00 2001 From: octospacc Date: Thu, 20 Apr 2023 18:13:47 +0200 Subject: [PATCH] Api fixes and progress --- .gitlab-ci.yml | 1 + Source/ApiTransform.js | 44 ++++++++++++++++++++++++------------------ Source/FakeApi.js | 31 ++++++++++++++++++++++------- Source/Main.js | 40 ++++++++++++++++++++++++++++++-------- Source/Style.css | 2 +- Source/Utils.js | 12 ++++++++---- 6 files changed, 91 insertions(+), 39 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fcef306..82e9260 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,6 +18,7 @@ pages: script: | python3 ./Build.py mv ./Dist ./public + cp ./public/Friendiiverse.html ./public/index.html artifacts: paths: - public diff --git a/Source/ApiTransform.js b/Source/ApiTransform.js index 7ad46a4..fd7f428 100644 --- a/Source/ApiTransform.js +++ b/Source/ApiTransform.js @@ -18,33 +18,39 @@ var TransSchemas = { }, }, }; -var TransSchemas_ = { - "Author": { - "Banner": { - "Mastodon": "header" + +var ApiSchema = { + __Account__: { + Banner: { + Mastodon: "header", }, - "Picture": { - "Mastodon": "avatar" + Icon: { + Mastodon: "avatar", }, - "Url": { - "Mastodon": "url" + Name: { + Mastodon: "display_name", + }, + Url: { + Mastodon: "url", }, }, - "Note": { - "Author": { - "Mastodon": "account" + Note: { + Author: { + Mastodon: "account", }, - "Content": { - "Mastodon": "content" + Content: { + Mastodon: "content", }, - "Time": { - "Mastodon": "created_at" + Time: { + Mastodon: "created_at", }, - "Url": { - "Mastodon": "url" + Url: { + Mastodon: "url", }, }, }; +ApiSchema.Author = CopyObj(ApiSchema.__Account__); +ApiSchema.Channel = CopyObj(ApiSchema.__Account__); var TransParsers = { Mastodon: { @@ -55,8 +61,8 @@ var TransParsers = { return JsonTransformA(Data, TransSchemas.Mastodon.Instance, TransSchemas.Mastodon); }, Status(Data) { - return JsonTransformA(Data, TransSchemas.Mastodon.Status, TransSchemas.Mastodon); - //return JsonTransformB(Data, TransSchemas_, TransSchemas_.Note, 'Mastodon'); + //return JsonTransformA(Data, TransSchemas.Mastodon.Status, TransSchemas.Mastodon); + return JsonTransformB(Data, ApiSchema, ApiSchema.Note, 'Mastodon'); }, }, }; diff --git a/Source/FakeApi.js b/Source/FakeApi.js index d13f238..693bc1d 100644 --- a/Source/FakeApi.js +++ b/Source/FakeApi.js @@ -1,18 +1,35 @@ var FakeApi = { - // + Friendiiverse: { + Featured: { + Featured: [], + Categories: [], + }, + }, Mastodon: { //"timelines/public": }, }; +FakeApi.Friendiiverse.Featured.Featured.push({ + Banner: "https://picsum.photos/seed/Testing.Banner/320/180", + Icon: "https://picsum.photos/seed/Testing.Icon/64", + Name: "Testing Channel", +}); +FakeApi.Friendiiverse.Featured.Categories.push({ + Banner: "https://picsum.photos/seed/fediverse.Banner/320/180", + Icon: "https://picsum.photos/seed/fediverse.Icon/64", + Name: "#fediverse", +}); + FakeApi.Mastodon.Account = { - "avatar": "https://picsum.photos/64", - "url": "https://mastodon.example.com/@Tester", + avatar: "https://picsum.photos/seed/Tester.Icon/64", + display_name: "The Tester", + url: "https://mastodon.example.com/@Tester", }; FakeApi.Mastodon.Status = { - "account": FakeApi.Mastodon.Account, - "content": "

Lorem ipsum dolor sit amet...

", - "created_at": "2023-01-01T13:00:00.123Z", - "url": "https://mastodon.example.com/@Tester/1234567890", + account: FakeApi.Mastodon.Account, + content: "

Lorem ipsum dolor sit amet...

", + created_at: "2023-01-01T13:00:00.123Z", + url: "https://mastodon.example.com/@Tester/1234567890", }; diff --git a/Source/Main.js b/Source/Main.js index 04a233f..01b56f6 100644 --- a/Source/Main.js +++ b/Source/Main.js @@ -92,12 +92,6 @@ 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) { if (UseFakeApi) { ResFetchMastodon({responseJson: [FakeApi.Mastodon.Status], Proc: Proc}); @@ -105,13 +99,18 @@ function FetchMastodon(Proc) { ApiCall({Target: "Mastodon", Method: "timelines/public", CallFine: ResFetchMastodon}, Proc); }; }; +function ResFetchMastodon(Res) { + var Notes = TransParsers.Mastodon.Status(Res.responseJson); + LogDebug(Notes, 'l'); + CurrTasks[Res.Proc[0]].Return(Notes); +}; function FillTimeline(Notes) { Notes.forEach(function(Note){ TimelineView.innerHTML += `
- - ${Note.Author.Url} + + ${Note.Author.Name} ${Note.Content} ${Note.Time} @@ -119,6 +118,28 @@ function FillTimeline(Notes) { }); }; +function FetchFeatured(Proc) { + //if (UseFakeApi) { + CurrTasks[Proc[0]].Return(FakeApi.Friendiiverse.Featured); + //} else { + + //}; +}; + +function FillFeatured(Categories) { + Object.values(Categories).forEach(function(Channels){ + Channels.forEach(function(Channel){ + PlazasView.innerHTML += `
+ + + + ${Channel.Name} + +
`; + }); + }); +}; + PlazasView.innerHTML = `

Featured

@@ -139,3 +160,6 @@ PlazasView.innerHTML = `
`; + +DoAsync(FetchFeatured, FillFeatured); + diff --git a/Source/Style.css b/Source/Style.css index e163e72..92528a2 100644 --- a/Source/Style.css +++ b/Source/Style.css @@ -25,6 +25,6 @@ footer { margin: 12px; } -.View.Note .Author.Picture { +.View.Note .Author.Icon { width: 64px; } diff --git a/Source/Utils.js b/Source/Utils.js index f3d146a..cc28c7c 100644 --- a/Source/Utils.js +++ b/Source/Utils.js @@ -20,6 +20,10 @@ function LogDebug(Data, Status) { }; }; +function CopyObj(Obj) { + return JSON.parse(JSON.stringify(Obj)); +}; + // Transform JSON tree into a new using a template schema // DEVNOTE: Unsafe, should check for colliding "__" keys from input tree and act accordingly function JsonTransformA(TreesOld, SchemaCurr, SchemaRoot) { @@ -78,10 +82,10 @@ function JsonTransformB(TreesOld, SchemaNew, NodeNew, TypeOld) { }; }; 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 TreeNew = CopyObj(NodeNew); + Object.keys(TreeNew).forEach(function(KeyNew){ + if (TypeOld in TreeNew[KeyNew]) { + var KeyOld = TreeNew[KeyNew][TypeOld]; var ObjOld = TreeOld[KeyOld]; if (typeof(KeyOld) == 'object') { // Deep nested children in TreeOld