golgi/src/messages.rs

99 lines
3.5 KiB
Rust
Raw Normal View History

2021-12-29 16:31:53 +00:00
//! Message types and conversion methods for `golgi`.
2021-12-29 15:59:05 +00:00
use kuska_ssb::api::dto::content::TypedMessage;
2021-12-29 16:31:53 +00:00
use serde::{Deserialize, Serialize};
use serde_json::Value;
2022-01-05 18:58:48 +00:00
use std::fmt::Debug;
2021-12-29 15:59:05 +00:00
use crate::error::GolgiError;
2021-12-29 16:31:53 +00:00
/// 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)
2021-12-29 15:59:05 +00:00
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)]
2021-12-29 16:31:53 +00:00
#[allow(missing_docs)]
2021-12-29 15:59:05 +00:00
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,
}
2021-12-29 20:12:20 +00:00
// Enum representing the different possible message content types
#[derive(Debug)]
2022-01-04 20:02:20 +00:00
#[allow(missing_docs)]
2021-12-29 20:12:20 +00:00
pub enum SsbMessageContentType {
About,
Vote,
Post,
Contact,
2022-01-05 18:58:48 +00:00
Unrecognized,
2021-12-29 20:12:20 +00:00
}
2021-12-29 15:59:05 +00:00
impl SsbMessageValue {
2022-01-05 18:58:48 +00:00
/// Gets the type field of the message content as an enum, if found.
/// if no type field is found or the type field is not a string, it returns an Err(GolgiError::ContentType)
/// if a type field is found but with an unknown string it returns an Ok(SsbMessageContentType::Unrecognized)
pub fn get_message_type(&self) -> Result<SsbMessageContentType, GolgiError> {
let msg_type = self
.content
.get("type")
2022-01-12 17:42:50 +00:00
.ok_or_else(|| GolgiError::ContentType("type field not found".to_string()))?;
let mtype_str: &str = msg_type.as_str().ok_or_else(|| {
GolgiError::ContentType("type field value is not a string as expected".to_string())
})?;
2022-01-05 18:58:48 +00:00
let enum_type = match mtype_str {
"about" => SsbMessageContentType::About,
"post" => SsbMessageContentType::Post,
"vote" => SsbMessageContentType::Vote,
"contact" => SsbMessageContentType::Contact,
_ => SsbMessageContentType::Unrecognized,
};
Ok(enum_type)
}
2021-12-29 20:12:20 +00:00
/// Helper function which returns true if this message is of the given type,
/// and false if the type does not match or is not found
2022-02-07 11:54:47 +00:00
pub fn is_message_type(&self, _message_type: SsbMessageContentType) -> bool {
2021-12-29 20:12:20 +00:00
let self_message_type = self.get_message_type();
match self_message_type {
Ok(mtype) => {
2022-02-07 11:54:47 +00:00
matches!(mtype, _message_type)
2021-12-29 20:12:20 +00:00
}
2022-01-05 18:58:48 +00:00
Err(_err) => false,
2021-12-29 20:12:20 +00:00
}
2021-12-29 15:59:05 +00:00
}
2021-12-29 16:31:53 +00:00
/// 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
2021-12-29 15:59:05 +00:00
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)]
2021-12-29 16:31:53 +00:00
#[allow(missing_docs)]
2021-12-30 01:08:55 +00:00
pub struct SsbMessageKVT {
2021-12-29 15:59:05 +00:00
pub key: String,
pub value: SsbMessageValue,
pub timestamp: f64,
pub rts: Option<f64>,
}