forked from projectmoon/tenebrous-dicebot
Upgrade to matrix SDK 0.4.
This commit is contained in:
parent
ee3ec18e06
commit
6f09a11586
File diff suppressed because it is too large
Load Diff
|
@ -32,7 +32,7 @@ combine = "4.5"
|
|||
futures = "0.3"
|
||||
html2text = "0.2"
|
||||
phf = { version = "0.8", features = ["macros"] }
|
||||
matrix-sdk = { version = "0.3" }
|
||||
matrix-sdk = { version = "0.4.1" }
|
||||
refinery = { version = "0.6", features = ["rusqlite"]}
|
||||
barrel = { version = "0.6", features = ["sqlite3"] }
|
||||
tempfile = "3"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use matrix_sdk::identifiers::room_id;
|
||||
use matrix_sdk::ruma::identifiers::room_id;
|
||||
use matrix_sdk::Client;
|
||||
use tenebrous_dicebot::commands;
|
||||
use tenebrous_dicebot::commands::ResponseExtractor;
|
||||
|
|
|
@ -8,12 +8,8 @@ use crate::{
|
|||
models::Account,
|
||||
};
|
||||
use futures::stream::{self, StreamExt};
|
||||
use matrix_sdk::{
|
||||
self,
|
||||
identifiers::{EventId, RoomId},
|
||||
room::Joined,
|
||||
Client,
|
||||
};
|
||||
use matrix_sdk::ruma::{EventId, RoomId};
|
||||
use matrix_sdk::{self, room::Joined, Client};
|
||||
use std::clone::Clone;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
|
|
|
@ -2,18 +2,12 @@ use super::DiceBot;
|
|||
use crate::db::sqlite::Database;
|
||||
use crate::db::Rooms;
|
||||
use crate::error::BotError;
|
||||
use async_trait::async_trait;
|
||||
use log::{debug, error, info, warn};
|
||||
use matrix_sdk::{
|
||||
self,
|
||||
events::{
|
||||
room::member::MemberEventContent,
|
||||
room::message::{MessageEventContent, MessageType, TextMessageEventContent},
|
||||
StrippedStateEvent, SyncMessageEvent,
|
||||
},
|
||||
room::Room,
|
||||
EventHandler,
|
||||
};
|
||||
use matrix_sdk::ruma::events::room::member::MemberEventContent;
|
||||
use matrix_sdk::ruma::events::room::message::{MessageType, TextMessageEventContent};
|
||||
use matrix_sdk::ruma::events::{StrippedStateEvent, SyncMessageEvent};
|
||||
use matrix_sdk::Client;
|
||||
use matrix_sdk::{self, room::Room, ruma::events::room::message::MessageEventContent};
|
||||
use std::ops::Sub;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::{clone::Clone, time::UNIX_EPOCH};
|
||||
|
@ -101,58 +95,56 @@ async fn should_process_event(db: &Database, room_id: &str, event_id: &str) -> b
|
|||
})
|
||||
}
|
||||
|
||||
/// This event emitter listens for messages with dice rolling commands.
|
||||
/// Originally adapted from the matrix-rust-sdk examples.
|
||||
#[async_trait]
|
||||
impl EventHandler for DiceBot {
|
||||
async fn on_stripped_state_member(
|
||||
&self,
|
||||
room: Room,
|
||||
event: &StrippedStateEvent<MemberEventContent>,
|
||||
_: Option<MemberEventContent>,
|
||||
) {
|
||||
let room = match room {
|
||||
Room::Invited(invited_room) => invited_room,
|
||||
_ => return,
|
||||
};
|
||||
pub(super) async fn on_stripped_state_member(
|
||||
event: StrippedStateEvent<MemberEventContent>,
|
||||
client: Client,
|
||||
room: Room,
|
||||
) {
|
||||
let room = match room {
|
||||
Room::Invited(invited_room) => invited_room,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
if room.own_user_id().as_str() != event.state_key {
|
||||
return;
|
||||
}
|
||||
|
||||
info!(
|
||||
"Autojoining room {}",
|
||||
room.display_name().await.ok().unwrap_or_default()
|
||||
);
|
||||
|
||||
if let Err(e) = self.client.join_room_by_id(&room.room_id()).await {
|
||||
warn!("Could not join room: {}", e.to_string())
|
||||
}
|
||||
if room.own_user_id().as_str() != event.state_key {
|
||||
return;
|
||||
}
|
||||
|
||||
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
|
||||
let room = match room {
|
||||
Room::Joined(joined_room) => joined_room,
|
||||
_ => return,
|
||||
};
|
||||
info!(
|
||||
"Autojoining room {}",
|
||||
room.display_name().await.ok().unwrap_or_default()
|
||||
);
|
||||
|
||||
let room_id = room.room_id().as_str();
|
||||
if !should_process_event(&self.db, room_id, event.event_id.as_str()).await {
|
||||
return;
|
||||
}
|
||||
|
||||
let (msg_body, sender_username) =
|
||||
if let Ok((msg_body, sender_username)) = should_process_message(self, &event).await {
|
||||
(msg_body, sender_username)
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let results = self
|
||||
.execute_commands(&room, &sender_username, &msg_body)
|
||||
.await;
|
||||
|
||||
self.handle_results(&room, &sender_username, event.event_id.clone(), results)
|
||||
.await;
|
||||
if let Err(e) = client.join_room_by_id(&room.room_id()).await {
|
||||
warn!("Could not join room: {}", e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn on_room_message(
|
||||
event: SyncMessageEvent<MessageEventContent>,
|
||||
room: Room,
|
||||
bot: DiceBot,
|
||||
) {
|
||||
let room = match room {
|
||||
Room::Joined(joined_room) => joined_room,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
let room_id = room.room_id().as_str();
|
||||
if !should_process_event(&bot.db, room_id, event.event_id.as_str()).await {
|
||||
return;
|
||||
}
|
||||
|
||||
let (msg_body, sender_username) =
|
||||
if let Ok((msg_body, sender_username)) = should_process_message(&bot, &event).await {
|
||||
(msg_body, sender_username)
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let results = bot
|
||||
.execute_commands(&room, &sender_username, &msg_body)
|
||||
.await;
|
||||
|
||||
bot.handle_results(&room, &sender_username, event.event_id.clone(), results)
|
||||
.await;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,11 @@ use crate::db::DbState;
|
|||
use crate::error::BotError;
|
||||
use crate::state::DiceBotState;
|
||||
use log::info;
|
||||
use matrix_sdk::{self, identifiers::EventId, room::Joined, Client, SyncSettings};
|
||||
use matrix_sdk::room::Room;
|
||||
use matrix_sdk::ruma::events::room::message::MessageEventContent;
|
||||
use matrix_sdk::ruma::events::SyncMessageEvent;
|
||||
use matrix_sdk::ruma::EventId;
|
||||
use matrix_sdk::{self, room::Joined, Client, SyncSettings};
|
||||
use std::clone::Clone;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
|
@ -18,6 +22,7 @@ const MAX_COMMANDS_PER_MESSAGE: usize = 50;
|
|||
|
||||
/// The DiceBot struct represents an active dice bot. The bot is not
|
||||
/// connected to Matrix until its run() function is called.
|
||||
#[derive(Clone)]
|
||||
pub struct DiceBot {
|
||||
/// A reference to the configuration read in on application start.
|
||||
config: Arc<Config>,
|
||||
|
@ -79,14 +84,32 @@ impl DiceBot {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn bind_events(&self) {
|
||||
//on room message: need closure to pass bot ref in.
|
||||
self.client
|
||||
.register_event_handler({
|
||||
let bot: DiceBot = self.clone();
|
||||
move |event: SyncMessageEvent<MessageEventContent>, room: Room| {
|
||||
let bot = bot.clone();
|
||||
async move { event_handlers::on_room_message(event, room, bot).await }
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
//auto-join handler
|
||||
self.client
|
||||
.register_event_handler(event_handlers::on_stripped_state_member)
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Logs the bot in to Matrix and listens for events until program
|
||||
/// terminated, or a panic occurs. Originally adapted from the
|
||||
/// matrix-rust-sdk command bot example.
|
||||
pub async fn run(self) -> Result<(), BotError> {
|
||||
let client = self.client.clone();
|
||||
self.login(&client).await?;
|
||||
self.bind_events().await;
|
||||
|
||||
client.set_event_handler(Box::new(self)).await;
|
||||
info!("Listening for commands");
|
||||
|
||||
// TODO replace with sync_with_callback for cleaner shutdown
|
||||
|
|
|
@ -332,7 +332,7 @@ mod tests {
|
|||
macro_rules! dummy_room {
|
||||
() => {
|
||||
crate::context::RoomContext {
|
||||
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
||||
id: &matrix_sdk::ruma::identifiers::room_id!("!fakeroomid:example.com"),
|
||||
display_name: "displayname".to_owned(),
|
||||
secure: false,
|
||||
}
|
||||
|
|
|
@ -166,7 +166,7 @@ mod tests {
|
|||
macro_rules! dummy_room {
|
||||
() => {
|
||||
crate::context::RoomContext {
|
||||
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
||||
id: &matrix_sdk::ruma::identifiers::room_id!("!fakeroomid:example.com"),
|
||||
display_name: "displayname".to_owned(),
|
||||
secure: false,
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ mod tests {
|
|||
macro_rules! secure_room {
|
||||
() => {
|
||||
crate::context::RoomContext {
|
||||
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
||||
id: &matrix_sdk::ruma::identifiers::room_id!("!fakeroomid:example.com"),
|
||||
display_name: "displayname".to_owned(),
|
||||
secure: true,
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::matrix;
|
|||
use async_trait::async_trait;
|
||||
use fuse_rust::{Fuse, FuseProperty, Fuseable};
|
||||
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||
use matrix_sdk::{identifiers::UserId, Client};
|
||||
use matrix_sdk::{ruma::UserId, Client};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
/// Holds matrix room ID and display name as strings, for use with
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::db::sqlite::Database;
|
||||
use crate::error::BotError;
|
||||
use crate::models::Account;
|
||||
use matrix_sdk::identifiers::{RoomId, UserId};
|
||||
use matrix_sdk::room::Joined;
|
||||
use matrix_sdk::ruma::identifiers::{RoomId, UserId};
|
||||
use matrix_sdk::Client;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
|
|
|
@ -431,7 +431,7 @@ mod tests {
|
|||
macro_rules! dummy_room {
|
||||
() => {
|
||||
crate::context::RoomContext {
|
||||
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
|
||||
id: &matrix_sdk::ruma::identifiers::room_id!("!fakeroomid:example.com"),
|
||||
display_name: "displayname".to_owned(),
|
||||
secure: false,
|
||||
}
|
||||
|
|
|
@ -39,9 +39,6 @@ pub enum BotError {
|
|||
#[error("uncategorized matrix SDK error: {0}")]
|
||||
MatrixError(#[from] matrix_sdk::Error),
|
||||
|
||||
#[error("uncategorized matrix SDK base error: {0}")]
|
||||
MatrixBaseError(#[from] matrix_sdk::BaseError),
|
||||
|
||||
#[error("future canceled")]
|
||||
FutureCanceledError,
|
||||
|
||||
|
@ -80,7 +77,7 @@ pub enum BotError {
|
|||
TryFromIntError(#[from] std::num::TryFromIntError),
|
||||
|
||||
#[error("identifier error: {0}")]
|
||||
IdentifierError(#[from] matrix_sdk::identifiers::Error),
|
||||
IdentifierError(#[from] matrix_sdk::ruma::identifiers::Error),
|
||||
|
||||
#[error("password creation error: {0}")]
|
||||
PasswordCreationError(argon2::Error),
|
||||
|
|
|
@ -2,15 +2,12 @@ use std::path::PathBuf;
|
|||
|
||||
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||
use log::error;
|
||||
use matrix_sdk::{events::room::message::NoticeMessageEventContent, room::Joined, ClientConfig};
|
||||
use matrix_sdk::{
|
||||
events::room::message::{InReplyTo, Relation},
|
||||
events::room::message::{MessageEventContent, MessageType},
|
||||
events::AnyMessageEventContent,
|
||||
identifiers::EventId,
|
||||
Error as MatrixError,
|
||||
};
|
||||
use matrix_sdk::{identifiers::RoomId, identifiers::UserId, Client};
|
||||
use matrix_sdk::ruma::events::room::message::{InReplyTo, MessageEventContent, Relation};
|
||||
use matrix_sdk::ruma::events::AnyMessageEventContent;
|
||||
use matrix_sdk::ruma::{EventId, RoomId, UserId};
|
||||
use matrix_sdk::Client;
|
||||
use matrix_sdk::Error as MatrixError;
|
||||
use matrix_sdk::{room::Joined, ClientConfig};
|
||||
use url::Url;
|
||||
|
||||
use crate::{config::Config, error::BotError};
|
||||
|
@ -95,9 +92,7 @@ pub async fn send_message(
|
|||
_ => return,
|
||||
};
|
||||
|
||||
let mut content = MessageEventContent::new(MessageType::Notice(
|
||||
NoticeMessageEventContent::html(plain.trim(), html),
|
||||
));
|
||||
let mut content = MessageEventContent::notice_html(plain.trim(), html);
|
||||
|
||||
content.relates_to = reply_to.map(|event_id| Relation::Reply {
|
||||
in_reply_to: InReplyTo::new(event_id),
|
||||
|
|
|
@ -4,7 +4,8 @@ use crate::matrix;
|
|||
use crate::{config::Config, db::sqlite::Database};
|
||||
use futures::stream;
|
||||
use futures::{StreamExt, TryFutureExt, TryStreamExt};
|
||||
use matrix_sdk::{identifiers::UserId, room::Joined, Client};
|
||||
use matrix_sdk::ruma::UserId;
|
||||
use matrix_sdk::{room::Joined, Client};
|
||||
use std::convert::TryFrom;
|
||||
use std::sync::Arc;
|
||||
use tenebrous_rpc::protos::dicebot::{
|
||||
|
|
Loading…
Reference in New Issue