Refactor things from bridgers.rs to platform-specific files

legacy
Charlotte Som 2021-09-12 06:45:46 +01:00
parent bb245687de
commit 799c034784
4 changed files with 76 additions and 57 deletions

View File

@ -1,15 +1,9 @@
use std::{cell::RefCell, str::FromStr, sync::Mutex};
use matrix_sdk::ruma::{
events::{room::message::MessageEventContent, AnyMessageEventContent},
RoomId,
};
use serenity::model::id::ChannelId;
use crate::{
discord, matrix,
message_ast::{self, format_discord, MessageContent},
messages::MessageReference,
discord::{self, forward_to_discord},
matrix::{self, forward_to_matrix},
messages::{MessageReference, SentMessage},
};
pub struct Bridgers {
@ -25,50 +19,42 @@ impl Bridgers {
}
}
pub async fn send_message(
&self,
source: MessageReference,
content: MessageContent,
) -> Vec<MessageReference> {
fn get_linked_discord_channel(&self, source: &MessageReference) -> Option<discord::ChannelId> {
match source {
MessageReference::Matrix(_room_id, _event_id) => {
// TODO: Look up the linked channel
Some(discord::ChannelId(885690775193661463))
}
_ => None,
}
}
fn get_linked_matrix_room(&self, source: &MessageReference) -> Option<matrix::RoomId> {
match source {
MessageReference::Discord(_, _) => {
// TODO: Look up the linked channel
Some(matrix::RoomId::from_str("!SjQatGOikRshcWNcln:matrix.org").unwrap())
}
_ => None,
}
}
pub async fn send_message(&self, message: SentMessage) -> Vec<MessageReference> {
let mut created_messages = Vec::new();
if let Some(discord) = self.discord.lock().unwrap().borrow().as_ref() {
// We probably want a function that returns an Option<ChannelId> taking the source
match &source {
MessageReference::Matrix(_room_id, _event_id) => {
let channel_id = ChannelId(885690775193661463); // TODO: Look up linked channel
let discord_message = channel_id
.send_message(&discord.http, |m| m.content(format_discord(&content)))
.await
.expect("Failed to send discord message");
created_messages.push(MessageReference::from(&discord_message));
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);
}
_ => {}
};
}
}
if let Some(matrix) = self.matrix.lock().unwrap().borrow().as_ref() {
match &source {
MessageReference::Discord(_, _) => {
let room_id = RoomId::from_str("!SjQatGOikRshcWNcln:matrix.org").unwrap(); // TODO: Get a room id
if let Some(room) = matrix.get_joined_room(&room_id) {
let event = room
.send(
AnyMessageEventContent::RoomMessage(
MessageEventContent::text_plain(message_ast::format_discord(
&content, // TODO: Format as HTML
)),
),
None,
)
.await
.unwrap();
created_messages.push(MessageReference::from((&room_id, &event.event_id)));
}
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);
}
_ => {}
}
}

View File

@ -3,11 +3,12 @@ use serenity::{async_trait, model::prelude::*, prelude::*};
use tokio::sync::mpsc;
use crate::{
message_ast,
message_ast::{self, format_discord},
messages::{MessageAuthor, MessageReference, SentMessage},
};
pub use serenity::client::Context;
pub use serenity::model::id::ChannelId;
impl From<&Message> for MessageReference {
fn from(message: &Message) -> Self {
@ -55,6 +56,21 @@ impl EventHandler for DiscordHandler {
}
}
pub async fn forward_to_discord(
discord_ctx: &Context,
channel: ChannelId,
message: &SentMessage,
) -> Option<MessageReference> {
channel
.send_message(&discord_ctx.http, |m| {
m.content(format_discord(&message.content))
})
.await
.as_ref()
.ok()
.map(MessageReference::from)
}
pub async fn create_discord_client(
ctx_tx: mpsc::UnboundedSender<Context>,
message_tx: mpsc::UnboundedSender<SentMessage>,

View File

@ -86,13 +86,6 @@ async fn main() {
.await;
while let Some(message) = message_rx.recv().await {
let mut content = message.content;
content.insert(0, Styled::Plain(": ".to_string()));
content.insert(
0,
Styled::Bold(vec![Styled::Plain(message.author.display_name.to_string())]),
);
let _ = bridgers.send_message(message.source, content).await;
let _ = bridgers.send_message(message).await;
}
}

View File

@ -9,18 +9,18 @@ use matrix_sdk::{
},
AnyMessageEventContent, AnySyncRoomEvent, SyncMessageEvent,
},
EventId, RoomId, UserId,
EventId, UserId,
},
ClientConfig, EventHandler, SyncSettings,
};
pub use matrix_sdk::{ruma::RoomId, Client};
use log::info;
pub use matrix_sdk::Client;
use tokio::sync::mpsc;
use url::Url;
use crate::{
message_ast::convert_plain,
message_ast::{convert_plain, format_discord},
messages::{MessageAuthor, MessageReference, SentMessage},
};
@ -123,6 +123,30 @@ impl EventHandler for MatrixHandler {
}
}
pub async fn forward_to_matrix(
client: &Client,
room_id: RoomId,
message: &SentMessage,
) -> Option<MessageReference> {
if let Some(room) = client.get_joined_room(&room_id) {
let event = room
.send(
AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain(
format_discord(
&message.content, // TODO: Format as HTML
),
)),
None,
)
.await
.ok()?;
return Some(MessageReference::from((&room_id, &event.event_id)));
}
None
}
pub async fn create_matrix_client(
homeserver_url: String,
username: String,