2024-07-01 03:51:25 +00:00
|
|
|
import util from "./util"
|
|
|
|
import {AdapterState, AdapterData} from "./adapter";
|
|
|
|
import {Message, Attachment, Author} from "./message"
|
2024-07-16 19:43:35 +00:00
|
|
|
import {Settings} from "./settings"
|
2024-12-08 01:04:04 +00:00
|
|
|
import {SettingsElement} from "./settings-element"
|
|
|
|
import {AuthorMessagesElement} from "./author-messages-element"
|
|
|
|
import {ProfileElement} from "./profile-element"
|
|
|
|
import {MessageElement} from "./message-element"
|
|
|
|
import {TimelineElement} from "./timeline-element"
|
|
|
|
import {TimelineFilterElement} from "./timeline-filter-element"
|
2024-07-01 03:51:25 +00:00
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
export class DatagramSocket {
|
|
|
|
public static skey: string | null = null;
|
|
|
|
public static conn: WebSocket | null;
|
2024-12-08 01:04:04 +00:00
|
|
|
private static _gateway: string = ""
|
2024-07-01 03:51:25 +00:00
|
|
|
|
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
private static onOpen(e: Event) {
|
|
|
|
console.log("websocket connection opened");
|
|
|
|
console.log(JSON.stringify(e));
|
|
|
|
}
|
|
|
|
|
2024-12-08 01:04:04 +00:00
|
|
|
private static gatewayWithScheme(): string {
|
|
|
|
return location.protocol + "//" + DatagramSocket._gateway
|
|
|
|
}
|
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
private static onMsg(e: MessageEvent) {
|
|
|
|
const data = JSON.parse(e.data);
|
|
|
|
console.log(data);
|
|
|
|
if (data.key) {
|
|
|
|
DatagramSocket.skey = data.key;
|
2024-12-08 01:04:04 +00:00
|
|
|
util.authorizedFetch("POST", DatagramSocket.gatewayWithScheme() + "/api/adapters", JSON.stringify(Settings._instance.adapters))
|
2024-07-16 19:43:35 +00:00
|
|
|
.then(r=> {
|
|
|
|
if (r.ok) {
|
2024-12-01 21:08:02 +00:00
|
|
|
// 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 ?? "");
|
|
|
|
});
|
2024-12-02 00:35:49 +00:00
|
|
|
const feeds = document.querySelectorAll("underbbs-author-messages");
|
|
|
|
feeds.forEach(f=>{
|
|
|
|
const target = f.getAttribute("data-target");
|
|
|
|
f.setAttribute("data-target", target ?? "");
|
|
|
|
});
|
2024-12-06 04:05:01 +00:00
|
|
|
const timelines = document.querySelectorAll("underbbs-timeline");
|
|
|
|
timelines.forEach(t=>{
|
|
|
|
const target = t.getAttribute("data-target");
|
|
|
|
t.setAttribute("data-target", target ?? "");
|
|
|
|
});
|
2024-07-01 03:51:25 +00:00
|
|
|
}
|
2024-07-16 19:43:35 +00:00
|
|
|
})
|
|
|
|
.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);
|
2024-12-01 21:08:02 +00:00
|
|
|
}
|
|
|
|
if (store) {
|
2024-07-01 03:51:25 +00:00
|
|
|
// 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);
|
2024-07-01 03:51:25 +00:00
|
|
|
break;
|
|
|
|
case "author":
|
2024-07-16 19:43:35 +00:00
|
|
|
store.profileCache.set(data.id, <Author>data);
|
2024-07-01 03:51:25 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-12-02 00:35:49 +00:00
|
|
|
// 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=>{
|
2024-12-01 21:08:02 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2024-12-06 04:05:01 +00:00
|
|
|
if (data.target) {
|
2024-12-09 17:35:44 +00:00
|
|
|
|
2024-12-06 04:05:01 +00:00
|
|
|
console.log("data has target: " + data.target);
|
|
|
|
let e = document.querySelector(`underbbs-timeline#${data.target}[data-adapter="${data.adapter}"]`);
|
|
|
|
if (e) {
|
2024-12-09 17:35:44 +00:00
|
|
|
e.setAttribute("data-latest", data.renoteId ?? data.id);
|
2024-12-06 04:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-16 19:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-08 01:04:04 +00:00
|
|
|
static connect(gateway: string): void {
|
|
|
|
DatagramSocket._gateway = gateway;
|
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
const wsProto = location.protocol == "https:" ? "wss" : "ws";
|
2024-12-08 01:04:04 +00:00
|
|
|
let wsUrl = ""
|
|
|
|
if (!gateway) {
|
|
|
|
wsUrl = `${wsProto}://${location.host}/subscribe`;
|
|
|
|
} else {
|
|
|
|
wsUrl = wsProto + "://" + gateway + "/subscribe"
|
|
|
|
}
|
|
|
|
const _conn = new WebSocket(wsUrl, "underbbs");
|
2024-08-31 17:01:31 +00:00
|
|
|
|
2024-07-16 19:43:35 +00:00
|
|
|
_conn.addEventListener("open", DatagramSocket.onOpen);
|
|
|
|
_conn.addEventListener("message", DatagramSocket.onMsg);
|
2024-08-31 17:01:31 +00:00
|
|
|
|
2024-07-01 03:51:25 +00:00
|
|
|
_conn.addEventListener("error", (e: any) => {
|
|
|
|
console.log("websocket connection error");
|
|
|
|
console.log(JSON.stringify(e));
|
|
|
|
});
|
2024-07-16 19:43:35 +00:00
|
|
|
DatagramSocket.conn = _conn;
|
|
|
|
}
|
2024-07-01 03:51:25 +00:00
|
|
|
}
|
|
|
|
|
2024-12-08 01:04:04 +00:00
|
|
|
function init() {
|
|
|
|
const saveData = localStorage.getItem("underbbs_settings");
|
|
|
|
Settings._instance = saveData ? JSON.parse(saveData) : new Settings();
|
|
|
|
|
|
|
|
customElements.define("underbbs-message", MessageElement);
|
|
|
|
customElements.define("underbbs-settings", SettingsElement);
|
|
|
|
customElements.define("underbbs-profile", ProfileElement);
|
|
|
|
customElements.define("underbbs-author-messages", AuthorMessagesElement);
|
|
|
|
customElements.define("underbbs-timeline", TimelineElement);
|
|
|
|
customElements.define("underbbs-timeline-filter", TimelineFilterElement);
|
|
|
|
|
|
|
|
console.log("underbbs initialized!")
|
|
|
|
}
|
|
|
|
|
|
|
|
init();
|
|
|
|
|