Prepare for message editing

legacy
Charlotte Som 2021-09-13 03:08:35 +01:00
parent 33e13eb4b0
commit fee137f947
4 changed files with 40 additions and 21 deletions

View File

@ -4,7 +4,7 @@ use tokio::sync::mpsc;
use crate::{
message_ast::{self, format_discord},
messages::{MessageAuthor, MessageReference, SentMessage},
messages::{MessageAuthor, MessageEvent, MessageReference, SentMessage},
};
pub use serenity::client::Context;
@ -18,7 +18,7 @@ impl From<&Message> for MessageReference {
struct DiscordHandler {
ctx_tx: mpsc::UnboundedSender<Context>,
message_tx: mpsc::UnboundedSender<SentMessage>,
message_tx: mpsc::UnboundedSender<MessageEvent>,
}
#[async_trait]
@ -40,7 +40,7 @@ impl EventHandler for DiscordHandler {
let content = discord_message_format::parse(&message.content);
let content = message_ast::convert_discord(&content);
let _ = self.message_tx.send(SentMessage {
let _ = self.message_tx.send(MessageEvent::Send(SentMessage {
source: message_ref,
content,
author: MessageAuthor {
@ -49,7 +49,7 @@ impl EventHandler for DiscordHandler {
.await
.unwrap_or(message.author.name),
},
});
}));
}
}
@ -70,7 +70,7 @@ pub async fn forward_to_discord(
pub async fn create_discord_client(
ctx_tx: mpsc::UnboundedSender<Context>,
message_tx: mpsc::UnboundedSender<SentMessage>,
message_tx: mpsc::UnboundedSender<MessageEvent>,
token: &str,
) -> Client {
let handler = DiscordHandler { ctx_tx, message_tx };

View File

@ -11,13 +11,14 @@ pub mod discord;
pub mod matrix;
use bridgers::Bridgers;
use messages::SentMessage;
use tokio::sync::mpsc;
use crate::messages::MessageEvent;
async fn setup_discord(
token: String,
bridgers: Arc<Bridgers>,
discord_tx: mpsc::UnboundedSender<SentMessage>,
discord_tx: mpsc::UnboundedSender<MessageEvent>,
) {
let (discord_ctx_tx, mut discord_ctx_rx) = mpsc::unbounded_channel::<discord::Context>();
tokio::spawn(async move {
@ -38,9 +39,9 @@ async fn setup_matrix(
username: String,
password: String,
bridgers: Arc<Bridgers>,
message_tx: mpsc::UnboundedSender<SentMessage>,
event_tx: mpsc::UnboundedSender<MessageEvent>,
) {
let client = create_matrix_client(homeserver_url, username, password, message_tx).await;
let client = create_matrix_client(homeserver_url, username, password, event_tx).await;
bridgers
.matrix
@ -60,7 +61,7 @@ async fn main() {
let bridgers = Arc::new(Bridgers::new());
let (message_tx, mut message_rx) = tokio::sync::mpsc::unbounded_channel::<SentMessage>();
let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel::<MessageEvent>();
#[inline]
fn get_env_var(key: &str) -> String {
@ -70,7 +71,7 @@ async fn main() {
setup_discord(
get_env_var("DISCORD_TOKEN"),
Arc::clone(&bridgers),
message_tx.clone(),
event_tx.clone(),
)
.await;
@ -79,11 +80,18 @@ async fn main() {
get_env_var("MATRIX_USERNAME"),
get_env_var("MATRIX_PASSWORD"),
Arc::clone(&bridgers),
message_tx.clone(),
event_tx.clone(),
)
.await;
while let Some(message) = message_rx.recv().await {
let _ = bridgers.send_message(message).await;
while let Some(event) = event_rx.recv().await {
match event {
MessageEvent::Send(sent_message) => {
let _ = bridgers.send_message(sent_message).await;
}
MessageEvent::Edit(edited_message) => {
todo!();
}
}
}
}

View File

@ -26,7 +26,7 @@ use crate::{
convert_matrix, convert_plain, format_discord, format_matrix, MessageComponent,
MessageContent,
},
messages::{MessageAuthor, MessageReference, SentMessage},
messages::{MessageAuthor, MessageEvent, MessageReference, SentMessage},
};
impl From<(&RoomId, &EventId)> for MessageReference {
@ -69,7 +69,7 @@ fn _find_content(event: &AnySyncRoomEvent) -> Option<AnyMessageEventContent> {
}
struct MatrixHandler {
message_tx: mpsc::UnboundedSender<SentMessage>,
message_tx: mpsc::UnboundedSender<MessageEvent>,
current_user_id: UserId,
}
@ -122,11 +122,11 @@ impl EventHandler for MatrixHandler {
let content = self.get_content(&text.body, &text.formatted);
if let Some(author) = self.get_message_author(&room, &event.sender).await {
let _ = self.message_tx.send(SentMessage {
let _ = self.message_tx.send(MessageEvent::Send(SentMessage {
source: message_ref,
content,
author,
});
}));
}
}
@ -135,11 +135,11 @@ impl EventHandler for MatrixHandler {
content.insert(0, MessageComponent::Plain("* ".to_string()));
if let Some(author) = self.get_message_author(&room, &event.sender).await {
let _ = self.message_tx.send(SentMessage {
let _ = self.message_tx.send(MessageEvent::Send(SentMessage {
source: message_ref,
content,
author,
});
}));
}
}
@ -179,7 +179,7 @@ pub async fn create_matrix_client(
homeserver_url: String,
username: String,
password: String,
message_tx: mpsc::UnboundedSender<SentMessage>,
message_tx: mpsc::UnboundedSender<MessageEvent>,
) -> Client {
let client_config = ClientConfig::new().store_path("./data/matrix_state");

View File

@ -17,3 +17,14 @@ pub struct SentMessage {
pub content: MessageContent,
pub author: MessageAuthor,
}
pub struct EditedMessage {
pub replacing: MessageReference,
pub content: MessageContent,
pub author: MessageAuthor,
}
pub enum MessageEvent {
Send(SentMessage),
Edit(EditedMessage),
}