mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Add route to see subscriptions.
This commit is contained in:
parent
b4c96729b7
commit
f66e77330e
4 changed files with 67 additions and 0 deletions
|
@ -260,6 +260,13 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return getErrorResponse(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);
|
return new CustomServletDecorator(router);
|
||||||
|
|
|
@ -46,6 +46,15 @@ public class DatabaseHelper {
|
||||||
return s.createQuery(cr).uniqueResult();
|
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) {
|
public static final List<Video> getVideosFromChannelIds(Session s, List<String> id) {
|
||||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||||
CriteriaQuery<Video> cr = cb.createQuery(Video.class);
|
CriteriaQuery<Video> cr = cb.createQuery(Video.class);
|
||||||
|
|
|
@ -75,6 +75,7 @@ import me.kavin.piped.utils.obj.SearchResults;
|
||||||
import me.kavin.piped.utils.obj.StreamItem;
|
import me.kavin.piped.utils.obj.StreamItem;
|
||||||
import me.kavin.piped.utils.obj.Streams;
|
import me.kavin.piped.utils.obj.Streams;
|
||||||
import me.kavin.piped.utils.obj.StreamsPage;
|
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.Subtitle;
|
||||||
import me.kavin.piped.utils.obj.db.PubSub;
|
import me.kavin.piped.utils.obj.db.PubSub;
|
||||||
import me.kavin.piped.utils.obj.db.User;
|
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 {
|
private static final String getLBRYStreamURL(String videoId) throws IOException, InterruptedException {
|
||||||
|
|
||||||
String lbryId = new JSONObject(Constants.h2client.send(HttpRequest
|
String lbryId = new JSONObject(Constants.h2client.send(HttpRequest
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue