Skip to content

Commit

Permalink
Add support for feed pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
FireMasterK committed May 1, 2023
1 parent 8614e6a commit ac121aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/main/java/me/kavin/piped/server/ServerLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ AsyncServlet mainServlet(Executor executor) {
}
})).map(GET, "/feed", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(FeedHandlers.feedResponse(request.getQueryParameter("authToken")),
return getJsonResponse(FeedHandlers.feedResponse(request.getQueryParameter("authToken"), request.getQueryParameter("start")),
"private");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
Expand All @@ -335,7 +335,7 @@ AsyncServlet mainServlet(Executor executor) {
})).map(GET, "/feed/unauthenticated", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(
getArray(request.getQueryParameter("channels"))
getArray(request.getQueryParameter("channels")), request.getQueryParameter("start")
), "public, s-maxage=120");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
Expand All @@ -344,7 +344,7 @@ AsyncServlet mainServlet(Executor executor) {
try {
String[] subscriptions = mapper.readValue(request.loadBody().getResult().asArray(),
String[].class);
return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(subscriptions), "public, s-maxage=120");
return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(subscriptions, request.getQueryParameter("start")), "public, s-maxage=120");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static byte[] isSubscribedResponse(String session, String channelId) thro
}
}

public static byte[] feedResponse(String session) throws IOException {
public static byte[] feedResponse(String session, String start) throws IOException {

if (StringUtils.isBlank(session))
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session is a required parameter"));
Expand All @@ -114,13 +114,20 @@ public static byte[] feedResponse(String session) throws IOException {
subquery.select(subroot.get("subscribed_ids"))
.where(cb.equal(subroot.get("id"), user.getId()));

var channelPredicate = root.get("channel").get("uploader_id").in(subquery);

criteria.select(root)
.where(
root.get("channel").get("uploader_id").in(subquery)
start == null ? channelPredicate : cb.and(
channelPredicate,
cb.lessThan(root.get("uploaded"), Long.parseLong(start))
)
)
.orderBy(cb.desc(root.get("uploaded")));

List<StreamItem> feedItems = s.createQuery(criteria).setTimeout(20).stream()
List<StreamItem> feedItems = s.createQuery(criteria)
.setMaxResults(100)
.setTimeout(20).stream()
.parallel().map(video -> {
var channel = video.getChannel();

Expand Down Expand Up @@ -193,7 +200,7 @@ public static byte[] feedResponseRSS(String session) throws FeedException {
return null;
}

public static byte[] unauthenticatedFeedResponse(String[] channelIds) throws Exception {
public static byte[] unauthenticatedFeedResponse(String[] channelIds, String start) throws Exception {

Set<String> filtered = Arrays.stream(channelIds)
.filter(ChannelHelpers::isValidId)
Expand All @@ -211,13 +218,20 @@ public static byte[] unauthenticatedFeedResponse(String[] channelIds) throws Exc
var root = criteria.from(Video.class);
root.fetch("channel", JoinType.RIGHT);

var channelPredicate = root.get("channel").get("id").in(filtered);

criteria.select(root)
.where(cb.and(
root.get("channel").get("id").in(filtered)
))
.where(
start == null ? channelPredicate : cb.and(
channelPredicate,
cb.lessThan(root.get("uploaded"), Long.parseLong(start))
)
)
.orderBy(cb.desc(root.get("uploaded")));

List<StreamItem> feedItems = s.createQuery(criteria).setTimeout(20).stream()
List<StreamItem> feedItems = s.createQuery(criteria)
.setMaxResults(100)
.setTimeout(20).stream()
.parallel().map(video -> {
var channel = video.getChannel();

Expand Down Expand Up @@ -334,6 +348,7 @@ private static void addMissingChannels(Collection<String> channelIds) {

var tr = s.beginTransaction();
channelIds.stream()
.filter(ChannelHelpers::isValidId)
.filter(id -> !existing.contains(id))
.map(UnauthenticatedSubscription::new)
.forEach(s::insert);
Expand Down

0 comments on commit ac121aa

Please sign in to comment.