Skip to content

Commit

Permalink
feat(backend): allow committing collections from yank integrations pr…
Browse files Browse the repository at this point in the history
…ogress

And other changes.
  • Loading branch information
IgnisDa committed Jun 24, 2024
1 parent 72bf3ce commit d9214a8
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 249 deletions.
9 changes: 5 additions & 4 deletions apps/backend/src/fitness/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,13 @@ impl UserWorkoutInput {
totals.weight = Some(we * Decimal::from_usize(*re).unwrap());
}
let mut value = WorkoutSetRecord {
statistic: set.statistic.clone(),
lot: set.lot,
confirmed_at: set.confirmed_at,
totals,
personal_bests: vec![],
lot: set.lot,
actual_rest_time,
note: set.note.clone(),
personal_bests: vec![],
confirmed_at: set.confirmed_at,
statistic: set.statistic.clone(),
};
value.statistic.one_rm = value.calculate_one_rm();
value.statistic.pace = value.calculate_pace();
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/fitness/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,9 @@ impl ExerciseService {
.sets
.into_iter()
.map(|s| UserWorkoutSetRecord {
statistic: s.statistic,
lot: s.lot,
note: s.note,
statistic: s.statistic,
confirmed_at: s.confirmed_at,
})
.collect(),
Expand Down
15 changes: 2 additions & 13 deletions apps/backend/src/importer/audiobookshelf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use reqwest::{
header::{HeaderValue, AUTHORIZATION},
Client,
};
use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::{
Expand All @@ -25,16 +24,6 @@ use crate::{

use super::DeployUrlAndKeyImportInput;

#[derive(Debug, Serialize, Deserialize)]
pub struct LibrariesListResponse {
pub libraries: Vec<audiobookshelf_models::Item>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ListResponse {
pub results: Vec<audiobookshelf_models::Item>,
}

pub async fn import<F>(
input: DeployUrlAndKeyImportInput,
isbn_service: &GoogleBooksService,
Expand All @@ -57,7 +46,7 @@ where
.send()
.await
.map_err(|e| anyhow!(e))?
.json::<LibrariesListResponse>()
.json::<audiobookshelf_models::LibrariesListResponse>()
.await
.unwrap();
for library in libraries_resp.libraries {
Expand All @@ -72,7 +61,7 @@ where
.send()
.await
.map_err(|e| anyhow!(e))?
.json::<ListResponse>()
.json::<audiobookshelf_models::ListResponse>()
.await
.unwrap();
let len = finished_items.results.len();
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/importer/strong_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub async fn import(
weight,
..Default::default()
},
note: None,
lot: SetLot::Normal,
confirmed_at: None,
});
Expand Down
28 changes: 18 additions & 10 deletions apps/backend/src/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IntegrationMedia {
pub struct IntegrationMediaSeen {
pub identifier: String,
pub lot: MediaLot,
#[serde(default)]
Expand All @@ -35,6 +35,14 @@ pub struct IntegrationMedia {
pub provider_watched_on: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IntegrationMediaCollection {
pub identifier: String,
pub lot: MediaLot,
pub source: MediaSource,
pub collection: String,
}

#[derive(Debug)]
pub struct IntegrationService {
db: DatabaseConnection,
Expand All @@ -45,7 +53,7 @@ impl IntegrationService {
Self { db: db.clone() }
}

pub async fn jellyfin_progress(&self, payload: &str) -> Result<IntegrationMedia> {
pub async fn jellyfin_progress(&self, payload: &str) -> Result<IntegrationMediaSeen> {
mod models {
use super::*;

Expand Down Expand Up @@ -112,7 +120,7 @@ impl IntegrationService {
"Movie" => MediaLot::Movie,
_ => bail!("Only movies and shows supported"),
};
Ok(IntegrationMedia {
Ok(IntegrationMediaSeen {
identifier,
lot,
source: MediaSource::Tmdb,
Expand All @@ -128,7 +136,7 @@ impl IntegrationService {
&self,
payload: &str,
plex_user: Option<String>,
) -> Result<IntegrationMedia> {
) -> Result<IntegrationMediaSeen> {
mod models {
use super::*;

Expand Down Expand Up @@ -238,7 +246,7 @@ impl IntegrationService {
},
};

Ok(IntegrationMedia {
Ok(IntegrationMediaSeen {
identifier,
lot,
source: MediaSource::Tmdb,
Expand All @@ -250,8 +258,8 @@ impl IntegrationService {
})
}

pub async fn kodi_progress(&self, payload: &str) -> Result<IntegrationMedia> {
let mut payload = match serde_json::from_str::<IntegrationMedia>(payload) {
pub async fn kodi_progress(&self, payload: &str) -> Result<IntegrationMediaSeen> {
let mut payload = match serde_json::from_str::<IntegrationMediaSeen>(payload) {
Result::Ok(val) => val,
Result::Err(err) => bail!(err),
};
Expand All @@ -266,7 +274,7 @@ impl IntegrationService {
access_token: &str,
isbn_service: &GoogleBooksService,
commit_metadata: impl Fn(CommitMediaInput) -> F,
) -> Result<Vec<IntegrationMedia>>
) -> Result<(Vec<IntegrationMediaSeen>, Vec<IntegrationMediaCollection>)>
where
F: Future<Output = GqlResult<metadata::Model>>,
{
Expand Down Expand Up @@ -372,7 +380,7 @@ impl IntegrationService {
} else {
resp.progress
};
media_items.push(IntegrationMedia {
media_items.push(IntegrationMediaSeen {
lot,
source,
identifier,
Expand All @@ -388,6 +396,6 @@ impl IntegrationService {
}
};
}
Ok(media_items)
Ok((media_items, vec![]))
}
}
10 changes: 10 additions & 0 deletions apps/backend/src/miscellaneous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ pub mod audiobookshelf_models {
pub struct Response {
pub library_items: Vec<Item>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LibrariesListResponse {
pub libraries: Vec<Item>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ListResponse {
pub results: Vec<Item>,
}
}

pub fn itunes_podcast_episode_by_name(name: &str, podcast: metadata::Model) -> Option<i32> {
Expand Down
35 changes: 29 additions & 6 deletions apps/backend/src/miscellaneous/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use crate::{
},
file_storage::FileStorageService,
fitness::resolver::ExerciseService,
integrations::{IntegrationMedia, IntegrationService},
integrations::{IntegrationMediaSeen, IntegrationService},
jwt,
miscellaneous::{CustomService, DefaultCollection},
models::{
Expand Down Expand Up @@ -5572,6 +5572,7 @@ impl MiscellaneousService {
.all(&self.db)
.await?;
let mut progress_updates = vec![];
let mut collection_updates = vec![];
let mut to_update_integrations = vec![];
for integration in integrations.into_iter() {
let response = match integration.source {
Expand All @@ -5588,16 +5589,38 @@ impl MiscellaneousService {
}
_ => continue,
};
if let Ok(data) = response {
progress_updates.extend(data);
if let Ok((seen_progress, collection_progress)) = response {
progress_updates.extend(seen_progress);
collection_updates.extend(collection_progress);
to_update_integrations.push(integration.id);
}
}
for pu in progress_updates.into_iter() {
self.integration_progress_update(pu, user_id)
for progress_update in progress_updates.into_iter() {
self.integration_progress_update(progress_update, user_id)
.await
.trace_ok();
}
for col_update in collection_updates.into_iter() {
let metadata::Model { id, .. } = self
.commit_metadata(CommitMediaInput {
lot: col_update.lot,
source: col_update.source,
identifier: col_update.identifier.clone(),
force_update: None,
})
.await?;
self.add_entity_to_collection(
user_id,
ChangeCollectionToEntityInput {
creator_user_id: user_id.to_owned(),
collection_name: col_update.collection,
metadata_id: Some(id.clone()),
..Default::default()
},
)
.await
.trace_ok();
}
Integration::update_many()
.filter(integration::Column::Id.is_in(to_update_integrations))
.col_expr(
Expand Down Expand Up @@ -5702,7 +5725,7 @@ impl MiscellaneousService {
#[tracing::instrument(skip(self))]
async fn integration_progress_update(
&self,
pu: IntegrationMedia,
pu: IntegrationMediaSeen,
user_id: &String,
) -> Result<()> {
let maximum_limit =
Expand Down
4 changes: 3 additions & 1 deletion apps/backend/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,7 @@ pub mod fitness {
#[serde(default)]
pub totals: WorkoutSetTotals,
pub actual_rest_time: Option<i64>,
pub note: Option<String>,
}

impl WorkoutSetRecord {
Expand Down Expand Up @@ -1878,8 +1879,9 @@ pub mod fitness {

#[derive(Clone, Debug, Deserialize, Serialize, InputObject)]
pub struct UserWorkoutSetRecord {
pub statistic: WorkoutSetStatistic,
pub lot: SetLot,
pub note: Option<String>,
pub statistic: WorkoutSetStatistic,
pub confirmed_at: Option<DateTimeUtc>,
}

Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ pub async fn add_entity_to_collection(
information: ActiveValue::Set(information),
..Default::default()
};
if created_collection.insert(db).await.is_ok() {
if let Ok(created) = created_collection.insert(db).await {
tracing::debug!("Created collection to entity: {:?}", created);
associate_user_with_entity(
user_id,
input.metadata_id,
Expand Down
Loading

0 comments on commit d9214a8

Please sign in to comment.