summaryrefslogtreecommitdiffstats
path: root/src/repository/client.rs
blob: 0273097a21a344468b54b626a30eeb734289dc22 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use std::io::Cursor;
use std::sync::Arc;
use std::ops::Deref;

use ipfs_api::IpfsClient;
use ipfs_api::KeyType;
use failure::Error;
use failure::err_msg;
use futures::future::Future;
use futures::stream::Stream;
use serde_json::from_str as serde_json_from_str;
use serde_json::to_string as serde_json_to_str;
use chrono::NaiveDateTime;

use types::block::Block;
use types::content::Content;
use types::content::Payload;
use types::util::IPFSHash;
use types::util::IPNSHash;
use types::util::Timestamp;
use repository::{ProfileName, ProfileKey};
use version::protocol_version;

pub fn deref_ipns_hash(client: Arc<IpfsClient>,
                     hash: &IPNSHash)
    -> impl Future<Item = IPFSHash, Error = Error>
{
    client
        .name_resolve(Some(hash.deref()), false, false)
        .map_err(Error::from)
        .map(|resp| IPFSHash::from(resp.path))
}

pub fn resolve_plain(client: Arc<IpfsClient>, hash: &IPFSHash)
    -> impl Future<Item = Vec<u8>, Error = Error>
{
    client
        .cat(hash)
        .concat2()
        .map_err(Error::from)
        .map(|blob| blob.into_bytes().to_vec())
}

pub fn resolve_block(client: Arc<IpfsClient>, hash: &IPFSHash)
    -> impl Future<Item = Block, Error = Error>
{
    client
        .cat(hash)
        .concat2()
        .map_err(Error::from)
        .and_then(|block| {
            debug!("Got Block data, building Block object");

            String::from_utf8(block.into_bytes().to_vec())
                .map_err(Error::from)
                .and_then(|s| serde_json_from_str(&s).map_err(Error::from))
        })
}

pub fn get_key_id_from_key_name(client: Arc<IpfsClient>, name: ProfileName)
    -> impl Future<Item = ProfileKey, Error = Error>
{
    client.key_list()
        .map_err(Error::from)
        .and_then(move |list| {
            list.keys
                .into_iter()
                .filter(|pair| pair.name == *name.deref())
                .next()
                .map(|pair| ProfileKey::from(pair.id))
                .ok_or_else(|| err_msg("No Key"))
        })
}

pub fn resolve_latest_block(client: Arc<IpfsClient>, hash: &IPNSHash)
    -> impl Future<Item = Block, Error = Error>
{
    deref_ipns_hash(client.clone(), hash)
        .map_err(Error::from)
        .and_then(|ipfs_hash| resolve_block(client, &ipfs_hash))
}

pub fn resolve_content(client: Arc<IpfsClient>, hash: &IPFSHash)
    -> impl Future<Item = Content, Error = Error>
{
    client
        .cat(hash)
        .concat2()
        .map_err(Error::from)
        .and_then(|content| {
            debug!("Got Content data, building Content object");

            String::from_utf8(content.into_bytes().to_vec())
                .map_err(Error::from)
                .and_then(|s| serde_json_from_str(&s).map_err(Error::from))
        })
}

pub fn resolve_content_none(client: Arc<IpfsClient>, hash: &IPFSHash)
    -> impl Future<Item = Content, Error = Error>
{
    resolve_content(client, hash).and_then(|content| {
        debug!("Got Content object, checking whether it is None");
        match content.payload() {
            &Payload::None  => Ok(content),
            _               => Err(err_msg("Content is not None")),
        }
    })
}

pub fn resolve_content_post(client: Arc<IpfsClient>, hash: &IPFSHash)
    -> impl Future<Item = Content, Error = Error>
{
    resolve_content(client, hash)
        .and_then(|content| {
            debug!("Got Content object, checking whether it is Post");
            match content.payload() {
                &Payload::Post {..} => Ok(content),
                _                   => Err(err_msg("Content is not a Post")),
            }
        })
}

pub fn resolve_content_attached_post_comments(client: Arc<IpfsClient>, hash: &IPFSHash)
    -> impl Future<Item = Content, Error = Error>
{
    resolve_content(client, hash)
        .and_then(|content| {
            debug!("Got Content object, checking whether it is AttachedPostComments");
            match content.payload() {
                &Payload::AttachedPostComments {..} => Ok(content),
                _                                   => Err(err_msg("Content is not AttachedPostComments")),
            }
        })
}

pub fn resolve_content_profile(client: Arc<IpfsClient>, hash: &IPFSHash)
    -> impl Future<Item = Content, Error = Error>
{
    resolve_content(client, hash)
        .and_then(|content| {
            debug!("Got Content object, checking whether it is Profile");
            match content.payload() {
                &Payload::Profile {..} => Ok(content),
                _                      => Err(err_msg("Content is not a Profile")),
            }
        })
}

pub fn announce_block(client: Arc<IpfsClient>,
                      key: ProfileKey,
                      state: &IPFSHash,
                      lifetime: Option<String>,
                      ttl: Option<String>)
    -> impl Future<Item = (), Error = Error>
{
    let name   = format!("/ipfs/{}", state);

    resolve_block(client.clone(), state)
        .and_then(move |_| {
            debug!("Publishing block.");
            client.name_publish(&name,
                                false,
                                lifetime.as_ref().map(String::deref),
                                ttl.as_ref().map(String::deref),
                                Some(&key))
                .map_err(From::from)
                .map(|_| ())
        })
}


pub fn put_plain(client: Arc<IpfsClient>, data: Vec<u8>)
    -> impl Future<Item = IPFSHash, Error = Error>
{
    client
        .add(Cursor::new(data))
        .map(|res| IPFSHash::from(res.hash))
        .map_err(Into::into)
}

pub fn put_block(client: Arc<IpfsClient>, block: &Block)
    -> impl Future<Item = IPFSHash, Error = Error>
{
    let data = serde_json_to_str(block);

    ::futures::future::result(data)
        .map_err(Into::into)
        .and_then(move |data| put_plain(client, data.into_bytes()))
}

pub fn put_content(client: Arc<IpfsClient>, content: &Content)
    -> impl Future<Item = IPFSHash, Error = Error>
{
    let data = serde_json_to_str(content);
    ::futures::future::result(data)
        .map_err(Into::into)
        .and_then(move |data| put_plain(client, data.into_bytes()))
}

pub fn new_profile(client: Arc<IpfsClient>,
                   keyname: String,
                   profile: Content,
                   lifetime: Option<String>,
                   ttl: Option<String>)
    -> impl Future<Item = (ProfileName, ProfileKey), Error = Error>
{
    let client1 = client.clone();
    let client2 = client.clone();
    let client3 = client.clone();

    client
        .key_gen(&keyname, KeyType::Rsa, 4096)
        .map_err(Error::from)
        .map(|kp| (kp.name, kp.id))
        .and_then(move |(key_name, key_id)| { // put the content into IPFS
            let mut prof = profile;
            prof.push_device(IPNSHash::from(key_id.clone()));

            put_content(client1, &prof)
                .map(move |content_hash| (content_hash, key_name, key_id))
                .map_err(Error::from)
        })
        .map(|(content_hash, key_name, key_id)| {
            let block = Block::new(protocol_version(),
                                   vec![], // no parents for new profile
                                   content_hash);

            (block, key_name, key_id)
        })
        .