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
+18 -2
View File
@@ -165,14 +165,30 @@ export default class Enclave<P = Page> {
return;
case "Messages":
Object.entries(msg.messages).forEach(([channelId, messages]) => {
const authors = Object.entries(msg.messages).flatMap(
([channelId, messages]) => {
if (!server.messages[channelId]) {
server.messages[channelId] = {};
}
messages.forEach((message) => {
return messages.map((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();
+8
View File
@@ -15,6 +15,10 @@ export type ClientMethod =
| {
method: "Messages";
messages: Record<string, StoredMessage[]>;
}
| {
method: "Users";
users: Record<string, ClientMeta>;
};
export type ServerMethod =
@@ -41,4 +45,8 @@ export type ServerMethod =
method: "GetMessages";
channel_id: string;
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 { sha512 } from "@noble/hashes/sha2.js";
import { getWSUrl } from "@/lib/serverList";
import { ServerMeta, StoredMessage } from "@/lib/types";
import { ClientMeta, ServerMeta, StoredMessage } from "@/lib/types";
ed.hashes.sha512 = sha512;
@@ -30,11 +30,13 @@ export default class EnclaveServer {
public websocket?: EnclaveWebSocket;
public meta?: ServerMeta;
public messages: Record<string, Record<string, StoredMessage>>;
public users: Record<string, ClientMeta | null>;
public constructor(hostname: string, isSecure: boolean) {
this.hostname = hostname;
this.isSecure = isSecure;
this.messages = {};
this.users = {};
}
public disconnect() {
@@ -94,4 +96,14 @@ export default class EnclaveServer {
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 });
}
}