use std::sync::Arc;
use adk_core::Result;
use adk_core::subject::{StoreErasure, StoreSupport, SubjectDataStore, SubjectScope};
use async_trait::async_trait;
use serde_json::{Value, json};
use crate::service::ArtifactService;
#[derive(Clone)]
pub struct ArtifactSubjectData {
service: Arc<dyn ArtifactService>,
name: String,
support: StoreSupport,
}
impl ArtifactSubjectData {
pub fn new(service: Arc<dyn ArtifactService>) -> Self {
Self { service, name: "artifacts".to_string(), support: StoreSupport::Hard }
}
#[must_use]
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
#[must_use]
pub fn with_support(mut self, support: StoreSupport) -> Self {
self.support = support;
self
}
}
#[async_trait]
impl SubjectDataStore for ArtifactSubjectData {
fn name(&self) -> &str {
&self.name
}
fn support(&self) -> StoreSupport {
self.support
}
async fn export(&self, scope: &SubjectScope) -> Result<Value> {
let mut groups = Vec::new();
for app_name in &scope.app_names {
for user_id in scope.user_ids() {
let artifacts: Vec<Value> = self
.service
.list_user(app_name, user_id)
.await?
.into_iter()
.map(|artifact| {
json!({
"sessionId": artifact.session_id,
"fileName": artifact.file_name,
"version": artifact.version,
"mimeType": artifact.mime_type,
"size": artifact.size,
})
})
.collect();
if !artifacts.is_empty() {
groups.push(
json!({ "appName": app_name, "userId": user_id, "artifacts": artifacts }),
);
}
}
}
Ok(json!({ "artifacts": groups }))
}
async fn erase(&self, scope: &SubjectScope) -> Result<StoreErasure> {
let mut deleted = 0;
for app_name in &scope.app_names {
for user_id in scope.user_ids() {
deleted += self.service.delete_user(app_name, user_id).await?;
}
}
Ok(StoreErasure::deleted(deleted))
}
}