yak shaving to store and display messages

This commit is contained in:
Iris Lightshard 2024-06-29 11:04:08 -06:00
parent 2c35e9c304
commit 2244bbcf7a
Signed by: nilix
GPG key ID: 688407174966CAF3
3 changed files with 49 additions and 25 deletions

View file

@ -1,17 +1,9 @@
import {Adapter} from "./adapter"; import {Adapter} from "./adapter";
import {Message, Attachment} from "./message" import {Message, Attachment} from "./message"
import util from "./util"
function _(key: string, value: any | null | undefined = undefined): any | null { var $ = util.$
const x = <any>window; var _ = util._
if (value !== undefined) {
x[key] = value;
}
return x[key];
}
function $(id: string): HTMLElement | null {
return document.getElementById(id);
}
function main():void { function main():void {
const settings = _("settings", JSON.parse(localStorage.getItem("settings") ?? "{}")); const settings = _("settings", JSON.parse(localStorage.getItem("settings") ?? "{}"));
@ -179,24 +171,12 @@ function saveAdapter(): void {
break; break;
} }
const settings = _("settings"); const settings = _("settings");
let adapters = _("adapters");
if (settings) { if (settings) {
if (!settings.adapters) { if (!settings.adapters) {
settings.adapters = []; settings.adapters = [];
} }
settings.adapters.push(self); settings.adapters.push(self);
if (!adapters) {
adapters = []
}
let a: Adapter = Adapter.create();
switch (self.protocol) {
case "nostr":
adapters.push(Adapter.toNostr(a, self));
break;
case "mastodon":
adapters.push(Adapter.toMasto(a, self));
break;
}
localStorage.setItem("settings", JSON.stringify(settings)); localStorage.setItem("settings", JSON.stringify(settings));
showSettings(); showSettings();
} }
@ -217,7 +197,6 @@ async function authorizedFetch(method: string, uri: string, body: any): Promise<
function connect() { function connect() {
// open the websocket connection with settings as subprotocol
const wsProto = location.protocol == "https:" ? "wss" : "ws"; const wsProto = location.protocol == "https:" ? "wss" : "ws";
_conn = new WebSocket(`${wsProto}://${location.host}/subscribe`, "underbbs"); _conn = new WebSocket(`${wsProto}://${location.host}/subscribe`, "underbbs");
_conn.addEventListener("open", (e: any) => { _conn.addEventListener("open", (e: any) => {
@ -226,11 +205,22 @@ function connect() {
}); });
_conn.addEventListener("message", (e: any) => { _conn.addEventListener("message", (e: any) => {
// debugging
console.log(e) console.log(e)
// now we'll figure out what to do with it
const data = JSON.parse(e.data); const data = JSON.parse(e.data);
if (data.key) { if (data.key) {
_("skey", data.key) _("skey", data.key)
authorizedFetch("POST", "/api/adapters", JSON.stringify(_("settings").adapters)) authorizedFetch("POST", "/api/adapters", JSON.stringify(_("settings").adapters))
} else {
// typeswitch on the incoming data and adapters
// if it's a regular message, add it to the store
// if the adapter is active, inject the web components
// FOR HOTFETCHED DATA:
// before fetching, we can set properties on the DOM,
// so when those data return to us we know where to inject components!
} }
}); });
_conn.addEventListener("error", (e: any) => { _conn.addEventListener("error", (e: any) => {

20
ts/message-element.ts Normal file
View file

@ -0,0 +1,20 @@
import util from "./util"
var _ = util._
export class MessageElement extends HTMLElement {
static observedAttributes = [ "data-id", "data-adapter", "data-replyCt", "data-reactionCt", "data-boostCt" ]
private _id: string | null = null;
private _adapter: any;
constructor() {
super();
}
connectedCallback() {
this._id = this.getAttribute("data-id");
this._adapter = _("settings").adapters.filter((a: any)=>a.nickname == this.getAttribute("data-adapter"))[0];
// grab message content from the store and format our innerHTML
}
}

14
ts/util.ts Normal file
View file

@ -0,0 +1,14 @@
function _(key: string, value: any | null | undefined = undefined): any | null {
const x = <any>window;
if (value !== undefined) {
x[key] = value;
}
return x[key];
}
function $(id: string): HTMLElement | null {
return document.getElementById(id);
}
export default { _, $ }