Compare commits
No commits in common. "b0483d90dafd01744e7886663386822e39a17db9" and "88257caac73d97520a97ed9560ef075c1eb43c63" have entirely different histories.
b0483d90da
...
88257caac7
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 = "b5de20349945afcde13a78f023b906f421bb9764"
|
rev = "b66c666997cbcf32071d24ffe2484a215de8d0e4"
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
src = ./.;
|
src = ./.;
|
||||||
|
|
||||||
cargoSha256 = "sha256-dNf/FYhNRu85Q4ZinvFGcJmMRayVdTJ9j28fu9BIinY=";
|
cargoSha256 = "sha256-nWEWOY1F0TsVW5DHm2p8eOABF97Wku7Grw0Vsd79syU=";
|
||||||
|
|
||||||
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,8 +4,7 @@ use matrix_sdk::{
|
||||||
self, async_trait,
|
self, async_trait,
|
||||||
events::{room::member::MemberEventContent, StrippedStateEvent},
|
events::{room::member::MemberEventContent, StrippedStateEvent},
|
||||||
identifiers::RoomId,
|
identifiers::RoomId,
|
||||||
room::Room,
|
Client, EventEmitter, RoomState,
|
||||||
Client, EventHandler,
|
|
||||||
};
|
};
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
@ -21,10 +20,10 @@ impl AutoJoinHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for AutoJoinHandler {
|
impl EventEmitter for AutoJoinHandler {
|
||||||
async fn on_stripped_state_member(
|
async fn on_stripped_state_member(
|
||||||
&self,
|
&self,
|
||||||
room: Room,
|
room: RoomState,
|
||||||
room_member: &StrippedStateEvent<MemberEventContent>,
|
room_member: &StrippedStateEvent<MemberEventContent>,
|
||||||
_: Option<MemberEventContent>,
|
_: Option<MemberEventContent>,
|
||||||
) {
|
) {
|
||||||
|
@ -32,12 +31,9 @@ impl EventHandler for AutoJoinHandler {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Room::Invited(room) = room {
|
if let RoomState::Invited(room) = room {
|
||||||
let room_id = room.room_id();
|
let room_id = room.room_id();
|
||||||
let room_name = room
|
let room_name = room.display_name().await;
|
||||||
.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
|
||||||
|
@ -48,14 +44,16 @@ impl EventHandler for AutoJoinHandler {
|
||||||
"Bot isn't authorized to join room `{}`, declining invitation",
|
"Bot isn't authorized to join room `{}`, declining invitation",
|
||||||
room_id
|
room_id
|
||||||
);
|
);
|
||||||
room.reject_invitation().await.unwrap();
|
// 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;
|
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) = room.accept_invitation().await {
|
while let Err(err) = self.client.join_room_by_id(room.room_id()).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,7 +4,10 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
events::{room::message::MessageEventContent, AnyMessageEventContent},
|
events::{
|
||||||
|
room::message::{MessageEventContent, TextMessageEventContent},
|
||||||
|
AnyMessageEventContent,
|
||||||
|
},
|
||||||
Client, ClientConfig, Session, SyncSettings,
|
Client, ClientConfig, Session, SyncSettings,
|
||||||
};
|
};
|
||||||
use systemd::{journal, JournalRecord};
|
use systemd::{journal, JournalRecord};
|
||||||
|
@ -37,7 +40,7 @@ impl BadNewsBot {
|
||||||
load_or_init_session(&self).await?;
|
load_or_init_session(&self).await?;
|
||||||
|
|
||||||
self.client
|
self.client
|
||||||
.set_event_handler(Box::new(AutoJoinHandler::new(
|
.add_event_emitter(Box::new(AutoJoinHandler::new(
|
||||||
self.client.clone(),
|
self.client.clone(),
|
||||||
self.config.room_id.clone(),
|
self.config.room_id.clone(),
|
||||||
)))
|
)))
|
||||||
|
@ -118,8 +121,9 @@ impl BadNewsBot {
|
||||||
unit.strip_suffix(".service").unwrap_or(unit),
|
unit.strip_suffix(".service").unwrap_or(unit),
|
||||||
message,
|
message,
|
||||||
);
|
);
|
||||||
let content =
|
let content = AnyMessageEventContent::RoomMessage(MessageEventContent::Text(
|
||||||
AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain(message));
|
TextMessageEventContent::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