68 lines
2.2 KiB
Rust
68 lines
2.2 KiB
Rust
//! Message types and conversion methods for `golgi`.
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use kuska_ssb::api::dto::content::TypedMessage;
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::Value;
|
|
|
|
use crate::error::GolgiError;
|
|
|
|
/// This is an alias to TypedMessage in kuska,
|
|
/// which is renamed as SsbMessageContent in golgi to fit the naming convention
|
|
/// of the other types (SsbMessageKVT and SsbMessageValue)
|
|
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)]
|
|
#[allow(missing_docs)]
|
|
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 {
|
|
|
|
/// Gets the type field of the message content if found,
|
|
/// and if not returns "none"
|
|
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
|
|
}
|
|
|
|
/// Converts the content json value into an SsbMessageContent enum,
|
|
/// using the "type" field as a tag to select which variant of the enum
|
|
/// to deserialize into.
|
|
///
|
|
/// See more info on this here https://serde.rs/enum-representations.html#internally-tagged
|
|
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)]
|
|
#[allow(missing_docs)]
|
|
pub struct SsbKVT {
|
|
pub key: String,
|
|
pub value: SsbMessageValue,
|
|
pub timestamp: f64,
|
|
pub rts: Option<f64>,
|
|
}
|