Messages
This commit is contained in:
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
use crate::protocol::ClientMeta;
|
||||
use crate::types::ClientMeta;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
||||
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashMap, fs};
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
use crate::protocol::ServerMeta;
|
||||
use crate::types::ServerMeta;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub mod commands;
|
||||
pub mod protocol;
|
||||
pub mod types;
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ClientMeta {
|
||||
pub display_name: String,
|
||||
pub avatar: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ServerMeta {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "method")]
|
||||
pub enum ClientMethod {
|
||||
Initialized {
|
||||
public_key: String,
|
||||
signature: String,
|
||||
|
||||
timestamp: u64,
|
||||
hostname: String,
|
||||
},
|
||||
|
||||
Error {
|
||||
error: Cow<'static, str>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "method")]
|
||||
pub enum ServerMethod {
|
||||
Initialize {
|
||||
public_key: String,
|
||||
signature: String,
|
||||
|
||||
timestamp: u64,
|
||||
hostname: String,
|
||||
},
|
||||
|
||||
Meta(ClientMeta),
|
||||
|
||||
Error {
|
||||
error: String,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ClientMeta {
|
||||
pub display_name: String,
|
||||
pub avatar: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ServerMeta {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
}
|
||||
+40
-1
@@ -7,6 +7,7 @@ import { base58 } from "@scure/base";
|
||||
import axios from "axios";
|
||||
import { Account, AccountsFile, getAccounts } from "@/lib/accounts";
|
||||
import { Page } from "@/components/page/PageView";
|
||||
import { ClientMethod } from "./protocol";
|
||||
|
||||
ed.hashes.sha512 = sha512;
|
||||
|
||||
@@ -124,7 +125,12 @@ export default class Enclave<P = Page> {
|
||||
return;
|
||||
}
|
||||
|
||||
this.server.websocket?.send({ method: "Meta", ...account.meta });
|
||||
if (this.server.websocket) {
|
||||
this.server.websocket.send({ method: "Meta", ...account.meta });
|
||||
this.server.websocket.websocket.onmessage = (msg) => {
|
||||
this.onMessage(JSON.parse(msg.data));
|
||||
};
|
||||
}
|
||||
|
||||
const metaResponse = await axios.get(
|
||||
getHTTPUrl(hostname, isSecure, "/meta"),
|
||||
@@ -140,4 +146,37 @@ export default class Enclave<P = Page> {
|
||||
|
||||
this.forceRender();
|
||||
}
|
||||
|
||||
public async onMessage(msg: ClientMethod) {
|
||||
const server = this.server;
|
||||
|
||||
if (!server) {
|
||||
console.error("Unable to get server for message, MSG:", msg);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (msg.method) {
|
||||
case "Initialized":
|
||||
console.error("Already initialized");
|
||||
return;
|
||||
|
||||
case "Error":
|
||||
console.error("Server error:", msg.error);
|
||||
return;
|
||||
|
||||
case "Messages":
|
||||
console.log("msg", msg.messages);
|
||||
Object.entries(msg.messages).forEach(([channelId, messages]) => {
|
||||
if (!server.messages[channelId]) {
|
||||
server.messages[channelId] = [];
|
||||
}
|
||||
|
||||
messages.forEach((message) => {
|
||||
server.messages[channelId].push(message);
|
||||
});
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-3
@@ -1,4 +1,4 @@
|
||||
import { ClientMeta, MessageData } from "@/lib/types";
|
||||
import { ClientMeta, MessageData, StoredMessage } from "@/lib/types";
|
||||
|
||||
export type ClientMethod =
|
||||
| {
|
||||
@@ -11,6 +11,10 @@ export type ClientMethod =
|
||||
| {
|
||||
method: "Error";
|
||||
error: string;
|
||||
}
|
||||
| {
|
||||
method: "Messages";
|
||||
messages: Record<string, StoredMessage[]>;
|
||||
};
|
||||
|
||||
export type ServerMethod =
|
||||
@@ -21,6 +25,10 @@ export type ServerMethod =
|
||||
timestamp: number;
|
||||
hostname: string;
|
||||
}
|
||||
| {
|
||||
method: "Error";
|
||||
error: string;
|
||||
}
|
||||
| ({
|
||||
method: "Meta";
|
||||
} & ClientMeta)
|
||||
@@ -30,6 +38,7 @@ export type ServerMethod =
|
||||
data: MessageData;
|
||||
}
|
||||
| {
|
||||
method: "Error";
|
||||
error: string;
|
||||
method: "GetMessages";
|
||||
channel_id: string;
|
||||
chunk: number;
|
||||
};
|
||||
|
||||
+3
-1
@@ -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 } from "@/lib/types";
|
||||
import { ServerMeta, StoredMessage } from "@/lib/types";
|
||||
|
||||
ed.hashes.sha512 = sha512;
|
||||
|
||||
@@ -29,10 +29,12 @@ export default class EnclaveServer {
|
||||
public isSecure: boolean;
|
||||
public websocket?: EnclaveWebSocket;
|
||||
public meta?: ServerMeta;
|
||||
public messages: Record<string, StoredMessage[]>;
|
||||
|
||||
public constructor(hostname: string, isSecure: boolean) {
|
||||
this.hostname = hostname;
|
||||
this.isSecure = isSecure;
|
||||
this.messages = {};
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import Enclave from "@/app/app";
|
||||
import { ChannelPageProps } from "@/components/page/PageView";
|
||||
import { useEffect } from "react";
|
||||
|
||||
export default function TextChannel({
|
||||
appRef,
|
||||
@@ -10,13 +11,17 @@ export default function TextChannel({
|
||||
|
||||
if (!channel) return null;
|
||||
|
||||
useEffect(() => {
|
||||
appRef.current?.server?.websocket?.send({
|
||||
method: "GetMessages",
|
||||
channel_id: channel.id,
|
||||
chunk: 0,
|
||||
});
|
||||
|
||||
appRef.current?.forceRender();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div
|
||||
onClick={() => {
|
||||
appRef.current?.sendMessage("Hello, World", channel.id);
|
||||
}}
|
||||
>
|
||||
{channel.name}
|
||||
</div>
|
||||
<div>{JSON.stringify(appRef.current?.server?.messages[channel.id])}</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user