mirror of
https://github.com/TeamPiped/Piped-Backend
synced 2025-09-06 05:21:09 +02:00
Use try with for sessions (#222)
This commit is contained in:
parent
860bd57b3d
commit
43d6dc93c0
@ -31,8 +31,7 @@ public class Main {
|
|||||||
new Timer().scheduleAtFixedRate(new TimerTask() {
|
new Timer().scheduleAtFixedRate(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
|
||||||
|
|
||||||
CriteriaBuilder cb = s.getCriteriaBuilder();
|
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||||
CriteriaQuery<PubSub> criteria = cb.createQuery(PubSub.class);
|
CriteriaQuery<PubSub> criteria = cb.createQuery(PubSub.class);
|
||||||
@ -51,16 +50,13 @@ public class Main {
|
|||||||
for (PubSub pubsub : pubSubList)
|
for (PubSub pubsub : pubSubList)
|
||||||
if (pubsub != null)
|
if (pubsub != null)
|
||||||
Multithreading.runAsyncLimitedPubSub(() -> {
|
Multithreading.runAsyncLimitedPubSub(() -> {
|
||||||
Session sess = DatabaseSessionFactory.createSession();
|
try (Session sess = DatabaseSessionFactory.createSession()) {
|
||||||
try {
|
|
||||||
ResponseHelper.subscribePubSub(pubsub.getId(), sess);
|
ResponseHelper.subscribePubSub(pubsub.getId(), sess);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
sess.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
s.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -70,8 +66,7 @@ public class Main {
|
|||||||
new Timer().scheduleAtFixedRate(new TimerTask() {
|
new Timer().scheduleAtFixedRate(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
|
||||||
|
|
||||||
Transaction tr = s.getTransaction();
|
Transaction tr = s.getTransaction();
|
||||||
|
|
||||||
@ -84,7 +79,6 @@ public class Main {
|
|||||||
|
|
||||||
tr.commit();
|
tr.commit();
|
||||||
|
|
||||||
s.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -55,12 +55,12 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
|||||||
new InputSource(new ByteArrayInputStream(request.loadBody().getResult().asArray())));
|
new InputSource(new ByteArrayInputStream(request.loadBody().getResult().asArray())));
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
Multithreading.runAsync(() -> {
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
feed.getEntries().forEach(entry -> {
|
for (var entry : feed.getEntries()) {
|
||||||
ResponseHelper.handleNewVideo(entry.getLinks().get(0).getHref(),
|
ResponseHelper.handleNewVideo(entry.getLinks().get(0).getHref(),
|
||||||
entry.getPublishedDate().getTime(), null, s);
|
entry.getPublishedDate().getTime(), null, s);
|
||||||
});
|
}
|
||||||
s.close();
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return HttpResponse.ofCode(204);
|
return HttpResponse.ofCode(204);
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
package me.kavin.piped.utils;
|
package me.kavin.piped.utils;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.SessionFactory;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
|
|
||||||
import me.kavin.piped.consts.Constants;
|
import me.kavin.piped.consts.Constants;
|
||||||
import me.kavin.piped.utils.obj.db.Channel;
|
import me.kavin.piped.utils.obj.db.Channel;
|
||||||
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;
|
||||||
import me.kavin.piped.utils.obj.db.Video;
|
import me.kavin.piped.utils.obj.db.Video;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
|
||||||
public class DatabaseSessionFactory {
|
public class DatabaseSessionFactory {
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package me.kavin.piped.utils;
|
package me.kavin.piped.utils;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class Multithreading {
|
public class Multithreading {
|
||||||
|
|
||||||
@ -26,4 +28,8 @@ public class Multithreading {
|
|||||||
public static ExecutorService getCachedExecutor() {
|
public static ExecutorService getCachedExecutor() {
|
||||||
return es;
|
return es;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
|
||||||
|
return CompletableFuture.supplyAsync(supplier, es);
|
||||||
|
}
|
||||||
}
|
}
|
@ -50,7 +50,6 @@ import javax.persistence.criteria.JoinType;
|
|||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
@ -66,25 +65,25 @@ public class ResponseHelper {
|
|||||||
|
|
||||||
public static byte[] streamsResponse(String videoId) throws Exception {
|
public static byte[] streamsResponse(String videoId) throws Exception {
|
||||||
|
|
||||||
CompletableFuture<StreamInfo> futureStream = CompletableFuture.supplyAsync(() -> {
|
final var futureStream = Multithreading.supplyAsync(() -> {
|
||||||
try {
|
try {
|
||||||
return StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId);
|
return StreamInfo.getInfo("https://www.youtube.com/watch?v=" + videoId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ExceptionUtils.rethrow(e);
|
ExceptionUtils.rethrow(e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}, Multithreading.getCachedExecutor());
|
});
|
||||||
|
|
||||||
CompletableFuture<String> futureLbryId = CompletableFuture.supplyAsync(() -> {
|
final var futureLbryId = Multithreading.supplyAsync(() -> {
|
||||||
try {
|
try {
|
||||||
return LbryHelper.getLBRYId(videoId);
|
return LbryHelper.getLBRYId(videoId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ExceptionHandler.handle(e);
|
ExceptionHandler.handle(e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}, Multithreading.getCachedExecutor());
|
});
|
||||||
|
|
||||||
CompletableFuture<String> futureLBRY = CompletableFuture.supplyAsync(() -> {
|
final var futureLBRY = Multithreading.supplyAsync(() -> {
|
||||||
try {
|
try {
|
||||||
String lbryId = futureLbryId.completeOnTimeout(null, 2, TimeUnit.SECONDS).get();
|
String lbryId = futureLbryId.completeOnTimeout(null, 2, TimeUnit.SECONDS).get();
|
||||||
|
|
||||||
@ -93,7 +92,7 @@ public class ResponseHelper {
|
|||||||
ExceptionHandler.handle(e);
|
ExceptionHandler.handle(e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}, Multithreading.getCachedExecutor());
|
});
|
||||||
|
|
||||||
final List<Subtitle> subtitles = new ObjectArrayList<>();
|
final List<Subtitle> subtitles = new ObjectArrayList<>();
|
||||||
final List<ChapterSegment> chapters = new ObjectArrayList<>();
|
final List<ChapterSegment> chapters = new ObjectArrayList<>();
|
||||||
@ -187,7 +186,7 @@ public class ResponseHelper {
|
|||||||
info.getRelatedItems().forEach(o -> relatedStreams.add(collectRelatedStream(o)));
|
info.getRelatedItems().forEach(o -> relatedStreams.add(collectRelatedStream(o)));
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
Multithreading.runAsync(() -> {
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
me.kavin.piped.utils.obj.db.Channel channel = DatabaseHelper.getChannelFromId(s, info.getId());
|
me.kavin.piped.utils.obj.db.Channel channel = DatabaseHelper.getChannelFromId(s, info.getId());
|
||||||
|
|
||||||
@ -214,8 +213,7 @@ public class ResponseHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
s.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
String nextpage = null;
|
String nextpage = null;
|
||||||
@ -527,15 +525,14 @@ public class ResponseHelper {
|
|||||||
|
|
||||||
user = user.toLowerCase();
|
user = user.toLowerCase();
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
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);
|
||||||
cr.select(root).where(root.get("username").in(user));
|
cr.select(root).where(cb.equal(root.get("username"), user));
|
||||||
boolean registered = s.createQuery(cr).uniqueResult() != null;
|
boolean registered = s.createQuery(cr).uniqueResult() != null;
|
||||||
|
|
||||||
if (registered) {
|
if (registered) {
|
||||||
s.close();
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AlreadyRegisteredResponse());
|
return Constants.mapper.writeValueAsBytes(new AlreadyRegisteredResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,10 +554,9 @@ public class ResponseHelper {
|
|||||||
s.getTransaction().begin();
|
s.getTransaction().begin();
|
||||||
s.getTransaction().commit();
|
s.getTransaction().commit();
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new LoginResponse(newuser.getSessionId()));
|
return Constants.mapper.writeValueAsBytes(new LoginResponse(newuser.getSessionId()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
|
private static final BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
|
||||||
@ -573,7 +569,7 @@ public class ResponseHelper {
|
|||||||
|
|
||||||
user = user.toLowerCase();
|
user = user.toLowerCase();
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
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);
|
||||||
@ -585,25 +581,21 @@ public class ResponseHelper {
|
|||||||
String hash = dbuser.getPassword();
|
String hash = dbuser.getPassword();
|
||||||
if (hash.startsWith("$argon2")) {
|
if (hash.startsWith("$argon2")) {
|
||||||
if (argon2PasswordEncoder.matches(pass, hash)) {
|
if (argon2PasswordEncoder.matches(pass, hash)) {
|
||||||
s.close();
|
|
||||||
return Constants.mapper.writeValueAsBytes(new LoginResponse(dbuser.getSessionId()));
|
return Constants.mapper.writeValueAsBytes(new LoginResponse(dbuser.getSessionId()));
|
||||||
}
|
}
|
||||||
} else if (bcryptPasswordEncoder.matches(pass, hash)) {
|
} else if (bcryptPasswordEncoder.matches(pass, hash)) {
|
||||||
s.close();
|
|
||||||
return Constants.mapper.writeValueAsBytes(new LoginResponse(dbuser.getSessionId()));
|
return Constants.mapper.writeValueAsBytes(new LoginResponse(dbuser.getSessionId()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new IncorrectCredentialsResponse());
|
return Constants.mapper.writeValueAsBytes(new IncorrectCredentialsResponse());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] subscribeResponse(String session, String channelId)
|
public static byte[] subscribeResponse(String session, String channelId)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session);
|
User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session);
|
||||||
|
|
||||||
@ -614,10 +606,9 @@ public class ResponseHelper {
|
|||||||
s.createNativeQuery("insert into users_subscribed (subscriber, channel) values (?,?)")
|
s.createNativeQuery("insert into users_subscribed (subscriber, channel) values (?,?)")
|
||||||
.setParameter(1, user.getId()).setParameter(2, channelId).executeUpdate();
|
.setParameter(1, user.getId()).setParameter(2, channelId).executeUpdate();
|
||||||
s.getTransaction().commit();
|
s.getTransaction().commit();
|
||||||
s.close();
|
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
Multithreading.runAsync(() -> {
|
||||||
Session sess = DatabaseSessionFactory.createSession();
|
try (Session sess = DatabaseSessionFactory.createSession()) {
|
||||||
var channel = DatabaseHelper.getChannelFromId(sess, channelId);
|
var channel = DatabaseHelper.getChannelFromId(sess, channelId);
|
||||||
|
|
||||||
if (channel == null) {
|
if (channel == null) {
|
||||||
@ -635,10 +626,8 @@ public class ResponseHelper {
|
|||||||
sess.beginTransaction().commit();
|
sess.beginTransaction().commit();
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
Multithreading.runAsync(() -> {
|
||||||
try {
|
try (Session sessSub = DatabaseSessionFactory.createSession()) {
|
||||||
Session sessSub = DatabaseSessionFactory.createSession();
|
|
||||||
subscribePubSub(channelId, sessSub);
|
subscribePubSub(channelId, sessSub);
|
||||||
sessSub.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ExceptionHandler.handle(e);
|
ExceptionHandler.handle(e);
|
||||||
}
|
}
|
||||||
@ -652,25 +641,23 @@ public class ResponseHelper {
|
|||||||
handleNewVideo(item.getUrl(), time, channel, sess);
|
handleNewVideo(item.getUrl(), time, channel, sess);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
sess.close();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] unsubscribeResponse(String session, String channelId)
|
public static byte[] unsubscribeResponse(String session, String channelId)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSession(s, session);
|
User user = DatabaseHelper.getUserFromSession(s, session);
|
||||||
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
@ -678,21 +665,16 @@ public class ResponseHelper {
|
|||||||
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();
|
||||||
s.close();
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] isSubscribedResponse(String session, String channelId)
|
public static byte[] isSubscribedResponse(String session, String channelId) throws IOException {
|
||||||
throws IOException {
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
|
||||||
|
|
||||||
var cb = s.getCriteriaBuilder();
|
var cb = s.getCriteriaBuilder();
|
||||||
var query = cb.createQuery(Long.class);
|
var query = cb.createQuery(Long.class);
|
||||||
var root = query.from(User.class);
|
var root = query.from(User.class);
|
||||||
@ -703,15 +685,12 @@ public class ResponseHelper {
|
|||||||
));
|
));
|
||||||
var subscribed = s.createQuery(query).getSingleResult() > 0;
|
var subscribed = s.createQuery(query).getSingleResult() > 0;
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new SubscribeStatusResponse(subscribed));
|
return Constants.mapper.writeValueAsBytes(new SubscribeStatusResponse(subscribed));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] feedResponse(String session)
|
public static byte[] feedResponse(String session) throws IOException {
|
||||||
throws IOException {
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSession(s, session);
|
User user = DatabaseHelper.getUserFromSession(s, session);
|
||||||
|
|
||||||
@ -746,21 +725,17 @@ public class ResponseHelper {
|
|||||||
|
|
||||||
feedItems.sort(Comparator.<StreamItem>comparingLong(o -> o.uploaded).reversed());
|
feedItems.sort(Comparator.<StreamItem>comparingLong(o -> o.uploaded).reversed());
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(feedItems);
|
return Constants.mapper.writeValueAsBytes(feedItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] feedResponseRSS(String session)
|
public static byte[] feedResponseRSS(String session) throws IOException, FeedException {
|
||||||
throws IOException, FeedException {
|
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSession(s, session);
|
User user = DatabaseHelper.getUserFromSession(s, session);
|
||||||
|
|
||||||
@ -812,22 +787,16 @@ public class ResponseHelper {
|
|||||||
|
|
||||||
feed.setEntries(entries);
|
feed.setEntries(entries);
|
||||||
|
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return new SyndFeedOutput().outputString(feed).getBytes(UTF_8);
|
return new SyndFeedOutput().outputString(feed).getBytes(UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
return Constants.mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] importResponse(String session, String[] channelIds, boolean override)
|
public static byte[] importResponse(String session, String[] channelIds, boolean override) throws IOException {
|
||||||
throws IOException {
|
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session);
|
User user = DatabaseHelper.getUserFromSessionWithSubscribed(s, session);
|
||||||
|
|
||||||
@ -845,16 +814,12 @@ public class ResponseHelper {
|
|||||||
s.update(user);
|
s.update(user);
|
||||||
s.beginTransaction().commit();
|
s.beginTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
s.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (String channelId : channelIds) {
|
for (String channelId : channelIds) {
|
||||||
|
|
||||||
Multithreading.runAsyncLimited(() -> {
|
Multithreading.runAsyncLimited(() -> {
|
||||||
try {
|
try (Session sess = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
Session sess = DatabaseSessionFactory.createSession();
|
|
||||||
|
|
||||||
var channel = DatabaseHelper.getChannelFromId(sess, channelId);
|
var channel = DatabaseHelper.getChannelFromId(sess, channelId);
|
||||||
|
|
||||||
@ -872,10 +837,8 @@ public class ResponseHelper {
|
|||||||
sess.save(channel);
|
sess.save(channel);
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
Multithreading.runAsync(() -> {
|
||||||
try {
|
try (Session sessSub = DatabaseSessionFactory.createSession()) {
|
||||||
Session sessSub = DatabaseSessionFactory.createSession();
|
|
||||||
subscribePubSub(channelId, sessSub);
|
subscribePubSub(channelId, sessSub);
|
||||||
sessSub.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ExceptionHandler.handle(e);
|
ExceptionHandler.handle(e);
|
||||||
}
|
}
|
||||||
@ -894,8 +857,6 @@ public class ResponseHelper {
|
|||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
sess.close();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ExceptionHandler.handle(e);
|
ExceptionHandler.handle(e);
|
||||||
}
|
}
|
||||||
@ -907,16 +868,14 @@ public class ResponseHelper {
|
|||||||
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
return Constants.mapper.writeValueAsBytes(new AcceptedResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
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 {
|
||||||
|
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
|
||||||
User user = DatabaseHelper.getUserFromSession(s, session);
|
User user = DatabaseHelper.getUserFromSession(s, session);
|
||||||
|
|
||||||
@ -941,27 +900,22 @@ public class ResponseHelper {
|
|||||||
|
|
||||||
subscriptionItems.sort(Comparator.comparing(o -> o.name));
|
subscriptionItems.sort(Comparator.comparing(o -> o.name));
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return Constants.mapper.writeValueAsBytes(subscriptionItems);
|
return Constants.mapper.writeValueAsBytes(subscriptionItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
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()) {
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
|
||||||
|
|
||||||
long registered = (Long) s.createQuery("select count(*) from User").uniqueResult();
|
long registered = (Long) s.createQuery("select count(*) from User").uniqueResult();
|
||||||
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
return String.format("https://img.shields.io/badge/Registered%%20Users-%s-blue", registered);
|
return String.format("https://img.shields.io/badge/Registered%%20Users-%s-blue", registered);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void handleNewVideo(String url, long time, me.kavin.piped.utils.obj.db.Channel channel, Session s) {
|
public static void handleNewVideo(String url, long time, me.kavin.piped.utils.obj.db.Channel channel, Session s) {
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user