autojoin: filter room based on ID

This commit is contained in:
Antoine Martin 2021-02-02 08:25:01 +01:00
parent 57e783e8a3
commit 813ea9848a
3 changed files with 29 additions and 4 deletions

View file

@ -3,17 +3,19 @@ use std::time::Duration;
use matrix_sdk::{
self, async_trait,
events::{room::member::MemberEventContent, StrippedStateEvent},
identifiers::RoomId,
Client, EventEmitter, RoomState,
};
use tokio::time::sleep;
pub struct AutoJoinHandler {
client: Client,
room_id: RoomId,
}
impl AutoJoinHandler {
pub fn new(client: Client) -> Self {
Self { client }
pub fn new(client: Client, room_id: RoomId) -> Self {
Self { client, room_id }
}
}
@ -30,7 +32,23 @@ impl EventEmitter for AutoJoinHandler {
}
if let RoomState::Invited(room) = room {
// TODO: only join room if it's the room specified in the configuration
let room_id = room.room_id();
let room_name = room.display_name().await;
println!(
"Received invitation for room `{}`: `{}`",
room_id, room_name
);
if room_id != &self.room_id {
println!(
"Bot isn't authorized to join room `{}`, declining invitation",
room_id
);
// leaving a room is equivalent to rejecting the invitation, as per
// https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-rooms-roomid-leave
self.client.leave_room(room_id).await.unwrap();
return;
}
println!("Autojoining room {}", room.room_id());
let mut delay = 2;

View file

@ -32,7 +32,10 @@ impl BadNewsBot {
load_or_init_session(&self).await?;
self.client
.add_event_emitter(Box::new(AutoJoinHandler::new(self.client.clone())))
.add_event_emitter(Box::new(AutoJoinHandler::new(
self.client.clone(),
self.config.room_id.clone(),
)))
.await;
Ok(())

View file

@ -5,6 +5,7 @@ use std::{
};
use clap::Clap;
use matrix_sdk::identifiers::RoomId;
use serde::Deserialize;
use thiserror::Error;
use url::Url;
@ -41,6 +42,9 @@ pub struct Config {
password: String,
/// Path to a directory where the bot will store Matrix state and current session information.
state_dir: PathBuf,
/// ID of the Matrix room where the bot should post messages. The bot will only accept
/// invitations to this room.
room_id: RoomId,
}
#[tokio::main]