From 84d72140fb2b7352e19c5e69ae3753b6e8c2d4e5 Mon Sep 17 00:00:00 2001 From: Iris Lightshard Date: Sun, 12 May 2024 18:26:44 -0600 Subject: [PATCH] more boilerplate --- adapter/adapter.go | 50 +++++++++++++++++++++++++++++++++++++++++- dist/index.html | 1 + models/settings.go | 10 +++++++++ server/server.go | 33 +++++++++++++++++++++++++++- ts/index.ts | 54 +++++++++++++++++++++++++++++++++++++++++++--- underbbs.go | 6 ------ 6 files changed, 143 insertions(+), 11 deletions(-) create mode 100644 models/settings.go diff --git a/adapter/adapter.go b/adapter/adapter.go index 3140a66..e77caf1 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -2,10 +2,13 @@ package adapter import ( . "forge.lightcrystal.systems/lightcrystal/underbbs/models" + nostr "github.com/nbd-wtf/go-nostr" + "strings" + "context" ) type Adapter interface { - Init(map[string]string, chan Message) error + Init(Settings, chan Message) error Subscribe(string) error SendMessage(Message) error Follow(Author) error @@ -13,3 +16,48 @@ type Adapter interface { GetFollowers() error UpdateMetadata(interface{}) error } + +type NostrAdapter struct { + msgChan chan Message + nickname string + privkey string + relays []*nostr.Relay +} + +func (self *NostrAdapter) Init(settings Settings, msgChan chan Message) error { + self.nickname = settings.Nickname + self.privkey = *settings.PrivKey + self.msgChan = msgChan + + ctx := context.Background() + + relays := strings.Split(*settings.Relays, ",") + + for _, r := range relays { + pr := nostr.RelayConnect(ctx, strings.Trim(r) + if pr == nil { + return errors.New("Relay connection could not be completed") + } + self.relays = append(self.relays, pr) + } + return nil +} + +func (self *NostrAdapter) Subscribe(filter string) error { + return nil +} +func (self *NostrAdapter) SendMessage(msg Message) error { + return nil +} +func (self *NostrAdapter) Follow(author Author) error { + return nil +} +func (self *NostrAdapter) Unfollow(author Author) error { + return nil +} +func (self *NostrAdapter) GetFollowers() error { + return nil +} +func (self *NostrAdapter) UpdateMetadata(data interface{}) error { + return nil +} \ No newline at end of file diff --git a/dist/index.html b/dist/index.html index 74331a4..60057ab 100644 --- a/dist/index.html +++ b/dist/index.html @@ -22,6 +22,7 @@
+
diff --git a/models/settings.go b/models/settings.go new file mode 100644 index 0000000..69e51f3 --- /dev/null +++ b/models/settings.go @@ -0,0 +1,10 @@ +package models + +type Settings struct { + Nickname string + Protocol string + PrivKey *string `json:"privkey",omitempty` + Relays *string `json:"relays",omitempty` + Server *string `json:"server",omitempty` + ApiKey *string `json:"apiKey",omitempty` +} \ No newline at end of file diff --git a/server/server.go b/server/server.go index 0481fbc..4a564a6 100644 --- a/server/server.go +++ b/server/server.go @@ -2,10 +2,11 @@ package server import ( "forge.lightcrystal.systems/lightcrystal/underbbs/models" - "hacklab.nilfm.cc/quartzgun/auth" "hacklab.nilfm.cc/quartzgun/renderer" "net/http" "nhooyr.io/websocket" + "encoding/base64" + "encoding/json" ) @@ -54,6 +55,36 @@ func (self *GameTableServer) subscribeHandler(w http.ResponseWriter, r *http.Req self.logf("%v", err) return } + + // decode subprotocol data into settings objects + data := c.Subprotocol() + + // base64 decode + decoded, err := base64.StdEncoding.DecodeString(data) + if err != nil { + c.Close(3000, err.Error()) + } + + settings := []models.Settings + // unmarshal the json into the settings array + err := json.Unmarshal([]byte(decoded), &settings) + if (err != nil) { + c.Close(3001, err.Erorr()) + } + + // attempt to initialize adapters + for i, s := range settings { + switch(s.Protocol) { + case "nostr": + break; + case "masto": + break; + default: + break; + } + } + // keep reference to the adapters? + defer c.Close(websocket.StatusInternalError, "") err = self.subscribe(r, c) diff --git a/ts/index.ts b/ts/index.ts index 6f2a5b8..57016ef 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -18,7 +18,7 @@ function main():void { const adapters = _("adapters", []); if (settings != null) { - for (let s of settings.adapters) { + for (let s of settings.adapters ?? []) { let a: Adapter = Adapter.create() switch (s.protocol) { case "nostr": @@ -37,8 +37,31 @@ function main():void { _("settings", { adapters: [] }); showSettings(); } + registerServiceWorker(); }; +async function registerServiceWorker() { + if ("serviceWorker" in navigator) { + try { + const registration = await navigator.serviceWorker.register("/serviceWorker.js", { + scope: "/", + }); + if (registration.installing) { + console.log("Service worker installing"); + } else if (registration.waiting) { + console.log("Service worker installed"); + } else if (registration.active) { + console.log("Service worker active"); + } + } catch (error) { + console.error(`Registration failed with ${error}`); + } + const registration = await navigator.serviceWorker.ready; + (registration as any).sync.register("testdata").then((r:any)=>{console.log("but i will see this!")}); + } +}; + + function showSettings():void { // tab bar hidden const tabbar = $("tabbar"); @@ -48,7 +71,7 @@ function showSettings():void { // tabcontent to show settings ui const tabcontent = $("tabcontent"); - const adapters = _("adapters") as Adapter[]; + const adapters = _("adapters") as Adapter[] ?? []; if (tabcontent) { let html = "

this is our settings dialogue

"; @@ -156,6 +179,9 @@ function saveAdapter(): void { const settings = _("settings"); const adapters = _("adapters"); if (settings && adapters) { + if (!settings.adapters) { + settings.adapters = []; + } settings.adapters.push(self); let a: Adapter = Adapter.create(); switch (self.protocol) { @@ -171,10 +197,32 @@ function saveAdapter(): void { } } + +let _conn: WebSocket | null = null; + +function connect() { + // import the data from the settings + const settings = _("settings"); + if (settings) { + + // base64 encode the settings data + let subprotocol: string = "["; + for (let a of settings.adapters) { + subprotocol += JSON.stringify(a) + ","; + } + subprotocol += "]"; + subprotocol = btoa(subprotocol); + // open the websocket connection with settings as subprotocol + + _conn = new WebSocket("/subscribe", subprotocol); + _("websocket", _conn); + } +} + _("addAdapter", addAdapter); _("saveAdapter", saveAdapter); _("fillAdapterProtocolOptions", fillAdapterProtocolOptions); _("showSettings", showSettings); _("saveSettings", saveSettings); - +_("connect", connect); main(); diff --git a/underbbs.go b/underbbs.go index f9233bf..713b7e6 100644 --- a/underbbs.go +++ b/underbbs.go @@ -2,12 +2,6 @@ package main import ( "context" - "hacklab.nilfm.cc/felt/cmd" - "hacklab.nilfm.cc/felt/config" - "hacklab.nilfm.cc/felt/gametable" - "hacklab.nilfm.cc/felt/mongodb" - "hacklab.nilfm.cc/felt/register" - "hacklab.nilfm.cc/quartzgun/indentalUserDB" "log" "net" "net/http"