mirror of
https://github.com/System-End/plura.git
synced 2026-04-19 16:28:21 +00:00
fix(slack): upgrade slack-morphism to 2.20 and adapt rich text rewriting
This commit is contained in:
parent
c42500cef3
commit
aa170e7e42
5 changed files with 1026 additions and 801 deletions
1693
Cargo.lock
generated
1693
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -26,7 +26,7 @@ oauth2 = "5.0.0"
|
||||||
redact = "0.1.10"
|
redact = "0.1.10"
|
||||||
rustls = "0.23.28"
|
rustls = "0.23.28"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
slack-morphism = { version = "2.12.0", features = ["axum"] }
|
slack-morphism = { version = "2.20.0", features = ["axum"] }
|
||||||
sqlx = { version = "0.8.6", features = [
|
sqlx = { version = "0.8.6", features = [
|
||||||
"runtime-tokio",
|
"runtime-tokio",
|
||||||
"sqlite",
|
"sqlite",
|
||||||
|
|
|
||||||
|
|
@ -402,7 +402,7 @@ impl Member {
|
||||||
))
|
))
|
||||||
.opt_accessory(member.profile_picture_url.and_then(|url| Some(
|
.opt_accessory(member.profile_picture_url.and_then(|url| Some(
|
||||||
SlackSectionBlockElement::Image(SlackBlockImageElement::new(
|
SlackSectionBlockElement::Image(SlackBlockImageElement::new(
|
||||||
url.parse().ok()?,
|
url.parse::<url::Url>().ok()?.into(),
|
||||||
"Profile picture".into()
|
"Profile picture".into()
|
||||||
))
|
))
|
||||||
)))
|
)))
|
||||||
|
|
|
||||||
|
|
@ -342,34 +342,118 @@ fn rewrite_content(content: &mut SlackMessageContent, member: &models::DetectedM
|
||||||
|
|
||||||
if let Some(blocks) = &mut content.blocks {
|
if let Some(blocks) = &mut content.blocks {
|
||||||
for block in blocks {
|
for block in blocks {
|
||||||
if let SlackBlock::RichText(richtext) = block {
|
let SlackBlock::RichText(richtext) = block else {
|
||||||
let elements = richtext["elements"].as_array_mut().unwrap();
|
continue;
|
||||||
let len = elements.len();
|
};
|
||||||
// The first and last elements would have the prefix and suffix respectively, so we can filter them
|
|
||||||
let first = elements.get_mut(0).unwrap();
|
|
||||||
|
|
||||||
if let Some(first_text) = first.pointer_mut("/elements/0/text") {
|
match member.typ {
|
||||||
if member.typ == trigger::Type::Prefix {
|
trigger::Type::Prefix => {
|
||||||
if let Some(new_text) = first_text
|
let Some(first) = richtext.elements.first_mut() else {
|
||||||
.as_str()
|
continue;
|
||||||
.and_then(|text| text.strip_prefix(&member.trigger_text))
|
};
|
||||||
.map(ToString::to_string)
|
|
||||||
{
|
match first {
|
||||||
*first_text = serde_json::Value::String(new_text);
|
SlackRichTextElement::Section(section) => {
|
||||||
|
let Some(SlackRichTextInlineElement::Text(text)) =
|
||||||
|
section.elements.first_mut()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(new_text) = text.text.strip_prefix(&member.trigger_text) {
|
||||||
|
text.text = new_text.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SlackRichTextElement::List(list) => {
|
||||||
|
let Some(first_section) = list.elements.first_mut() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Some(SlackRichTextInlineElement::Text(text)) =
|
||||||
|
first_section.elements.first_mut()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(new_text) = text.text.strip_prefix(&member.trigger_text) {
|
||||||
|
text.text = new_text.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SlackRichTextElement::Preformatted(preformatted) => {
|
||||||
|
let Some(SlackRichTextInlineElement::Text(text)) =
|
||||||
|
preformatted.elements.first_mut()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(new_text) = text.text.strip_prefix(&member.trigger_text) {
|
||||||
|
text.text = new_text.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SlackRichTextElement::Quote(quote) => {
|
||||||
|
let Some(SlackRichTextInlineElement::Text(text)) =
|
||||||
|
quote.elements.first_mut()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(new_text) = text.text.strip_prefix(&member.trigger_text) {
|
||||||
|
text.text = new_text.to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
trigger::Type::Suffix => {
|
||||||
|
let Some(last) = richtext.elements.last_mut() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
let last = elements.get_mut(len - 1).unwrap();
|
match last {
|
||||||
|
SlackRichTextElement::Section(section) => {
|
||||||
|
let Some(SlackRichTextInlineElement::Text(text)) =
|
||||||
|
section.elements.last_mut()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(last_text) = last.pointer_mut("/elements/0/text") {
|
if let Some(new_text) = text.text.strip_suffix(&member.trigger_text) {
|
||||||
if member.typ == trigger::Type::Suffix {
|
text.text = new_text.to_string();
|
||||||
if let Some(new_text) = last_text
|
}
|
||||||
.as_str()
|
}
|
||||||
.and_then(|text| text.strip_suffix(&member.trigger_text))
|
SlackRichTextElement::List(list) => {
|
||||||
.map(ToString::to_string)
|
let Some(last_section) = list.elements.last_mut() else {
|
||||||
{
|
continue;
|
||||||
*last_text = serde_json::Value::String(new_text);
|
};
|
||||||
|
let Some(SlackRichTextInlineElement::Text(text)) =
|
||||||
|
last_section.elements.last_mut()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(new_text) = text.text.strip_suffix(&member.trigger_text) {
|
||||||
|
text.text = new_text.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SlackRichTextElement::Preformatted(preformatted) => {
|
||||||
|
let Some(SlackRichTextInlineElement::Text(text)) =
|
||||||
|
preformatted.elements.last_mut()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(new_text) = text.text.strip_suffix(&member.trigger_text) {
|
||||||
|
text.text = new_text.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SlackRichTextElement::Quote(quote) => {
|
||||||
|
let Some(SlackRichTextInlineElement::Text(text)) =
|
||||||
|
quote.elements.last_mut()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(new_text) = text.text.strip_suffix(&member.trigger_text) {
|
||||||
|
text.text = new_text.to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -664,7 +664,7 @@ pub async fn info(
|
||||||
))
|
))
|
||||||
.opt_accessory(member.profile_picture_url.and_then(|url| Some(
|
.opt_accessory(member.profile_picture_url.and_then(|url| Some(
|
||||||
SlackSectionBlockElement::Image(SlackBlockImageElement::new(
|
SlackSectionBlockElement::Image(SlackBlockImageElement::new(
|
||||||
url.parse().ok()?,
|
url.parse::<url::Url>().ok()?.into(),
|
||||||
"Profile picture".into()
|
"Profile picture".into()
|
||||||
))
|
))
|
||||||
)))
|
)))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue