Skip to content

Commit b3d0aaf

Browse files
committed
minor refactors, add OutgoingMessage
1 parent a758ffc commit b3d0aaf

File tree

10 files changed

+184
-208
lines changed

10 files changed

+184
-208
lines changed

ntfy-daemon/src/actor_utils.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! send_command {
2+
($self:expr, $command:expr) => {{
3+
let (resp_tx, rx) = oneshot::channel();
4+
$self
5+
.command_tx
6+
.send($command(resp_tx))
7+
.await
8+
.map_err(|_| anyhow::anyhow!("Actor mailbox error"))?;
9+
rx.await
10+
.map_err(|_| anyhow::anyhow!("Actor response error"))?
11+
}};
12+
}
13+
14+
pub(crate) use send_command;

ntfy-daemon/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod actor_utils;
12
pub mod credentials;
23
mod http_client;
34
mod listener;
@@ -31,6 +32,8 @@ pub enum Error {
3132
InvalidTopic(String),
3233
#[error("invalid server base url {0:?}")]
3334
InvalidServer(#[from] url::ParseError),
35+
#[error("multiple errors in subscription model: {0:?}")]
36+
InvalidSubscription(Vec<Error>),
3437
#[error("duplicate message")]
3538
DuplicateMessage,
3639
#[error("can't parse the minimum set of required fields from the message {0}")]

ntfy-daemon/src/listener.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub enum ServerEvent {
3636
topic: String,
3737
},
3838
#[serde(rename = "message")]
39-
Message(models::Message),
39+
Message(models::ReceivedMessage),
4040
#[serde(rename = "keepalive")]
4141
KeepAlive {
4242
id: String,
@@ -48,7 +48,7 @@ pub enum ServerEvent {
4848

4949
#[derive(Debug, Clone)]
5050
pub enum ListenerEvent {
51-
Message(models::Message),
51+
Message(models::ReceivedMessage),
5252
ConnectionStateChanged(ConnectionState),
5353
}
5454

@@ -281,7 +281,7 @@ impl ListenerHandle {
281281
}
282282

283283
// the response will be sent as an event in self.events
284-
pub async fn request_state(&self) -> ConnectionState {
284+
pub async fn state(&self) -> ConnectionState {
285285
let (tx, rx) = oneshot::channel();
286286
self.commands
287287
.send(ListenerCommand::GetState(tx))
@@ -300,20 +300,6 @@ mod tests {
300300

301301
use super::*;
302302

303-
// takes a list of pattern matches. It recvs events and then matches them
304-
// against the macro parameters
305-
macro_rules! assert_event_matches {
306-
($listener:expr, $( $pattern:pat_param ),+ $(,)?) => {
307-
$(
308-
$listener.events.changed().await.unwrap();
309-
let event = $listener.events.borrow().clone();
310-
311-
panic!("{:?}", &event);
312-
assert!(matches!(event, $pattern));
313-
)+
314-
};
315-
}
316-
317303
#[tokio::test]
318304
async fn test_listener_reconnects_on_http_status_500() {
319305
let local_set = LocalSet::new();

ntfy-daemon/src/models.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn validate_topic(topic: &str) -> Result<&str, Error> {
2727
}
2828

2929
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
30-
pub struct Message {
30+
pub struct ReceivedMessage {
3131
pub id: String,
3232
pub topic: String,
3333
pub expires: Option<u64>,
@@ -59,7 +59,7 @@ pub struct Message {
5959
pub actions: Vec<Action>,
6060
}
6161

62-
impl Message {
62+
impl ReceivedMessage {
6363
fn extend_with_emojis(&self, text: &mut String) {
6464
// Add emojis
6565
for t in &self.tags {
@@ -107,6 +107,37 @@ impl Message {
107107
}
108108
}
109109

110+
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
111+
pub struct OutgoingMessage {
112+
pub topic: String,
113+
pub message: Option<String>,
114+
#[serde(default = "Default::default")]
115+
pub time: u64,
116+
#[serde(skip_serializing_if = "Option::is_none")]
117+
pub title: Option<String>,
118+
#[serde(default)]
119+
#[serde(skip_serializing_if = "Vec::is_empty")]
120+
pub tags: Vec<String>,
121+
#[serde(skip_serializing_if = "Option::is_none")]
122+
pub priority: Option<i8>,
123+
#[serde(skip_serializing_if = "Option::is_none")]
124+
#[serde(default)]
125+
pub attachment: Option<Attachment>,
126+
#[serde(skip_serializing_if = "Option::is_none")]
127+
pub icon: Option<String>,
128+
#[serde(skip_serializing_if = "Option::is_none")]
129+
pub filename: Option<String>,
130+
#[serde(skip_serializing_if = "Option::is_none")]
131+
pub delay: Option<usize>,
132+
#[serde(skip_serializing_if = "Option::is_none")]
133+
pub email: Option<String>,
134+
#[serde(skip_serializing_if = "Option::is_none")]
135+
pub call: Option<String>,
136+
#[serde(default)]
137+
#[serde(skip_serializing_if = "Vec::is_empty")]
138+
pub actions: Vec<Action>,
139+
}
140+
110141
#[derive(Clone, Debug, Serialize, Deserialize)]
111142
pub struct MinMessage {
112143
pub id: String,
@@ -167,7 +198,7 @@ impl Subscription {
167198
.push("auth");
168199
Ok(url)
169200
}
170-
pub fn validate(self) -> Result<Self, Vec<crate::Error>> {
201+
pub fn validate(self) -> Result<Self, crate::Error> {
171202
let mut errs = vec![];
172203
if let Err(e) = validate_topic(&self.topic) {
173204
errs.push(e);
@@ -176,7 +207,7 @@ impl Subscription {
176207
errs.push(e);
177208
};
178209
if !errs.is_empty() {
179-
return Err(errs);
210+
return Err(Error::InvalidSubscription(errs));
180211
}
181212
Ok(self)
182213
}
@@ -239,7 +270,7 @@ impl SubscriptionBuilder {
239270
self
240271
}
241272

242-
pub fn build(self) -> Result<Subscription, Vec<Error>> {
273+
pub fn build(self) -> Result<Subscription, Error> {
243274
let res = Subscription {
244275
server: self.server,
245276
topic: self.topic,

0 commit comments

Comments
 (0)