underbbs/frontend/ts/websocket.ts

95 lines
3.4 KiB
TypeScript
Raw Normal View History

import util from "./util"
import {AdapterState, AdapterData} from "./adapter";
import {Message, Attachment, Author} from "./message"
import {Settings} from "./settings"
export class DatagramSocket {
public static skey: string | null = null;
public static conn: WebSocket | null;
private static onOpen(e: Event) {
console.log("websocket connection opened");
console.log(JSON.stringify(e));
}
private static onMsg(e: MessageEvent) {
const data = JSON.parse(e.data);
console.log(data);
if (data.key) {
DatagramSocket.skey = data.key;
util.authorizedFetch("POST", "/api/adapters", JSON.stringify(Settings._instance.adapters))
.then(r=> {
if (r.ok) {
// iterate through any components which might want to fetch data
const profiles = document.querySelectorAll("underbbs-profile");
profiles.forEach(p=>{
const target = p.getAttribute("data-target");
p.setAttribute("data-target", target ?? "");
});
const feeds = document.querySelectorAll("underbbs-author-messages");
feeds.forEach(f=>{
const target = f.getAttribute("data-target");
f.setAttribute("data-target", target ?? "");
});
}
})
.catch(e => {
util.errMsg(e.message);
});
} else {
let store = AdapterState._instance.data.get(data.adapter);
if (!store) {
AdapterState._instance.data.set(data.adapter, new AdapterData(data.protocol));
store = AdapterState._instance.data.get(data.adapter);
}
if (store) {
// typeswitch on the incoming data type and fill the memory
switch (data.type) {
case "message":
2024-12-04 06:44:11 +00:00
store.messages.set(data.renoteId ?? data.id, <Message>data);
break;
case "author":
store.profileCache.set(data.id, <Author>data);
break;
default:
break;
}
}
console.log(store);
// go through each type of component and give it the latest if it's relevant to them
let profileTargets = document.querySelectorAll(`underbbs-profile[data-adapter="${data.adapter}"][data-target="${data.id}"]`);
profileTargets.forEach(t=>{
t.setAttribute("data-latest", data.id);
});
let byAuthorTargets = document.querySelectorAll(`underbbs-author-messages[data-adapter="${data.adapter}"][data-target="${data.author}"]`);
byAuthorTargets.forEach(t=>{
t.setAttribute("data-latest", data.id);
});
2024-12-04 06:44:11 +00:00
if (data.renoter) {
let byAuthorTargetsForBoosts = document.querySelectorAll(`underbbs-author-messages[data-adapter="${data.adapter}"][data-target="${data.renoter}"]`);
byAuthorTargetsForBoosts.forEach(t=>{
console.log("setting renote id on data-latest")
t.setAttribute("data-latest", data.renoteId);
});
}
}
}
static connect(): void {
const wsProto = location.protocol == "https:" ? "wss" : "ws";
const _conn = new WebSocket(`${wsProto}://${location.host}/subscribe`, "underbbs");
2024-08-31 17:01:31 +00:00
_conn.addEventListener("open", DatagramSocket.onOpen);
_conn.addEventListener("message", DatagramSocket.onMsg);
2024-08-31 17:01:31 +00:00
_conn.addEventListener("error", (e: any) => {
console.log("websocket connection error");
console.log(JSON.stringify(e));
});
DatagramSocket.conn = _conn;
}
}