From b38c641b5f66a8a7efb7665b7eadc3653b027168 Mon Sep 17 00:00:00 2001 From: theanonymousexyz Date: Sat, 23 Apr 2022 00:06:50 +0200 Subject: [PATCH] Avoid raw SQL in unsubscribeResponse() --- .../java/me/kavin/piped/utils/ResponseHelper.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 091dad8..7dde9ae 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -794,10 +794,14 @@ public class ResponseHelper { 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(); + if (user.getSubscribed().contains(channelId)) { + + user.getSubscribed().removeIf(sub -> sub.equals(channelId)); + s.update(user); + s.getTransaction().begin(); + s.getTransaction().commit(); + } + return Constants.mapper.writeValueAsBytes(new AcceptedResponse()); }