93 lines
3.2 KiB
Java
93 lines
3.2 KiB
Java
package com.discdrop.service;
|
|
|
|
import com.discdrop.db.DatabaseService;
|
|
import com.discdrop.db.DatabaseService.FeedResult;
|
|
import com.discdrop.db.Record.ReleaseGroup;
|
|
import com.discdrop.db.Record.TrackedArtist;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import jakarta.inject.Inject;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@ApplicationScoped
|
|
public class FeedService {
|
|
|
|
@Inject
|
|
ArtistService artistService;
|
|
|
|
@Inject
|
|
ReleaseGroupService releaseGroupService;
|
|
|
|
@Inject
|
|
CacheService cacheService;
|
|
|
|
@Inject
|
|
DatabaseService db;
|
|
|
|
public static final int PAGE_SIZE = 10;
|
|
|
|
public List<ReleaseGroup> getAllFeedEntries() {
|
|
var artists = artistService.findAll();
|
|
if (artists.isEmpty()) return List.of();
|
|
|
|
var artistMbids = artistMbidsWithRefresh(artists);
|
|
var allTypes = List.of("Album", "Single", "EP", "Broadcast", "Other");
|
|
var allEntries = db.getFeed(artistMbids, allTypes, 0, 0).entries();
|
|
return filterByArtistMonitoring(allEntries, artists);
|
|
}
|
|
|
|
public FeedResult getFeedEntries(int page) {
|
|
var artists = artistService.findAll();
|
|
if (artists.isEmpty()) return new FeedResult(List.of(), false);
|
|
|
|
var artistMbids = artistMbidsWithRefresh(artists);
|
|
var allTypes = List.of("Album", "Single", "EP", "Broadcast", "Other");
|
|
var allEntries = db.getFeed(artistMbids, allTypes, 0, 0).entries();
|
|
var filtered = filterByArtistMonitoring(allEntries, artists);
|
|
|
|
var fromIndex = (page - 1) * PAGE_SIZE;
|
|
if (fromIndex >= filtered.size()) return new FeedResult(List.of(), false);
|
|
var toIndex = Math.min(fromIndex + PAGE_SIZE, filtered.size());
|
|
var pageEntries = filtered.subList(fromIndex, toIndex);
|
|
var hasMore = toIndex < filtered.size();
|
|
return new FeedResult(pageEntries, hasMore);
|
|
}
|
|
|
|
private List<String> artistMbidsWithRefresh(List<TrackedArtist> artists) {
|
|
var mbids = new ArrayList<String>();
|
|
for (var a : artists) {
|
|
if (cacheService.needsRefresh(a.mbid())) {
|
|
var types = getMonitoredTypes(a);
|
|
releaseGroupService.fetchAndCache(a.mbid(), a.name(), types);
|
|
}
|
|
mbids.add(a.mbid());
|
|
}
|
|
return mbids;
|
|
}
|
|
|
|
private List<ReleaseGroup> filterByArtistMonitoring(List<ReleaseGroup> entries, List<TrackedArtist> artists) {
|
|
var flags = new java.util.HashMap<String, java.util.Set<String>>();
|
|
for (var a : artists) {
|
|
flags.put(a.mbid(), java.util.Set.copyOf(getMonitoredTypes(a)));
|
|
}
|
|
var result = new ArrayList<ReleaseGroup>();
|
|
for (var e : entries) {
|
|
var artistFlags = flags.get(e.artistMbid());
|
|
if (artistFlags != null && artistFlags.contains(e.primaryType())) {
|
|
result.add(e);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private List<String> getMonitoredTypes(TrackedArtist a) {
|
|
var types = new ArrayList<String>();
|
|
if (a.albumMonitored()) types.add("Album");
|
|
if (a.singleMonitored()) types.add("Single");
|
|
if (a.epMonitored()) types.add("EP");
|
|
if (a.broadcastMonitored()) types.add("Broadcast");
|
|
if (a.otherMonitored()) types.add("Other");
|
|
return types;
|
|
}
|
|
}
|