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
use super::ApiError;
use crate::keybase_cmd::{call_chat_api, listen_chat_api};
use futures::{executor::block_on, future, stream::StreamExt};
use keybase_protocol::chat1::api;
use keybase_protocol::stellar1;
use serde::{Deserialize, Serialize};
use serde_json;
#[derive(Serialize, Deserialize, Debug)]
pub struct APIRPC<T> {
method: &'static str,
params: Option<T>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct OptionsOnly<T> {
pub options: T,
}
#[derive(Serialize, Debug)]
pub struct ReadConvParams<'a> {
pub channel: &'a ChannelParams,
}
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct ChannelParams {
pub name: String,
pub members_type: Option<String>,
pub topic_name: Option<String>,
}
pub type ReadConv<'a> = APIRPC<OptionsOnly<ReadConvParams<'a>>>;
const LISTMETHOD: APIRPC<()> = APIRPC {
method: "list",
params: None,
};
#[derive(Deserialize, Serialize)]
pub struct ListResult {
pub conversations: Vec<api::ConvSummary>,
}
pub fn list() -> Result<ListResult, ApiError> {
let input = serde_json::to_vec(&LISTMETHOD)?;
call_chat_api::<ListResult>(&input)
}
pub fn read_conv(channel: &ChannelParams) -> Result<api::Thread, ApiError> {
let input: ReadConv = APIRPC {
method: "read",
params: Some(OptionsOnly {
options: ReadConvParams { channel },
}),
};
println!("opts: {}", &serde_json::to_string(&input)?);
call_chat_api::<api::Thread>(&serde_json::to_vec(&input)?)
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(tag = "type")]
enum Notification {
#[serde(rename = "chat")]
Chat(api::MsgNotification),
#[serde(rename = "wallet")]
Wallet {
notification: stellar1::PaymentDetailsLocal,
},
}
pub fn listen() -> Result<(), ApiError> {
let (notif_stream, handler) = listen_chat_api::<Notification>()?;
let fut = notif_stream.for_each(|notif| {
println!("Got notif: {:?}", notif);
future::ready(())
});
block_on(fut);
handler.join().unwrap()
}
#[derive(Serialize, Debug)]
struct MessageOptions<'a> {
body: &'a str,
}
#[derive(Serialize, Debug)]
struct SendMessageOptions<'a> {
channel: &'a ChannelParams,
message: MessageOptions<'a>,
}
type SendTextRPC<'a> = APIRPC<OptionsOnly<SendMessageOptions<'a>>>;
pub fn send_msg<'a>(channel: &'a ChannelParams, msg: &'a str) -> Result<api::SendRes, ApiError> {
let options = SendMessageOptions {
channel,
message: MessageOptions { body: msg },
};
let input: SendTextRPC = APIRPC {
method: "send",
params: Some(OptionsOnly { options }),
};
call_chat_api::<api::SendRes>(&serde_json::to_vec(&input)?)
}