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
use std::time::Duration;
use assign::assign;
use async_stream::try_stream;
use futures_core::stream::Stream;
use ruma_client_api::r0::{
account::register::{self, RegistrationKind},
session::login::{self, LoginInfo},
sync::sync_events,
uiaa::UserIdentifier,
};
use ruma_common::presence::PresenceState;
use ruma_identifiers::DeviceId;
use super::{Client, Error, HttpClient};
impl<C: HttpClient> Client<C> {
pub async fn log_in(
&self,
user: &str,
password: &str,
device_id: Option<&DeviceId>,
initial_device_display_name: Option<&str>,
) -> Result<login::Response, Error<C::Error, ruma_client_api::Error>> {
let response = self
.send_request(assign!(
login::Request::new(
LoginInfo::Password { identifier: UserIdentifier::MatrixId(user), password }
), {
device_id,
initial_device_display_name,
}
))
.await?;
*self.0.access_token.lock().unwrap() = Some(response.access_token.clone());
Ok(response)
}
pub async fn register_guest(
&self,
) -> Result<register::Response, Error<C::Error, ruma_client_api::r0::uiaa::UiaaResponse>> {
let response = self
.send_request(assign!(register::Request::new(), { kind: RegistrationKind::Guest }))
.await?;
*self.0.access_token.lock().unwrap() = response.access_token.clone();
Ok(response)
}
pub async fn register_user(
&self,
username: Option<&str>,
password: &str,
) -> Result<register::Response, Error<C::Error, ruma_client_api::r0::uiaa::UiaaResponse>> {
let response = self
.send_request(assign!(register::Request::new(), { username, password: Some(password) }))
.await?;
*self.0.access_token.lock().unwrap() = response.access_token.clone();
Ok(response)
}
pub fn sync<'a>(
&'a self,
filter: Option<&'a sync_events::Filter<'a>>,
mut since: String,
set_presence: &'a PresenceState,
timeout: Option<Duration>,
) -> impl Stream<Item = Result<sync_events::Response, Error<C::Error, ruma_client_api::Error>>> + 'a
{
try_stream! {
loop {
let response = self
.send_request(assign!(sync_events::Request::new(), {
filter,
since: Some(&since),
set_presence,
timeout,
}))
.await?;
since = response.next_batch.clone();
yield response;
}
}
}
}