Last active
October 16, 2025 14:23
-
-
Save pagetronic/74fe2ab8406fba72bce6ca6b5db6ff4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.agroneo.web.gaia.events; | |
| import com.agroneo.web.gaia.utils.GaiaView; | |
| import com.mongodb.client.model.Aggregates; | |
| import com.mongodb.client.model.Field; | |
| import com.mongodb.client.model.Filters; | |
| import com.mongodb.client.model.IndexModel; | |
| import live.page.hubd.content.notices.NoticesExtender; | |
| import live.page.hubd.content.notices.NoticesView; | |
| import live.page.hubd.system.db.Db; | |
| import live.page.hubd.system.db.IndexBuilder; | |
| import live.page.hubd.system.db.utils.AggregateUtils; | |
| import live.page.hubd.system.db.utils.Pipeline; | |
| import live.page.hubd.system.db.utils.Updater; | |
| import live.page.hubd.system.db.utils.paginer.Paginer; | |
| import live.page.hubd.system.json.Json; | |
| import live.page.hubd.system.sessions.Users; | |
| import org.bson.conversions.Bson; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.Date; | |
| import java.util.List; | |
| public class EventsNotices extends NoticesExtender { | |
| public static IndexModel getIndex() { | |
| return IndexBuilder.IndexData.get(new Json().put("notify.user", 1).put("notify.received", 1) | |
| .put("date", 1), Filters.exists("notify", true), "notices"); | |
| } | |
| @Override | |
| public String getCollection() { | |
| return "Events"; | |
| } | |
| @Override | |
| public List<Bson> getPipeline(Users user, Date date, Paginer paginer) { | |
| Pipeline pipeline = new Pipeline(); | |
| List<Bson> filters = new ArrayList<>(); | |
| filters.add(Filters.eq("notify.user", user.getId())); | |
| Bson paging = paginer.getFilters(); | |
| if (paging != null) { | |
| filters.add(paging); | |
| } | |
| filters.add(Filters.lt("date", date)); | |
| pipeline.add(Aggregates.match(Filters.and(filters))); | |
| pipeline.add(paginer.getFirstSort()); | |
| pipeline.add(paginer.getLimit()); | |
| pipeline.add(paginer.getLastSort()); | |
| pipeline.addAll(GaiaView.asset("asset")); | |
| pipeline.add(GaiaView.makeUrl(GaiaView.GaiaCollection.Events)); | |
| pipeline.add(Aggregates.addFields( | |
| new Field<>("message", "$info"), | |
| new Field<>("icon", "$asset"), | |
| new Field<>("channel", "$_id"), | |
| new Field<>("read", new Json("$cond", new Json("if", | |
| new Json("$in", Arrays.asList(user.getId(), | |
| new Json("$map", new Json("input", new Json("$filter", new Json("input", AggregateUtils.forceArray("$notify")) | |
| .put("as", "notify").put("cond", new Json("$eq", Arrays.asList("$$notify.read", true))) | |
| )).put("as", "notify").put("in", "$$notify.user"))))).put("then", true).put("else", false))) | |
| )); | |
| pipeline.add(Aggregates.project(NoticesView.grouper.getProjectionOrder())); | |
| return pipeline; | |
| } | |
| @Override | |
| public long read(Users user, Json data) { | |
| Updater updater = new Updater(); | |
| updater.set("notify.$.read", true); | |
| List<Bson> filter = new ArrayList<>(); | |
| filter.add(Filters.eq("notify.user", user.getId())); | |
| if (data.getBoolean("readAll", false)) { | |
| filter.add(Filters.lt("date", new Date())); | |
| } else if (data.getId() != null) { | |
| filter.add(Filters.eq("_id", data.getId())); | |
| } else if (data.getList("ids") != null) { | |
| filter.add(Filters.in("_id", data.getList("ids"))); | |
| } else { | |
| return 0; | |
| } | |
| return Db.updateMany(getCollection(), Filters.and(filter), updater.get()).getModifiedCount(); | |
| } | |
| @Override | |
| public long delete(Users user, Json data) { | |
| Updater updater = new Updater().pull("notify", new Json("user", user.getId())); | |
| Bson filter; | |
| if (data.getId() != null) { | |
| filter = Filters.and(Filters.eq("notify.user", user.getId()), Filters.eq("_id", data.getId())); | |
| } else if (data.getList("ids", String.class) != null) { | |
| filter = Filters.and(Filters.eq("notify.user", user.getId()), Filters.in("_id", data.getList("ids", String.class))); | |
| } else { | |
| return 0; | |
| } | |
| return Db.updateMany(getCollection(), filter, updater.get()).getModifiedCount(); | |
| } | |
| @Override | |
| public boolean receive(Users user, List<String> ids) { | |
| Updater updater = new Updater().set("notify.$.received", true); | |
| return Db.updateMany(getCollection(), Filters.and(Filters.in("_id", ids), Filters.eq("notify.user", user.getId())), updater.get()).getModifiedCount() > 0; | |
| } | |
| @Override | |
| public long count(Users user) { | |
| return Db.count(getCollection(), Filters.and(Filters.eq("notify.user", user.getId()), Filters.ne("notify.received", true), Filters.lt("date", new Date())), 100); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment