mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Fix subscription imports and improve session handling. (#228)
This commit is contained in:
parent
d1789ace9f
commit
b1e875bcf5
2 changed files with 85 additions and 83 deletions
|
@ -13,7 +13,8 @@ import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
public class DatabaseHelper {
|
public class DatabaseHelper {
|
||||||
|
|
||||||
public static final User getUserFromSession(Session s, String session) {
|
public static final User getUserFromSession(String session) {
|
||||||
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||||
CriteriaQuery<User> cr = cb.createQuery(User.class);
|
CriteriaQuery<User> cr = cb.createQuery(User.class);
|
||||||
Root<User> root = cr.from(User.class);
|
Root<User> root = cr.from(User.class);
|
||||||
|
@ -21,8 +22,10 @@ public class DatabaseHelper {
|
||||||
|
|
||||||
return s.createQuery(cr).uniqueResult();
|
return s.createQuery(cr).uniqueResult();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static final User getUserFromSessionWithSubscribed(Session s, String session) {
|
public static final User getUserFromSessionWithSubscribed(String session) {
|
||||||
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||||
CriteriaQuery<User> cr = cb.createQuery(User.class);
|
CriteriaQuery<User> cr = cb.createQuery(User.class);
|
||||||
Root<User> root = cr.from(User.class);
|
Root<User> root = cr.from(User.class);
|
||||||
|
@ -31,6 +34,7 @@ public class DatabaseHelper {
|
||||||
|
|
||||||
return s.createQuery(cr).uniqueResult();
|
return s.createQuery(cr).uniqueResult();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static final Channel getChannelFromId(Session s, String id) {
|
public static final Channel getChannelFromId(Session s, String id) {
|
||||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||||
|
|
|
@ -597,7 +597,7 @@ public class ResponseHelper {
|
||||||
|
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session);
|
User user = DatabaseHelper.getUserFromSessionWithSubscribed(session);
|
||||||
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
if (!user.getSubscribed().contains(channelId)) {
|
if (!user.getSubscribed().contains(channelId)) {
|
||||||
|
@ -657,16 +657,17 @@ public class ResponseHelper {
|
||||||
public static byte[] unsubscribeResponse(String session, String channelId)
|
public static byte[] unsubscribeResponse(String session, String channelId)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
User user = DatabaseHelper.getUserFromSession(session);
|
||||||
User user = DatabaseHelper.getUserFromSession(s, session);
|
|
||||||
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
s.getTransaction().begin();
|
s.getTransaction().begin();
|
||||||
s.createNativeQuery("delete from users_subscribed where subscriber = :id and channel = :channel")
|
s.createNativeQuery("delete from users_subscribed where subscriber = :id and channel = :channel")
|
||||||
.setParameter("id", user.getId()).setParameter("channel", channelId).executeUpdate();
|
.setParameter("id", user.getId()).setParameter("channel", channelId).executeUpdate();
|
||||||
s.getTransaction().commit();
|
s.getTransaction().commit();
|
||||||
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
|
@ -690,11 +691,11 @@ public class ResponseHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] feedResponse(String session) throws IOException {
|
public static byte[] feedResponse(String session) throws IOException {
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSession(s, session);
|
User user = DatabaseHelper.getUserFromSession(session);
|
||||||
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||||
|
|
||||||
|
@ -727,20 +728,19 @@ public class ResponseHelper {
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(feedItems);
|
return Constants.mapper.writeValueAsBytes(feedItems);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] feedResponseRSS(String session) throws IOException, FeedException {
|
public static byte[] feedResponseRSS(String session) throws IOException, FeedException {
|
||||||
|
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
User user = DatabaseHelper.getUserFromSession(session);
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSession(s, session);
|
|
||||||
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
|
||||||
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
SyndFeed feed = new SyndFeedImpl();
|
SyndFeed feed = new SyndFeedImpl();
|
||||||
feed.setFeedType("atom_1.0");
|
feed.setFeedType("atom_1.0");
|
||||||
feed.setTitle("Piped - Feed");
|
feed.setTitle("Piped - Feed");
|
||||||
|
@ -789,20 +789,20 @@ public class ResponseHelper {
|
||||||
|
|
||||||
return new SyndFeedOutput().outputString(feed).getBytes(UTF_8);
|
return new SyndFeedOutput().outputString(feed).getBytes(UTF_8);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] importResponse(String session, String[] channelIds, boolean override) throws IOException {
|
public static byte[] importResponse(String session, String[] channelIds, boolean override) throws IOException {
|
||||||
|
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session);
|
User user = DatabaseHelper.getUserFromSessionWithSubscribed(session);
|
||||||
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
Multithreading.runAsync(() -> {
|
||||||
|
try (Session sess = DatabaseSessionFactory.createSession()) {
|
||||||
if (override)
|
if (override)
|
||||||
user.setSubscribed(Arrays.asList(channelIds));
|
user.setSubscribed(Arrays.asList(channelIds));
|
||||||
else
|
else
|
||||||
|
@ -811,8 +811,9 @@ public class ResponseHelper {
|
||||||
user.getSubscribed().add(channelId);
|
user.getSubscribed().add(channelId);
|
||||||
|
|
||||||
if (channelIds.length > 0) {
|
if (channelIds.length > 0) {
|
||||||
s.update(user);
|
sess.update(user);
|
||||||
s.beginTransaction().commit();
|
sess.beginTransaction().commit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -870,16 +871,14 @@ public class ResponseHelper {
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] subscriptionsResponse(String session)
|
public static byte[] subscriptionsResponse(String session)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
User user = DatabaseHelper.getUserFromSession(session);
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSession(s, session);
|
|
||||||
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
List<SubscriptionChannel> subscriptionItems = new ObjectArrayList<>();
|
List<SubscriptionChannel> subscriptionItems = new ObjectArrayList<>();
|
||||||
|
|
||||||
|
@ -902,13 +901,12 @@ public class ResponseHelper {
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(subscriptionItems);
|
return Constants.mapper.writeValueAsBytes(subscriptionItems);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String registeredBadgeRedirect() {
|
public static String registeredBadgeRedirect() {
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
long registered = (Long) s.createQuery("select count(*) from User").uniqueResult();
|
long registered = (Long) s.createQuery("select count(*) from User").uniqueResult();
|
||||||
|
|
Loading…
Reference in a new issue