@@ -93,7 +93,7 @@ public static byte[] isSubscribedResponse(String session, String channelId) thro
9393 }
9494 }
9595
96- public static byte [] feedResponse (String session ) throws IOException {
96+ public static byte [] feedResponse (String session , String start ) throws IOException {
9797
9898 if (StringUtils .isBlank (session ))
9999 ExceptionHandler .throwErrorResponse (new InvalidRequestResponse ("session is a required parameter" ));
@@ -114,13 +114,20 @@ public static byte[] feedResponse(String session) throws IOException {
114114 subquery .select (subroot .get ("subscribed_ids" ))
115115 .where (cb .equal (subroot .get ("id" ), user .getId ()));
116116
117+ var channelPredicate = root .get ("channel" ).get ("uploader_id" ).in (subquery );
118+
117119 criteria .select (root )
118120 .where (
119- root .get ("channel" ).get ("uploader_id" ).in (subquery )
121+ start == null ? channelPredicate : cb .and (
122+ channelPredicate ,
123+ cb .lessThan (root .get ("uploaded" ), Long .parseLong (start ))
124+ )
120125 )
121126 .orderBy (cb .desc (root .get ("uploaded" )));
122127
123- List <StreamItem > feedItems = s .createQuery (criteria ).setTimeout (20 ).stream ()
128+ List <StreamItem > feedItems = s .createQuery (criteria )
129+ .setMaxResults (100 )
130+ .setTimeout (20 ).stream ()
124131 .parallel ().map (video -> {
125132 var channel = video .getChannel ();
126133
@@ -193,7 +200,7 @@ public static byte[] feedResponseRSS(String session) throws FeedException {
193200 return null ;
194201 }
195202
196- public static byte [] unauthenticatedFeedResponse (String [] channelIds ) throws Exception {
203+ public static byte [] unauthenticatedFeedResponse (String [] channelIds , String start ) throws Exception {
197204
198205 Set <String > filtered = Arrays .stream (channelIds )
199206 .filter (ChannelHelpers ::isValidId )
@@ -211,13 +218,20 @@ public static byte[] unauthenticatedFeedResponse(String[] channelIds) throws Exc
211218 var root = criteria .from (Video .class );
212219 root .fetch ("channel" , JoinType .RIGHT );
213220
221+ var channelPredicate = root .get ("channel" ).get ("id" ).in (filtered );
222+
214223 criteria .select (root )
215- .where (cb .and (
216- root .get ("channel" ).get ("id" ).in (filtered )
217- ))
224+ .where (
225+ start == null ? channelPredicate : cb .and (
226+ channelPredicate ,
227+ cb .lessThan (root .get ("uploaded" ), Long .parseLong (start ))
228+ )
229+ )
218230 .orderBy (cb .desc (root .get ("uploaded" )));
219231
220- List <StreamItem > feedItems = s .createQuery (criteria ).setTimeout (20 ).stream ()
232+ List <StreamItem > feedItems = s .createQuery (criteria )
233+ .setMaxResults (100 )
234+ .setTimeout (20 ).stream ()
221235 .parallel ().map (video -> {
222236 var channel = video .getChannel ();
223237
@@ -334,6 +348,7 @@ private static void addMissingChannels(Collection<String> channelIds) {
334348
335349 var tr = s .beginTransaction ();
336350 channelIds .stream ()
351+ .filter (ChannelHelpers ::isValidId )
337352 .filter (id -> !existing .contains (id ))
338353 .map (UnauthenticatedSubscription ::new )
339354 .forEach (s ::insert );
0 commit comments