Merge pull request #324 from TeamPiped/channels-info-unauth

Unauthenticated channel info endpoint.
This commit is contained in:
Kavin 2022-08-01 17:43:05 +05:30 committed by GitHub
commit bfabe1e9ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

View file

@ -306,6 +306,14 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/subscriptions/unauthenticated", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(ResponseHelper.unauthenticatedSubscriptionsResponse(
Objects.requireNonNull(request.getQueryParameter("channels")).split(",")
), "public, s-maxage=120");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(POST, "/user/playlists/create", AsyncServlet.ofBlocking(executor, request -> {
try {
var name = Constants.mapper.readTree(request.loadBody().getResult().asArray()).get("name").textValue();

View file

@ -1213,6 +1213,35 @@ public class ResponseHelper {
}
public static byte[] unauthenticatedSubscriptionsResponse(String[] channelIds)
throws IOException {
Set<String> filtered = Arrays.stream(channelIds)
.filter(StringUtils::isNotBlank)
.filter(id -> id.matches("[A-Za-z\\d_-]+"))
.collect(Collectors.toUnmodifiableSet());
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
CriteriaBuilder cb = s.getCriteriaBuilder();
var query = cb.createQuery(me.kavin.piped.utils.obj.db.Channel.class);
var root = query.from(me.kavin.piped.utils.obj.db.Channel.class);
query.select(root);
query.where(root.get("uploader_id").in(filtered));
var channels = s.createQuery(query).list();
List<SubscriptionChannel> subscriptionItems = channels
.stream().parallel()
.sorted(Comparator.comparing(me.kavin.piped.utils.obj.db.Channel::getUploader, String.CASE_INSENSITIVE_ORDER))
.map(channel -> new SubscriptionChannel("/channel/" + channel.getUploaderId(),
channel.getUploader(), rewriteURL(channel.getUploaderAvatar()), channel.isVerified()))
.collect(Collectors.toUnmodifiableList());
return mapper.writeValueAsBytes(subscriptionItems);
}
}
public static byte[] createPlaylist(String session, String name) throws IOException {
if (StringUtils.isBlank(name))

View file

@ -131,3 +131,4 @@ curl ${CURLOPTS[@]} $HOST/user/delete -X POST -H "Content-Type: application/json
CHANNEL_IDS=UCsXVk37bltHxD1rDPwtNM8Q,UCXuqSBlHAE6Xw-yeJA0Tunw
curl ${CURLOPTS[@]} $HOST/feed/unauthenticated -G --data-urlencode "channels=$CHANNEL_IDS" || exit 1
curl ${CURLOPTS[@]} $HOST/feed/unauthenticated/rss -G --data-urlencode "channels=$CHANNEL_IDS" || exit 1
curl ${CURLOPTS[@]} $HOST/subscriptions/unauthenticated -G --data-urlencode "channels=$CHANNEL_IDS" || exit 1