@@ -2,7 +2,9 @@ use std::collections::HashMap;
2
2
3
3
use async_graphql:: { Context , Object , Result , ID } ;
4
4
use models:: {
5
- entity:: { finished_reading_session, media, reading_session, user:: AuthUser } ,
5
+ entity:: {
6
+ finished_reading_session, media, media_metadata, reading_session, user:: AuthUser ,
7
+ } ,
6
8
shared:: {
7
9
alphabet:: { AvailableAlphabet , EntityLetter } ,
8
10
ordering:: OrderBy ,
@@ -250,8 +252,9 @@ impl MediaQuery {
250
252
let conn = ctx. data :: < CoreContext > ( ) ?. conn . as_ref ( ) ;
251
253
252
254
let user_id = user. id . clone ( ) ;
253
- // FIXME(sea-orm): I think this might be wrong, see https://github.com/SeaQL/sea-orm/issues/2407
254
- let query = media:: ModelWithMetadata :: find_for_user ( user)
255
+
256
+ let query = media:: Entity :: apply_for_user ( user, media:: Entity :: find ( ) )
257
+ . select_also ( reading_session:: Entity )
255
258
. filter ( media:: Column :: DeletedAt . is_null ( ) )
256
259
. join_rev (
257
260
JoinType :: InnerJoin ,
@@ -267,71 +270,81 @@ impl MediaQuery {
267
270
. order_by_desc ( reading_session:: Column :: UpdatedAt ) ;
268
271
269
272
match pagination. resolve ( ) {
270
- Pagination :: Cursor ( info) => {
271
- let user_id = user. id . clone ( ) ;
272
- let mut cursor = query. cursor_by ( reading_session:: Column :: UpdatedAt ) ;
273
- if let Some ( ref id) = info. after {
274
- let id = id. clone ( ) ; // Clone for closure on_condition
275
- let record = media:: Entity :: find_for_user ( user)
276
- . select_only ( )
277
- . column ( reading_session:: Column :: UpdatedAt )
278
- . join_rev (
279
- JoinType :: InnerJoin ,
280
- reading_session:: Entity :: belongs_to ( media:: Entity )
281
- . from ( reading_session:: Column :: MediaId )
282
- . to ( media:: Column :: Id )
283
- . on_condition ( move |_left, _right| {
284
- Condition :: all ( ) . add (
285
- reading_session:: Column :: UserId
286
- . eq ( user_id. clone ( ) )
287
- . and (
288
- reading_session:: Column :: MediaId
289
- . eq ( id. clone ( ) ) ,
290
- ) ,
291
- )
292
- } )
293
- . into ( ) ,
294
- )
295
- . into_model :: < media:: ReadingSessionUpdatedAtCmpSelect > ( )
296
- . one ( conn)
297
- . await ?
298
- . ok_or ( "Cursor not found" ) ?;
299
- cursor. desc ( ) . after ( record. updated_at ) ;
300
- }
301
- cursor. first ( info. limit ) ;
302
-
303
- let models = cursor
304
- . group_by ( media:: Column :: Id )
305
- . into_model :: < media:: ModelWithMetadata > ( )
306
- . all ( conn)
307
- . await ?;
308
- let current_cursor = info
309
- . after
310
- . or_else ( || models. first ( ) . map ( |m| m. media . id . clone ( ) ) ) ;
311
- let next_cursor = match models. last ( ) . map ( |m| m. media . id . clone ( ) ) {
312
- Some ( id) if models. len ( ) == info. limit as usize => Some ( id) ,
313
- _ => None ,
314
- } ;
315
-
316
- Ok ( PaginatedResponse {
317
- nodes : models. into_iter ( ) . map ( Media :: from) . collect ( ) ,
318
- page_info : CursorPaginationInfo {
319
- current_cursor,
320
- next_cursor,
321
- limit : info. limit ,
322
- }
323
- . into ( ) ,
324
- } )
273
+ Pagination :: Cursor ( _) => {
274
+ // FIXME: See https://github.com/SeaQL/sea-orm/issues/2407
275
+ Err ( "Cursor pagination not supported for keepReading at this time" . into ( ) )
276
+ // let user_id = user.id.clone();
277
+ // let mut cursor =
278
+ // query.cursor_by_other(reading_session::Column::UpdatedAt);
279
+
280
+ // if let Some(ref id) = info.after {
281
+ // let id = id.clone(); // Clone for closure on_condition
282
+ // let record = media::Entity::find_for_user(user)
283
+ // .select_only()
284
+ // .column(reading_session::Column::UpdatedAt)
285
+ // .join_rev(
286
+ // JoinType::InnerJoin,
287
+ // reading_session::Entity::belongs_to(media::Entity)
288
+ // .from(reading_session::Column::MediaId)
289
+ // .to(media::Column::Id)
290
+ // .on_condition(move |_left, _right| {
291
+ // Condition::all().add(
292
+ // reading_session::Column::UserId
293
+ // .eq(user_id.clone())
294
+ // .and(
295
+ // reading_session::Column::MediaId
296
+ // .eq(id.clone()),
297
+ // ),
298
+ // )
299
+ // })
300
+ // .into(),
301
+ // )
302
+ // .into_model::<media::ReadingSessionUpdatedAtCmpSelect>()
303
+ // .one(conn)
304
+ // .await?
305
+ // .ok_or("Cursor not found")?;
306
+ // cursor.after(record.updated_at);
307
+ // }
308
+ // cursor.first(info.limit).desc();
309
+
310
+ // let models = cursor
311
+ // .group_by(media::Column::Id)
312
+ // .into_model::<media::ModelWithMetadata>()
313
+ // .all(conn)
314
+ // .await?;
315
+ // let current_cursor = info
316
+ // .after
317
+ // .or_else(|| models.first().map(|m| m.media.id.clone()));
318
+ // let next_cursor = match models.last().map(|m| m.media.id.clone()) {
319
+ // Some(id) if models.len() == info.limit as usize => Some(id),
320
+ // _ => None,
321
+ // };
322
+
323
+ // Ok(PaginatedResponse {
324
+ // nodes: models.into_iter().map(Media::from).collect(),
325
+ // page_info: CursorPaginationInfo {
326
+ // current_cursor,
327
+ // next_cursor,
328
+ // limit: info.limit,
329
+ // }
330
+ // .into(),
331
+ // })
325
332
} ,
326
333
Pagination :: Offset ( info) => {
327
334
let count = query. clone ( ) . count ( conn) . await ?;
328
335
329
336
let models = query
337
+ . find_also_related ( media_metadata:: Entity )
330
338
. offset ( info. offset ( ) )
331
339
. limit ( info. limit ( ) )
332
- . into_model :: < media:: ModelWithMetadata > ( )
333
340
. all ( conn)
334
- . await ?;
341
+ . await ?
342
+ . into_iter ( )
343
+ . map ( |( media, _session, metadata) | media:: ModelWithMetadata {
344
+ media,
345
+ metadata,
346
+ } )
347
+ . collect :: < Vec < _ > > ( ) ;
335
348
336
349
Ok ( PaginatedResponse {
337
350
nodes : models. into_iter ( ) . map ( Media :: from) . collect ( ) ,
@@ -340,9 +353,16 @@ impl MediaQuery {
340
353
} ,
341
354
Pagination :: None ( _) => {
342
355
let models = query
343
- . into_model :: < media :: ModelWithMetadata > ( )
356
+ . find_also_related ( media_metadata :: Entity )
344
357
. all ( conn)
345
- . await ?;
358
+ . await ?
359
+ . into_iter ( )
360
+ . map ( |( media, _session, metadata) | media:: ModelWithMetadata {
361
+ media,
362
+ metadata,
363
+ } )
364
+ . collect :: < Vec < _ > > ( ) ;
365
+
346
366
let count = models. len ( ) . try_into ( ) ?;
347
367
348
368
Ok ( PaginatedResponse {
0 commit comments