Fix subscription imports and improve session handling. (#228)

This commit is contained in:
Kavin 2022-03-19 18:57:00 +00:00 committed by GitHub
parent d1789ace9f
commit b1e875bcf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 83 deletions

View file

@ -13,7 +13,8 @@ import javax.persistence.criteria.Root;
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();
CriteriaQuery<User> cr = cb.createQuery(User.class);
Root<User> root = cr.from(User.class);
@ -21,8 +22,10 @@ public class DatabaseHelper {
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();
CriteriaQuery<User> cr = cb.createQuery(User.class);
Root<User> root = cr.from(User.class);
@ -31,6 +34,7 @@ public class DatabaseHelper {
return s.createQuery(cr).uniqueResult();
}
}
public static final Channel getChannelFromId(Session s, String id) {
CriteriaBuilder cb = s.getCriteriaBuilder();

View file

@ -597,7 +597,7 @@ public class ResponseHelper {
try (Session s = DatabaseSessionFactory.createSession()) {
User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session);
User user = DatabaseHelper.getUserFromSessionWithSubscribed(session);
if (user != null) {
if (!user.getSubscribed().contains(channelId)) {
@ -657,16 +657,17 @@ public class ResponseHelper {
public static byte[] unsubscribeResponse(String session, String channelId)
throws IOException {
try (Session s = DatabaseSessionFactory.createSession()) {
User user = DatabaseHelper.getUserFromSession(s, session);
User user = DatabaseHelper.getUserFromSession(session);
if (user != null) {
try (Session s = DatabaseSessionFactory.createSession()) {
s.getTransaction().begin();
s.createNativeQuery("delete from users_subscribed where subscriber = :id and channel = :channel")
.setParameter("id", user.getId()).setParameter("channel", channelId).executeUpdate();
s.getTransaction().commit();
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
}
}
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
@ -690,11 +691,11 @@ public class ResponseHelper {
}
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) {
try (Session s = DatabaseSessionFactory.createSession()) {
CriteriaBuilder cb = s.getCriteriaBuilder();
@ -727,20 +728,19 @@ public class ResponseHelper {
return Constants.mapper.writeValueAsBytes(feedItems);
}
}
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
}
}
public static byte[] feedResponseRSS(String session) throws IOException, FeedException {
try (Session s = DatabaseSessionFactory.createSession()) {
User user = DatabaseHelper.getUserFromSession(s, session);
User user = DatabaseHelper.getUserFromSession(session);
if (user != null) {
try (Session s = DatabaseSessionFactory.createSession()) {
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("atom_1.0");
feed.setTitle("Piped - Feed");
@ -789,20 +789,20 @@ public class ResponseHelper {
return new SyndFeedOutput().outputString(feed).getBytes(UTF_8);
}
}
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
}
}
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) {
Multithreading.runAsync(() -> {
try (Session sess = DatabaseSessionFactory.createSession()) {
if (override)
user.setSubscribed(Arrays.asList(channelIds));
else
@ -811,8 +811,9 @@ public class ResponseHelper {
user.getSubscribed().add(channelId);
if (channelIds.length > 0) {
s.update(user);
s.beginTransaction().commit();
sess.update(user);
sess.beginTransaction().commit();
}
}
});
@ -870,16 +871,14 @@ public class ResponseHelper {
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
}
}
public static byte[] subscriptionsResponse(String session)
throws IOException {
try (Session s = DatabaseSessionFactory.createSession()) {
User user = DatabaseHelper.getUserFromSession(s, session);
User user = DatabaseHelper.getUserFromSession(session);
if (user != null) {
try (Session s = DatabaseSessionFactory.createSession()) {
List<SubscriptionChannel> subscriptionItems = new ObjectArrayList<>();
@ -902,13 +901,12 @@ public class ResponseHelper {
return Constants.mapper.writeValueAsBytes(subscriptionItems);
}
}
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
}
}
public static String registeredBadgeRedirect() {
try (Session s = DatabaseSessionFactory.createSession()) {
long registered = (Long) s.createQuery("select count(*) from User").uniqueResult();