Skip to content

Commit

Permalink
More sync support for file paths + saved searches (#2067)
Browse files Browse the repository at this point in the history
more sync support for file paths + saved searches
  • Loading branch information
Brendonovich committed Feb 9, 2024
1 parent 1469ef6 commit 6f28d8e
Show file tree
Hide file tree
Showing 12 changed files with 495 additions and 190 deletions.
4 changes: 4 additions & 0 deletions core/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ model Location {
hidden Boolean?
date_created DateTime?
/// @local
// this is just a client side cache which is annoying but oh well (@brendan)
instance_id Int?
instance Instance? @relation(fields: [instance_id], references: [id], onDelete: SetNull)
Expand Down Expand Up @@ -514,6 +516,7 @@ model Notification {
@@map("notification")
}

/// @shared(id: pub_id)
model SavedSearch {
id Int @id @default(autoincrement())
pub_id Bytes @unique
Expand All @@ -532,6 +535,7 @@ model SavedSearch {
@@map("saved_search")
}

/// @local(id: id)
model CloudCRDTOperation {
id Bytes @id
timestamp BigInt
Expand Down
148 changes: 123 additions & 25 deletions core/src/api/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ use sd_file_path_helper::{
};
use sd_images::ConvertableExtension;
use sd_media_metadata::MediaMetadata;
use sd_prisma::prisma::{file_path, location, object};
use sd_prisma::{
prisma::{file_path, location, object},
prisma_sync,
};
use sd_sync::OperationFactory;
use sd_utils::{db::maybe_missing, error::FileIOError};
use serde_json::json;

use std::{
ffi::OsString,
Expand Down Expand Up @@ -177,15 +182,36 @@ pub(crate) fn mount() -> AlphaRouter<Ctx> {

R.with2(library())
.mutation(|(_, library), args: SetNoteArgs| async move {
library
.db
let Library { db, sync, .. } = library.as_ref();

let object = db
.object()
.update(
.find_unique(object::id::equals(args.id))
.select(object::select!({ pub_id }))
.exec()
.await?
.ok_or_else(|| {
rspc::Error::new(
rspc::ErrorCode::NotFound,
"Object not found".to_string(),
)
})?;

sync.write_op(
&db,

Check warning on line 201 in core/src/api/files.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> core/src/api/files.rs:201:7 | 201 | &db, | ^^^ help: change this to: `db` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
sync.shared_update(
prisma_sync::object::SyncId {
pub_id: object.pub_id,
},
object::note::NAME,
json!(&args.note),
),
db.object().update(
object::id::equals(args.id),
vec![object::note::set(args.note)],
)
.exec()
.await?;
),
)
.await?;

invalidate_query!(library, "search.paths");
invalidate_query!(library, "search.objects");
Expand All @@ -202,15 +228,36 @@ pub(crate) fn mount() -> AlphaRouter<Ctx> {

R.with2(library())
.mutation(|(_, library), args: SetFavoriteArgs| async move {
library
.db
let Library { sync, db, .. } = library.as_ref();

let object = db
.object()
.update(
.find_unique(object::id::equals(args.id))
.select(object::select!({ pub_id }))
.exec()
.await?
.ok_or_else(|| {
rspc::Error::new(
rspc::ErrorCode::NotFound,
"Object not found".to_string(),
)
})?;

sync.write_op(
&db,

Check warning on line 247 in core/src/api/files.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> core/src/api/files.rs:247:7 | 247 | &db, | ^^^ help: change this to: `db` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
sync.shared_update(
prisma_sync::object::SyncId {
pub_id: object.pub_id,
},
object::favorite::NAME,
json!(&args.favorite),
),
db.object().update(
object::id::equals(args.id),
vec![object::favorite::set(Some(args.favorite))],
)
.exec()
.await?;
),
)
.await?;

invalidate_query!(library, "search.paths");
invalidate_query!(library, "search.objects");
Expand Down Expand Up @@ -251,16 +298,43 @@ pub(crate) fn mount() -> AlphaRouter<Ctx> {
.procedure("updateAccessTime", {
R.with2(library())
.mutation(|(_, library), ids: Vec<i32>| async move {
library
.db
let Library { sync, db, .. } = library.as_ref();

let objects = db
.object()
.update_many(
vec![object::id::in_vec(ids)],
vec![object::date_accessed::set(Some(Utc::now().into()))],
)
.find_many(vec![object::id::in_vec(ids)])
.select(object::select!({ id pub_id }))
.exec()
.await?;

let date_accessed = Utc::now().into();

let (sync_params, db_params): (Vec<_>, Vec<_>) = objects
.into_iter()
.map(|d| {
(
sync.shared_update(
prisma_sync::object::SyncId { pub_id: d.pub_id },
object::date_accessed::NAME,
json!(date_accessed),
),
d.id,
)
})
.unzip();

sync.write_ops(
&db,

Check warning on line 327 in core/src/api/files.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> core/src/api/files.rs:327:7 | 327 | &db, | ^^^ help: change this to: `db` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
(
sync_params,
db.object().update_many(
vec![object::id::in_vec(db_params)],
vec![object::date_accessed::set(Some(date_accessed))],
),
),
)
.await?;

invalidate_query!(library, "search.paths");
invalidate_query!(library, "search.objects");
Ok(())
Expand All @@ -269,16 +343,40 @@ pub(crate) fn mount() -> AlphaRouter<Ctx> {
.procedure("removeAccessTime", {
R.with2(library())
.mutation(|(_, library), object_ids: Vec<i32>| async move {
library
.db
let Library { db, sync, .. } = library.as_ref();

let objects = db
.object()
.update_many(
vec![object::id::in_vec(object_ids)],
vec![object::date_accessed::set(None)],
)
.find_many(vec![object::id::in_vec(object_ids)])
.select(object::select!({ id pub_id }))
.exec()
.await?;

let (sync_params, db_params): (Vec<_>, Vec<_>) = objects
.into_iter()
.map(|d| {
(
sync.shared_update(
prisma_sync::object::SyncId { pub_id: d.pub_id },
object::date_accessed::NAME,
json!(null),
),
d.id,
)
})
.unzip();
sync.write_ops(
&db,

Check warning on line 369 in core/src/api/files.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> core/src/api/files.rs:369:7 | 369 | &db, | ^^^ help: change this to: `db` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
(
sync_params,
db.object().update_many(
vec![object::id::in_vec(db_params)],
vec![object::date_accessed::set(None)],
),
),
)
.await?;

invalidate_query!(library, "search.objects");
invalidate_query!(library, "search.paths");
Ok(())
Expand Down
Loading

0 comments on commit 6f28d8e

Please sign in to comment.