mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 01:12:24 +00:00
caldav: Implement Dav Push topic
This commit is contained in:
@@ -27,3 +27,4 @@ rustical_store = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
rustical_xml.workspace = true
|
||||
uuid.workspace = true
|
||||
|
||||
@@ -68,6 +68,7 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
|
||||
deleted_at: None,
|
||||
synctoken: 0,
|
||||
subscription_url: None,
|
||||
push_topic: uuid::Uuid::new_v4().to_string(),
|
||||
};
|
||||
|
||||
match store.insert_calendar(calendar).await {
|
||||
|
||||
@@ -17,7 +17,6 @@ use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner};
|
||||
use rustical_store::auth::User;
|
||||
use rustical_store::{Calendar, CalendarStore};
|
||||
use rustical_xml::{XmlDeserialize, XmlSerialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||
@@ -36,9 +35,6 @@ pub enum CalendarProp {
|
||||
Getcontenttype(&'static str),
|
||||
|
||||
// WebDav Push
|
||||
// NOTE: Here we implement an older version of the spec since the new property name is not reflected
|
||||
// in DAVx5 yet
|
||||
// https://github.com/bitfireAT/webdav-push/commit/461259a2f2174454b2b00033419b11fac52b79e3
|
||||
#[xml(skip_deserializing)]
|
||||
#[xml(ns = "rustical_dav::namespace::NS_DAVPUSH")]
|
||||
Transports(Transports),
|
||||
@@ -141,16 +137,7 @@ impl Resource for CalendarResource {
|
||||
CalendarProp::Getcontenttype("text/calendar;charset=utf-8")
|
||||
}
|
||||
CalendarPropName::Transports => CalendarProp::Transports(Default::default()),
|
||||
CalendarPropName::Topic => {
|
||||
// TODO: Add salt since this could be public
|
||||
// let url =
|
||||
// CalendarResource::get_url(rmap, [&self.cal.principal, &self.cal.id]).unwrap();
|
||||
let url = "TODO!".to_owned();
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(url);
|
||||
let topic = format!("{:x}", hasher.finalize());
|
||||
CalendarProp::Topic(topic)
|
||||
}
|
||||
CalendarPropName::Topic => CalendarProp::Topic(self.cal.push_topic.to_owned()),
|
||||
CalendarPropName::MaxResourceSize => CalendarProp::MaxResourceSize(10000000),
|
||||
CalendarPropName::SupportedReportSet => {
|
||||
CalendarProp::SupportedReportSet(SupportedReportSet::default())
|
||||
|
||||
@@ -26,3 +26,4 @@ rustical_dav = { workspace = true }
|
||||
rustical_store = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
rustical_xml.workspace = true
|
||||
uuid.workspace = true
|
||||
|
||||
@@ -54,6 +54,7 @@ pub async fn route_mkcol<AS: AddressbookStore + ?Sized>(
|
||||
description: request.description,
|
||||
deleted_at: None,
|
||||
synctoken: 0,
|
||||
push_topic: uuid::Uuid::new_v4().to_string(),
|
||||
};
|
||||
|
||||
match store.get_addressbook(&principal, &addressbook_id).await {
|
||||
|
||||
@@ -10,6 +10,7 @@ pub struct Addressbook {
|
||||
pub description: Option<String>,
|
||||
pub deleted_at: Option<NaiveDateTime>,
|
||||
pub synctoken: i64,
|
||||
pub push_topic: String,
|
||||
}
|
||||
|
||||
impl Addressbook {
|
||||
|
||||
@@ -15,6 +15,7 @@ pub struct Calendar {
|
||||
pub deleted_at: Option<NaiveDateTime>,
|
||||
pub synctoken: i64,
|
||||
pub subscription_url: Option<String>,
|
||||
pub push_topic: String,
|
||||
}
|
||||
|
||||
impl Calendar {
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::{
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::Constructor;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
#[derive(Constructor, Clone)]
|
||||
pub struct ContactBirthdayStore<AS: AddressbookStore + ?Sized>(Arc<AS>);
|
||||
@@ -24,6 +25,12 @@ fn birthday_calendar(addressbook: Addressbook) -> Calendar {
|
||||
deleted_at: addressbook.deleted_at,
|
||||
synctoken: addressbook.synctoken,
|
||||
subscription_url: None,
|
||||
push_topic: {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update("birthdays");
|
||||
hasher.update(addressbook.push_topic);
|
||||
format!("{:x}", hasher.finalize())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ CREATE TABLE calendars (
|
||||
timezone_id TEXT,
|
||||
deleted_at DATETIME,
|
||||
subscription_url TEXT,
|
||||
push_topic TEXT UNIQUE NOT NULL,
|
||||
PRIMARY KEY (principal, id)
|
||||
);
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ CREATE TABLE addressbooks (
|
||||
displayname TEXT,
|
||||
description TEXT,
|
||||
deleted_at DATETIME,
|
||||
push_topic TEXT UNIQUE NOT NULL,
|
||||
PRIMARY KEY (principal, id)
|
||||
);
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ impl AddressbookStore for SqliteStore {
|
||||
) -> Result<Addressbook, rustical_store::Error> {
|
||||
let addressbook = sqlx::query_as!(
|
||||
Addressbook,
|
||||
r#"SELECT principal, id, synctoken, displayname, description, deleted_at
|
||||
r#"SELECT principal, id, synctoken, displayname, description, deleted_at, push_topic
|
||||
FROM addressbooks
|
||||
WHERE (principal, id) = (?, ?)"#,
|
||||
principal,
|
||||
@@ -82,7 +82,7 @@ impl AddressbookStore for SqliteStore {
|
||||
) -> Result<Vec<Addressbook>, rustical_store::Error> {
|
||||
let addressbooks = sqlx::query_as!(
|
||||
Addressbook,
|
||||
r#"SELECT principal, id, synctoken, displayname, description, deleted_at
|
||||
r#"SELECT principal, id, synctoken, displayname, description, deleted_at, push_topic
|
||||
FROM addressbooks
|
||||
WHERE principal = ? AND deleted_at IS NULL"#,
|
||||
principal
|
||||
@@ -100,7 +100,7 @@ impl AddressbookStore for SqliteStore {
|
||||
) -> Result<Vec<Addressbook>, rustical_store::Error> {
|
||||
let addressbooks = sqlx::query_as!(
|
||||
Addressbook,
|
||||
r#"SELECT principal, id, synctoken, displayname, description, deleted_at
|
||||
r#"SELECT principal, id, synctoken, displayname, description, deleted_at, push_topic
|
||||
FROM addressbooks
|
||||
WHERE principal = ? AND deleted_at IS NOT NULL"#,
|
||||
principal
|
||||
@@ -119,12 +119,13 @@ impl AddressbookStore for SqliteStore {
|
||||
addressbook: Addressbook,
|
||||
) -> Result<(), rustical_store::Error> {
|
||||
let result = sqlx::query!(
|
||||
r#"UPDATE addressbooks SET principal = ?, id = ?, displayname = ?, description = ?
|
||||
r#"UPDATE addressbooks SET principal = ?, id = ?, displayname = ?, description = ?, push_topic = ?
|
||||
WHERE (principal, id) = (?, ?)"#,
|
||||
addressbook.principal,
|
||||
addressbook.id,
|
||||
addressbook.displayname,
|
||||
addressbook.description,
|
||||
addressbook.push_topic,
|
||||
principal,
|
||||
id
|
||||
)
|
||||
@@ -143,12 +144,13 @@ impl AddressbookStore for SqliteStore {
|
||||
addressbook: Addressbook,
|
||||
) -> Result<(), rustical_store::Error> {
|
||||
sqlx::query!(
|
||||
r#"INSERT INTO addressbooks (principal, id, displayname, description)
|
||||
VALUES (?, ?, ?, ?)"#,
|
||||
r#"INSERT INTO addressbooks (principal, id, displayname, description, push_topic)
|
||||
VALUES (?, ?, ?, ?, ?)"#,
|
||||
addressbook.principal,
|
||||
addressbook.id,
|
||||
addressbook.displayname,
|
||||
addressbook.description,
|
||||
addressbook.push_topic,
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await
|
||||
|
||||
@@ -62,7 +62,7 @@ impl CalendarStore for SqliteStore {
|
||||
async fn get_calendar(&self, principal: &str, id: &str) -> Result<Calendar, Error> {
|
||||
let cal = sqlx::query_as!(
|
||||
Calendar,
|
||||
r#"SELECT principal, id, synctoken, "order", displayname, description, color, timezone, timezone_id, deleted_at, subscription_url
|
||||
r#"SELECT principal, id, synctoken, "order", displayname, description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic
|
||||
FROM calendars
|
||||
WHERE (principal, id) = (?, ?)"#,
|
||||
principal,
|
||||
@@ -77,7 +77,7 @@ impl CalendarStore for SqliteStore {
|
||||
async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error> {
|
||||
let cals = sqlx::query_as!(
|
||||
Calendar,
|
||||
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, timezone_id, deleted_at, subscription_url
|
||||
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic
|
||||
FROM calendars
|
||||
WHERE principal = ? AND deleted_at IS NULL"#,
|
||||
principal
|
||||
@@ -91,7 +91,7 @@ impl CalendarStore for SqliteStore {
|
||||
async fn get_deleted_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error> {
|
||||
let cals = sqlx::query_as!(
|
||||
Calendar,
|
||||
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, timezone_id, deleted_at, subscription_url
|
||||
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic
|
||||
FROM calendars
|
||||
WHERE principal = ? AND deleted_at IS NOT NULL"#,
|
||||
principal
|
||||
@@ -104,8 +104,8 @@ impl CalendarStore for SqliteStore {
|
||||
#[instrument]
|
||||
async fn insert_calendar(&self, calendar: Calendar) -> Result<(), Error> {
|
||||
sqlx::query!(
|
||||
r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone, timezone_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)"#,
|
||||
r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone, timezone_id, push_topic)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"#,
|
||||
calendar.principal,
|
||||
calendar.id,
|
||||
calendar.displayname,
|
||||
@@ -113,7 +113,8 @@ impl CalendarStore for SqliteStore {
|
||||
calendar.order,
|
||||
calendar.color,
|
||||
calendar.timezone,
|
||||
calendar.timezone_id
|
||||
calendar.timezone_id,
|
||||
calendar.push_topic,
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await.map_err(crate::Error::from)?;
|
||||
@@ -128,7 +129,7 @@ impl CalendarStore for SqliteStore {
|
||||
calendar: Calendar,
|
||||
) -> Result<(), Error> {
|
||||
let result = sqlx::query!(
|
||||
r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?, timezone_id = ?
|
||||
r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?, timezone_id = ?, push_topic = ?
|
||||
WHERE (principal, id) = (?, ?)"#,
|
||||
calendar.principal,
|
||||
calendar.id,
|
||||
@@ -138,6 +139,7 @@ impl CalendarStore for SqliteStore {
|
||||
calendar.color,
|
||||
calendar.timezone,
|
||||
calendar.timezone_id,
|
||||
calendar.push_topic,
|
||||
principal,
|
||||
id
|
||||
).execute(&self.db).await.map_err(crate::Error::from)?;
|
||||
|
||||
Reference in New Issue
Block a user