Compare commits
2 commits
88257caac7
...
b0483d90da
Author | SHA1 | Date | |
---|---|---|---|
Antoine Martin | b0483d90da | ||
Antoine Martin | 31a7bb029b |
459
Cargo.lock
generated
459
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -23,4 +23,4 @@ void = "1"
|
||||||
|
|
||||||
[dependencies.matrix-sdk]
|
[dependencies.matrix-sdk]
|
||||||
git = "https://github.com/matrix-org/matrix-rust-sdk"
|
git = "https://github.com/matrix-org/matrix-rust-sdk"
|
||||||
rev = "b66c666997cbcf32071d24ffe2484a215de8d0e4"
|
rev = "b5de20349945afcde13a78f023b906f421bb9764"
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
src = ./.;
|
src = ./.;
|
||||||
|
|
||||||
cargoSha256 = "sha256-nWEWOY1F0TsVW5DHm2p8eOABF97Wku7Grw0Vsd79syU=";
|
cargoSha256 = "sha256-dNf/FYhNRu85Q4ZinvFGcJmMRayVdTJ9j28fu9BIinY=";
|
||||||
|
|
||||||
meta = with pkgs.lib; {
|
meta = with pkgs.lib; {
|
||||||
description = "A Matrix bot, bringer of bad news";
|
description = "A Matrix bot, bringer of bad news";
|
||||||
|
|
|
@ -4,7 +4,8 @@ use matrix_sdk::{
|
||||||
self, async_trait,
|
self, async_trait,
|
||||||
events::{room::member::MemberEventContent, StrippedStateEvent},
|
events::{room::member::MemberEventContent, StrippedStateEvent},
|
||||||
identifiers::RoomId,
|
identifiers::RoomId,
|
||||||
Client, EventEmitter, RoomState,
|
room::Room,
|
||||||
|
Client, EventHandler,
|
||||||
};
|
};
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
@ -20,10 +21,10 @@ impl AutoJoinHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventEmitter for AutoJoinHandler {
|
impl EventHandler for AutoJoinHandler {
|
||||||
async fn on_stripped_state_member(
|
async fn on_stripped_state_member(
|
||||||
&self,
|
&self,
|
||||||
room: RoomState,
|
room: Room,
|
||||||
room_member: &StrippedStateEvent<MemberEventContent>,
|
room_member: &StrippedStateEvent<MemberEventContent>,
|
||||||
_: Option<MemberEventContent>,
|
_: Option<MemberEventContent>,
|
||||||
) {
|
) {
|
||||||
|
@ -31,9 +32,12 @@ impl EventEmitter for AutoJoinHandler {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let RoomState::Invited(room) = room {
|
if let Room::Invited(room) = room {
|
||||||
let room_id = room.room_id();
|
let room_id = room.room_id();
|
||||||
let room_name = room.display_name().await;
|
let room_name = room
|
||||||
|
.display_name()
|
||||||
|
.await
|
||||||
|
.expect("couldn't get joined room name!");
|
||||||
println!(
|
println!(
|
||||||
"Received invitation for room `{}`: `{}`",
|
"Received invitation for room `{}`: `{}`",
|
||||||
room_id, room_name
|
room_id, room_name
|
||||||
|
@ -44,16 +48,14 @@ impl EventEmitter for AutoJoinHandler {
|
||||||
"Bot isn't authorized to join room `{}`, declining invitation",
|
"Bot isn't authorized to join room `{}`, declining invitation",
|
||||||
room_id
|
room_id
|
||||||
);
|
);
|
||||||
// leaving a room is equivalent to rejecting the invitation, as per
|
room.reject_invitation().await.unwrap();
|
||||||
// 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Autojoining room {}", room.room_id());
|
println!("Autojoining room {}", room.room_id());
|
||||||
let mut delay = 2;
|
let mut delay = 2;
|
||||||
|
|
||||||
while let Err(err) = self.client.join_room_by_id(room.room_id()).await {
|
while let Err(err) = room.accept_invitation().await {
|
||||||
// retry autojoin due to synapse sending invites, before the
|
// retry autojoin due to synapse sending invites, before the
|
||||||
// invited user can join for more information see
|
// invited user can join for more information see
|
||||||
// https://github.com/matrix-org/synapse/issues/4345
|
// https://github.com/matrix-org/synapse/issues/4345
|
||||||
|
|
12
src/bot.rs
12
src/bot.rs
|
@ -4,10 +4,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
events::{
|
events::{room::message::MessageEventContent, AnyMessageEventContent},
|
||||||
room::message::{MessageEventContent, TextMessageEventContent},
|
|
||||||
AnyMessageEventContent,
|
|
||||||
},
|
|
||||||
Client, ClientConfig, Session, SyncSettings,
|
Client, ClientConfig, Session, SyncSettings,
|
||||||
};
|
};
|
||||||
use systemd::{journal, JournalRecord};
|
use systemd::{journal, JournalRecord};
|
||||||
|
@ -40,7 +37,7 @@ impl BadNewsBot {
|
||||||
load_or_init_session(&self).await?;
|
load_or_init_session(&self).await?;
|
||||||
|
|
||||||
self.client
|
self.client
|
||||||
.add_event_emitter(Box::new(AutoJoinHandler::new(
|
.set_event_handler(Box::new(AutoJoinHandler::new(
|
||||||
self.client.clone(),
|
self.client.clone(),
|
||||||
self.config.room_id.clone(),
|
self.config.room_id.clone(),
|
||||||
)))
|
)))
|
||||||
|
@ -121,9 +118,8 @@ impl BadNewsBot {
|
||||||
unit.strip_suffix(".service").unwrap_or(unit),
|
unit.strip_suffix(".service").unwrap_or(unit),
|
||||||
message,
|
message,
|
||||||
);
|
);
|
||||||
let content = AnyMessageEventContent::RoomMessage(MessageEventContent::Text(
|
let content =
|
||||||
TextMessageEventContent::plain(message),
|
AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain(message));
|
||||||
));
|
|
||||||
let room_id = self.config.room_id.clone();
|
let room_id = self.config.room_id.clone();
|
||||||
let client_clone = self.client.clone();
|
let client_clone = self.client.clone();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue