Messages
This commit is contained in:
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use tauri::{AppHandle, Manager};
|
use tauri::{AppHandle, Manager};
|
||||||
|
|
||||||
use crate::protocol::ClientMeta;
|
use crate::types::ClientMeta;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::{collections::HashMap, fs};
|
use std::{collections::HashMap, fs};
|
||||||
use tauri::{AppHandle, Manager};
|
use tauri::{AppHandle, Manager};
|
||||||
|
|
||||||
use crate::protocol::ServerMeta;
|
use crate::types::ServerMeta;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod protocol;
|
pub mod types;
|
||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
pub fn run() {
|
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 axios from "axios";
|
||||||
import { Account, AccountsFile, getAccounts } from "@/lib/accounts";
|
import { Account, AccountsFile, getAccounts } from "@/lib/accounts";
|
||||||
import { Page } from "@/components/page/PageView";
|
import { Page } from "@/components/page/PageView";
|
||||||
|
import { ClientMethod } from "./protocol";
|
||||||
|
|
||||||
ed.hashes.sha512 = sha512;
|
ed.hashes.sha512 = sha512;
|
||||||
|
|
||||||
@@ -124,7 +125,12 @@ export default class Enclave<P = Page> {
|
|||||||
return;
|
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(
|
const metaResponse = await axios.get(
|
||||||
getHTTPUrl(hostname, isSecure, "/meta"),
|
getHTTPUrl(hostname, isSecure, "/meta"),
|
||||||
@@ -140,4 +146,37 @@ export default class Enclave<P = Page> {
|
|||||||
|
|
||||||
this.forceRender();
|
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 =
|
export type ClientMethod =
|
||||||
| {
|
| {
|
||||||
@@ -11,6 +11,10 @@ export type ClientMethod =
|
|||||||
| {
|
| {
|
||||||
method: "Error";
|
method: "Error";
|
||||||
error: string;
|
error: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
method: "Messages";
|
||||||
|
messages: Record<string, StoredMessage[]>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ServerMethod =
|
export type ServerMethod =
|
||||||
@@ -21,6 +25,10 @@ export type ServerMethod =
|
|||||||
timestamp: number;
|
timestamp: number;
|
||||||
hostname: string;
|
hostname: string;
|
||||||
}
|
}
|
||||||
|
| {
|
||||||
|
method: "Error";
|
||||||
|
error: string;
|
||||||
|
}
|
||||||
| ({
|
| ({
|
||||||
method: "Meta";
|
method: "Meta";
|
||||||
} & ClientMeta)
|
} & ClientMeta)
|
||||||
@@ -30,6 +38,7 @@ export type ServerMethod =
|
|||||||
data: MessageData;
|
data: MessageData;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
method: "Error";
|
method: "GetMessages";
|
||||||
error: string;
|
channel_id: string;
|
||||||
|
chunk: number;
|
||||||
};
|
};
|
||||||
|
|||||||
+3
-1
@@ -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 } from "@/lib/types";
|
import { ServerMeta, StoredMessage } from "@/lib/types";
|
||||||
|
|
||||||
ed.hashes.sha512 = sha512;
|
ed.hashes.sha512 = sha512;
|
||||||
|
|
||||||
@@ -29,10 +29,12 @@ export default class EnclaveServer {
|
|||||||
public isSecure: boolean;
|
public isSecure: boolean;
|
||||||
public websocket?: EnclaveWebSocket;
|
public websocket?: EnclaveWebSocket;
|
||||||
public meta?: ServerMeta;
|
public meta?: ServerMeta;
|
||||||
|
public messages: Record<string, StoredMessage[]>;
|
||||||
|
|
||||||
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 = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnect() {
|
public disconnect() {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Enclave from "@/app/app";
|
import Enclave from "@/app/app";
|
||||||
import { ChannelPageProps } from "@/components/page/PageView";
|
import { ChannelPageProps } from "@/components/page/PageView";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export default function TextChannel({
|
export default function TextChannel({
|
||||||
appRef,
|
appRef,
|
||||||
@@ -10,13 +11,17 @@ export default function TextChannel({
|
|||||||
|
|
||||||
if (!channel) return null;
|
if (!channel) return null;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
appRef.current?.server?.websocket?.send({
|
||||||
|
method: "GetMessages",
|
||||||
|
channel_id: channel.id,
|
||||||
|
chunk: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
appRef.current?.forceRender();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div>{JSON.stringify(appRef.current?.server?.messages[channel.id])}</div>
|
||||||
onClick={() => {
|
|
||||||
appRef.current?.sendMessage("Hello, World", channel.id);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{channel.name}
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user