mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Replace native query with criteria api for PubSub subscriptions (#191)
This commit is contained in:
parent
498849ec6b
commit
a03ae434be
1 changed files with 15 additions and 8 deletions
|
@ -7,16 +7,16 @@ import me.kavin.piped.utils.DownloaderImpl;
|
||||||
import me.kavin.piped.utils.Multithreading;
|
import me.kavin.piped.utils.Multithreading;
|
||||||
import me.kavin.piped.utils.ResponseHelper;
|
import me.kavin.piped.utils.ResponseHelper;
|
||||||
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 org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
import org.hibernate.query.Query;
|
import org.hibernate.query.Query;
|
||||||
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 java.util.Collections;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
import java.util.List;
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
import java.util.Timer;
|
import java.util.*;
|
||||||
import java.util.TimerTask;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
@ -33,10 +33,17 @@ public class Main {
|
||||||
try {
|
try {
|
||||||
Session s = DatabaseSessionFactory.createSession();
|
Session s = DatabaseSessionFactory.createSession();
|
||||||
|
|
||||||
List<PubSub> pubSubList = s.createNativeQuery(
|
CriteriaBuilder cb = s.getCriteriaBuilder();
|
||||||
"select distinct pubsub.* from pubsub inner join users_subscribed on pubsub.id = users_subscribed.channel where pubsub.subbed_at < :time",
|
CriteriaQuery<PubSub> criteria = cb.createQuery(PubSub.class);
|
||||||
PubSub.class).setParameter("time", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4))
|
var root = criteria.from(PubSub.class);
|
||||||
.getResultList();
|
var userRoot = criteria.from(User.class);
|
||||||
|
criteria.select(root)
|
||||||
|
.where(cb.and(
|
||||||
|
cb.lessThan(root.get("subbedAt"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4)),
|
||||||
|
cb.isMember(root.get("id"), userRoot.<Collection<String>>get("subscribed_ids"))
|
||||||
|
)).distinct(true);
|
||||||
|
|
||||||
|
List<PubSub> pubSubList = s.createQuery(criteria).list();
|
||||||
|
|
||||||
Collections.shuffle(pubSubList);
|
Collections.shuffle(pubSubList);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue