Get users

This commit is contained in:
2026-08-22 12:14:11 +02:00
parent 78f5544952
commit 546670b9d2
3 changed files with 44 additions and 8 deletions
+23 -7
View File
@@ -165,14 +165,30 @@ export default class Enclave<P = Page> {
return; return;
case "Messages": case "Messages":
Object.entries(msg.messages).forEach(([channelId, messages]) => { const authors = Object.entries(msg.messages).flatMap(
if (!server.messages[channelId]) { ([channelId, messages]) => {
server.messages[channelId] = {}; if (!server.messages[channelId]) {
} server.messages[channelId] = {};
}
messages.forEach((message) => { return messages.map((message) => {
server.messages[channelId][message.id] = message; server.messages[channelId][message.id] = message;
}); return message.author;
});
},
);
const dedupedAuthors = Array.from(new Set(authors));
this.server?.getUsers(dedupedAuthors);
this.forceRender();
return;
case "Users":
Object.entries(msg.users).forEach(([pubKey, user]) => {
server.users[pubKey] = user;
}); });
this.forceRender(); this.forceRender();
+8
View File
@@ -15,6 +15,10 @@ export type ClientMethod =
| { | {
method: "Messages"; method: "Messages";
messages: Record<string, StoredMessage[]>; messages: Record<string, StoredMessage[]>;
}
| {
method: "Users";
users: Record<string, ClientMeta>;
}; };
export type ServerMethod = export type ServerMethod =
@@ -41,4 +45,8 @@ export type ServerMethod =
method: "GetMessages"; method: "GetMessages";
channel_id: string; channel_id: string;
chunk: number; chunk: number;
}
| {
method: "GetUsers";
pubkeys: string[];
}; };
+13 -1
View File
@@ -3,7 +3,7 @@ import * as ed from "@noble/ed25519";
import EnclaveWebSocket from "./ws"; import EnclaveWebSocket from "./ws";
import { sha512 } from "@noble/hashes/sha2.js"; import { sha512 } from "@noble/hashes/sha2.js";
import { getWSUrl } from "@/lib/serverList"; import { getWSUrl } from "@/lib/serverList";
import { ServerMeta, StoredMessage } from "@/lib/types"; import { ClientMeta, ServerMeta, StoredMessage } from "@/lib/types";
ed.hashes.sha512 = sha512; ed.hashes.sha512 = sha512;
@@ -30,11 +30,13 @@ export default class EnclaveServer {
public websocket?: EnclaveWebSocket; public websocket?: EnclaveWebSocket;
public meta?: ServerMeta; public meta?: ServerMeta;
public messages: Record<string, Record<string, StoredMessage>>; public messages: Record<string, Record<string, StoredMessage>>;
public users: Record<string, ClientMeta | null>;
public constructor(hostname: string, isSecure: boolean) { public constructor(hostname: string, isSecure: boolean) {
this.hostname = hostname; this.hostname = hostname;
this.isSecure = isSecure; this.isSecure = isSecure;
this.messages = {}; this.messages = {};
this.users = {};
} }
public disconnect() { public disconnect() {
@@ -94,4 +96,14 @@ export default class EnclaveServer {
throw Error("Invalid signature"); throw Error("Invalid signature");
} }
} }
public getUsers(pubkeys: string[]) {
const filteredPubkeys = pubkeys.filter((pubkey) => {
const exists = this.users[pubkey] !== undefined;
if (!exists) this.users[pubkey] = null;
return !exists;
});
this.websocket?.send({ method: "GetUsers", pubkeys: filteredPubkeys });
}
} }