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
parent
799c034784
commit
ced035cf60
|
@ -1,3 +1 @@
|
|||
/target
|
||||
/credentials.txt
|
||||
/matrix_state
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
*
|
||||
!.gitignore
|
|
@ -1,5 +1,7 @@
|
|||
use std::{cell::RefCell, str::FromStr, sync::Mutex};
|
||||
|
||||
use sled::Db;
|
||||
|
||||
use crate::{
|
||||
discord::{self, forward_to_discord},
|
||||
matrix::{self, forward_to_matrix},
|
||||
|
@ -7,13 +9,17 @@ use crate::{
|
|||
};
|
||||
|
||||
pub struct Bridgers {
|
||||
pub db: Db,
|
||||
pub discord: Mutex<RefCell<Option<discord::Context>>>,
|
||||
pub matrix: Mutex<RefCell<Option<matrix::Client>>>,
|
||||
}
|
||||
|
||||
impl Bridgers {
|
||||
pub fn new() -> Self {
|
||||
let db = sled::open("data/phoebe.sled").expect("Failed to open database");
|
||||
|
||||
Self {
|
||||
db,
|
||||
discord: 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> {
|
||||
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(channel) = self.get_linked_discord_channel(&message.source) {
|
||||
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(room_id) = self.get_linked_matrix_room(&message.source) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,6 @@ use bridgers::Bridgers;
|
|||
use messages::SentMessage;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::message_ast::Styled;
|
||||
|
||||
async fn setup_discord(
|
||||
token: String,
|
||||
bridgers: Arc<Bridgers>,
|
||||
|
|
|
@ -153,7 +153,7 @@ pub async fn create_matrix_client(
|
|||
password: String,
|
||||
message_tx: mpsc::UnboundedSender<SentMessage>,
|
||||
) -> Client {
|
||||
let client_config = ClientConfig::new().store_path("./matrix_state");
|
||||
let client_config = ClientConfig::new().store_path("./data/matrix_state");
|
||||
|
||||
let homeserver_url =
|
||||
Url::parse(&homeserver_url).expect("Failed to parse the matrix homeserver URL");
|
||||
|
|
|
@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
use crate::message_ast::MessageContent;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||
pub enum MessageReference {
|
||||
Discord(u64, u64),
|
||||
Matrix(String, String),
|
||||
|
|
Loading…
Reference in New Issue