Small code cleanup in matrix.rs

This commit is contained in:
projectmoon 2021-02-08 20:14:11 +00:00
parent d42e075c5c
commit ef4f1ef02f
1 changed files with 22 additions and 24 deletions

View File

@ -1,40 +1,38 @@
use log::error; use log::error;
use matrix_sdk::events::{ use matrix_sdk::Error as MatrixError;
use matrix_sdk::{
events::{
room::message::{MessageEventContent::Notice, NoticeMessageEventContent}, room::message::{MessageEventContent::Notice, NoticeMessageEventContent},
AnyMessageEventContent::RoomMessage, AnyMessageEventContent::RoomMessage,
},
RoomMember,
}; };
use matrix_sdk::Error as MatrixError;
use matrix_sdk::{identifiers::RoomId, Client}; use matrix_sdk::{identifiers::RoomId, Client};
/// Extracts more detailed error messages out of a matrix SDK error. /// Extracts more detailed error messages out of a matrix SDK error.
fn extract_error_message(error: MatrixError) -> String { fn extract_error_message(error: MatrixError) -> String {
use matrix_sdk::{Error::Http, HttpError}; use matrix_sdk::{Error::Http, HttpError};
match error { if let Http(HttpError::FromHttpResponse(ruma_err)) = error {
Http(http_err) => match http_err { ruma_err.to_string()
HttpError::FromHttpResponse(ruma_err) => ruma_err.to_string(), } else {
_ => http_err.to_string(), error.to_string()
},
_ => error.to_string(),
} }
} }
/// Retrieve a list of users in a given room. /// Retrieve a list of users in a given room.
pub async fn get_users_in_room(client: &Client, room_id: &RoomId) -> Vec<String> { pub async fn get_users_in_room(client: &Client, room_id: &RoomId) -> Vec<String> {
if let Some(joined_room) = client.get_joined_room(room_id) { if let Some(joined_room) = client.get_joined_room(room_id) {
joined_room let members: Vec<RoomMember> = joined_room.joined_members().await.ok().unwrap_or_default();
.joined_members()
.await let to_username = |member: RoomMember| {
.ok()
.unwrap_or_default()
.into_iter()
.map(|member| {
format!( format!(
"@{}:{}", "@{}:{}",
member.user_id().localpart(), member.user_id().localpart(),
member.user_id().server_name() member.user_id().server_name()
) )
}) };
.collect()
members.into_iter().map(to_username).collect()
} else { } else {
vec![] vec![]
} }