use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::error::Result;
use crate::library::MediaItem;
use crate::types::ItemWithRelations;
use crate::types::book::Book;
use crate::types::episode::Episode;
use crate::types::library::ServerLibraryForRead;
use crate::types::media::MediaQuery;
use crate::types::movie::Movie;
use crate::types::person::Person;
use crate::types::serie::Serie;
use crate::types::tag::Tag;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SyncKind {
Tags,
People,
Series,
Movies,
Episodes,
Books,
Medias,
Deleted,
}
impl SyncKind {
pub const ALL: [SyncKind; 8] = [
SyncKind::Tags,
SyncKind::People,
SyncKind::Series,
SyncKind::Movies,
SyncKind::Episodes,
SyncKind::Books,
SyncKind::Medias,
SyncKind::Deleted,
];
pub fn name(self) -> &'static str {
match self {
SyncKind::Tags => "tags",
SyncKind::People => "people",
SyncKind::Series => "series",
SyncKind::Movies => "movies",
SyncKind::Episodes => "episodes",
SyncKind::Books => "books",
SyncKind::Medias => "medias",
SyncKind::Deleted => "deleted",
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SyncState {
pub generation: i64,
pub watermarks: BTreeMap<SyncKind, i64>,
#[serde(default)]
pub media_resume: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct Changes {
pub medias: Vec<MediaItem>,
pub tags: Vec<Tag>,
pub people: Vec<Person>,
pub series: Vec<ItemWithRelations<Serie>>,
pub movies: Vec<ItemWithRelations<Movie>>,
pub episodes: Vec<Episode>,
pub books: Vec<ItemWithRelations<Book>>,
pub removed: Vec<Removal>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Removal {
pub kind: SyncKind,
pub id: String,
pub date: Option<i64>,
}
impl Changes {
pub fn is_empty(&self) -> bool {
self.medias.is_empty()
&& self.tags.is_empty()
&& self.people.is_empty()
&& self.series.is_empty()
&& self.movies.is_empty()
&& self.episodes.is_empty()
&& self.books.is_empty()
&& self.removed.is_empty()
}
pub fn kinds(&self) -> Vec<SyncKind> {
let mut kinds = Vec::new();
let mut add = |kind: SyncKind, present: bool| {
if present && !kinds.contains(&kind) {
kinds.push(kind);
}
};
add(SyncKind::Tags, !self.tags.is_empty());
add(SyncKind::People, !self.people.is_empty());
add(SyncKind::Series, !self.series.is_empty());
add(SyncKind::Movies, !self.movies.is_empty());
add(SyncKind::Episodes, !self.episodes.is_empty());
add(SyncKind::Books, !self.books.is_empty());
add(SyncKind::Medias, !self.medias.is_empty());
for removal in &self.removed {
add(removal.kind, true);
}
kinds
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThumbRecord {
pub library: String,
pub media: String,
pub size: u64,
pub version: String,
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
pub trait MirrorStore: crate::transport::MaybeSend {
async fn libraries(&self) -> Result<Vec<ServerLibraryForRead>>;
async fn set_libraries(&self, libraries: Vec<ServerLibraryForRead>) -> Result<Vec<String>>;
async fn put_library(&self, library: ServerLibraryForRead) -> Result<()>;
async fn remove_library(&self, library: &str) -> Result<()>;
async fn sync_state(&self, library: &str) -> Result<Option<SyncState>>;
async fn set_sync_state(&self, library: &str, state: SyncState) -> Result<()>;
async fn apply(&self, library: &str, generation: i64, changes: Changes) -> Result<()>;
async fn sweep(&self, library: &str, kind: SyncKind, generation: i64) -> Result<Vec<String>>;
async fn medias(
&self,
library: &str,
query: MediaQuery,
offset: usize,
) -> Result<Vec<MediaItem>>;
async fn count_medias(&self, library: &str, query: MediaQuery) -> Result<u64>;
async fn media(&self, library: &str, id: &str) -> Result<Option<MediaItem>>;
async fn items(
&self,
library: &str,
kind: SyncKind,
serie: Option<String>,
) -> Result<Vec<serde_json::Value>>;
async fn count(&self, library: &str, kind: SyncKind) -> Result<u64>;
async fn thumb(&self, library: &str, media: &str) -> Result<Option<ThumbRecord>>;
async fn put_thumb(&self, record: ThumbRecord) -> Result<()>;
async fn remove_thumbs(&self, library: &str, medias: Vec<String>) -> Result<()>;
async fn thumbs_size(&self) -> Result<u64>;
async fn thumbs_over(&self, limit: u64) -> Result<Vec<ThumbRecord>>;
}