2021-06-02 21:09:58 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
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;
|
2023-04-13 19:04:48 +00:00
|
|
|
use matrix_sdk::ruma::events::room::message::{InReplyTo, RoomMessageEventContent, Relation};
|
|
|
|
use matrix_sdk::ruma::events::AnyMessageLikeEventContent;
|
|
|
|
use matrix_sdk::ruma::{RoomId, OwnedEventId, OwnedUserId};
|
2021-09-19 14:16:29 +00:00
|
|
|
use matrix_sdk::Client;
|
|
|
|
use matrix_sdk::Error as MatrixError;
|
2023-04-13 19:04:48 +00:00
|
|
|
use matrix_sdk::room::Joined;
|
2021-06-02 21:09:58 +00:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
use crate::{config::Config, error::BotError};
|
|
|
|
|
|
|
|
fn cache_dir() -> Result<PathBuf, BotError> {
|
|
|
|
let mut dir = dirs::cache_dir().ok_or(BotError::NoCacheDirectoryError)?;
|
|
|
|
dir.push("matrix-dicebot");
|
|
|
|
Ok(dir)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 21:09:58 +00:00
|
|
|
/// Creates the matrix client.
|
2023-04-13 19:04:48 +00:00
|
|
|
pub async fn create_client(config: &Config) -> Result<Client, BotError> {
|
2021-06-02 21:09:58 +00:00
|
|
|
let cache_dir = cache_dir()?;
|
|
|
|
let homeserver_url = Url::parse(&config.matrix_homeserver())?;
|
|
|
|
|
2023-04-13 19:04:48 +00:00
|
|
|
let client = Client::builder()
|
|
|
|
.sled_store(cache_dir, None)?
|
|
|
|
.homeserver_url(homeserver_url).build()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(client)
|
2021-06-02 21:09:58 +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,
|
2023-04-13 19:04:48 +00:00
|
|
|
user: &OwnedUserId,
|
2021-05-24 21:32:00 +00:00
|
|
|
) -> 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),
|
2023-04-13 19:04:48 +00:00
|
|
|
reply_to: Option<OwnedEventId>,
|
2021-02-09 22:22:09 +00:00
|
|
|
) {
|
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,
|
|
|
|
};
|
|
|
|
|
2023-04-13 19:04:48 +00:00
|
|
|
let mut content = RoomMessageEventContent::notice_html(plain.trim(), html);
|
2021-02-09 22:22:09 +00:00
|
|
|
|
|
|
|
content.relates_to = reply_to.map(|event_id| Relation::Reply {
|
2023-04-13 19:04:48 +00:00
|
|
|
in_reply_to: InReplyTo::new(event_id)
|
2021-02-09 22:22:09 +00:00
|
|
|
});
|
|
|
|
|
2023-04-13 19:04:48 +00:00
|
|
|
let content = AnyMessageLikeEventContent::RoomMessage(content);
|
2021-03-15 20:10:42 +00:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|