Skip to content

Commit 4f91956

Browse files
authored
Common fixes (#705)
* fix(frontend): allow re-ordering dashboard elements * docs: clarify docs * build(backend): bump version * feat(frontend): remove media details layout from create pages * refactor(frontend): extract upload files to service handler * refactor(frontend): uploader to upload to s3 * feat(frontend): handle file upload in action * refactor(backend): allow associating user to exercise * fix(backend): associate user with exercise on collection update * fix(backend): do not force unwrap information * feat(frontend): upload files in action when creating exercise * feat(ts-utils): import clone deep for backend * fix(frontend): upload exercise image to correct prefix * feat(frontend): use clone deep for deeply nested attributes
1 parent 3f34655 commit 4f91956

17 files changed

+337
-352
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/backend/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ryot"
3-
version = "4.3.1"
3+
version = "4.3.2"
44
edition = "2021"
55
repository = "https://github.com/IgnisDa/ryot"
66
license = "GPL-3.0"

apps/backend/src/entities/collection_to_entity.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,15 @@ impl ActiveModelBehavior for ActiveModel {
106106
.one(db)
107107
.await?
108108
.unwrap();
109-
associate_user_with_entity(&collection.user_id, model.metadata_id, model.person_id, db)
110-
.await
111-
.ok();
109+
associate_user_with_entity(
110+
&collection.user_id,
111+
model.metadata_id,
112+
model.person_id,
113+
model.exercise_id.clone(),
114+
db,
115+
)
116+
.await
117+
.ok();
112118
}
113119
Ok(model)
114120
}

