Added support for resolving room aliases to room ids and persisting this lookup in the config file
This commit is contained in:
parent
4bd50c3806
commit
e900d9c1e6
4 changed files with 28 additions and 2 deletions
|
@ -11,6 +11,7 @@ serde = "1.0.144"
|
||||||
serde_yaml = "0.9.10"
|
serde_yaml = "0.9.10"
|
||||||
|
|
||||||
postbus = "0.2.0"
|
postbus = "0.2.0"
|
||||||
|
ruma = "0.6.4"
|
||||||
matrix-sdk = "0.5.0"
|
matrix-sdk = "0.5.0"
|
||||||
|
|
||||||
[target.x86_64-unknown-linux-musl.dependencies]
|
[target.x86_64-unknown-linux-musl.dependencies]
|
||||||
|
|
|
@ -23,4 +23,4 @@ mappings:
|
||||||
room_id: "!idofWARNINGroomyoucreatedbefore:example.com"
|
room_id: "!idofWARNINGroomyoucreatedbefore:example.com"
|
||||||
- to: "critical@mydomain.com"
|
- to: "critical@mydomain.com"
|
||||||
sender_mxid: "@firstbot:example.com"
|
sender_mxid: "@firstbot:example.com"
|
||||||
room'id: "!idofIMPORTANTroomyoucreatedbefore:example.com"
|
room_id: "#IMPORTANTroomyoucreatedbefore:example.com"
|
|
@ -24,6 +24,7 @@ pub struct Mapping {
|
||||||
pub to: Option<String>,
|
pub to: Option<String>,
|
||||||
pub mxid_sender: Option<String>,
|
pub mxid_sender: Option<String>,
|
||||||
pub room_id: String,
|
pub room_id: String,
|
||||||
|
pub room_alias: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_config(file_path: &str) -> Config {
|
pub fn load_config(file_path: &str) -> Config {
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
use matrix_sdk::{Client, ruma::{UserId, RoomId, events::{room::message::RoomMessageEventContent}}, Session, room::Room, config::SyncSettings};
|
use matrix_sdk::{Client, ruma::{UserId, RoomId, events::{room::message::RoomMessageEventContent}}, Session, room::Room, config::SyncSettings};
|
||||||
|
use ruma::OwnedRoomAliasId;
|
||||||
use postbus::command::Mailbox;
|
use postbus::command::Mailbox;
|
||||||
use crate::config::{Mapping, Config};
|
use crate::config::{Mapping, Config};
|
||||||
|
|
||||||
pub async fn get_clients(config: &mut Config) -> Vec<Client> {
|
pub async fn get_clients(config: &mut Config) -> Vec<Client> {
|
||||||
let mut clients: Vec<Client> = vec![];
|
let mut clients: Vec<Client> = vec![];
|
||||||
|
|
||||||
|
let mut room_aliases_resolved = false;
|
||||||
// Try to build a matrix_sdk::Client object for each configured client
|
// Try to build a matrix_sdk::Client object for each configured client
|
||||||
for conf in &mut config.clients {
|
for conf in &mut config.clients {
|
||||||
let user_id = match UserId::parse(&conf.mxid) {
|
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.access_token = Some(session.access_token);
|
||||||
conf.device_id = Some(session.device_id.to_string());
|
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());
|
clients.push(client.clone());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue