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( pub async fn forward_to_matrix(
client: &Client, client: &Client,
room_id: RoomId, room_id: RoomId,
@ -255,18 +287,14 @@ pub async fn forward_to_matrix(
None None
}; };
let content = if let Some(replied_message_event) = &replied_message_event { let (plaintext_content, html_content) =
MessageEventContent::text_reply_html( generate_html_content(&message.content, &message.author);
format_discord(&message.content),
format_matrix(&message.content), let content = generate_event_content_struct(
replied_message_event, replied_message_event.as_ref(),
) plaintext_content,
} else { html_content,
MessageEventContent::text_html( );
format_discord(&message.content),
format_matrix(&message.content),
)
};
let event = room let event = room
.send(AnyMessageEventContent::RoomMessage(content), None) .send(AnyMessageEventContent::RoomMessage(content), None)
@ -299,27 +327,20 @@ pub async fn edit_on_matrix(
None None
}; };
let plain_reply = format_discord(&message.content); let (plaintext_content, html_content) =
let html_reply = format_matrix(&message.content); generate_html_content(&message.content, &message.author);
let mut edit_content = if let Some(replied_message_event) = &replied_message_event { let mut edit_content = generate_event_content_struct(
MessageEventContent::text_reply_html( replied_message_event.as_ref(),
format!("* {}", &plain_reply), format!("* {}", &plaintext_content),
format!("* {}", html_reply), format!("* {}", &html_content),
replied_message_event, );
)
} else {
MessageEventContent::text_html(
format!("* {}", &plain_reply),
format!("* {}", html_reply),
)
};
let basic_content = if let Some(replied_message_event) = &replied_message_event { let basic_content = generate_event_content_struct(
MessageEventContent::text_reply_html(plain_reply, html_reply, replied_message_event) replied_message_event.as_ref(),
} else { plaintext_content,
MessageEventContent::text_html(plain_reply, html_reply) html_content,
}; );
edit_content.relates_to = Some(Relation::Replacement(Replacement::new( edit_content.relates_to = Some(Relation::Replacement(Replacement::new(
event_id, event_id,
@ -334,6 +355,7 @@ pub async fn edit_on_matrix(
return Some(MessageReference::from((&room_id, &new_event.event_id))); return Some(MessageReference::from((&room_id, &new_event.event_id)));
} }
} }
None None
} }