mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Cleanup transaction code. (#306)
This commit is contained in:
parent
7c087d82b3
commit
e6c0f7c0f4
2 changed files with 37 additions and 36 deletions
|
@ -10,7 +10,6 @@ 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.Session;
|
||||||
import org.hibernate.StatelessSession;
|
import org.hibernate.StatelessSession;
|
||||||
import org.hibernate.Transaction;
|
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
import org.schabi.newpipe.extractor.localization.Localization;
|
import org.schabi.newpipe.extractor.localization.Localization;
|
||||||
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
|
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
|
||||||
|
@ -99,7 +98,7 @@ public class Main {
|
||||||
var root = cd.from(Video.class);
|
var root = cd.from(Video.class);
|
||||||
cd.where(cb.lessThan(root.get("uploaded"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)));
|
cd.where(cb.lessThan(root.get("uploaded"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)));
|
||||||
|
|
||||||
Transaction tr = s.beginTransaction();
|
var tr = s.beginTransaction();
|
||||||
|
|
||||||
var query = s.createMutationQuery(cd);
|
var query = s.createMutationQuery(cd);
|
||||||
|
|
||||||
|
|
|
@ -1325,7 +1325,7 @@ public class ResponseHelper {
|
||||||
pvQuery.where(cb.not(pvRoot.get("id").in(subQuery)));
|
pvQuery.where(cb.not(pvRoot.get("id").in(subQuery)));
|
||||||
|
|
||||||
var tr = s.beginTransaction();
|
var tr = s.beginTransaction();
|
||||||
s.createQuery(pvQuery).executeUpdate();
|
s.createMutationQuery(pvQuery).executeUpdate();
|
||||||
tr.commit();
|
tr.commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1425,42 +1425,44 @@ public class ResponseHelper {
|
||||||
if (!channelId.matches("[A-Za-z\\d_-]+"))
|
if (!channelId.matches("[A-Za-z\\d_-]+"))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
|
||||||
|
|
||||||
final ChannelInfo info;
|
final ChannelInfo info;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId);
|
info = ChannelInfo.getInfo("https://youtube.com/channel/" + channelId);
|
||||||
} catch (IOException | ExtractionException e) {
|
} catch (IOException | ExtractionException e) {
|
||||||
ExceptionUtils.rethrow(e);
|
ExceptionUtils.rethrow(e);
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
var channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(),
|
|
||||||
info.getAvatarUrl(), info.isVerified());
|
|
||||||
s.persist(channel);
|
|
||||||
s.beginTransaction().commit();
|
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
|
||||||
try {
|
|
||||||
subscribePubSub(channelId);
|
|
||||||
} catch (IOException e) {
|
|
||||||
ExceptionHandler.handle(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Multithreading.runAsync(() -> {
|
|
||||||
for (StreamInfoItem item : info.getRelatedItems()) {
|
|
||||||
long time = item.getUploadDate() != null
|
|
||||||
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
|
|
||||||
: System.currentTimeMillis();
|
|
||||||
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
|
|
||||||
handleNewVideo(item.getUrl(), time, channel);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return channel;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var channel = new me.kavin.piped.utils.obj.db.Channel(channelId, info.getName(),
|
||||||
|
info.getAvatarUrl(), info.isVerified());
|
||||||
|
|
||||||
|
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||||
|
var tr = s.beginTransaction();
|
||||||
|
s.persist(channel);
|
||||||
|
tr.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
Multithreading.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
subscribePubSub(channelId);
|
||||||
|
} catch (IOException e) {
|
||||||
|
ExceptionHandler.handle(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Multithreading.runAsync(() -> {
|
||||||
|
for (StreamInfoItem item : info.getRelatedItems()) {
|
||||||
|
long time = item.getUploadDate() != null
|
||||||
|
? item.getUploadDate().offsetDateTime().toInstant().toEpochMilli()
|
||||||
|
: System.currentTimeMillis();
|
||||||
|
if ((System.currentTimeMillis() - time) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION))
|
||||||
|
handleNewVideo(item.getUrl(), time, channel);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void subscribePubSub(String channelId) throws IOException {
|
public static void subscribePubSub(String channelId) throws IOException {
|
||||||
|
|
Loading…
Reference in a new issue