-
Notifications
You must be signed in to change notification settings - Fork 5
/
r0.rs
673 lines (576 loc) · 29.2 KB
/
r0.rs
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
use std::collections::HashMap;
use std::convert::TryFrom;
use std::io::Read;
use pulldown_cmark::{html, Options, Parser};
use reqwest::{Method, StatusCode};
use reqwest::header::{ContentType, Headers};
use ruma_client_api::Endpoint;
use ruma_client_api::r0::alias::get_alias::{self, Endpoint as GetAliasEndpoint};
use ruma_client_api::r0::alias::delete_alias::Endpoint as DeleteAliasEndpoint;
use ruma_client_api::r0::account::register::{self, Endpoint as RegisterEndpoint};
use ruma_client_api::r0::media::create_content::{self, Endpoint as CreateContentEndpoint};
use ruma_client_api::r0::media::get_content::{self, Endpoint as GetContentEndpoint};
use ruma_client_api::r0::membership::forget_room::{self, Endpoint as ForgetRoomEndpoint};
use ruma_client_api::r0::membership::invite_user::{self, Endpoint as InviteUserEndpoint};
use ruma_client_api::r0::membership::join_room_by_id::{self, Endpoint as JoinRoomByIdEndpoint};
use ruma_client_api::r0::membership::leave_room::{self, Endpoint as LeaveRoomEndpoint};
use ruma_client_api::r0::profile::get_display_name::{self, Endpoint as GetDisplayNameEndpoint};
use ruma_client_api::r0::profile::set_display_name::{self, Endpoint as SetDisplayNameEndpoint};
use ruma_client_api::r0::room::create_room::{self, Endpoint as CreateRoomEndpoint, RoomPreset};
use ruma_client_api::r0::send::send_message_event::{self, Endpoint as SendMessageEventEndpoint};
use ruma_client_api::r0::send::send_state_event_for_empty_key::{self, Endpoint as SendStateEventForEmptyKeyEndpoint};
use ruma_client_api::r0::sync::sync_events::Endpoint as SyncEventsEndpoint;
use ruma_client_api::r0::sync::get_member_events::{self, Endpoint as GetMemberEventsEndpoint};
use ruma_client_api::r0::sync::get_state_events::{self, Endpoint as GetStateEvents};
use ruma_client_api::r0::sync::get_state_events_for_empty_key::{self, Endpoint as GetStateEventsForEmptyKeyEndpoint};
use ruma_events::EventType;
use ruma_events::collections::all::Event;
use ruma_events::room::member::MemberEvent;
use ruma_events::room::message::MessageType;
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId};
use serde_json::{self, Map, Value};
use slog::Logger;
use url;
use api::{RequestData, RestApi};
use config::Config;
use errors::*;
#[derive(Clone)]
/// Rocket.Chat REST API v0
pub struct MatrixApi {
/// URL to call the API
pub base_url: String,
/// Access token for authentication
pub access_token: String,
/// Logger passed to the Matrix API
logger: Logger,
}
impl MatrixApi {
/// Create a new MatrixApi.
pub fn new(config: &Config, logger: Logger) -> MatrixApi {
MatrixApi {
base_url: config.hs_url.to_string(),
access_token: config.as_token.to_string(),
logger: logger,
}
}
fn params_hash(&self) -> HashMap<&str, &str> {
let mut params: HashMap<&str, &str> = HashMap::new();
params.insert("access_token", &self.access_token);
params
}
}
impl super::MatrixApi for MatrixApi {
fn create_room(
&self,
room_name: Option<String>,
room_alias_name: Option<String>,
room_creator_id: &UserId,
) -> Result<RoomId> {
let endpoint = self.base_url.clone() + &CreateRoomEndpoint::request_path(());
let body_params = create_room::BodyParams {
creation_content: None,
invite: vec![],
name: room_name,
preset: Some(RoomPreset::PrivateChat),
room_alias_name: room_alias_name,
topic: None,
visibility: Some("private".to_string()),
};
let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("create_room"))?;
let user_id = room_creator_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let (body, status_code) = RestApi::call_matrix(&CreateRoomEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
let create_room_response: create_room::Response = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!("Could not deserialize response from Matrix create_room API endpoint: `{}`", body))
})?;
debug!(self.logger, "Successfully created room with ID {}", create_room_response.room_id);
Ok(create_room_response.room_id)
}
fn delete_room_alias(&self, matrix_room_alias_id: RoomAliasId) -> Result<()> {
// the ruma client api path params cannot be used here, because they are not url encoded
let encoded_room_alias =
url::form_urlencoded::byte_serialize(matrix_room_alias_id.to_string().as_bytes()).collect::<String>();
let endpoint = self.base_url.clone() + &format!("/_matrix/client/r0/directory/room/{}", &encoded_room_alias);
let params = self.params_hash();
let (body, status_code) = RestApi::call_matrix(&DeleteAliasEndpoint::method(), &endpoint, "{}", ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn forget_room(&self, room_id: RoomId, user_id: UserId) -> Result<()> {
let path_params = forget_room::PathParams {
room_id: room_id,
};
let endpoint = self.base_url.clone() + &ForgetRoomEndpoint::request_path(path_params);
let user_id = user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let (body, status_code) = RestApi::call_matrix(&ForgetRoomEndpoint::method(), &endpoint, "{}", ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn get_content(&self, server_name: String, media_id: String) -> Result<Vec<u8>> {
let path_params = get_content::PathParams {
server_name: server_name,
media_id: media_id,
};
let endpoint = self.base_url.clone() + &GetContentEndpoint::request_path(path_params);
let params = self.params_hash();
let mut resp = RestApi::get_matrix_file(&GetContentEndpoint::method(), &endpoint, "", ¶ms)?;
if !resp.status().is_success() {
let mut body = String::new();
resp.read_to_string(&mut body).chain_err(|| ErrorKind::ApiCallFailed(endpoint.clone()))?;
return Err(build_error(&endpoint, &body, &resp.status()));
}
let mut buffer = Vec::new();
resp.read_to_end(&mut buffer).chain_err(|| ErrorKind::InternalServerError)?;
Ok(buffer)
}
fn get_display_name(&self, user_id: UserId) -> Result<Option<String>> {
let path_params = get_display_name::PathParams {
user_id: user_id,
};
let endpoint = self.base_url.clone() + &GetDisplayNameEndpoint::request_path(path_params);
let params = self.params_hash();
let (body, status_code) = RestApi::call_matrix(&GetDisplayNameEndpoint::method(), &endpoint, "", ¶ms)?;
if status_code == StatusCode::NotFound {
return Ok(None);
}
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
let get_display_name_response: get_display_name::Response = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!(
"Could not deserialize response from Matrix get_display_name API endpoint: `{}`",
body
))
})?;
Ok(Some(get_display_name_response.displayname.unwrap_or_default()))
}
fn get_joined_rooms(&self, user_id: UserId) -> Result<Vec<RoomId>> {
let endpoint = self.base_url.clone() + &SyncEventsEndpoint::request_path(());
let user_id = user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let (body, status_code) = RestApi::call_matrix(&SyncEventsEndpoint::method(), &endpoint, "", ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
let sync_response: Value = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!("Could not deserialize response from Matrix sync_events API endpoint: `{}`", body))
})?;
let empty_rooms = Value::Object(Map::new());
let raw_rooms = sync_response.get("rooms").unwrap_or(&empty_rooms).get("join").unwrap_or(&empty_rooms);
let rooms: HashMap<RoomId, Value> = serde_json::from_value(raw_rooms.clone()).unwrap_or_default();
Ok(rooms.keys().map(|k| k.to_owned()).collect())
}
fn get_room_alias(&self, matrix_room_alias_id: RoomAliasId) -> Result<Option<RoomId>> {
// the ruma client api path params cannot be used here, because they are not url encoded
let encoded_room_alias =
url::form_urlencoded::byte_serialize(matrix_room_alias_id.to_string().as_bytes()).collect::<String>();
let endpoint = self.base_url.clone() + &format!("/_matrix/client/r0/directory/room/{}", &encoded_room_alias);
let params = self.params_hash();
let (body, status_code) = RestApi::call_matrix(&GetAliasEndpoint::method(), &endpoint, "{}", ¶ms)?;
if status_code == StatusCode::NotFound {
return Ok(None);
}
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
let get_alias_response: get_alias::Response = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!("Could not deserialize response from Matrix get_alias API endpoint: `{}`", body))
})?;
Ok(Some(get_alias_response.room_id.clone()))
}
fn get_room_aliases(&self, room_id: RoomId, user_id: UserId) -> Result<Vec<RoomAliasId>> {
let path_params = get_state_events::PathParams {
room_id: room_id,
};
let endpoint = self.base_url.clone() + &GetStateEvents::request_path(path_params);
let user_id = user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let (body, status_code) = RestApi::call_matrix(&GetStateEvents::method(), &endpoint, "", ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
let state_events: Vec<Event> = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!(
"Could not deserialize response from Matrix get_state_events API endpoint: `{}`",
body
))
})?;
let mut aliases = Vec::new();
for event in state_events {
if let Event::RoomAliases(mut aliases_event) = event {
aliases.append(&mut aliases_event.content.aliases);
}
}
Ok(aliases)
}
fn get_room_canonical_alias(&self, room_id: RoomId) -> Result<Option<RoomAliasId>> {
let path_params = get_state_events_for_empty_key::PathParams {
room_id: room_id,
event_type: EventType::RoomCanonicalAlias.to_string(),
};
let endpoint = self.base_url.clone() + &GetStateEventsForEmptyKeyEndpoint::request_path(path_params);
let params = self.params_hash();
let (body, status_code) = RestApi::call_matrix(&GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?;
if status_code == StatusCode::NotFound {
return Ok(None);
}
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
let room_canonical_alias_response: Value = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!(
"Could not deserialize response from Matrix get_state_events_for_empty_key API endpoint: `{}`",
body
))
})?;
let alias = room_canonical_alias_response["alias"].to_string().replace("\"", "");
if alias.is_empty() {
return Ok(None);
}
let room_canonical_alias = RoomAliasId::try_from(&alias).chain_err(|| ErrorKind::InvalidRoomAliasId(alias))?;
Ok(Some(room_canonical_alias))
}
fn get_room_creator(&self, room_id: RoomId) -> Result<UserId> {
let path_params = get_state_events_for_empty_key::PathParams {
room_id: room_id,
event_type: EventType::RoomCreate.to_string(),
};
let endpoint = self.base_url.clone() + &GetStateEventsForEmptyKeyEndpoint::request_path(path_params);
let params = self.params_hash();
let (body, status_code) = RestApi::call_matrix(&GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
let room_create: Value = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!(
"Could not deserialize response from Matrix get_state_events_for_empty_key API endpoint: `{}`",
body
))
})?;
let room_creator = room_create["creator"].to_string().replace("\"", "");
let user_id = UserId::try_from(&room_creator).chain_err(|| ErrorKind::InvalidUserId(room_creator))?;
Ok(user_id)
}
fn get_room_members(&self, room_id: RoomId, sender_id: Option<UserId>) -> Result<Vec<MemberEvent>> {
let path_params = get_member_events::PathParams {
room_id: room_id.clone(),
};
let endpoint = self.base_url.clone() + &GetMemberEventsEndpoint::request_path(path_params);
let user_id;
let mut params = self.params_hash();
if let Some(id) = sender_id {
user_id = id.to_string();
params.insert("user_id", &user_id);
}
let (body, status_code) = RestApi::call_matrix(&GetMemberEventsEndpoint::method(), &endpoint, "{}", ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
debug!(self.logger, "List of room members for room {} successfully received", room_id);
let room_member_events: get_member_events::Response = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!("Could not deserialize response from Matrix members API endpoint: `{}`", body))
})?;
Ok(room_member_events.chunk)
}
fn get_room_topic(&self, room_id: RoomId) -> Result<Option<String>> {
let path_params = get_state_events_for_empty_key::PathParams {
room_id: room_id,
event_type: EventType::RoomTopic.to_string(),
};
let endpoint = self.base_url.clone() + &GetStateEventsForEmptyKeyEndpoint::request_path(path_params);
let params = self.params_hash();
let (body, status_code) = RestApi::call_matrix(&GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?;
if status_code == StatusCode::NotFound {
return Ok(None);
}
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
let room_topic_response: Value = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!(
"Could not deserialize response from Matrix get_state_events_for_empty_key API endpoint: `{}`",
body
))
})?;
Ok(Some(room_topic_response["topic"].to_string().replace("\"", "")))
}
fn invite(&self, room_id: RoomId, receiver_user_id: UserId, sender_user_id: UserId) -> Result<()> {
let path_params = invite_user::PathParams {
room_id: room_id.clone(),
};
let endpoint = self.base_url.clone() + &InviteUserEndpoint::request_path(path_params);
let user_id = sender_user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let body_params = invite_user::BodyParams {
user_id: receiver_user_id.clone(),
};
let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("invite"))?;
let (body, status_code) = RestApi::call_matrix(&InviteUserEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
debug!(self.logger, "User {} successfully invited into room {} by {}", receiver_user_id, room_id, sender_user_id);
Ok(())
}
fn is_room_accessible_by_bot(&self, room_id: RoomId) -> Result<bool> {
let path_params = get_state_events_for_empty_key::PathParams {
room_id: room_id,
event_type: EventType::RoomCreate.to_string(),
};
let endpoint = self.base_url.clone() + &GetStateEventsForEmptyKeyEndpoint::request_path(path_params);
let params = self.params_hash();
let (_, status_code) = RestApi::call_matrix(&GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?;
Ok(status_code != StatusCode::Forbidden)
}
fn join(&self, room_id: RoomId, user_id: UserId) -> Result<()> {
let path_params = join_room_by_id::PathParams {
room_id: room_id.clone(),
};
let endpoint = self.base_url.clone() + &JoinRoomByIdEndpoint::request_path(path_params);
let user_id = user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let (body, status_code) = RestApi::call_matrix(&JoinRoomByIdEndpoint::method(), &endpoint, "{}", ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
debug!(self.logger, "User {} successfully joined room {}", user_id, room_id);
Ok(())
}
fn leave_room(&self, room_id: RoomId, user_id: UserId) -> Result<()> {
let path_params = leave_room::PathParams {
room_id: room_id,
};
let endpoint = self.base_url.clone() + &LeaveRoomEndpoint::request_path(path_params);
let user_id = user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let (body, status_code) = RestApi::call_matrix(&LeaveRoomEndpoint::method(), &endpoint, "{}", ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn put_canonical_room_alias(&self, room_id: RoomId, matrix_room_alias_id: Option<RoomAliasId>) -> Result<()> {
let path_params = send_state_event_for_empty_key::PathParams {
room_id: room_id,
event_type: EventType::RoomCanonicalAlias,
};
let endpoint = self.base_url.clone() + &SendStateEventForEmptyKeyEndpoint::request_path(path_params);
let room_alias = match matrix_room_alias_id {
Some(matrix_room_alias_id) => matrix_room_alias_id.to_string(),
None => String::new(),
};
let params = self.params_hash();
let mut body_params = serde_json::Map::new();
body_params.insert("alias".to_string(), json!(room_alias));
let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("canonical room alias"))?;
let (body, status_code) =
RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn register(&self, user_id_local_part: String) -> Result<()> {
let endpoint = self.base_url.clone() + &RegisterEndpoint::request_path(());
let params = self.params_hash();
let body_params = register::BodyParams {
bind_email: None,
password: None,
username: Some(user_id_local_part.to_lowercase()),
device_id: None,
initial_device_display_name: None,
auth: None,
};
let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("account"))?;
let (body, status_code) = RestApi::call_matrix(&RegisterEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn send_text_message_event(&self, room_id: RoomId, user_id: UserId, body: String) -> Result<()> {
let formatted_body = render_markdown(&body);
let mut message = Map::new();
message.insert("body".to_string(), json!(body));
message.insert("formatted_body".to_string(), json!(formatted_body));
message.insert("msgtype".to_string(), json!(MessageType::Text));
message.insert("format".to_string(), json!("org.matrix.custom.html"));
let payload = serde_json::to_string(&message).chain_err(|| body_params_error!("send text message"))?;
let txn_id = EventId::new(&self.base_url).chain_err(|| ErrorKind::EventIdGenerationFailed)?;
let path_params = send_message_event::PathParams {
room_id: room_id.clone(),
event_type: EventType::RoomMessage,
txn_id: txn_id.to_string(),
};
let endpoint = self.base_url.clone() + &SendMessageEventEndpoint::request_path(path_params);
let user_id = user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let (body, status_code) = RestApi::call_matrix(&SendMessageEventEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
debug!(self.logger, "User {} successfully sent a text message to room {}", user_id, room_id);
Ok(())
}
fn send_image_message_event(&self, room_id: RoomId, user_id: UserId, body: String, url: String) -> Result<()> {
let mut message = Map::new();
message.insert("body".to_string(), json!(body));
message.insert("msgtype".to_string(), json!(MessageType::Image));
message.insert("url".to_string(), json!(url));
let payload = serde_json::to_string(&message).chain_err(|| body_params_error!("send image message"))?;
let txn_id = EventId::new(&self.base_url).chain_err(|| ErrorKind::EventIdGenerationFailed)?;
let path_params = send_message_event::PathParams {
room_id: room_id.clone(),
event_type: EventType::RoomMessage,
txn_id: txn_id.to_string(),
};
let endpoint = self.base_url.clone() + &SendMessageEventEndpoint::request_path(path_params);
let user_id = user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let (body, status_code) = RestApi::call_matrix(&SendMessageEventEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
debug!(self.logger, "User {} successfully sent an image message to room {}", user_id, room_id);
Ok(())
}
fn set_default_powerlevels(&self, room_id: RoomId, room_creator_user_id: UserId) -> Result<()> {
let path_params = send_state_event_for_empty_key::PathParams {
room_id: room_id,
event_type: EventType::RoomPowerLevels,
};
let endpoint = self.base_url.clone() + &SendStateEventForEmptyKeyEndpoint::request_path(path_params);
let user_id = room_creator_user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let mut body_params = serde_json::Map::new();
let mut users = serde_json::Map::new();
users.insert(room_creator_user_id.to_string(), json!(100));
body_params.insert("invite".to_string(), json!(50));
body_params.insert("kick".to_string(), json!(50));
body_params.insert("ban".to_string(), json!(50));
body_params.insert("redact".to_string(), json!(50));
body_params.insert("users".to_string(), json!(users));
body_params.insert("events".to_string(), json!(serde_json::Map::new()));
let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("power levels"))?;
let (body, status_code) =
RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn set_display_name(&self, user_id: UserId, name: String) -> Result<()> {
let path_params = set_display_name::PathParams {
user_id: user_id.clone(),
};
let endpoint = self.base_url.clone() + &SetDisplayNameEndpoint::request_path(path_params);
let user_id = user_id.to_string();
let mut params = self.params_hash();
params.insert("user_id", &user_id);
let body_params = set_display_name::BodyParams {
displayname: Some(name),
};
let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("set display name"))?;
let (body, status_code) = RestApi::call_matrix(&SetDisplayNameEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn set_room_name(&self, room_id: RoomId, name: String) -> Result<()> {
let path_params = send_state_event_for_empty_key::PathParams {
room_id: room_id,
event_type: EventType::RoomName,
};
let endpoint = self.base_url.clone() + &SendStateEventForEmptyKeyEndpoint::request_path(path_params);
let params = self.params_hash();
let mut body_params = serde_json::Map::new();
body_params.insert("name".to_string(), Value::String(name));
let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("room name"))?;
let (body, status_code) =
RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn set_room_topic(&self, room_id: RoomId, topic: String) -> Result<()> {
let path_params = send_state_event_for_empty_key::PathParams {
room_id: room_id,
event_type: EventType::RoomTopic,
};
let endpoint = self.base_url.clone() + &SendStateEventForEmptyKeyEndpoint::request_path(path_params);
let params = self.params_hash();
let mut body_params = serde_json::Map::new();
body_params.insert("topic".to_string(), Value::String(topic));
let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("room topic"))?;
let (body, status_code) =
RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
Ok(())
}
fn upload(&self, data: Vec<u8>, content_type: ContentType) -> Result<String> {
let endpoint = self.base_url.clone() + &CreateContentEndpoint::request_path(());
let params = self.params_hash();
let mut headers = Headers::new();
headers.set(content_type);
let (body, status_code) = RestApi::call(&Method::Post, &endpoint, RequestData::Body(data), ¶ms, Some(headers))?;
if !status_code.is_success() {
return Err(build_error(&endpoint, &body, &status_code));
}
//TODO: Read response and return image url
let create_content_response: create_content::Response = serde_json::from_str(&body).chain_err(|| {
ErrorKind::InvalidJSON(format!(
"Could not deserialize response from Matrix create_content API endpoint: `{}`",
body
))
})?;
Ok(create_content_response.content_uri.clone())
}
}
fn build_error(endpoint: &str, body: &str, status_code: &StatusCode) -> Error {
if status_code == &StatusCode::NotFound {
return Error::from(ErrorKind::MatrixError("Not found".to_string()));
}
let json_error_msg = format!(
"Could not deserialize error from Matrix API endpoint {} with status code {}: `{}`",
endpoint, status_code, body
);
let json_error = ErrorKind::InvalidJSON(json_error_msg);
let matrix_error_resp: MatrixErrorResponse = match serde_json::from_str(body).chain_err(|| json_error).map_err(Error::from)
{
Ok(matrix_error_resp) => matrix_error_resp,
Err(err) => {
return err;
}
};
Error::from(ErrorKind::MatrixError(matrix_error_resp.error))
}
fn render_markdown(input: &str) -> String {
// The html will not have the same length as the msg, but it's a good starting point
let mut output = String::with_capacity(input.len());
let opts = Options::empty();
let parser = Parser::new_ext(input, opts);
html::push_html(&mut output, parser);
output
}