add: broadcast socket id for user sockets

This commit is contained in:
trisua 2025-05-02 23:29:38 -04:00
parent ecde5d3d46
commit 1724f798ca
6 changed files with 48 additions and 16 deletions
crates/app/src/public
html/communities
js

View file

@ -100,7 +100,7 @@
</div>
{% if user %}
<div class="card flex gap-2" id="join_or_leave">
<div class="card flex gap-2 flex-wrap" id="join_or_leave">
{% if not is_owner %} {% if not is_joined %} {% if not
is_pending %}
<button class="primary" onclick="join_community()">
@ -216,8 +216,15 @@
});
};
</script>
{% endif %} {% endif %} {% if can_manage_community or
is_manager %}
{% endif %} {% else %}
<a
href="/chats/{{ community.id }}/0"
class="button quaternary"
>
{{ icon "message-circle" }}
<span>{{ text "communities:label.chats" }}</span>
</a>
{% endif %} {% if can_manage_community or is_manager %}
<a
href="/community/{{ community.id }}/manage"
class="button primary"
@ -229,6 +236,7 @@
</a>
{% endif %}
</div>
{% endif %}
</div>

View file

@ -240,18 +240,13 @@
streams.subscribe("notifs");
streams.event("notifs", "message", (data) => {
if (data === "Ping") {
return;
}
const json = JSON.parse(data);
if (!json.method.Packet) {
if (!data.method.Packet) {
console.warn("notifications stream cannot read this message");
return;
}
const inner_data = JSON.parse(json.data);
if (json.method.Packet.Crud === "Create") {
const inner_data = JSON.parse(data.data);
if (data.method.Packet.Crud === "Create") {
const current = Number.parseInt(element.innerText || "0");
if (current <= 0) {
@ -293,7 +288,7 @@
console.info("notification created");
}
} else if (json.method.Packet.Crud === "Delete") {
} else if (data.method.Packet.Crud === "Delete") {
const current = Number.parseInt(element.innerText || "0");
if (current - 1 <= 0) {

View file

@ -12,6 +12,14 @@
return $.STREAMS[stream];
});
self.define("get", ({ $ }, id) => {
for (const stream of Object.values($.STREAMS)) {
if (stream.id === id) {
return stream;
}
}
});
self.define("subscribe", ({ $ }, stream) => {
if (!$.USER) {
console.warn("cannot subscribe without user id");
@ -27,6 +35,7 @@
const socket = new WebSocket(endpoint);
$.STREAMS[stream] = {
id: null,
socket,
events: {
message: () => {},
@ -38,10 +47,17 @@
return socket.send("Pong");
}
return $.sock(stream).events.message(event.data);
const data = JSON.parse(event.data);
if (data.method.Forward === "Key") {
$.STREAMS[stream].id = data.data;
return console.info(`${stream} ${data.data}`);
}
return $.sock(stream).events.message(data);
});
return socket;
return $.STREAMS[stream];
});
self.define("close", ({ $ }, stream) => {