Skip to content

Commit ac121aa

Browse files
committed
Add support for feed pagination
1 parent 8614e6a commit ac121aa

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/main/java/me/kavin/piped/server/ServerLauncher.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ AsyncServlet mainServlet(Executor executor) {
320320
}
321321
})).map(GET, "/feed", AsyncServlet.ofBlocking(executor, request -> {
322322
try {
323-
return getJsonResponse(FeedHandlers.feedResponse(request.getQueryParameter("authToken")),
323+
return getJsonResponse(FeedHandlers.feedResponse(request.getQueryParameter("authToken"), request.getQueryParameter("start")),
324324
"private");
325325
} catch (Exception e) {
326326
return getErrorResponse(e, request.getPath());
@@ -335,7 +335,7 @@ AsyncServlet mainServlet(Executor executor) {
335335
})).map(GET, "/feed/unauthenticated", AsyncServlet.ofBlocking(executor, request -> {
336336
try {
337337
return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(
338-
getArray(request.getQueryParameter("channels"))
338+
getArray(request.getQueryParameter("channels")), request.getQueryParameter("start")
339339
), "public, s-maxage=120");
340340
} catch (Exception e) {
341341
return getErrorResponse(e, request.getPath());
@@ -344,7 +344,7 @@ AsyncServlet mainServlet(Executor executor) {
344344
try {
345345
String[] subscriptions = mapper.readValue(request.loadBody().getResult().asArray(),
346346
String[].class);
347-
return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(subscriptions), "public, s-maxage=120");
347+
return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(subscriptions, request.getQueryParameter("start")), "public, s-maxage=120");
348348
} catch (Exception e) {
349349
return getErrorResponse(e, request.getPath());
350350
}

src/main/java/me/kavin/piped/server/handlers/auth/FeedHandlers.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)