2021-05-24 21:32:00 +00:00
|
|
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
2021-01-31 14:06:25 +00:00
|
|
|
use log::error;
|
2021-05-24 22:10:41 +00:00
|
|
|
use matrix_sdk::{events::room::message::NoticeMessageEventContent, room::Joined};
|
2021-02-09 22:22:09 +00:00
|
|
|
use matrix_sdk::{
|
|
|
|
events::room::message::{InReplyTo, Relation},
|
2021-03-15 20:10:42 +00:00
|
|
|
events::room::message::{MessageEventContent, MessageType},
|
|
|
|
events::AnyMessageEventContent,
|
2021-02-09 22:22:09 +00:00
|
|
|
identifiers::EventId,
|
|
|
|
Error as MatrixError,
|
|
|
|
};
|
2021-05-24 21:32:00 +00:00
|
|
|
use matrix_sdk::{identifiers::RoomId, identifiers::UserId, Client};
|
2020-11-22 21:30:24 +00:00
|
|
|
|
2021-01-31 14:06:25 +00:00
|
|
|
/// Extracts more detailed error messages out of a matrix SDK error.
|
|
|
|
fn extract_error_message(error: MatrixError) -> String {
|
2021-02-07 21:58:22 +00:00
|
|
|
use matrix_sdk::{Error::Http, HttpError};
|
2021-05-13 19:48:29 +00:00
|
|
|
if let Http(HttpError::Api(ruma_err)) = error {
|
2021-02-08 20:14:11 +00:00
|
|
|
ruma_err.to_string()
|
|
|
|
} else {
|
|
|
|
error.to_string()
|
2021-01-31 14:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 21:30:24 +00:00
|
|
|
/// Retrieve a list of users in a given room.
|
2021-05-16 21:39:19 +00:00
|
|
|
pub async fn get_users_in_room(
|
|
|
|
client: &Client,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> Result<Vec<String>, MatrixError> {
|
2021-01-29 22:35:07 +00:00
|
|
|
if let Some(joined_room) = client.get_joined_room(room_id) {
|
2021-05-16 21:39:19 +00:00
|
|
|
let members = joined_room.joined_members().await?;
|
2021-02-08 20:14:11 +00:00
|
|
|
|
2021-05-16 21:39:19 +00:00
|
|
|
Ok(members
|
2021-02-10 20:18:47 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|member| member.user_id().to_string())
|
2021-05-16 21:39:19 +00:00
|
|
|
.collect())
|
2020-11-22 21:30:24 +00:00
|
|
|
} else {
|
2021-05-16 21:39:19 +00:00
|
|
|
Ok(vec![])
|
2020-11-22 21:30:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-31 14:06:25 +00:00
|
|
|
|
2021-05-24 21:32:00 +00:00
|
|
|
pub async fn get_rooms_for_user(
|
|
|
|
client: &Client,
|
|
|
|
user: &UserId,
|
|
|
|
) -> Result<Vec<Joined>, MatrixError> {
|
|
|
|
// Carries errors through, in case we cannot load joined user IDs
|
|
|
|
// from the room for some reason.
|
|
|
|
let user_is_in_room = |room: Joined| async move {
|
|
|
|
match room.joined_user_ids().await {
|
|
|
|
Ok(users) => match users.contains(user) {
|
|
|
|
true => Some(Ok(room)),
|
|
|
|
false => None,
|
|
|
|
},
|
|
|
|
Err(e) => Some(Err(e)),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let rooms_for_user: Vec<Joined> = stream::iter(client.joined_rooms())
|
|
|
|
.filter_map(user_is_in_room)
|
|
|
|
.try_collect()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(rooms_for_user)
|
|
|
|
}
|
|
|
|
|
2021-05-26 22:20:53 +00:00
|
|
|
/// Send a message. The message is a tuple of HTML and plain text
|
|
|
|
/// responses.
|
2021-02-09 22:22:09 +00:00
|
|
|
pub async fn send_message(
|
|
|
|
client: &Client,
|
|
|
|
room_id: &RoomId,
|
2021-05-26 22:20:53 +00:00
|
|
|
message: (&str, &str),
|
2021-02-09 22:22:09 +00:00
|
|
|
reply_to: Option<EventId>,
|
|
|
|
) {
|
2021-05-26 22:20:53 +00:00
|
|
|
let (html, plain) = message;
|
2021-03-15 20:10:42 +00:00
|
|
|
let room = match client.get_joined_room(room_id) {
|
|
|
|
Some(room) => room,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut content = MessageEventContent::new(MessageType::Notice(
|
2021-05-26 22:20:53 +00:00
|
|
|
NoticeMessageEventContent::html(plain.trim(), html),
|
2021-03-15 20:10:42 +00:00
|
|
|
));
|
2021-02-09 22:22:09 +00:00
|
|
|
|
|
|
|
content.relates_to = reply_to.map(|event_id| Relation::Reply {
|
2021-05-17 23:12:27 +00:00
|
|
|
in_reply_to: InReplyTo::new(event_id),
|
2021-02-09 22:22:09 +00:00
|
|
|
});
|
|
|
|
|
2021-03-15 20:10:42 +00:00
|
|
|
let content = AnyMessageEventContent::RoomMessage(content);
|
|
|
|
|
|
|
|
let result = room.send(content, None).await;
|
2021-01-31 14:06:25 +00:00
|
|
|
|
|
|
|
if let Err(e) = result {
|
2021-05-26 22:20:53 +00:00
|
|
|
let html = extract_error_message(e);
|
|
|
|
error!("Error sending html: {}", html);
|
2021-01-31 14:06:25 +00:00
|
|
|
};
|
|
|
|
}
|