Skip to content

Commit 98e2395

Browse files
committed
Decouple config::Notifications into switches and options
1 parent 4bb38bb commit 98e2395

File tree

3 files changed

+129
-63
lines changed

3 files changed

+129
-63
lines changed

src/config/mod.rs

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,46 @@ pub struct SubscriptionRef<'a> {
189189
pub notify: Vec<Accessor<NotifierConfig>>,
190190
}
191191

192+
// Should be always used with `#[serde(flatten)]`
192193
#[derive(Clone, Debug, PartialEq, Deserialize)]
193-
pub struct Notifications {
194-
// Toggles
194+
pub struct NotifierBase {
195+
#[serde(default)]
196+
pub switch: NotificationSwitch,
197+
#[serde(default)]
198+
pub option: NotificationOption,
199+
}
200+
201+
serde_impl_default_for!(NotifierBase);
202+
203+
impl Overridable for NotifierBase {
204+
type Override = NotifierBaseOverride;
205+
206+
fn override_into(self, new: Self::Override) -> Self
207+
where
208+
Self: Sized,
209+
{
210+
Self {
211+
switch: match new.switch {
212+
Some(switch) => self.switch.override_into(switch),
213+
None => self.switch,
214+
},
215+
option: match new.option {
216+
Some(option) => self.option.override_into(option),
217+
None => self.option,
218+
},
219+
}
220+
}
221+
}
222+
223+
// Should be always used with `#[serde(flatten)]`
224+
#[derive(Clone, Debug, PartialEq, Deserialize)]
225+
pub struct NotifierBaseOverride {
226+
switch: Option<NotificationSwitchOverride>,
227+
option: Option<NotificationOptionOverride>,
228+
}
229+
230+
#[derive(Clone, Debug, PartialEq, Deserialize)]
231+
pub struct NotificationSwitch {
195232
#[serde(default = "helper::refl_bool::<true>")]
196233
pub live_online: bool,
197234
#[serde(default = "helper::refl_bool::<false>")]
@@ -204,16 +241,12 @@ pub struct Notifications {
204241
pub playback: bool,
205242
#[serde(default = "helper::refl_bool::<true>")]
206243
pub document: bool,
207-
208-
// Options
209-
#[serde(default = "helper::refl_bool::<false>")]
210-
pub author_name: bool,
211244
}
212245

213-
serde_impl_default_for!(Notifications);
246+
serde_impl_default_for!(NotificationSwitch);
214247

215-
impl Overridable for Notifications {
216-
type Override = NotificationsOverride;
248+
impl Overridable for NotificationSwitch {
249+
type Override = NotificationSwitchOverride;
217250

218251
fn override_into(self, new: Self::Override) -> Self
219252
where
@@ -226,19 +259,43 @@ impl Overridable for Notifications {
226259
log: new.log.unwrap_or(self.log),
227260
playback: new.playback.unwrap_or(self.playback),
228261
document: new.document.unwrap_or(self.document),
229-
author_name: new.author_name.unwrap_or(self.author_name),
230262
}
231263
}
232264
}
233265

234266
#[derive(Clone, Debug, PartialEq, Deserialize)]
235-
pub struct NotificationsOverride {
267+
pub struct NotificationSwitchOverride {
236268
pub live_online: Option<bool>,
237269
pub live_title: Option<bool>,
238270
pub post: Option<bool>,
239271
pub log: Option<bool>,
240272
pub playback: Option<bool>,
241273
pub document: Option<bool>,
274+
}
275+
276+
#[derive(Clone, Debug, PartialEq, Deserialize)]
277+
pub struct NotificationOption {
278+
#[serde(default = "helper::refl_bool::<false>")]
279+
pub author_name: bool,
280+
}
281+
282+
serde_impl_default_for!(NotificationOption);
283+
284+
impl Overridable for NotificationOption {
285+
type Override = NotificationOptionOverride;
286+
287+
fn override_into(self, new: Self::Override) -> Self
288+
where
289+
Self: Sized,
290+
{
291+
Self {
292+
author_name: new.author_name.unwrap_or(self.author_name),
293+
}
294+
}
295+
}
296+
297+
#[derive(Clone, Debug, PartialEq, Deserialize)]
298+
pub struct NotificationOptionOverride {
242299
pub author_name: Option<bool>,
243300
}
244301

@@ -318,7 +375,7 @@ playback = { bililive_recorder = { listen_webhook = { host = "127.0.0.1", port =
318375
319376
[notify]
320377
meow = { platform = "Telegram", id = 1234, thread_id = 123, token = "xxx" }
321-
woof = { platform = "Telegram", id = 5678, thread_id = 900, notifications = { post = false } }
378+
woof = { platform = "Telegram", id = 5678, thread_id = 900, switch = { post = false } }
322379
323380
[[subscription.meow]]
324381
platform = { name = "bilibili.live", user_id = 123456 }
@@ -385,7 +442,7 @@ notify = ["meow", "woof", { ref = "woof", id = 123 }]
385442
(
386443
"meow".into(),
387444
Accessor::new(NotifierConfig::Telegram(Accessor::new(telegram::notify::ConfigParams {
388-
notifications: Notifications::default(),
445+
base: NotifierBase::default(),
389446
chat: telegram::ConfigChat::Id(1234),
390447
thread_id: Some(123),
391448
token: Some(telegram::ConfigToken::with_raw("xxx")),
@@ -394,14 +451,18 @@ notify = ["meow", "woof", { ref = "woof", id = 123 }]
394451
(
395452
"woof".into(),
396453
Accessor::new(NotifierConfig::Telegram(Accessor::new(telegram::notify::ConfigParams {
397-
notifications: Notifications {
398-
live_online: true,
399-
live_title: false,
400-
post: false,
401-
log: true,
402-
playback: true,
403-
document: true,
404-
author_name: false,
454+
base: NotifierBase {
455+
switch: NotificationSwitch {
456+
live_online: true,
457+
live_title: false,
458+
post: false,
459+
log: true,
460+
playback: true,
461+
document: true,
462+
},
463+
option: NotificationOption {
464+
author_name: false,
465+
}
405466
},
406467
chat: telegram::ConfigChat::Id(5678),
407468
thread_id: Some(900),
@@ -572,7 +633,7 @@ woof = { platform = "Telegram", id = 5678, thread_id = 456, token = "yyy" }
572633
573634
[[subscription.meow]]
574635
platform = { name = "bilibili.live", user_id = 123456 }
575-
notify = ["meow", { ref = "woof", thread_id = 114 }, { ref = "woof", notifications = { post = false } }]
636+
notify = ["meow", { ref = "woof", thread_id = 114 }, { ref = "woof", switch = { post = false } }]
576637
"#,
577638
|c| {
578639
let subscriptions = c.unwrap().subscriptions().collect::<Vec<_>>();
@@ -589,25 +650,28 @@ notify = ["meow", { ref = "woof", thread_id = 114 }, { ref = "woof", notificatio
589650
notify: vec![
590651
Accessor::new(NotifierConfig::Telegram(Accessor::new(
591652
telegram::notify::ConfigParams {
592-
notifications: Notifications::default(),
653+
base: NotifierBase::default(),
593654
chat: telegram::ConfigChat::Id(1234),
594655
thread_id: Some(123),
595656
token: Some(telegram::ConfigToken::with_raw("xxx")),
596657
}
597658
))),
598659
Accessor::new(NotifierConfig::Telegram(Accessor::new(
599660
telegram::notify::ConfigParams {
600-
notifications: Notifications::default(),
661+
base: NotifierBase::default(),
601662
chat: telegram::ConfigChat::Id(5678),
602663
thread_id: Some(114),
603664
token: Some(telegram::ConfigToken::with_raw("yyy")),
604665
}
605666
))),
606667
Accessor::new(NotifierConfig::Telegram(Accessor::new(
607668
telegram::notify::ConfigParams {
608-
notifications: Notifications {
609-
post: false,
610-
..Default::default()
669+
base: NotifierBase {
670+
switch: NotificationSwitch {
671+
post: false,
672+
..Default::default()
673+
},
674+
option: NotificationOption::default()
611675
},
612676
chat: telegram::ConfigChat::Id(5678),
613677
thread_id: Some(456),

src/platform/qq/notify/mod.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::{
1717

1818
#[derive(Clone, Debug, PartialEq, Deserialize)]
1919
pub struct ConfigParams {
20-
#[serde(default)]
21-
pub notifications: config::Notifications,
20+
#[serde(default, flatten)]
21+
pub base: config::NotifierBase,
2222
#[serde(flatten)]
2323
pub chat: ConfigChat,
2424
#[serde(default)]
@@ -55,7 +55,8 @@ impl fmt::Display for ConfigParams {
5555
#[derive(Clone, Debug, PartialEq, Deserialize)]
5656
#[serde(deny_unknown_fields)]
5757
pub struct ConfigOverride {
58-
pub notifications: Option<config::NotificationsOverride>,
58+
#[serde(flatten)]
59+
pub base: Option<config::NotifierBaseOverride>,
5960
#[serde(flatten)]
6061
pub chat: Option<ConfigChat>,
6162
pub mention_all: Option<bool>,
@@ -71,9 +72,9 @@ impl Overridable for ConfigParams {
7172
Self: Sized,
7273
{
7374
Self {
74-
notifications: match new.notifications {
75-
Some(notifications) => self.notifications.override_into(notifications),
76-
None => self.notifications,
75+
base: match new.base {
76+
Some(base) => self.base.override_into(base),
77+
None => self.base,
7778
},
7879
chat: new.chat.unwrap_or(self.chat),
7980
mention_all: new.mention_all.unwrap_or(self.mention_all),
@@ -143,7 +144,7 @@ impl Notifier {
143144
live_status: &LiveStatus,
144145
source: &StatusSource,
145146
) -> anyhow::Result<()> {
146-
if !self.params.notifications.live_online {
147+
if !self.params.base.switch.live_online {
147148
info!("live_online notification is disabled, skip notifying");
148149
return Ok(());
149150
}
@@ -154,7 +155,7 @@ impl Notifier {
154155
.text(format!(
155156
"[{}] 🟢 {}{}\n{}",
156157
source.platform.display_name,
157-
if self.params.notifications.author_name {
158+
if self.params.base.option.author_name {
158159
Cow::Owned(format!("[{}] ", live_status.streamer_name))
159160
} else {
160161
Cow::Borrowed("")
@@ -178,7 +179,7 @@ impl Notifier {
178179
live_status: &LiveStatus,
179180
source: &StatusSource,
180181
) -> anyhow::Result<()> {
181-
if !self.params.notifications.live_title {
182+
if !self.params.base.switch.live_title {
182183
info!("live_title notification is disabled, skip notifying");
183184
return Ok(());
184185
}
@@ -187,7 +188,7 @@ impl Notifier {
187188
.text(format!(
188189
"[{}] ✏️ {}{}",
189190
source.platform.display_name,
190-
if self.params.notifications.author_name {
191+
if self.params.base.option.author_name {
191192
Cow::Owned(format!("[{}] ", live_status.streamer_name))
192193
} else {
193194
Cow::Borrowed("")
@@ -209,7 +210,7 @@ impl Notifier {
209210
posts: &PostsRef<'_>,
210211
source: &StatusSource,
211212
) -> anyhow::Result<()> {
212-
if !self.params.notifications.post {
213+
if !self.params.base.switch.post {
213214
info!("post notification is disabled, skip notifying");
214215
return Ok(());
215216
}
@@ -242,7 +243,7 @@ impl Notifier {
242243
Some(RepostFrom::Recursion(repost_from)) => {
243244
if !post.content.is_empty() {
244245
builder.ref_text("💬 ");
245-
if self.params.notifications.author_name {
246+
if self.params.base.option.author_name {
246247
builder.ref_text(format!("{}: ", post.user.nickname));
247248
}
248249
append_media(&mut builder, post.attachments(false));
@@ -256,7 +257,7 @@ impl Notifier {
256257
builder.ref_text(repost_from.content.fallback());
257258
}
258259
None => {
259-
if self.params.notifications.author_name {
260+
if self.params.base.option.author_name {
260261
builder.ref_text(format!("{}: ", post.user.nickname));
261262
}
262263
append_media(&mut builder, post.attachments(false));
@@ -285,7 +286,7 @@ impl Notifier {
285286
}
286287

287288
async fn notify_log(&self, message: &str) -> anyhow::Result<()> {
288-
if !self.params.notifications.log {
289+
if !self.params.base.switch.log {
289290
info!("log notification is disabled, skip notifying");
290291
return Ok(());
291292
}

0 commit comments

Comments
 (0)