Added support for resolving room aliases to room ids and persisting this lookup in the config file

This commit is contained in:
Philip (a-0) 2022-09-01 19:17:23 +02:00
parent 4bd50c3806
commit e900d9c1e6
4 changed files with 28 additions and 2 deletions

View file

@ -11,7 +11,8 @@ serde = "1.0.144"
serde_yaml = "0.9.10"
postbus = "0.2.0"
matrix-sdk= "0.5.0"
ruma = "0.6.4"
matrix-sdk = "0.5.0"
[target.x86_64-unknown-linux-musl.dependencies]
openssl = { version = "0.10", features = ["vendored"] }

View file

@ -23,4 +23,4 @@ mappings:
room_id: "!idofWARNINGroomyoucreatedbefore:example.com"
- to: "critical@mydomain.com"
sender_mxid: "@firstbot:example.com"
room'id: "!idofIMPORTANTroomyoucreatedbefore:example.com"
room_id: "#IMPORTANTroomyoucreatedbefore:example.com"

View file

@ -24,6 +24,7 @@ pub struct Mapping {
pub to: Option<String>,
pub mxid_sender: Option<String>,
pub room_id: String,
pub room_alias: Option<String>,
}
pub fn load_config(file_path: &str) -> Config {

View file

@ -1,10 +1,12 @@
use matrix_sdk::{Client, ruma::{UserId, RoomId, events::{room::message::RoomMessageEventContent}}, Session, room::Room, config::SyncSettings};
use ruma::OwnedRoomAliasId;
use postbus::command::Mailbox;
use crate::config::{Mapping, Config};
pub async fn get_clients(config: &mut Config) -> Vec<Client> {
let mut clients: Vec<Client> = vec![];
let mut room_aliases_resolved = false;
// Try to build a matrix_sdk::Client object for each configured client
for conf in &mut config.clients {
let user_id = match UserId::parse(&conf.mxid) {
@ -44,6 +46,28 @@ pub async fn get_clients(config: &mut Config) -> Vec<Client> {
conf.access_token = Some(session.access_token);
conf.device_id = Some(session.device_id.to_string());
}
// If room aliases are supplied as room_id in the config file, resolve them and store the pairs properly
if !room_aliases_resolved && client.logged_in().await {
for mapping in &mut config.mappings {
if mapping.room_id.as_str().chars().nth(0).unwrap() == '#' || mapping.room_id.len() == 0 {
// TODO: Clean this up once matrix-rust-sdk 0.6.0 is available
let alias: OwnedRoomAliasId = match OwnedRoomAliasId::try_from(mapping.room_id.as_str()) {
Ok(a) => a,
Err(_e) => continue,
};
let resolve_request = matrix_sdk::ruma::api::client::alias::get_alias::v3::Request::new(&alias);
match client.send(resolve_request, None).await {
Ok(result) => {
let matrix_sdk::ruma::api::client::alias::get_alias::v3::Response{room_id, ..} = result;
mapping.room_alias = Some(mapping.room_id.clone());
mapping.room_id = room_id.to_string();
},
Err(_e) => (),
};
}let a = ruma::RoomAliasId::parse(&String::from("abc"));
}
room_aliases_resolved = true;
}
clients.push(client.clone());
}