Add route to see subscriptions.

This commit is contained in:
FireMasterK 2021-07-23 01:34:21 +05:30
parent b4c96729b7
commit f66e77330e
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
4 changed files with 67 additions and 0 deletions

View file

@ -260,6 +260,13 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) {
return getErrorResponse(e);
}
})).map(GET, "/subscriptions", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(ResponseHelper.subscriptionsResponse(request.getHeader(AUTHORIZATION)),
"private");
} catch (Exception e) {
return getErrorResponse(e);
}
}));
return new CustomServletDecorator(router);

View file

@ -46,6 +46,15 @@ public class DatabaseHelper {
return s.createQuery(cr).uniqueResult();
}
public static final List<Channel> getChannelFromIds(Session s, List<String> id) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<Channel> cr = cb.createQuery(Channel.class);
Root<Channel> root = cr.from(Channel.class);
cr.select(root).where(root.get("uploader_id").in(id));
return s.createQuery(cr).getResultList();
}
public static final List<Video> getVideosFromChannelIds(Session s, List<String> id) {
CriteriaBuilder cb = s.getCriteriaBuilder();
CriteriaQuery<Video> cr = cb.createQuery(Video.class);

View file

@ -75,6 +75,7 @@ import me.kavin.piped.utils.obj.SearchResults;
import me.kavin.piped.utils.obj.StreamItem;
import me.kavin.piped.utils.obj.Streams;
import me.kavin.piped.utils.obj.StreamsPage;
import me.kavin.piped.utils.obj.SubscriptionChannel;
import me.kavin.piped.utils.obj.Subtitle;
import me.kavin.piped.utils.obj.db.PubSub;
import me.kavin.piped.utils.obj.db.User;
@ -864,6 +865,41 @@ public class ResponseHelper {
}
public static final byte[] subscriptionsResponse(String session)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Session s = DatabaseSessionFactory.createSession();
User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session);
if (user != null) {
List<SubscriptionChannel> subscriptionItems = new ObjectArrayList<>();
if (user.getSubscribed() != null && !user.getSubscribed().isEmpty()) {
List<me.kavin.piped.utils.obj.db.Channel> channels = DatabaseHelper.getChannelFromIds(s,
user.getSubscribed());
channels.forEach(channel -> {
subscriptionItems.add(new SubscriptionChannel("/channel/" + channel.getUploaderId(),
channel.getUploader(), rewriteURL(channel.getUploaderAvatar()), channel.isVerified()));
});
Collections.sort(subscriptionItems, (a, b) -> (a.name.compareTo(b.name)));
}
s.close();
return Constants.mapper.writeValueAsBytes(subscriptionItems);
}
s.close();
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
}
private static final String getLBRYStreamURL(String videoId) throws IOException, InterruptedException {
String lbryId = new JSONObject(Constants.h2client.send(HttpRequest

View file

@ -0,0 +1,15 @@
package me.kavin.piped.utils.obj;
public class SubscriptionChannel {
public String url, name, avatar;
public boolean verified;
public SubscriptionChannel(String url, String name, String avatar, boolean verified) {
this.url = url;
this.name = name;
this.avatar = avatar;
this.verified = verified;
}
}