-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
85 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod prop; | ||
mod push_notifier; | ||
mod push_register; | ||
|
||
pub use prop::*; | ||
pub use push_notifier::push_notifier; | ||
pub use push_register::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::xml::multistatus::PropstatElement; | ||
use actix_web::http::StatusCode; | ||
use rustical_store::{CollectionOperation, CollectionOperationType, SubscriptionStore}; | ||
use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot}; | ||
use std::sync::Arc; | ||
use tokio::sync::mpsc::Receiver; | ||
use tracing::{error, info}; | ||
|
||
#[derive(XmlSerialize, Debug)] | ||
struct PushMessageProp { | ||
#[xml(ns = "crate::namespace::NS_DAV")] | ||
topic: String, | ||
#[xml(ns = "crate::namespace::NS_DAV")] | ||
sync_token: Option<String>, | ||
} | ||
|
||
#[derive(XmlSerialize, XmlRootTag, Debug)] | ||
#[xml(root = b"push-message", ns = "crate::namespace::NS_DAVPUSH")] | ||
#[xml(ns_prefix(crate::namespace::NS_DAVPUSH = b"", crate::namespace::NS_DAV = b"D",))] | ||
struct PushMessage { | ||
#[xml(ns = "crate::namespace::NS_DAV")] | ||
propstat: PropstatElement<PushMessageProp>, | ||
} | ||
|
||
pub async fn push_notifier( | ||
mut recv: Receiver<CollectionOperation>, | ||
sub_store: Arc<impl SubscriptionStore>, | ||
) { | ||
while let Some(message) = recv.recv().await { | ||
if let Ok(subscribers) = sub_store.get_subscriptions(&message.topic).await { | ||
let status = match message.r#type { | ||
CollectionOperationType::Object => StatusCode::OK, | ||
CollectionOperationType::Delete => StatusCode::NOT_FOUND, | ||
}; | ||
let push_message = PushMessage { | ||
propstat: PropstatElement { | ||
prop: PushMessageProp { | ||
topic: message.topic, | ||
sync_token: message.sync_token, | ||
}, | ||
status, | ||
}, | ||
}; | ||
let mut output: Vec<_> = b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".into(); | ||
let mut writer = quick_xml::Writer::new_with_indent(&mut output, b' ', 4); | ||
if let Err(err) = push_message.serialize_root(&mut writer) { | ||
error!("Could not serialize push message: {}", err); | ||
continue; | ||
} | ||
let payload = String::from_utf8(output).unwrap(); | ||
for subscriber in subscribers { | ||
info!( | ||
"Sending a push message to {}: {}", | ||
subscriber.push_resource, payload | ||
); | ||
let client = reqwest::Client::new(); | ||
if let Err(err) = client | ||
.post(subscriber.push_resource) | ||
.body(payload.to_owned()) | ||
.send() | ||
.await | ||
{ | ||
error!("{err}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters