-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsubscription.rs
More file actions
276 lines (256 loc) · 10.7 KB
/
subscription.rs
File metadata and controls
276 lines (256 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::listener::{ListenerEvent, ListenerHandle};
use crate::models::{self, ReceivedMessage};
use crate::{Error, SharedEnv};
use tokio::select;
use tokio::sync::{broadcast, mpsc, oneshot};
use tokio::task::spawn_local;
use tracing::{debug, error, info, trace, warn};
#[derive(Debug)]
enum SubscriptionCommand {
GetModel {
resp_tx: oneshot::Sender<models::Subscription>,
},
UpdateInfo {
new_model: models::Subscription,
resp_tx: oneshot::Sender<anyhow::Result<()>>,
},
Attach {
resp_tx: oneshot::Sender<(Vec<ListenerEvent>, broadcast::Receiver<ListenerEvent>)>,
},
Publish {
msg: String,
resp_tx: oneshot::Sender<anyhow::Result<()>>,
},
ClearNotifications {
resp_tx: oneshot::Sender<anyhow::Result<()>>,
},
UpdateReadUntil {
timestamp: u64,
resp_tx: oneshot::Sender<anyhow::Result<()>>,
},
}
#[derive(Clone)]
pub struct SubscriptionHandle {
command_tx: mpsc::Sender<SubscriptionCommand>,
listener: ListenerHandle,
}
impl SubscriptionHandle {
pub fn new(listener: ListenerHandle, model: models::Subscription, env: &SharedEnv) -> Self {
let (command_tx, command_rx) = mpsc::channel(32);
let broadcast_tx = broadcast::channel(8).0;
let actor = SubscriptionActor {
listener: listener.clone(),
model,
command_rx,
env: env.clone(),
broadcast_tx: broadcast_tx.clone(),
};
spawn_local(actor.run());
Self {
command_tx,
listener,
}
}
pub async fn model(&self) -> models::Subscription {
let (resp_tx, resp_rx) = oneshot::channel();
self.command_tx
.send(SubscriptionCommand::GetModel { resp_tx })
.await
.unwrap();
resp_rx.await.unwrap()
}
pub async fn update_info(&self, new_model: models::Subscription) -> anyhow::Result<()> {
let (resp_tx, resp_rx) = oneshot::channel();
self.command_tx
.send(SubscriptionCommand::UpdateInfo { new_model, resp_tx })
.await?;
resp_rx.await.unwrap()
}
pub async fn restart(&self) -> anyhow::Result<()> {
self.listener
.commands
.send(crate::ListenerCommand::Restart)
.await?;
Ok(())
}
pub async fn shutdown(&self) -> anyhow::Result<()> {
self.listener
.commands
.send(crate::ListenerCommand::Shutdown)
.await?;
Ok(())
}
// returns a vector containing all the past messages stored in the database and the current connection state.
// The first vector is useful to get a summary of what happened before.
// The `ListenerHandle` is returned to receive new events.
pub async fn attach(&self) -> (Vec<ListenerEvent>, broadcast::Receiver<ListenerEvent>) {
let (resp_tx, resp_rx) = oneshot::channel();
self.command_tx
.send(SubscriptionCommand::Attach { resp_tx })
.await
.unwrap();
resp_rx.await.unwrap()
}
pub async fn publish(&self, msg: String) -> anyhow::Result<()> {
let (resp_tx, resp_rx) = oneshot::channel();
self.command_tx
.send(SubscriptionCommand::Publish { msg, resp_tx })
.await
.unwrap();
resp_rx.await.unwrap()
}
pub async fn clear_notifications(&self) -> anyhow::Result<()> {
let (resp_tx, resp_rx) = oneshot::channel();
self.command_tx
.send(SubscriptionCommand::ClearNotifications { resp_tx })
.await
.unwrap();
resp_rx.await.unwrap()
}
pub async fn update_read_until(&self, timestamp: u64) -> anyhow::Result<()> {
let (resp_tx, resp_rx) = oneshot::channel();
self.command_tx
.send(SubscriptionCommand::UpdateReadUntil { timestamp, resp_tx })
.await
.unwrap();
resp_rx.await.unwrap()
}
}
struct SubscriptionActor {
listener: ListenerHandle,
model: models::Subscription,
command_rx: mpsc::Receiver<SubscriptionCommand>,
env: SharedEnv,
broadcast_tx: broadcast::Sender<ListenerEvent>,
}
impl SubscriptionActor {
async fn run(mut self) {
loop {
select! {
Ok(event) = self.listener.events.recv() => {
debug!(?event, "received listener event");
match event {
ListenerEvent::Message(msg) => self.handle_msg_event(msg),
other => {
let _ = self.broadcast_tx.send(other);
}
}
}
Some(command) = self.command_rx.recv() => {
trace!(?command, "processing subscription command");
match command {
SubscriptionCommand::GetModel { resp_tx } => {
debug!("getting subscription model");
let _ = resp_tx.send(self.model.clone());
}
SubscriptionCommand::UpdateInfo {
mut new_model,
resp_tx,
} => {
debug!(server=?new_model.server, topic=?new_model.topic, "updating subscription info");
new_model.server = self.model.server.clone();
new_model.topic = self.model.topic.clone();
let res = self.env.db.update_subscription(new_model.clone());
if let Ok(_) = res {
self.model = new_model;
}
let _ = resp_tx.send(res.map_err(|e| e.into()));
}
SubscriptionCommand::Publish {msg, resp_tx} => {
debug!(topic=?self.model.topic, "publishing message");
let _ = resp_tx.send(self.publish(msg).await);
}
SubscriptionCommand::Attach { resp_tx } => {
debug!(topic=?self.model.topic, "attaching new listener");
let messages = self
.env
.db
.list_messages(&self.model.server, &self.model.topic, 0)
.unwrap_or_default();
let mut previous_events: Vec<ListenerEvent> = messages
.into_iter()
.filter_map(|msg| {
let msg = serde_json::from_str(&msg);
match msg {
Err(e) => {
error!(error = ?e, "error parsing stored message");
None
}
Ok(msg) => Some(msg),
}
})
.map(ListenerEvent::Message)
.collect();
previous_events.push(ListenerEvent::ConnectionStateChanged(self.listener.state().await));
let _ = resp_tx.send((previous_events, self.broadcast_tx.subscribe()));
}
SubscriptionCommand::ClearNotifications {resp_tx} => {
debug!(topic=?self.model.topic, "clearing notifications");
let _ = resp_tx.send(self.env.db.delete_messages(&self.model.server, &self.model.topic).map_err(|e| anyhow::anyhow!(e)));
}
SubscriptionCommand::UpdateReadUntil { timestamp, resp_tx } => {
debug!(topic=?self.model.topic, timestamp=timestamp, "updating read until timestamp");
let res = self.env.db.update_read_until(&self.model.server, &self.model.topic, timestamp);
let _ = resp_tx.send(res.map_err(|e| anyhow::anyhow!(e)));
}
}
}
}
}
}
async fn publish(&self, msg: String) -> anyhow::Result<()> {
let server = &self.model.server;
debug!(server=?server, "preparing to publish message");
let creds = self.env.credentials.get(server);
let mut req = self.env.http_client.post(server);
if let Some(creds) = creds {
req = req.basic_auth(creds.username, Some(creds.password));
}
info!(server=?server, "sending message");
let res = req.body(msg).send().await?;
res.error_for_status()?;
debug!(server=?server, "message published successfully");
Ok(())
}
fn handle_msg_event(&mut self, msg: ReceivedMessage) {
debug!(topic=?self.model.topic, "handling new message");
// Store in database
let already_stored: bool = {
let json_ev = &serde_json::to_string(&msg).unwrap();
match self.env.db.insert_message(&self.model.server, json_ev) {
Err(Error::DuplicateMessage) => {
warn!(topic=?self.model.topic, "received duplicate message");
true
}
Err(e) => {
error!(error=?e, topic=?self.model.topic, "can't store the message");
false
}
_ => {
debug!(topic=?self.model.topic, "message stored successfully");
false
}
}
};
if !already_stored {
debug!(topic=?self.model.topic, muted=?self.model.muted, "checking if notification should be shown");
// Show notification. If this fails, panic
if !{ self.model.muted } {
let notifier = self.env.notifier.clone();
let title = { msg.notification_title(&self.model) };
let n = models::Notification {
title,
body: msg.display_message().as_deref().unwrap_or("").to_string(),
actions: msg.actions.clone(),
};
debug!(topic=?self.model.topic, "sending notification through proxy");
notifier.send(n).unwrap();
} else {
debug!(topic=?self.model.topic, "notification muted, skipping");
}
// Forward to app
debug!(topic=?self.model.topic, "forwarding message to app");
let _ = self.broadcast_tx.send(ListenerEvent::Message(msg));
}
}
}