Skip to content

Calendar crates overhaul #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 96 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ resolver = "2"
members = ["apps/app/server", "apps/desktop/src-tauri", "apps/mobile/src-tauri", "crates/*", "plugins/*"]

[workspace.dependencies]
hypr-calendar = { path = "crates/calendar", package = "calendar" }
hypr-calendar-apple = { path = "crates/calendar-apple", package = "calendar-apple" }
hypr-calendar-google = { path = "crates/calendar-google", package = "calendar-google" }
hypr-calendar-interface = { path = "crates/calendar-interface", package = "calendar-interface" }
hypr-calendar-outlook = { path = "crates/calendar-outlook", package = "calendar-outlook" }
hypr-data = { path = "crates/data", package = "data" }
hypr-db-admin = { path = "crates/db-admin", package = "db-admin" }
hypr-db-core = { path = "crates/db-core", package = "db-core" }
Expand Down
68 changes: 44 additions & 24 deletions apps/app/server/src/worker/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use std::collections::HashMap;
use apalis::prelude::{Data, Error};
use chrono::{DateTime, Utc};

use crate::state::WorkerState;
use hypr_calendar_interface::CalendarSource;
use hypr_calendar_google::GetTokenOutput;
use hypr_nango::{NangoCredentials, NangoIntegration};

use crate::state::WorkerState;

#[allow(unused)]
#[derive(Default, Debug, Clone)]
pub struct Job(DateTime<Utc>);
Expand All @@ -17,6 +18,25 @@ impl From<DateTime<Utc>> for Job {
}
}

#[derive(Clone)]
struct AuthProvider {
pub nango: hypr_nango::NangoClient,
}

impl AuthProvider {
pub async fn impl_get_token(
&self,
) -> Result<Option<String>, Box<dyn std::error::Error + Send + Sync>> {
Ok(Some("TODO".to_string()))
}
}

impl hypr_calendar_google::GetToken for AuthProvider {
fn get_token(&self, _scopes: &[&str]) -> GetTokenOutput {
Box::pin(self.impl_get_token())
}
}

#[tracing::instrument(skip(ctx))]
pub async fn perform(job: Job, ctx: Data<WorkerState>) -> Result<(), Error> {
let now = DateTime::<Utc>::from_timestamp(job.0.timestamp(), 0).unwrap();
Expand Down Expand Up @@ -72,37 +92,37 @@ pub async fn perform(job: Job, ctx: Data<WorkerState>) -> Result<(), Error> {

for (kind, integrations) in integration_groups {
for integration in integrations {
let token = get_oauth_access_token(&ctx.nango, &integration)
.await
.map_err(|e| e.as_worker_error())?;
// let token = get_oauth_access_token(&ctx.nango, &integration)
// .await
// .map_err(|e| e.as_worker_error())?;

// assert!(kind == NangoIntegration::GoogleCalendar);

assert!(kind == NangoIntegration::GoogleCalendar);
let gcal = hypr_calendar_google::Handle::new(token).await;
let gcal = hypr_calendar_google::Client::new(AuthProvider {
nango: ctx.nango.clone(),
});

let filter = hypr_calendar_interface::EventFilter {
calendars: vec![],
from: now - chrono::Duration::days(1),
to: now + chrono::Duration::days(1),
};
let events = gcal
.list_events(filter)
.await
.map_err(|e| Into::<crate::Error>::into(e).as_worker_error())?;
let events = gcal.list_events("TODO_CALENDAR_ID").await.unwrap();

for e in events {
let event = hypr_db_user::Event {
id: uuid::Uuid::new_v4().to_string(),
tracking_id: e.id.clone(),
user_id: user_id.clone(),
calendar_id: "TODO".to_string(),
name: e.name.clone(),
note: e.note.clone(),
start_date: e.start_date,
end_date: e.end_date,
google_event_url: None,
};

let _ = user_db.upsert_event(event).await;
// let event = hypr_db_user::Event {
// id: uuid::Uuid::new_v4().to_string(),
// tracking_id: e.id.clone(),
// user_id: user_id.clone(),
// calendar_id: "TODO".to_string(),
// name: e.name.clone(),
// note: e.note.clone(),
// start_date: e.start_date,
// end_date: e.end_date,
// google_event_url: None,
// };

// let _ = user_db.upsert_event(event).await;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/calendar-apple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use hypr_calendar_interface::{
Calendar, CalendarSource, Error, Event, EventFilter, Participant, Platform,
};

mod models;
pub use models::*;

pub struct Handle {
event_store: Retained<EKEventStore>,
contacts_store: Retained<CNContactStore>,
Expand Down
3 changes: 3 additions & 0 deletions crates/calendar-apple/src/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub struct Calendar {}

pub struct Event {}
5 changes: 5 additions & 0 deletions crates/calendar-google/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ hypr-calendar-interface = { path = "../calendar-interface", package = "calendar-

chrono = { workspace = true }
google-calendar = "0.7.0"
google-calendar3 = "6.0.0"

serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
35 changes: 35 additions & 0 deletions crates/calendar-google/src/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
pub use google_calendar3::common::GetToken;
use std::{future::Future, pin::Pin};

#[derive(Debug, Clone)]
pub struct Storage {
// TODO: some user_id

// we might want to store nango client here, with lifetime specified

// Actually, we just make GetToken public, and expect server provide it.
// we expect that we receive impl GetToken.
}

pub type GetTokenOutput<'a> = Pin<
Box<
dyn Future<Output = Result<Option<String>, Box<dyn std::error::Error + Send + Sync>>>
+ Send
+ 'a,
>,
>;

impl Storage {
pub async fn impl_get_token(
&self,
) -> Result<Option<String>, Box<dyn std::error::Error + Send + Sync>> {
// Fetch token from nango, using user_id
Ok(Some("TODO".to_string()))
}
}

impl GetToken for Storage {
fn get_token<'a>(&'a self, _scopes: &'a [&str]) -> GetTokenOutput<'a> {
Box::pin(self.impl_get_token())
}
}
16 changes: 16 additions & 0 deletions crates/calendar-google/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use serde::{ser::Serializer, Serialize};

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
GoogleCalendarError(#[from] google_calendar3::Error),
}

impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
Loading
Loading