Skip to main content

kuska_ssb/api/dto/
content.rs

1#![allow(clippy::large_enum_variant)]
2
3use std::collections::HashMap;
4
5pub type SsbHash = String;
6pub type SsbId = String;
7pub type SsbMsgType = String;
8
9#[derive(Debug, Serialize, Deserialize)]
10pub struct Mention {
11    pub link: SsbId,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub name: Option<String>,
14}
15
16#[derive(Debug, Serialize, Deserialize)]
17pub struct Post {
18    #[serde(rename = "type")]
19    pub xtype: String,
20    pub text: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub mentions: Option<Vec<Mention>>,
23}
24
25impl Post {
26    pub fn new(text: String, mentions: Option<Vec<Mention>>) -> Self {
27        Post {
28            xtype: String::from("post"),
29            text,
30            mentions,
31        }
32    }
33    pub fn to_msg(&self) -> serde_json::Result<serde_json::Value> {
34        serde_json::to_value(self)
35    }
36}
37
38#[derive(Debug, Serialize, Deserialize)]
39pub struct PubAddress {
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub host: Option<String>,
42    pub port: u16,
43    pub key: String,
44}
45
46#[derive(Debug, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum VoteValue {
49    Numeric(i64),
50    Boolean(bool),
51}
52
53#[derive(Debug, Serialize, Deserialize)]
54pub struct Vote {
55    link: SsbHash,
56    value: VoteValue,
57    #[serde(skip_serializing_if = "Option::is_none")]
58    expression: Option<String>,
59}
60
61#[derive(Debug, Serialize, Deserialize)]
62#[serde(untagged)]
63pub enum Image {
64    OnlyLink(SsbHash),
65    Complete {
66        link: SsbHash,
67        #[serde(skip_serializing_if = "Option::is_none")]
68        name: Option<String>,
69        size: u64,
70        #[serde(skip_serializing_if = "Option::is_none")]
71        width: Option<u32>,
72        #[serde(skip_serializing_if = "Option::is_none")]
73        height: Option<u32>,
74        #[serde(rename = "type")]
75        content_type: String,
76    },
77}
78
79#[derive(Debug, Serialize, Deserialize)]
80pub struct DateTime {
81    epoch: u64,
82    tz: String,
83}
84
85#[derive(Debug, Deserialize)]
86#[serde(untagged)]
87pub enum Branch {
88    One(SsbHash),
89    Many(Vec<SsbHash>),
90}
91
92#[derive(Debug, Deserialize)]
93#[serde(untagged)]
94pub enum Mentions {
95    Link(SsbHash),
96    One(Mention),
97    Vector(Vec<Mention>),
98    Map(HashMap<String, Mention>),
99}
100
101#[derive(Debug, Serialize, Deserialize)]
102#[serde(tag = "type")]
103pub enum TypedMessage {
104    #[serde(rename = "pub")]
105    Pub { address: Option<PubAddress> },
106    #[serde(rename = "post")]
107    Post {
108        text: String,
109        #[serde(skip_serializing_if = "Option::is_none")]
110        mentions: Option<Vec<Mention>>,
111    },
112    #[serde(rename = "contact")]
113    Contact {
114        contact: Option<SsbId>,
115        #[serde(skip_serializing_if = "Option::is_none")]
116        blocking: Option<bool>,
117        #[serde(skip_serializing_if = "Option::is_none")]
118        following: Option<bool>,
119        #[serde(skip_serializing_if = "Option::is_none")]
120        autofollow: Option<bool>,
121    },
122    #[serde(rename = "about")]
123    About {
124        about: SsbId,
125        #[serde(skip_serializing_if = "Option::is_none")]
126        name: Option<String>,
127        #[serde(skip_serializing_if = "Option::is_none")]
128        title: Option<String>,
129        #[serde(skip_serializing_if = "Option::is_none")]
130        branch: Option<SsbHash>,
131        #[serde(skip_serializing_if = "Option::is_none")]
132        image: Option<Image>,
133        #[serde(skip_serializing_if = "Option::is_none")]
134        description: Option<String>,
135        #[serde(skip_serializing_if = "Option::is_none")]
136        location: Option<String>,
137        #[serde(skip_serializing_if = "Option::is_none")]
138        #[serde(rename = "startDateTime")]
139        start_datetime: Option<DateTime>,
140    },
141    #[serde(rename = "channel")]
142    Channel { channel: String, subscribed: bool },
143    #[serde(rename = "vote")]
144    Vote { vote: Vote },
145}
146
147/// An ssb-ql-1 query as defined by the 'Subset replication for SSB'
148/// specification.
149#[derive(Debug, Serialize, Deserialize)]
150#[serde(untagged)]
151pub enum SubsetQuery {
152    Type { op: String, string: SsbMsgType },
153    Author { op: String, feed: SsbId },
154    And { op: String, args: Vec<SubsetQuery> },
155    Or { op: String, args: Vec<SubsetQuery> },
156}
157
158/// Optional parameters for defining the order, shape and length of
159/// returned query data.
160#[derive(Debug, Serialize, Deserialize)]
161pub struct SubsetQueryOptions {
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub descending: Option<bool>,
164    #[serde(skip_serializing_if = "Option::is_none")]
165    pub keys: Option<bool>,
166    #[serde(skip_serializing_if = "Option::is_none")]
167    #[serde(rename = "pageLimit")]
168    pub page_limit: Option<u32>,
169}
170
171/// Query the follow or block state of two peers.
172#[derive(Debug, Serialize, Deserialize)]
173pub struct RelationshipQuery {
174    pub source: String,
175    pub dest: String,
176}
177
178#[derive(Debug, Serialize, Deserialize)]
179pub struct FriendsHops {
180    /// A maximum hops distance. Nodes beyond this distance are omitted from
181    /// the output.
182    pub max: i32,
183    /// Reverse the perspective when calculating hops distance; from `start`
184    /// looking "out" (`false`) or from the peer ID(s) looking "in" (`true`).
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub reverse: Option<bool>,
187    /// Feed ID of the "central" node where distance is zero.
188    /// (Default: sbot.id).
189    #[serde(skip_serializing_if = "Option::is_none")]
190    pub start: Option<String>,
191}
192
193/// Optional parameter for defining the number of times an invite can be used.
194#[derive(Debug, Serialize, Deserialize)]
195pub struct InviteCreateOptions {
196    pub uses: u16,
197}