apps/backend/src/entities/review.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ impl ActiveModelBehavior for ActiveModel {
118118
C: ConnectionTrait,
119119
{
120120
if insert {
121-
associate_user_with_entity(&model.user_id, model.metadata_id, model.person_id, db)
122-
.await
123-
.ok();
121+
associate_user_with_entity(
122+
&model.user_id,
123+
model.metadata_id,
124+
model.person_id,
125+
None,
126+
db,
127+
)
128+
.await
129+
.ok();
124130
}
125131
Ok(model)
126132
}

apps/backend/src/entities/seen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl ActiveModelBehavior for ActiveModel {
9393
C: ConnectionTrait,
9494
{
9595
if insert {
96-
associate_user_with_entity(&model.user_id, Some(model.metadata_id), None, db)
96+
associate_user_with_entity(&model.user_id, Some(model.metadata_id), None, None, db)
9797
.await
9898
.ok();
9999
}

apps/backend/src/fitness/logic.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl UserWorkoutInput {
151151
}
152152
Some(e) => {
153153
let performed = e.exercise_num_times_interacted.unwrap_or_default();
154-
let mut extra_info = e.exercise_extra_information.clone().unwrap();
154+
let mut extra_info = e.exercise_extra_information.clone().unwrap_or_default();
155155
extra_info.history.insert(0, history_item);
156156
let mut up: user_to_entity::ActiveModel = e.into();
157157
up.exercise_num_times_interacted = ActiveValue::Set(Some(performed + 1));
@@ -205,7 +205,7 @@ impl UserWorkoutInput {
205205
let mut personal_bests = association
206206
.exercise_extra_information
207207
.clone()
208-
.unwrap()
208+
.unwrap_or_default()
209209
.personal_bests;
210210
let types_of_prs = match db_ex.lot {
211211
ExerciseLot::Duration => vec![WorkoutSetPersonalBest::Time],
@@ -260,8 +260,10 @@ impl UserWorkoutInput {
260260
}
261261
}
262262
}
263-
let mut association_extra_information =
264-
association.exercise_extra_information.clone().unwrap();
263+
let mut association_extra_information = association
264+
.exercise_extra_information
265+
.clone()
266+
.unwrap_or_default();
265267
let mut association: user_to_entity::ActiveModel = association.into();
266268
association_extra_information.lifetime_stats += total.clone();
267269
association_extra_information.personal_bests = personal_bests;
@@ -328,7 +330,10 @@ impl workout::Model {
328330
None => continue,
329331
Some(assoc) => assoc,
330332
};
331-
let mut ei = association.exercise_extra_information.clone().unwrap();
333+
let mut ei = association
334+
.exercise_extra_information
335+
.clone()
336+
.unwrap_or_default();
332337
if let Some(ex_idx) = ei
333338
.history
334339
.iter()

apps/backend/src/fitness/resolver.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,10 @@ impl ExerciseService {
387387
.one(&self.db)
388388
.await?
389389
{
390-
let user_to_exercise_extra_information =
391-
association.exercise_extra_information.clone().unwrap();
390+
let user_to_exercise_extra_information = association
391+
.exercise_extra_information
392+
.clone()
393+
.unwrap_or_default();
392394
let workouts = Workout::find()
393395
.filter(
394396
workout::Column::Id.is_in(
@@ -834,11 +836,11 @@ impl ExerciseService {
834836
association
835837
.exercise_extra_information
836838
.clone()
837-
.unwrap()
839+
.unwrap_or_default()
838840
.history
839841
.first()
840842
.cloned()
841-
.unwrap()
843+
.unwrap_or_default()
842844
.workout_id,
843845
)
844846
.one(&self.db)

apps/backend/src/miscellaneous/resolver.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -1824,7 +1824,7 @@ impl MiscellaneousService {
18241824
.unwrap();
18251825
let seen_by: i32 = seen_by.try_into().unwrap();
18261826
let user_to_meta =
1827-
get_user_to_entity_association(&user_id, Some(metadata_id), None, &self.db).await;
1827+
get_user_to_entity_association(&user_id, Some(metadata_id), None, None, &self.db).await;
18281828
let reminder = user_to_meta.clone().and_then(|n| n.media_reminder);
18291829
let units_consumed = user_to_meta.clone().and_then(|n| n.metadata_units_consumed);
18301830
let ownership = user_to_meta.and_then(|n| n.metadata_ownership);
@@ -1866,7 +1866,7 @@ impl MiscellaneousService {
18661866
let collections =
18671867
entity_in_collections(&self.db, user_id, None, Some(creator_id), None, None).await?;
18681868
let association =
1869-
get_user_to_entity_association(&user_id, None, Some(creator_id), &self.db).await;
1869+
get_user_to_entity_association(&user_id, None, Some(creator_id), None, &self.db).await;
18701870
Ok(UserPersonDetails {
18711871
reviews,
18721872
collections,
@@ -3459,10 +3459,10 @@ impl MiscellaneousService {
34593459
.exec(&self.db)
34603460
.await?;
34613461
if let Some(association) =
3462-
get_user_to_entity_association(&user_id, Some(merge_into), None, &self.db).await
3462+
get_user_to_entity_association(&user_id, Some(merge_into), None, None, &self.db).await
34633463
{
34643464
let old_association =
3465-
get_user_to_entity_association(&user_id, Some(merge_from), None, &self.db)
3465+
get_user_to_entity_association(&user_id, Some(merge_from), None, None, &self.db)
34663466
.await
34673467
.unwrap();
34683468
let mut cloned: user_to_entity::ActiveModel = old_association.clone().into();
@@ -6230,9 +6230,14 @@ GROUP BY
62306230
user_id: i32,
62316231
input: ToggleMediaMonitorInput,
62326232
) -> Result<bool> {
6233-
let metadata =
6234-
associate_user_with_entity(&user_id, input.metadata_id, input.person_id, &self.db)
6235-
.await?;
6233+
let metadata = associate_user_with_entity(
6234+
&user_id,
6235+
input.metadata_id,
6236+
input.person_id,
6237+
None,
6238+
&self.db,
6239+
)
6240+
.await?;
62366241
let mut new_monitored_value = !metadata.media_monitored.unwrap_or_default();
62376242
if let Some(force_value) = input.force_value {
62386243
new_monitored_value = force_value;
@@ -6248,9 +6253,14 @@ GROUP BY
62486253
user_id: i32,
62496254
to_monitor_metadata_id: i32,
62506255
) -> Result<bool> {
6251-
let metadata =
6252-
get_user_to_entity_association(&user_id, Some(to_monitor_metadata_id), None, &self.db)
6253-
.await;
6256+
let metadata = get_user_to_entity_association(
6257+
&user_id,
6258+
Some(to_monitor_metadata_id),
6259+
None,
6260+
None,
6261+
&self.db,
6262+
)
6263+
.await;
62546264
Ok(if let Some(m) = metadata {
62556265
m.media_monitored.unwrap_or_default()
62566266
} else {
@@ -6618,9 +6628,14 @@ GROUP BY
66186628
if input.remind_on < Utc::now().date_naive() {
66196629
return Ok(false);
66206630
}
6621-
let utm =
6622-
associate_user_with_entity(&user_id, input.metadata_id, input.person_id, &self.db)
6623-
.await?;
6631+
let utm = associate_user_with_entity(
6632+
&user_id,
6633+
input.metadata_id,
6634+
input.person_id,
6635+
None,
6636+
&self.db,
6637+
)
6638+
.await?;
66246639
if utm.media_reminder.is_some() {
66256640
self.delete_media_reminder(user_id, input.metadata_id, input.person_id)
66266641
.await?;
@@ -6640,7 +6655,8 @@ GROUP BY
66406655
metadata_id: Option<i32>,
66416656
person_id: Option<i32>,
66426657
) -> Result<bool> {
6643-
let utm = associate_user_with_entity(&user_id, metadata_id, person_id, &self.db).await?;
6658+
let utm =
6659+
associate_user_with_entity(&user_id, metadata_id, person_id, None, &self.db).await?;
66446660
let mut utm: user_to_entity::ActiveModel = utm.into();
66456661
utm.media_reminder = ActiveValue::Set(None);
66466662
utm.update(&self.db).await?;
@@ -6653,7 +6669,8 @@ GROUP BY
66536669
metadata_id: i32,
66546670
owned_on: Option<NaiveDate>,
66556671
) -> Result<bool> {
6656-
let utm = associate_user_with_entity(&user_id, Some(metadata_id), None, &self.db).await?;
6672+
let utm =
6673+
associate_user_with_entity(&user_id, Some(metadata_id), None, None, &self.db).await?;
66576674
let has_ownership = utm.metadata_ownership.is_some();
66586675
let mut utm: user_to_entity::ActiveModel = utm.into();
66596676
if has_ownership {

apps/backend/src/utils.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ pub async fn get_user_to_entity_association<C>(
123123
user_id: &i32,
124124
metadata_id: Option<i32>,
125125
person_id: Option<i32>,
126+
exercise_id: Option<String>,
126127
db: &C,
127128
) -> Option<user_to_entity::Model>
128129
where
@@ -133,7 +134,9 @@ where
133134
.filter(
134135
user_to_entity::Column::MetadataId
135136
.eq(metadata_id.to_owned())
136-
.or(user_to_entity::Column::PersonId.eq(person_id.to_owned())),
137+
.or(user_to_entity::Column::PersonId
138+
.eq(person_id.to_owned())
139+
.or(user_to_entity::Column::ExerciseId.eq(exercise_id.to_owned()))),
137140
)
138141
.one(db)
139142
.await
@@ -145,18 +148,22 @@ pub async fn associate_user_with_entity<C>(
145148
user_id: &i32,
146149
metadata_id: Option<i32>,
147150
person_id: Option<i32>,
151+
exercise_id: Option<String>,
148152
db: &C,
149153
) -> Result<user_to_entity::Model>
150154
where
151155
C: ConnectionTrait,
152156
{
153-
let user_to_meta = get_user_to_entity_association(user_id, metadata_id, person_id, db).await;
157+
let user_to_meta =
158+
get_user_to_entity_association(user_id, metadata_id, person_id, exercise_id.clone(), db)
159+
.await;
154160
Ok(match user_to_meta {
155161
None => {
156162
let user_to_meta = user_to_entity::ActiveModel {
157163
user_id: ActiveValue::Set(*user_id),
158164
metadata_id: ActiveValue::Set(metadata_id),
159165
person_id: ActiveValue::Set(person_id),
166+
exercise_id: ActiveValue::Set(exercise_id),
160167
last_updated_on: ActiveValue::Set(Utc::now()),
161168
needs_to_be_updated: ActiveValue::Set(Some(true)),
162169
..Default::default()

apps/frontend/app/lib/generals.ts

-33
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { MantineColorScheme } from "@mantine/core";
22
import {
3-
GetPresignedS3UrlDocument,
43
MediaSource,
54
MetadataLot,
6-
PresignedPutS3UrlDocument,
75
SetLot,
86
} from "@ryot/generated/graphql/backend/graphql";
97
import {
@@ -21,7 +19,6 @@ import dayjs from "dayjs";
2119
import duration from "dayjs/plugin/duration";
2220
import localizedFormat from "dayjs/plugin/localizedFormat";
2321
import relativeTime from "dayjs/plugin/relativeTime";
24-
import { GraphQLClient } from "graphql-request";
2522
import { match } from "ts-pattern";
2623

2724
dayjs.extend(relativeTime);
@@ -45,10 +42,6 @@ export const ApplicationKey = {
4542
Toast: getApplicationKeyAccessor(7),
4643
};
4744

48-
export const gqlClientSide = new GraphQLClient("/backend/graphql", {
49-
credentials: "include",
50-
});
51-
5245
export const getSetColor = (l: SetLot) =>
5346
match(l)
5447
.with(SetLot.WarmUp, () => "yellow")
@@ -216,32 +209,6 @@ export const getMetadataIcon = (lot: MetadataLot) => {
216209
.exhaustive();
217210
};
218211

219-
export const uploadFileAndGetKey = async (
220-
fileName: string,
221-
prefix: string,
222-
contentType: string,
223-
body: ArrayBuffer | Buffer,
224-
) => {
225-
const { presignedPutS3Url } = await gqlClientSide.request(
226-
PresignedPutS3UrlDocument,
227-
{ input: { fileName, prefix } },
228-
);
229-
await fetch(presignedPutS3Url.uploadUrl, {
230-
method: "PUT",
231-
body,
232-
headers: { "Content-Type": contentType },
233-
});
234-
return presignedPutS3Url.key;
235-
};
236-
237-
export const getPresignedGetUrl = async (key: string) => {
238-
const { getPresignedS3Url } = await gqlClientSide.request(
239-
GetPresignedS3UrlDocument,
240-
{ key },
241-
);
242-
return getPresignedS3Url;
243-
};
244-
245212
export { dayjs as dayjsLib };
246213

247214
export const redirectToQueryParam = "redirectTo";

0 commit comments

Comments
 (0)