Skip to content

Commit

Permalink
Default "Completed" collection (#815)
Browse files Browse the repository at this point in the history
* feat(backend): create default done collection

* feat(database): create "Done" collection for existing users

* fix(database): add on conflict clause

* feat(database): add items to done collection

* fix(database): do not add shows or podcasts to done

* fix(database): remove extra filter clause

* chore(backend,database): change name of new default collection

* build(backend): bump version

* refactor(backend): fns to easily add/remove from col

* feat(backend): add entity to "Done" correctly

* chore(database): change name of migration

* fix(database): filter by state instead of progress

* chore(backend): remove completed filter

* chore(backend): try to order by seen state

* Revert "chore(backend): try to order by seen state"

This reverts commit e21b35e.
  • Loading branch information
IgnisDa committed May 6, 2024
1 parent e4c9632 commit f94fdd0
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ryot"
version = "5.2.0"
version = "5.2.1"
edition = "2021"
repository = "https://github.com/IgnisDa/ryot"
license = "GPL-3.0"
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/miscellaneous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum DefaultCollection {
Watchlist,
#[strum(serialize = "In Progress")]
InProgress,
Completed,
Monitoring,
Custom,
}
Expand All @@ -18,6 +19,7 @@ meta! {
DefaultCollection, &'static str;
Watchlist, "Things I want to watch in the future.";
InProgress, "Media items that I am currently watching.";
Completed, "Media items that I have completed.";
Monitoring, "Items that I am keeping an eye on.";
Custom, "Items that I have created manually.";
}
Expand Down
128 changes: 42 additions & 86 deletions apps/backend/src/miscellaneous/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,6 @@ enum MediaGeneralFilter {
InProgress,
Dropped,
OnAHold,
Completed,
Unseen,
Owned,
}
Expand Down Expand Up @@ -2360,12 +2359,10 @@ impl MiscellaneousService {
}
MediaGeneralFilter::Dropped
| MediaGeneralFilter::InProgress
| MediaGeneralFilter::Completed
| MediaGeneralFilter::OnAHold => {
let state = match s {
MediaGeneralFilter::Dropped => SeenState::Dropped,
MediaGeneralFilter::InProgress => SeenState::InProgress,
MediaGeneralFilter::Completed => SeenState::Completed,
MediaGeneralFilter::OnAHold => SeenState::OnAHold,
_ => unreachable!(),
};
Expand Down Expand Up @@ -6090,50 +6087,39 @@ impl MiscellaneousService {
}

pub async fn after_media_seen_tasks(&self, seen: seen::Model) -> Result<()> {
self.remove_entity_from_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: DefaultCollection::Watchlist.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
.await
.ok();
let add_entity_to_collection = |collection_name: &str| {
self.add_entity_to_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: collection_name.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
};
let remove_entity_from_collection = |collection_name: &str| {
self.remove_entity_from_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: collection_name.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
};
remove_entity_from_collection(&DefaultCollection::Watchlist.to_string())
.await
.ok();
match seen.state {
SeenState::InProgress => {
self.add_entity_to_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: DefaultCollection::InProgress.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
.await
.ok();
self.add_entity_to_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: DefaultCollection::Monitoring.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
.await
.ok();
for col in &[DefaultCollection::InProgress, DefaultCollection::Monitoring] {
add_entity_to_collection(&col.to_string()).await.ok();
}
}
SeenState::Dropped | SeenState::OnAHold => {
self.remove_entity_from_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: DefaultCollection::InProgress.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
.await
.ok();
remove_entity_from_collection(&DefaultCollection::InProgress.to_string())
.await
.ok();
}
SeenState::Completed => {
let metadata = self.generic_metadata(seen.metadata_id).await?;
Expand Down Expand Up @@ -6201,54 +6187,24 @@ impl MiscellaneousService {
});
let is_complete = bag.values().all(|&e| e == 1);
if is_complete {
self.remove_entity_from_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: DefaultCollection::InProgress.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
.await
.ok();
} else {
for col in &[
DefaultCollection::InProgress.to_string(),
DefaultCollection::Monitoring.to_string(),
] {
self.add_entity_to_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: col.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
remove_entity_from_collection(&DefaultCollection::InProgress.to_string())
.await
.ok();
add_entity_to_collection(&DefaultCollection::Completed.to_string())
.await
.ok();
} else {
for col in &[DefaultCollection::InProgress, DefaultCollection::Monitoring] {
add_entity_to_collection(&col.to_string()).await.ok();
}
}
} else {
self.remove_entity_from_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: DefaultCollection::InProgress.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
.await
.ok();
self.remove_entity_from_collection(
seen.user_id,
ChangeCollectionToEntityInput {
collection_name: DefaultCollection::Monitoring.to_string(),
metadata_id: Some(seen.metadata_id),
..Default::default()
},
)
.await
.ok();
add_entity_to_collection(&DefaultCollection::Completed.to_string())
.await
.ok();
for col in &[DefaultCollection::InProgress, DefaultCollection::Monitoring] {
remove_entity_from_collection(&col.to_string()).await.ok();
}
};
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
db.execute_unprepared(r#"
DO $$
DECLARE
aUser RECORD;
BEGIN
FOR aUser IN SELECT id FROM "user"
LOOP
INSERT INTO collection (name, description, user_id, visibility, created_on, last_updated_on)
VALUES ('Completed', 'Media items that I have completed.', aUser.id, 'PR', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT DO NOTHING;
END LOOP;
END $$;
"#)
.await?;
Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let db = manager.get_connection();
db.execute_unprepared(
r#"
DO $$
DECLARE
user_rec RECORD;
seen_rec RECORD;
done_collection_id INTEGER;
BEGIN
FOR user_rec IN SELECT id FROM "user"
LOOP
SELECT id INTO done_collection_id FROM collection
WHERE user_id = user_rec.id AND name = 'Completed' LIMIT 1;
FOR seen_rec IN (
SELECT metadata_id FROM seen s
JOIN metadata m ON s.metadata_id = m.id
WHERE s.user_id = user_rec.id AND s.state = 'CO'
)
LOOP
INSERT INTO collection_to_entity (collection_id, metadata_id)
VALUES (done_collection_id, seen_rec.metadata_id)
ON CONFLICT DO NOTHING;
END LOOP;
END LOOP;
END$$;
"#,
)
.await?;
Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}
4 changes: 4 additions & 0 deletions libs/database/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ mod m20240416_change_json_to_generic_json;
mod m20240425_add_created_by_user_id_column_to_execise;
mod m20240503_update_user_to_entity_to_recalculate;
mod m20240504_add_columns_for_state_changes;
mod m20240506_0_add_done_collection_for_existing_users;
mod m20240506_1_add_entities_to_done_collection_for_existing_users;

pub use m20230410_create_metadata::Metadata as AliasedMetadata;
pub use m20230413_create_person::Person as AliasedPerson;
Expand Down Expand Up @@ -61,6 +63,8 @@ impl MigratorTrait for Migrator {
Box::new(m20240425_add_created_by_user_id_column_to_execise::Migration),
Box::new(m20240503_update_user_to_entity_to_recalculate::Migration),
Box::new(m20240504_add_columns_for_state_changes::Migration),
Box::new(m20240506_0_add_done_collection_for_existing_users::Migration),
Box::new(m20240506_1_add_entities_to_done_collection_for_existing_users::Migration),
]
}
}
1 change: 0 additions & 1 deletion libs/generated/src/graphql/backend/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,6 @@ export type MediaFilter = {

export enum MediaGeneralFilter {
All = 'ALL',
Completed = 'COMPLETED',
Dropped = 'DROPPED',
InProgress = 'IN_PROGRESS',
OnAHold = 'ON_A_HOLD',
Expand Down

0 comments on commit f94fdd0

Please sign in to comment.