Prepend author information to messages which are bridged to matrix

legacy
Charlotte Som 2021-10-02 17:20:31 +01:00
parent f3dce91862
commit 6032bf06d6
1 changed files with 53 additions and 31 deletions

View File

@ -242,6 +242,38 @@ async fn on_redact_event(ctx: Arc<MatrixHandler>, event: SyncRedactionEvent, roo
}));
}
fn generate_html_content(content: &[MessageComponent], author: &MessageAuthor) -> (String, String) {
let plaintext_message = format!(
"{} ({}): {}",
&author.display_name,
&author.service_name,
format_discord(content)
);
// TODO: Do we want to do something with different users' discord name colors? That could be a neat way
// to differentiate people easily without being able to see avatars
let html_message = format!(
r##"<p><font data-mx-color="#9a9a9a" data-mx-bg-color="#000000">{} <small>({})</small></font>:</p> <p>{}</p>"##,
&author.display_name,
&author.service_name,
format_matrix(content),
);
(plaintext_message, html_message)
}
fn generate_event_content_struct(
replied_message_event: Option<&events::MessageEvent<MessageEventContent>>,
plaintext_content: String,
html_content: String,
) -> MessageEventContent {
if let Some(replied_message_event) = replied_message_event {
MessageEventContent::text_reply_html(plaintext_content, html_content, replied_message_event)
} else {
MessageEventContent::text_html(plaintext_content, html_content)
}
}
pub async fn forward_to_matrix(
client: &Client,
room_id: RoomId,
@ -255,18 +287,14 @@ pub async fn forward_to_matrix(
None
};
let content = if let Some(replied_message_event) = &replied_message_event {
MessageEventContent::text_reply_html(
format_discord(&message.content),
format_matrix(&message.content),
replied_message_event,
)
} else {
MessageEventContent::text_html(
format_discord(&message.content),
format_matrix(&message.content),
)
};
let (plaintext_content, html_content) =
generate_html_content(&message.content, &message.author);
let content = generate_event_content_struct(
replied_message_event.as_ref(),
plaintext_content,
html_content,
);
let event = room
.send(AnyMessageEventContent::RoomMessage(content), None)
@ -299,27 +327,20 @@ pub async fn edit_on_matrix(
None
};
let plain_reply = format_discord(&message.content);
let html_reply = format_matrix(&message.content);
let (plaintext_content, html_content) =
generate_html_content(&message.content, &message.author);
let mut edit_content = if let Some(replied_message_event) = &replied_message_event {
MessageEventContent::text_reply_html(
format!("* {}", &plain_reply),
format!("* {}", html_reply),
replied_message_event,
)
} else {
MessageEventContent::text_html(
format!("* {}", &plain_reply),
format!("* {}", html_reply),
)
};
let mut edit_content = generate_event_content_struct(
replied_message_event.as_ref(),
format!("* {}", &plaintext_content),
format!("* {}", &html_content),
);
let basic_content = if let Some(replied_message_event) = &replied_message_event {
MessageEventContent::text_reply_html(plain_reply, html_reply, replied_message_event)
} else {
MessageEventContent::text_html(plain_reply, html_reply)
};
let basic_content = generate_event_content_struct(
replied_message_event.as_ref(),
plaintext_content,
html_content,
);
edit_content.relates_to = Some(Relation::Replacement(Replacement::new(
event_id,
@ -334,6 +355,7 @@ pub async fn edit_on_matrix(
return Some(MessageReference::from((&room_id, &new_event.event_id)));
}
}
None
}