Skip to content

Commit b6e0c1b

Browse files
committed
Allow hiding platform name in messages from config
1 parent c28a25f commit b6e0c1b

File tree

5 files changed

+91
-46
lines changed

5 files changed

+91
-46
lines changed

src/config/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ pub struct NotificationSwitchOverride {
286286

287287
#[derive(Clone, Debug, PartialEq, Deserialize)]
288288
pub struct NotificationOption<O> {
289+
#[serde(default = "helper::refl_bool::<true>")]
290+
pub platform_name: bool,
289291
#[serde(default = "helper::refl_bool::<false>")]
290292
pub author_name: bool,
291293
#[serde(default, flatten)]
@@ -306,6 +308,7 @@ impl<'a, O: Deserialize<'a> + Overridable> Overridable for NotificationOption<O>
306308
Self: Sized,
307309
{
308310
Self {
311+
platform_name: new.platform_name.unwrap_or(self.platform_name),
309312
author_name: new.author_name.unwrap_or(self.author_name),
310313
ext: match new.ext {
311314
Some(ext) => self.ext.override_into(ext),
@@ -317,6 +320,7 @@ impl<'a, O: Deserialize<'a> + Overridable> Overridable for NotificationOption<O>
317320

318321
#[derive(Clone, Debug, PartialEq, Deserialize)]
319322
pub struct NotificationOptionOverride<O> {
323+
pub platform_name: Option<bool>,
320324
pub author_name: Option<bool>,
321325
#[serde(flatten)]
322326
pub ext: Option<O>,
@@ -487,6 +491,7 @@ notify = ["meow", "woof", { ref = "woof", id = 123 }]
487491
document: true,
488492
},
489493
option: NotificationOption {
494+
platform_name: true,
490495
author_name: false,
491496
ext: telegram::notify::OptionExt {
492497
no_button: false,

src/helper.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ pub fn serde_default<'a, T: Default + Deserialize<'a>>() -> T {
4848
.unwrap()
4949
}
5050

51+
#[macro_export]
52+
macro_rules! format_if {
53+
( $cond:expr, $($fmt_arg:tt)+ ) => {
54+
if $cond {
55+
std::borrow::Cow::Owned(format!($($fmt_arg)+))
56+
} else {
57+
std::borrow::Cow::Borrowed("")
58+
}
59+
};
60+
}
61+
5162
pub fn format_duration_in_sec(dur: Duration) -> String {
5263
humantime::format_duration(Duration::from_secs(dur.as_secs())).to_string()
5364
}

src/platform/qq/lagrange.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,25 @@ impl Message {
170170
MessageBuilder(Message(vec![]))
171171
}
172172

173-
pub fn text(text: impl Into<String>) -> Self {
173+
pub fn text<'a>(text: impl Into<Cow<'a, str>>) -> Self {
174174
Self::builder().text(text).build()
175175
}
176176
}
177177

178178
pub struct MessageBuilder(Message);
179179

180180
impl MessageBuilder {
181-
pub fn ref_text(&mut self, text: impl Into<String>) -> &mut Self {
182-
self.0 .0.push(MessageSegment::Text(MessageSegmentText {
183-
text: text.into(),
184-
}));
181+
pub fn ref_text<'a>(&mut self, text: impl Into<Cow<'a, str>>) -> &mut Self {
182+
let text = text.into();
183+
if !text.is_empty() {
184+
self.0 .0.push(MessageSegment::Text(MessageSegmentText {
185+
text: text.into(),
186+
}));
187+
}
185188
self
186189
}
187190

188-
pub fn text(mut self, text: impl Into<String>) -> Self {
191+
pub fn text<'a>(mut self, text: impl Into<Cow<'a, str>>) -> Self {
189192
self.ref_text(text);
190193
self
191194
}

src/platform/qq/notify/mod.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, fmt, future::Future, pin::Pin};
1+
use std::{fmt, future::Future, pin::Pin};
22

33
use anyhow::{anyhow, ensure};
44
use serde::Deserialize;
@@ -7,7 +7,7 @@ use spdlog::prelude::*;
77
use super::{lagrange, ConfigChat};
88
use crate::{
99
config::{self, Accessor, AccountRef, Config, ContextualValidator, Overridable, Validator},
10-
helper,
10+
format_if, helper,
1111
notify::NotifierTrait,
1212
platform::{PlatformMetadata, PlatformTrait},
1313
source::{
@@ -180,13 +180,17 @@ impl Notifier {
180180
builder.text(format!("{custom_live_text}\n{}", live_status.live_url))
181181
} else {
182182
builder.text(format!(
183-
"[{}] 🟢 {}{}\n{}",
184-
source.platform.display_name,
185-
if self.params.base.option.author_name {
186-
Cow::Owned(format!("[{}] ", live_status.streamer_name))
187-
} else {
188-
Cow::Borrowed("")
189-
},
183+
"{}🟢 {}{}\n{}",
184+
format_if!(
185+
self.params.base.option.platform_name,
186+
"[{}] ",
187+
source.platform.display_name
188+
),
189+
format_if!(
190+
self.params.base.option.author_name,
191+
"[{}] ",
192+
live_status.streamer_name
193+
),
190194
live_status.title,
191195
live_status.live_url
192196
))
@@ -213,13 +217,17 @@ impl Notifier {
213217

214218
let message = lagrange::Message::builder()
215219
.text(format!(
216-
"[{}] ✏️ {}{}",
217-
source.platform.display_name,
218-
if self.params.base.option.author_name {
219-
Cow::Owned(format!("[{}] ", live_status.streamer_name))
220-
} else {
221-
Cow::Borrowed("")
222-
},
220+
"{}✏️ {}{}",
221+
format_if!(
222+
self.params.base.option.platform_name,
223+
"[{}] ",
224+
source.platform.display_name
225+
),
226+
format_if!(
227+
self.params.base.option.author_name,
228+
"[{}] ",
229+
live_status.streamer_name
230+
),
223231
live_status.title
224232
))
225233
.mention_all_if(self.params.mention_all, true)
@@ -283,9 +291,11 @@ impl Notifier {
283291
Some(RepostFrom::Recursion(repost_from)) => {
284292
if !post.content.is_empty() {
285293
builder.ref_text("💬 ");
286-
if self.params.base.option.author_name {
287-
builder.ref_text(format!("{}: ", post.user.nickname));
288-
}
294+
builder.ref_text(format_if!(
295+
self.params.base.option.author_name,
296+
"{}: ",
297+
post.user.nickname
298+
));
289299
append_media(&mut builder, post.attachments(false));
290300
builder.ref_text(post.content.fallback());
291301
builder.ref_text("\n\n");
@@ -297,9 +307,11 @@ impl Notifier {
297307
builder.ref_text(repost_from.content.fallback());
298308
}
299309
None => {
300-
if self.params.base.option.author_name {
301-
builder.ref_text(format!("{}: ", post.user.nickname));
302-
}
310+
builder.ref_text(format_if!(
311+
self.params.base.option.author_name,
312+
"{}: ",
313+
post.user.nickname
314+
));
303315
append_media(&mut builder, post.attachments(false));
304316
builder.ref_text(post.content.fallback());
305317
}

src/platform/telegram/notify/mod.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use tokio::sync::Mutex;
1919
use super::{ConfigChat, ConfigToken};
2020
use crate::{
2121
config::{self, Accessor, AsSecretRef, Config, Overridable, Validator},
22-
helper,
22+
format_if, helper,
2323
notify::NotifierTrait,
2424
platform::{PlatformMetadata, PlatformTrait},
2525
source::{
@@ -231,6 +231,7 @@ impl Notifier {
231231
let start_time = start_time.unwrap_or_else(SystemTime::now);
232232

233233
let text = make_live_text(
234+
self.params.base.option.platform_name,
234235
self.params.base.option.author_name,
235236
&title_history,
236237
live_status,
@@ -272,6 +273,7 @@ impl Notifier {
272273
let token = self.token()?;
273274

274275
let text = make_live_text(
276+
self.params.base.option.platform_name,
275277
self.params.base.option.author_name,
276278
&current_live.title_history,
277279
live_status,
@@ -319,13 +321,17 @@ impl Notifier {
319321

320322
let text = Text::link(
321323
format!(
322-
"[{}] ✏️ {}{}",
323-
source.platform.display_name,
324-
if self.params.base.option.author_name {
325-
Cow::Owned(format!("[{}] ", live_status.streamer_name))
326-
} else {
327-
Cow::Borrowed("")
328-
},
324+
"{}✏️ {}{}",
325+
format_if!(
326+
self.params.base.option.platform_name,
327+
"[{}] ",
328+
source.platform.display_name
329+
),
330+
format_if!(
331+
self.params.base.option.author_name,
332+
"[{}] ",
333+
live_status.streamer_name
334+
),
329335
live_status.title
330336
),
331337
&live_status.live_url,
@@ -362,6 +368,7 @@ impl Notifier {
362368
.push_front(live_status.title.clone());
363369

364370
let text = make_live_text(
371+
self.params.base.option.platform_name,
365372
self.params.base.option.author_name,
366373
&current_live.title_history,
367374
live_status,
@@ -412,7 +419,11 @@ impl Notifier {
412419
post: &Post,
413420
source: &StatusSource,
414421
) -> anyhow::Result<()> {
415-
let mut text = Text::plain(format!("[{}] ", source.platform.display_name));
422+
let mut text = Text::plain(format_if!(
423+
self.params.base.option.platform_name,
424+
"[{}] ",
425+
source.platform.display_name
426+
));
416427

417428
match &post.repost_from {
418429
Some(RepostFrom::Recursion(repost_from)) => {
@@ -626,6 +637,7 @@ impl Notifier {
626637
.send_message(
627638
&self.params.chat,
628639
make_file_text(
640+
self.params.base.option.platform_name,
629641
self.params.base.option.author_name,
630642
FileUploadStage::PlaybackUploading,
631643
&playback.file,
@@ -664,6 +676,7 @@ impl Notifier {
664676
}),
665677
)
666678
.text(make_file_text(
679+
self.params.base.option.platform_name,
667680
self.params.base.option.author_name,
668681
FileUploadStage::PlaybackFinished,
669682
&playback.file,
@@ -696,6 +709,7 @@ impl Notifier {
696709
&self.params.chat,
697710
message_id,
698711
make_file_text(
712+
self.params.base.option.platform_name,
699713
self.params.base.option.author_name,
700714
FileUploadStage::PlaybackFailed,
701715
&playback.file,
@@ -739,6 +753,7 @@ impl Notifier {
739753
},
740754
)
741755
.text(make_file_text(
756+
self.params.base.option.platform_name,
742757
self.params.base.option.author_name,
743758
FileUploadStage::MetadataFinished,
744759
&document.file,
@@ -761,25 +776,22 @@ impl Notifier {
761776
}
762777

763778
fn make_live_text<'a>(
779+
platform_name: bool,
764780
author_name: bool,
765781
title_history: impl IntoIterator<Item = &'a String>,
766782
live_status: &'a LiveStatus,
767783
source: &StatusSource,
768784
start_time: SystemTime,
769785
) -> Text<'a> {
770786
let text = format!(
771-
"[{}] {} {}{}{}",
772-
source.platform.display_name,
787+
"{}{} {}{}{}",
788+
format_if!(platform_name, "[{}] ", source.platform.display_name),
773789
match live_status.kind {
774790
LiveStatusKind::Online { start_time: _ } => "🟢",
775791
LiveStatusKind::Offline => "🟠",
776792
LiveStatusKind::Banned => "🔴",
777793
},
778-
if author_name {
779-
Cow::Owned(format!("[{}] ", live_status.streamer_name))
780-
} else {
781-
Cow::Borrowed("")
782-
},
794+
format_if!(author_name, "[{}] ", live_status.streamer_name),
783795
itertools::join(title_history, " ⬅️ "),
784796
if live_status.kind == LiveStatusKind::Offline || live_status.kind == LiveStatusKind::Banned
785797
{
@@ -803,6 +815,7 @@ enum FileUploadStage {
803815
}
804816

805817
fn make_file_text<'a>(
818+
platform_name: bool,
806819
_author_name: bool,
807820
stage: FileUploadStage,
808821
file: &FileRef<'a>,
@@ -816,8 +829,9 @@ fn make_file_text<'a>(
816829
};
817830
// TODO: Append author_name
818831
let mut text = Text::plain(format!(
819-
"[{}] {emoji} {}",
820-
source.platform.display_name, file.name,
832+
"{}{emoji} {}",
833+
format_if!(platform_name, "[{}] ", source.platform.display_name),
834+
file.name,
821835
));
822836
match stage {
823837
FileUploadStage::PlaybackUploading | FileUploadStage::PlaybackFailed => {

0 commit comments

Comments
 (0)