mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 08:12:24 +00:00
dav: Make reusable for other projects
This commit is contained in:
37
crates/dav_push/src/extension.rs
Normal file
37
crates/dav_push/src/extension.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use crate::Transports;
|
||||
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
|
||||
|
||||
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
|
||||
#[xml(unit_variants_ident = "DavPushExtensionPropName")]
|
||||
pub enum DavPushExtensionProp {
|
||||
// WebDav Push
|
||||
#[xml(skip_deserializing)]
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
Transports(Transports),
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
Topic(String),
|
||||
}
|
||||
|
||||
pub trait DavPushExtension {
|
||||
fn get_topic(&self) -> String;
|
||||
|
||||
fn get_prop(
|
||||
&self,
|
||||
prop: &DavPushExtensionPropName,
|
||||
) -> Result<DavPushExtensionProp, rustical_dav::Error> {
|
||||
Ok(match &prop {
|
||||
DavPushExtensionPropName::Transports => {
|
||||
DavPushExtensionProp::Transports(Default::default())
|
||||
}
|
||||
DavPushExtensionPropName::Topic => DavPushExtensionProp::Topic(self.get_topic()),
|
||||
})
|
||||
}
|
||||
|
||||
fn set_prop(&self, _prop: DavPushExtensionProp) -> Result<(), rustical_dav::Error> {
|
||||
Err(rustical_dav::Error::PropReadOnly)
|
||||
}
|
||||
|
||||
fn remove_prop(&self, _prop: &DavPushExtensionPropName) -> Result<(), rustical_dav::Error> {
|
||||
Err(rustical_dav::Error::PropReadOnly)
|
||||
}
|
||||
}
|
||||
7
crates/dav_push/src/lib.rs
Normal file
7
crates/dav_push/src/lib.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod extension;
|
||||
pub mod notifier;
|
||||
mod prop;
|
||||
pub mod register;
|
||||
|
||||
pub use extension::*;
|
||||
pub use prop::*;
|
||||
100
crates/dav_push/src/notifier.rs
Normal file
100
crates/dav_push/src/notifier.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use actix_web::http::StatusCode;
|
||||
use rustical_dav::xml::multistatus::PropstatElement;
|
||||
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, warn};
|
||||
|
||||
#[derive(XmlSerialize, Debug)]
|
||||
struct PushMessageProp {
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
|
||||
topic: String,
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
|
||||
sync_token: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(XmlSerialize, XmlRootTag, Debug)]
|
||||
#[xml(root = b"push-message", ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
#[xml(ns_prefix(
|
||||
rustical_dav::namespace::NS_DAVPUSH = b"",
|
||||
rustical_dav::namespace::NS_DAV = b"D",
|
||||
))]
|
||||
struct PushMessage {
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
|
||||
propstat: PropstatElement<PushMessageProp>,
|
||||
}
|
||||
|
||||
pub async fn push_notifier(
|
||||
allowed_push_servers: Option<Vec<String>>,
|
||||
mut recv: Receiver<CollectionOperation>,
|
||||
sub_store: Arc<impl SubscriptionStore>,
|
||||
) {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
while let Some(message) = recv.recv().await {
|
||||
let subscribers = match sub_store.get_subscriptions(&message.topic).await {
|
||||
Ok(subs) => subs,
|
||||
Err(err) => {
|
||||
error!("{err}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
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 {
|
||||
let push_resource = subscriber.push_resource;
|
||||
let allowed = if let Some(allowed_push_servers) = &allowed_push_servers {
|
||||
if let Ok(resource_url) = reqwest::Url::parse(&push_resource) {
|
||||
let origin = resource_url.origin().ascii_serialization();
|
||||
allowed_push_servers
|
||||
.iter()
|
||||
.any(|allowed_push_server| allowed_push_server == &origin)
|
||||
} else {
|
||||
warn!("Invalid push url: {push_resource}");
|
||||
false
|
||||
}
|
||||
} else {
|
||||
true
|
||||
};
|
||||
|
||||
if allowed {
|
||||
info!("Sending a push message to {}: {}", push_resource, payload);
|
||||
if let Err(err) = client
|
||||
.post(push_resource)
|
||||
.body(payload.to_owned())
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
error!("{err}");
|
||||
}
|
||||
} else {
|
||||
warn!(
|
||||
"Not sending a push notification to {} since it's not allowed in dav_push::allowed_push_servers",
|
||||
push_resource
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
crates/dav_push/src/prop.rs
Normal file
22
crates/dav_push/src/prop.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use rustical_xml::XmlSerialize;
|
||||
|
||||
#[derive(Debug, Clone, XmlSerialize, PartialEq)]
|
||||
pub enum Transport {
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
WebPush,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, XmlSerialize, PartialEq)]
|
||||
pub struct Transports {
|
||||
#[xml(flatten, ty = "untagged")]
|
||||
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
|
||||
transports: Vec<Transport>,
|
||||
}
|
||||
|
||||
impl Default for Transports {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
transports: vec![Transport::WebPush],
|
||||
}
|
||||
}
|
||||
}
|
||||
59
crates/dav_push/src/register.rs
Normal file
59
crates/dav_push/src/register.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use rustical_xml::{XmlDeserialize, XmlRootTag};
|
||||
|
||||
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
|
||||
#[xml(ns = "crate::namespace::NS_DAVPUSH")]
|
||||
pub struct WebPushSubscription {
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
pub push_resource: String,
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
|
||||
pub struct SubscriptionElement {
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
pub web_push_subscription: WebPushSubscription,
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug, PartialEq)]
|
||||
#[xml(root = b"push-register")]
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
pub struct PushRegister {
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
pub subscription: SubscriptionElement,
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
pub expires: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rustical_xml::XmlDocument;
|
||||
|
||||
#[test]
|
||||
fn test_xml_push_register() {
|
||||
let push_register = PushRegister::parse_str(
|
||||
r#"
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<push-register xmlns="https://bitfire.at/webdav-push">
|
||||
<subscription>
|
||||
<web-push-subscription>
|
||||
<push-resource>https://up.example.net/yohd4yai5Phiz1wi</push-resource>
|
||||
</web-push-subscription>
|
||||
</subscription>
|
||||
<expires>Wed, 20 Dec 2023 10:03:31 GMT</expires>
|
||||
</push-register>
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
push_register,
|
||||
PushRegister {
|
||||
subscription: SubscriptionElement {
|
||||
web_push_subscription: WebPushSubscription {
|
||||
push_resource: "https://up.example.net/yohd4yai5Phiz1wi".to_owned()
|
||||
}
|
||||
},
|
||||
expires: Some("Wed, 20 Dec 2023 10:03:31 GMT".to_owned())
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user