Initial commit

This commit is contained in:
2026-04-06 11:19:26 +02:00
commit 68e8205276
9 changed files with 7764 additions and 0 deletions

190
src/api.rs Normal file
View File

@@ -0,0 +1,190 @@
use std::{collections::HashMap, iter::repeat, sync::Arc};
use anyhow::{Context as _, anyhow};
use immich_sdk::{AssetId, AssetVisibility};
use kameo::{
Actor,
prelude::{Context, Message},
};
use slint::{Rgba8Pixel, SharedPixelBuffer};
use crate::thumbhash::thumbhashes_to_pixels;
pub type TimeBucketKey = String;
#[derive(Actor)]
pub struct Api {
client: immich_sdk::Client,
buckets: HashMap<TimeBucketKey, Arc<TimeBucket>>,
thumbnails: HashMap<AssetId, Arc<AssetThumbnail>>, // TODO
}
impl Api {
pub fn new(client: immich_sdk::Client) -> Self {
Self {
client,
buckets: Default::default(),
thumbnails: Default::default(),
}
}
}
pub struct GetTimeBuckets;
pub struct TimeBucketRef {
pub key: TimeBucketKey,
pub count: usize,
}
pub struct GetTimeBucket {
pub time_bucket: TimeBucketKey,
}
pub struct TimeBucket {
pub key: TimeBucketKey,
pub entries: Arc<[TimeBucketEntry]>,
}
pub struct TimeBucketEntry {
pub id: AssetId,
pub is_favorite: bool,
pub is_image: bool,
pub is_trashed: bool,
pub ratio: f64,
pub thumbhash: Option<SharedPixelBuffer<Rgba8Pixel>>,
pub visibility: AssetVisibility,
}
pub struct GetAssetThumbnail {
pub id: AssetId,
}
pub struct AssetThumbnail {
pub id: AssetId,
pub thumbnail: SharedPixelBuffer<Rgba8Pixel>,
}
impl Message<GetTimeBuckets> for Api {
type Reply = anyhow::Result<Arc<[TimeBucketRef]>>;
async fn handle(
&mut self,
_msg: GetTimeBuckets,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
let buckets = self
.client
.timeline()
.buckets()
.execute()
.await
.context(anyhow!("Failed to fetch list of time buckets",))
.inspect_err(|e| {
tracing::error!("{e:?}");
})?;
Ok(buckets
.into_iter()
.map(|b| TimeBucketRef {
key: b.time_bucket,
count: b.count.try_into().expect("count is non-negative"),
})
.collect())
}
}
impl Message<GetTimeBucket> for Api {
type Reply = anyhow::Result<Arc<TimeBucket>>;
async fn handle(
&mut self,
msg: GetTimeBucket,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
if let Some(time_bucket) = self.buckets.get(&msg.time_bucket).cloned() {
return Ok(time_bucket);
}
let bucket = self
.client
.timeline()
.bucket(&msg.time_bucket)
.execute()
.await
.context(anyhow!(
"Failed to fetch time bucket {:?}",
&msg.time_bucket
))
.inspect_err(|e| {
tracing::error!("{e:?}");
})?;
let thumbhashes = bucket.thumbhash.into_iter().flatten();
let thumbhashes = thumbhashes_to_pixels(thumbhashes)
.context("Failed to decode thumbhashes")?
.into_iter()
.chain(repeat(None));
let entries = bucket
.id
.into_iter()
.enumerate()
.zip(thumbhashes)
.map(|((i, id), thumbhash)| {
TimeBucketEntry {
id,
is_favorite: bucket.is_favorite.get(i).copied().unwrap_or(false),
is_image: bucket.is_image.get(i).copied().unwrap_or(true),
is_trashed: bucket.is_trashed.get(i).copied().unwrap_or(false),
thumbhash,
ratio: 1.0, // TODO
visibility: bucket.visibility.get(i).cloned().unwrap(), // TODO: no unwrap
}
})
.collect();
let bucket = Arc::new(TimeBucket {
key: msg.time_bucket,
entries,
});
self.buckets.insert(bucket.key.clone(), bucket.clone());
Ok(bucket)
}
}
impl Message<GetAssetThumbnail> for Api {
type Reply = anyhow::Result<Arc<AssetThumbnail>>;
async fn handle(
&mut self,
msg: GetAssetThumbnail,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
let response = self
.client
.assets()
.thumbnail(msg.id)
.size(immich_sdk::AssetMediaSize::Thumbnail)
.execute()
.await
.context(anyhow!("Failed to get asset thumbnail for {}", msg.id))?;
let thumbnail = response
.decode()
.context(anyhow!("Failed to decode asset thumbnail for {}", msg.id))?
.into_rgba8();
let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
&thumbnail,
thumbnail.width(),
thumbnail.height(),
);
Ok(Arc::new(AssetThumbnail {
id: msg.id,
thumbnail: pixel_buffer,
}))
}
}