summaryrefslogtreecommitdiffstats
path: root/src/types/content.rs
blob: 8b6eb4d45a32a59a5f71567005baa238447274e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::collections::BTreeMap;

use anyhow::Error;

use crate::types::util::IPFSHash;
use crate::types::util::IPNSHash;
use crate::types::util::MimeType;
use crate::types::util::Timestamp;
use crate::types::payload::Payload;
use crate::types::payload::LoadedPayload;
use crate::model::Model;

#[derive(Serialize, Deserialize, Debug)]
pub struct Content {

    //
    //
    // Metadata about the content
    // --------------------------
    //
    // This metadata should be added to each block version. It is a small amount of bytes, but it
    // makes the aggregation much simpler.
    //
    // In v2 of the API, we might change this and put all this meta-information into variants of
    // `Payload`, if we find that aggregation is fast enough.
    //

    /// A list of IPNS hashes which are posting to this chain (so if a client has one profile
    /// node, it can find the latest profile nodes from all devices a user posts from)
    #[serde(rename = "devices")]
    devices: Vec<IPNSHash>,

    /// Timestamp (UNIX timestamp) when this was created. Can be left out.
    #[serde(rename = "timestamp")]
    #[serde(default)]
    timestamp: Option<Timestamp>,

    /// The payload of the content block
    #[serde(rename = "payload")]
    payload: Payload,

}

impl Content {

    pub fn new(devices: Vec<IPNSHash>, timestamp: Option<Timestamp>, payload: Payload) -> Content {
        Content { devices, timestamp, payload }
    }

    pub fn devices(&self) -> &Vec<IPNSHash> {
        &self.devices
    }

    pub fn timestamp(&self) -> Option<&Timestamp> {
        self.timestamp.as_ref()
    }

    pub fn payload(&self) -> &Payload {
        &self.payload
    }

    pub(crate) fn push_device(&mut self, dev: IPNSHash) {
        self.devices.push(dev);
    }

    pub async fn load(self, r: &Model) -> Result<LoadedContent, Error> {
        Ok({
            LoadedContent {
                devices: self.devices,
                timestamp: self.timestamp,
                payload: self.payload.load(r).await?
            }
        })
    }

}

impl AsRef<Content> for Content {
    fn as_ref(&self) -> &Self {
        &self
    }
}

#[derive(Debug)]
pub struct LoadedContent {
    devices: Vec<IPNSHash>,
    timestamp: Option<Timestamp>,
    payload: LoadedPayload,
}

impl LoadedContent {
    pub fn devices(&self) -> &Vec<IPNSHash> {
        &self.devices
    }

    pub fn timestamp(&self) -> Option<&Timestamp> {
        self.timestamp.as_ref()
    }

    pub fn payload(&self) -> &LoadedPayload {
        &self.payload
    }
}