78 lines
2.7 KiB
Rust
78 lines
2.7 KiB
Rust
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,
|
|
};
|
|
|
|
pub struct Bridgers {
|
|
pub discord: Mutex<RefCell<Option<discord::Context>>>,
|
|
pub matrix: Mutex<RefCell<Option<matrix::Client>>>,
|
|
}
|
|
|
|
impl Bridgers {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
discord: Mutex::new(RefCell::new(None)),
|
|
matrix: Mutex::new(RefCell::new(None)),
|
|
}
|
|
}
|
|
|
|
pub async fn send_message(
|
|
&self,
|
|
source: MessageReference,
|
|
content: MessageContent,
|
|
) -> 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(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)));
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
created_messages
|
|
}
|
|
}
|