Store message relations as they are sent

This lets us look up previous messages by ID and correlate them in order
to support rich replies :)
legacy
Charlotte Som 2021-09-12 10:54:41 +01:00
parent 799c034784
commit ced035cf60
6 changed files with 32 additions and 10 deletions

2
.gitignore vendored
View File

@ -1,3 +1 @@
/target /target
/credentials.txt
/matrix_state

2
data/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -1,5 +1,7 @@
use std::{cell::RefCell, str::FromStr, sync::Mutex}; use std::{cell::RefCell, str::FromStr, sync::Mutex};
use sled::Db;
use crate::{ use crate::{
discord::{self, forward_to_discord}, discord::{self, forward_to_discord},
matrix::{self, forward_to_matrix}, matrix::{self, forward_to_matrix},
@ -7,13 +9,17 @@ use crate::{
}; };
pub struct Bridgers { pub struct Bridgers {
pub db: Db,
pub discord: Mutex<RefCell<Option<discord::Context>>>, pub discord: Mutex<RefCell<Option<discord::Context>>>,
pub matrix: Mutex<RefCell<Option<matrix::Client>>>, pub matrix: Mutex<RefCell<Option<matrix::Client>>>,
} }
impl Bridgers { impl Bridgers {
pub fn new() -> Self { pub fn new() -> Self {
let db = sled::open("data/phoebe.sled").expect("Failed to open database");
Self { Self {
db,
discord: Mutex::new(RefCell::new(None)), discord: Mutex::new(RefCell::new(None)),
matrix: Mutex::new(RefCell::new(None)), matrix: Mutex::new(RefCell::new(None)),
} }
@ -40,12 +46,12 @@ impl Bridgers {
} }
pub async fn send_message(&self, message: SentMessage) -> Vec<MessageReference> { pub async fn send_message(&self, message: SentMessage) -> Vec<MessageReference> {
let mut created_messages = Vec::new(); let mut related_messages = vec![message.source.clone()];
if let Some(discord) = self.discord.lock().unwrap().borrow().as_ref() { if let Some(discord) = self.discord.lock().unwrap().borrow().as_ref() {
if let Some(channel) = self.get_linked_discord_channel(&message.source) { if let Some(channel) = self.get_linked_discord_channel(&message.source) {
if let Some(m) = forward_to_discord(discord, channel, &message).await { if let Some(m) = forward_to_discord(discord, channel, &message).await {
created_messages.push(m); related_messages.push(m);
} }
} }
} }
@ -53,11 +59,29 @@ impl Bridgers {
if let Some(matrix) = self.matrix.lock().unwrap().borrow().as_ref() { if let Some(matrix) = self.matrix.lock().unwrap().borrow().as_ref() {
if let Some(room_id) = self.get_linked_matrix_room(&message.source) { if let Some(room_id) = self.get_linked_matrix_room(&message.source) {
if let Some(m) = forward_to_matrix(matrix, room_id, &message).await { if let Some(m) = forward_to_matrix(matrix, room_id, &message).await {
created_messages.push(m); related_messages.push(m);
} }
} }
} }
created_messages for source in related_messages.iter() {
let relations = related_messages
.iter()
.filter(|r| r != &source)
.collect::<Vec<_>>();
let tree = self
.db
.open_tree("message_relations")
.expect("Failed to open relations tree");
tree.insert(
bincode::serialize(source).expect("Failed to serialize message reference"),
bincode::serialize(&relations).expect("Failed to serialize message relations"),
)
.expect("Failed to store message relations");
}
related_messages
} }
} }

View File

@ -14,8 +14,6 @@ use bridgers::Bridgers;
use messages::SentMessage; use messages::SentMessage;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use crate::message_ast::Styled;
async fn setup_discord( async fn setup_discord(
token: String, token: String,
bridgers: Arc<Bridgers>, bridgers: Arc<Bridgers>,

View File

@ -153,7 +153,7 @@ pub async fn create_matrix_client(
password: String, password: String,
message_tx: mpsc::UnboundedSender<SentMessage>, message_tx: mpsc::UnboundedSender<SentMessage>,
) -> Client { ) -> Client {
let client_config = ClientConfig::new().store_path("./matrix_state"); let client_config = ClientConfig::new().store_path("./data/matrix_state");
let homeserver_url = let homeserver_url =
Url::parse(&homeserver_url).expect("Failed to parse the matrix homeserver URL"); Url::parse(&homeserver_url).expect("Failed to parse the matrix homeserver URL");

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::message_ast::MessageContent; use crate::message_ast::MessageContent;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum MessageReference { pub enum MessageReference {
Discord(u64, u64), Discord(u64, u64),
Matrix(String, String), Matrix(String, String),