Add optional limit parameter.

This commit is contained in:
Kavin 2023-05-09 22:31:17 +01:00
parent 148457e5d0
commit ac0ffefa33
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 43 additions and 7 deletions

View file

@ -320,7 +320,19 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} }
})).map(GET, "/feed", AsyncServlet.ofBlocking(executor, request -> { })).map(GET, "/feed", AsyncServlet.ofBlocking(executor, request -> {
try { 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"); "private");
} catch (Exception e) { } catch (Exception e) {
return getErrorResponse(e, request.getPath()); return getErrorResponse(e, request.getPath());
@ -334,8 +346,20 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} }
})).map(GET, "/feed/unauthenticated", AsyncServlet.ofBlocking(executor, request -> { })).map(GET, "/feed/unauthenticated", AsyncServlet.ofBlocking(executor, request -> {
try { 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( return getJsonResponse(FeedHandlers.unauthenticatedFeedResponse(
getArray(request.getQueryParameter("channels")), request.getQueryParameter("start") getArray(request.getQueryParameter("channels")), request.getQueryParameter("start"), limit
), "public, s-maxage=120"); ), "public, s-maxage=120");
} catch (Exception e) { } catch (Exception e) {
return getErrorResponse(e, request.getPath()); return getErrorResponse(e, request.getPath());
@ -344,7 +368,19 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
try { try {
String[] subscriptions = mapper.readValue(request.loadBody().getResult().asArray(), String[] subscriptions = mapper.readValue(request.loadBody().getResult().asArray(),
String[].class); 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) { } catch (Exception e) {
return getErrorResponse(e, request.getPath()); return getErrorResponse(e, request.getPath());
} }

View file

@ -93,7 +93,7 @@ public class FeedHandlers {
} }
} }
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)) if (StringUtils.isBlank(session))
ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session is a required parameter")); ExceptionHandler.throwErrorResponse(new InvalidRequestResponse("session is a required parameter"));
@ -126,7 +126,7 @@ public class FeedHandlers {
.orderBy(cb.desc(root.get("uploaded"))); .orderBy(cb.desc(root.get("uploaded")));
List<StreamItem> feedItems = s.createQuery(criteria) List<StreamItem> feedItems = s.createQuery(criteria)
.setMaxResults(100) .setMaxResults(limit)
.setTimeout(20).stream() .setTimeout(20).stream()
.parallel().map(video -> { .parallel().map(video -> {
var channel = video.getChannel(); var channel = video.getChannel();
@ -200,7 +200,7 @@ public class FeedHandlers {
return null; 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) Set<String> filtered = Arrays.stream(channelIds)
.filter(ChannelHelpers::isValidId) .filter(ChannelHelpers::isValidId)
@ -230,7 +230,7 @@ public class FeedHandlers {
.orderBy(cb.desc(root.get("uploaded"))); .orderBy(cb.desc(root.get("uploaded")));
List<StreamItem> feedItems = s.createQuery(criteria) List<StreamItem> feedItems = s.createQuery(criteria)
.setMaxResults(100) .setMaxResults(limit)
.setTimeout(20).stream() .setTimeout(20).stream()
.parallel().map(video -> { .parallel().map(video -> {
var channel = video.getChannel(); var channel = video.getChannel();