Skip to content

Commit

Permalink
Add optional limit parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
FireMasterK committed May 9, 2023
1 parent 148457e commit ac0ffef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
42 changes: 39 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,19 @@ AsyncServlet mainServlet(Executor executor) {
}
})).map(GET, "/feed", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(FeedHandlers.feedResponse(request.getQueryParameter("authToken"), request.getQueryParameter("start")),

int limit;
try {
var limitStr = request.getQueryParameter("limit");
if (!StringUtils.isEmpty(limitStr))
limit = Math.min(Integer.parseInt(limitStr), 100);
else
limit = 100;
} catch (NumberFormatException e) {
limit = 100;
}

return getJsonResponse(FeedHandlers.feedResponse(request.getQueryParameter("authToken"), request.getQueryParameter("start"), limit),
"private");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
Expand All @@ -334,8 +346,20 @@ AsyncServlet mainServlet(Executor executor) {
}
})).map(GET, "/feed/unauthenticated", AsyncServlet.ofBlocking(executor, request -> {
try {

int limit;
try {
var limitStr = request.getQueryParameter("limit");
if (!StringUtils.isEmpty(limitStr))
limit = Math.min(Integer.parseInt(limitStr), 100);
else
limit = 100;
} catch (NumberFormatException e) {
limit = 100;
}

return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(
getArray(request.getQueryParameter("channels")), request.getQueryParameter("start")
getArray(request.getQueryParameter("channels")), request.getQueryParameter("start"), limit
), "public, s-maxage=120");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
Expand All @@ -344,7 +368,19 @@ AsyncServlet mainServlet(Executor executor) {
try {
String[] subscriptions = mapper.readValue(request.loadBody().getResult().asArray(),
String[].class);
return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(subscriptions, request.getQueryParameter("start")), "public, s-maxage=120");

int limit;
try {
var limitStr = request.getQueryParameter("limit");
if (!StringUtils.isEmpty(limitStr))
limit = Math.min(Integer.parseInt(limitStr), 100);
else
limit = 100;
} catch (NumberFormatException e) {
limit = 100;
}

return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(subscriptions, request.getQueryParameter("start"), limit), "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, String start) throws IOException {
public static byte[] feedResponse(String session, String start, int limit) throws IOException {

if (StringUtils.isBlank(session))
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session is a required parameter"));
Expand Down Expand Up @@ -126,7 +126,7 @@ public static byte[] feedResponse(String session, String start) throws IOExcepti
.orderBy(cb.desc(root.get("uploaded")));

List<StreamItem> feedItems = s.createQuery(criteria)
.setMaxResults(100)
.setMaxResults(limit)
.setTimeout(20).stream()
.parallel().map(video -> {
var channel = video.getChannel();
Expand Down Expand Up @@ -200,7 +200,7 @@ public static byte[] feedResponseRSS(String session) throws FeedException {
return null;
}

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

Set<String> filtered = Arrays.stream(channelIds)
.filter(ChannelHelpers::isValidId)
Expand Down Expand Up @@ -230,7 +230,7 @@ public static byte[] unauthenticatedFeedResponse(String[] channelIds, String sta
.orderBy(cb.desc(root.get("uploaded")));

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

0 comments on commit ac0ffef

Please sign in to comment.