more boilerplate

This commit is contained in:
Iris Lightshard 2024-05-12 18:26:44 -06:00
parent 295df2eeec
commit 84d72140fb
Signed by: Iris Lightshard
GPG key ID: 688407174966CAF3
6 changed files with 143 additions and 11 deletions

View file

@ -2,10 +2,13 @@ package adapter
import ( import (
. "forge.lightcrystal.systems/lightcrystal/underbbs/models" . "forge.lightcrystal.systems/lightcrystal/underbbs/models"
nostr "github.com/nbd-wtf/go-nostr"
"strings"
"context"
) )
type Adapter interface { type Adapter interface {
Init(map[string]string, chan Message) error Init(Settings, chan Message) error
Subscribe(string) error Subscribe(string) error
SendMessage(Message) error SendMessage(Message) error
Follow(Author) error Follow(Author) error
@ -13,3 +16,48 @@ type Adapter interface {
GetFollowers() error GetFollowers() error
UpdateMetadata(interface{}) 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
}

1
dist/index.html vendored
View file

@ -22,6 +22,7 @@
</ul> </ul>
</nav> </nav>
<main> <main>
<button id="connectbtn" onclick=">Connect</button>
<div id="tabbar"></div> <div id="tabbar"></div>
<div id="tabcontent"></div> <div id="tabcontent"></div>
</main> </main>

10
models/settings.go Normal file
View file

@ -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`
}

View file

@ -2,10 +2,11 @@ package server
import ( import (
"forge.lightcrystal.systems/lightcrystal/underbbs/models" "forge.lightcrystal.systems/lightcrystal/underbbs/models"
"hacklab.nilfm.cc/quartzgun/auth"
"hacklab.nilfm.cc/quartzgun/renderer" "hacklab.nilfm.cc/quartzgun/renderer"
"net/http" "net/http"
"nhooyr.io/websocket" "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) self.logf("%v", err)
return 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, "") defer c.Close(websocket.StatusInternalError, "")
err = self.subscribe(r, c) err = self.subscribe(r, c)

View file

@ -18,7 +18,7 @@ function main():void {
const adapters = _("adapters", []); const adapters = _("adapters", []);
if (settings != null) { if (settings != null) {
for (let s of settings.adapters) { for (let s of settings.adapters ?? []) {
let a: Adapter = Adapter.create() let a: Adapter = Adapter.create()
switch (s.protocol) { switch (s.protocol) {
case "nostr": case "nostr":
@ -37,8 +37,31 @@ function main():void {
_("settings", { adapters: [] }); _("settings", { adapters: [] });
showSettings(); 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 { function showSettings():void {
// tab bar hidden // tab bar hidden
const tabbar = $("tabbar"); const tabbar = $("tabbar");
@ -48,7 +71,7 @@ function showSettings():void {
// tabcontent to show settings ui // tabcontent to show settings ui
const tabcontent = $("tabcontent"); const tabcontent = $("tabcontent");
const adapters = _("adapters") as Adapter[]; const adapters = _("adapters") as Adapter[] ?? [];
if (tabcontent) { if (tabcontent) {
let html = "<p>this is our settings dialogue</p>"; let html = "<p>this is our settings dialogue</p>";
@ -156,6 +179,9 @@ function saveAdapter(): void {
const settings = _("settings"); const settings = _("settings");
const adapters = _("adapters"); const adapters = _("adapters");
if (settings && adapters) { if (settings && adapters) {
if (!settings.adapters) {
settings.adapters = [];
}
settings.adapters.push(self); settings.adapters.push(self);
let a: Adapter = Adapter.create(); let a: Adapter = Adapter.create();
switch (self.protocol) { 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); _("addAdapter", addAdapter);
_("saveAdapter", saveAdapter); _("saveAdapter", saveAdapter);
_("fillAdapterProtocolOptions", fillAdapterProtocolOptions); _("fillAdapterProtocolOptions", fillAdapterProtocolOptions);
_("showSettings", showSettings); _("showSettings", showSettings);
_("saveSettings", saveSettings); _("saveSettings", saveSettings);
_("connect", connect);
main(); main();

View file

@ -2,12 +2,6 @@ package main
import ( import (
"context" "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" "log"
"net" "net"
"net/http" "net/http"