golgi/src/messages.rs

54 lines
1.7 KiB
Rust

use std::fmt::Debug;
use async_std::io::Read;
use serde_json::Value;
use serde::{Serialize, Deserialize};
use kuska_ssb::api::dto::WhoAmIOut;
use kuska_ssb::feed::{Feed, Message};
use kuska_ssb::rpc::{RecvMsg, RequestNo, RpcReader};
use kuska_ssb::api::dto::content::TypedMessage;
use crate::error::GolgiError;
pub type SsbMessageContent = TypedMessage;
/// Data type representing the `value` of a message object (`KVT`). More information concerning the
/// data model can be found
/// in the [`Metadata` documentation](https://spec.scuttlebutt.nz/feed/messages.html#metadata).
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct SsbMessageValue {
pub previous: Option<String>,
pub author: String,
pub sequence: u64,
pub timestamp: f64,
pub hash: String,
pub content: Value,
pub signature: String,
}
impl SsbMessageValue {
pub fn get_message_type(&self) -> String {
let msg_type: String = self.content.get("type").map(|msg_type| msg_type.to_string()).unwrap_or_else(|| "none".to_string());
msg_type
}
pub fn into_ssb_message_content(self) -> Result<SsbMessageContent, GolgiError> {
let m: SsbMessageContent = serde_json::from_value(self.content)?;
Ok(m)
}
}
/// Data type representing the `value` of a message object (`KVT`). More information concerning the
/// data model can be found
/// in the [`Metadata` documentation](https://spec.scuttlebutt.nz/feed/messages.html#metadata).
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct SsbKVT {
pub key: String,
pub value: SsbMessageValue,
pub timestamp: f64,
pub rts: Option<f64>,
}