You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.1 KiB
69 lines
2.1 KiB
use embedded_components::ListItemData; |
|
use kitchen_fridge::{ |
|
cache::Cache, |
|
client::Client, |
|
traits::{CalDavSource, CompleteCalendar}, |
|
CalDavProvider, |
|
}; |
|
use serde::{Deserialize, Serialize}; |
|
|
|
#[derive(Deserialize, Serialize)] |
|
pub struct User { |
|
pub username: String, |
|
pub url: String, |
|
pub password: String, |
|
pub calendar_urls: Vec<String>, |
|
} |
|
|
|
pub struct UserData { |
|
pub tasks: Vec<ListItemData>, |
|
} |
|
|
|
pub async fn get_user_data(user: &User) -> UserData { |
|
let cache_path = format!("test_cache/{}", user.username); |
|
let cache_path = std::path::Path::new(&cache_path); |
|
|
|
let client = Client::new(&user.url, &user.username, &user.password).unwrap(); |
|
let cache = Cache::from_folder(&cache_path).unwrap_or(Cache::new(&cache_path)); |
|
let mut provider = CalDavProvider::new(client, cache); |
|
provider.sync().await; |
|
provider.local().save_to_folder().unwrap(); |
|
let mut tasks = vec![]; |
|
let cals = provider.local().get_calendars().await.unwrap(); |
|
for (url, cal) in cals { |
|
if !user |
|
.calendar_urls |
|
.iter() |
|
.any(|calendar_url| url == calendar_url.parse().unwrap()) |
|
{ |
|
continue; |
|
} |
|
|
|
let cal = cal.lock().unwrap(); |
|
let items = cal.get_items().await.unwrap(); |
|
for (_, item) in items { |
|
match item { |
|
kitchen_fridge::Item::Event(_) => continue, |
|
kitchen_fridge::Item::Task(task) => { |
|
tasks.push( |
|
ListItemData::new( |
|
&user |
|
.username |
|
.chars() |
|
.nth(0) |
|
.unwrap_or(' ') |
|
.to_uppercase() |
|
.to_string(), |
|
&task.name().to_string(), |
|
) |
|
.with_progress(task.completion_percent()), |
|
); |
|
} |
|
} |
|
} |
|
} |
|
|
|
println!("Found {} tasks for {}", tasks.len(), user.username); |
|
|
|
return UserData { tasks }; |
|
}
|
|
|