Hack in autojoin invite example

This commit is contained in:
0xf8 2023-04-16 22:01:36 -04:00
parent e614407006
commit 2928e21873
Signed by: 0xf8
GPG Key ID: 446580D758689584

View File

@ -1,13 +1,18 @@
use matrix_sdk::{ use matrix_sdk::{
room::Room, room::Room,
ruma::{ ruma::{
events::room::message::{ events::room::{
MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEvent, message::{
MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEvent,
},
member::StrippedRoomMemberEvent
}, },
OwnedRoomId, OwnedRoomId,
}, },
Client,
Error, LoopCtrl, Error, LoopCtrl,
}; };
use tokio::time::{sleep, Duration};
use std::path::PathBuf; use std::path::PathBuf;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@ -24,6 +29,30 @@ static ROOMS_CONFIG_FILE: Lazy<PathBuf> = Lazy::new(|| DATA_DIR.join("rooms_conf
static CONFIG: Lazy<config::Config> = Lazy::new(|| config::Config::load()); static CONFIG: Lazy<config::Config> = Lazy::new(|| config::Config::load());
// Autojoin rooms the bot is invited to
async fn on_stripped_state_member(room_member: StrippedRoomMemberEvent, client: Client, room: Room) {
if room_member.state_key != client.user_id().unwrap() {
return;
}
if let Room::Invited(room) = room {
tokio::spawn(async move {
let mut delay = 2;
while let Err(err) = room.accept_invitation().await {
eprintln!("Warn: {} ({err:?}), retrying in {delay}s", room.room_id());
sleep(Duration::from_secs(delay)).await;
delay *= 2;
if delay > 3600 {
eprintln!("Warn: {} ({err:?})", room.room_id());
break;
}
}
});
}
}
async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) -> anyhow::Result<()> { async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) -> anyhow::Result<()> {
if let Room::Joined(room) = room { if let Room::Joined(room) = room {
@ -187,6 +216,7 @@ async fn main() -> anyhow::Result<()> {
}; };
client.add_event_handler(on_room_message); client.add_event_handler(on_room_message);
client.add_event_handler(on_stripped_state_member);
client client
.sync_with_result_callback(sync_settings, |sync_result| async move { .sync_with_result_callback(sync_settings, |sync_result| async move {