Refactor things from bridgers.rs to platform-specific files
parent
bb245687de
commit
799c034784
|
@ -1,15 +1,9 @@
|
||||||
use std::{cell::RefCell, str::FromStr, sync::Mutex};
|
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::{
|
use crate::{
|
||||||
discord, matrix,
|
discord::{self, forward_to_discord},
|
||||||
message_ast::{self, format_discord, MessageContent},
|
matrix::{self, forward_to_matrix},
|
||||||
messages::MessageReference,
|
messages::{MessageReference, SentMessage},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Bridgers {
|
pub struct Bridgers {
|
||||||
|
@ -25,51 +19,43 @@ impl Bridgers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_message(
|
fn get_linked_discord_channel(&self, source: &MessageReference) -> Option<discord::ChannelId> {
|
||||||
&self,
|
match source {
|
||||||
source: MessageReference,
|
MessageReference::Matrix(_room_id, _event_id) => {
|
||||||
content: MessageContent,
|
// TODO: Look up the linked channel
|
||||||
) -> Vec<MessageReference> {
|
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();
|
let mut created_messages = Vec::new();
|
||||||
|
|
||||||
if let Some(discord) = self.discord.lock().unwrap().borrow().as_ref() {
|
if let Some(discord) = self.discord.lock().unwrap().borrow().as_ref() {
|
||||||
// We probably want a function that returns an Option<ChannelId> taking the source
|
if let Some(channel) = self.get_linked_discord_channel(&message.source) {
|
||||||
match &source {
|
if let Some(m) = forward_to_discord(discord, channel, &message).await {
|
||||||
MessageReference::Matrix(_room_id, _event_id) => {
|
created_messages.push(m);
|
||||||
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() {
|
if let Some(matrix) = self.matrix.lock().unwrap().borrow().as_ref() {
|
||||||
match &source {
|
if let Some(room_id) = self.get_linked_matrix_room(&message.source) {
|
||||||
MessageReference::Discord(_, _) => {
|
if let Some(m) = forward_to_matrix(matrix, room_id, &message).await {
|
||||||
let room_id = RoomId::from_str("!SjQatGOikRshcWNcln:matrix.org").unwrap(); // TODO: Get a room id
|
created_messages.push(m);
|
||||||
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
|
created_messages
|
||||||
|
|
|
@ -3,11 +3,12 @@ use serenity::{async_trait, model::prelude::*, prelude::*};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
message_ast,
|
message_ast::{self, format_discord},
|
||||||
messages::{MessageAuthor, MessageReference, SentMessage},
|
messages::{MessageAuthor, MessageReference, SentMessage},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use serenity::client::Context;
|
pub use serenity::client::Context;
|
||||||
|
pub use serenity::model::id::ChannelId;
|
||||||
|
|
||||||
impl From<&Message> for MessageReference {
|
impl From<&Message> for MessageReference {
|
||||||
fn from(message: &Message) -> Self {
|
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(
|
pub async fn create_discord_client(
|
||||||
ctx_tx: mpsc::UnboundedSender<Context>,
|
ctx_tx: mpsc::UnboundedSender<Context>,
|
||||||
message_tx: mpsc::UnboundedSender<SentMessage>,
|
message_tx: mpsc::UnboundedSender<SentMessage>,
|
||||||
|
|
|
@ -86,13 +86,6 @@ async fn main() {
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
while let Some(message) = message_rx.recv().await {
|
while let Some(message) = message_rx.recv().await {
|
||||||
let mut content = message.content;
|
let _ = bridgers.send_message(message).await;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,18 +9,18 @@ use matrix_sdk::{
|
||||||
},
|
},
|
||||||
AnyMessageEventContent, AnySyncRoomEvent, SyncMessageEvent,
|
AnyMessageEventContent, AnySyncRoomEvent, SyncMessageEvent,
|
||||||
},
|
},
|
||||||
EventId, RoomId, UserId,
|
EventId, UserId,
|
||||||
},
|
},
|
||||||
ClientConfig, EventHandler, SyncSettings,
|
ClientConfig, EventHandler, SyncSettings,
|
||||||
};
|
};
|
||||||
|
pub use matrix_sdk::{ruma::RoomId, Client};
|
||||||
|
|
||||||
use log::info;
|
use log::info;
|
||||||
pub use matrix_sdk::Client;
|
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
message_ast::convert_plain,
|
message_ast::{convert_plain, format_discord},
|
||||||
messages::{MessageAuthor, MessageReference, SentMessage},
|
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(
|
pub async fn create_matrix_client(
|
||||||
homeserver_url: String,
|
homeserver_url: String,
|
||||||
username: String,
|
username: String,
|
||||||
|
|
Loading…
Reference in New Issue