mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
remove many to many relationship between entries and feeds
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import com.commafeed.backend.dao.FeedEntryContentDAO;
|
||||
import com.commafeed.backend.feeds.FeedUtils;
|
||||
import com.commafeed.backend.model.FeedEntryContent;
|
||||
|
||||
public class FeedEntryContentService {
|
||||
|
||||
@Inject
|
||||
FeedEntryContentDAO feedEntryContentDAO;
|
||||
|
||||
/**
|
||||
* this is NOT thread-safe
|
||||
*/
|
||||
public FeedEntryContent findOrCreate(FeedEntryContent content,
|
||||
String baseUrl) {
|
||||
|
||||
FeedEntryContent existing = feedEntryContentDAO.findExisting(content);
|
||||
if (existing == null) {
|
||||
content.setAuthor(FeedUtils.truncate(
|
||||
FeedUtils.handleContent(content.getAuthor(), baseUrl, true),
|
||||
128));
|
||||
content.setTitle(FeedUtils.truncate(
|
||||
FeedUtils.handleContent(content.getTitle(), baseUrl, true),
|
||||
2048));
|
||||
content.setContentHash(DigestUtils.sha1Hex(content.getContent()));
|
||||
content.setContent(FeedUtils.handleContent(content.getContent(),
|
||||
baseUrl, false));
|
||||
existing = content;
|
||||
feedEntryContentDAO.saveOrUpdate(existing);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
@@ -58,10 +58,7 @@ public class FeedEntryService {
|
||||
}
|
||||
|
||||
FeedEntryStatus status = feedEntryStatusDAO.getStatus(sub, entry);
|
||||
if (status.isMarkable()) {
|
||||
status.setStarred(starred);
|
||||
feedEntryStatusDAO.saveOrUpdate(status);
|
||||
}
|
||||
|
||||
status.setStarred(starred);
|
||||
feedEntryStatusDAO.saveOrUpdate(status);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,9 @@ import com.commafeed.backend.feeds.FeedRefreshTaskGiver;
|
||||
import com.commafeed.backend.feeds.FeedUtils;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.Models;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.google.api.client.util.Lists;
|
||||
import com.google.api.client.util.Maps;
|
||||
|
||||
public class FeedSubscriptionService {
|
||||
@@ -76,37 +73,16 @@ public class FeedSubscriptionService {
|
||||
Feed feed = feedService.findOrCreate(url);
|
||||
|
||||
FeedSubscription sub = feedSubscriptionDAO.findByFeed(user, feed);
|
||||
boolean newSubscription = false;
|
||||
if (sub == null) {
|
||||
sub = new FeedSubscription();
|
||||
sub.setFeed(feed);
|
||||
sub.setUser(user);
|
||||
newSubscription = true;
|
||||
}
|
||||
sub.setCategory(category);
|
||||
sub.setPosition(0);
|
||||
sub.setTitle(FeedUtils.truncate(title, 128));
|
||||
feedSubscriptionDAO.saveOrUpdate(sub);
|
||||
|
||||
if (newSubscription) {
|
||||
try {
|
||||
List<FeedEntryStatus> statuses = Lists.newArrayList();
|
||||
List<FeedEntry> allEntries = feedEntryDAO.findByFeed(feed, 0,
|
||||
10);
|
||||
for (FeedEntry entry : allEntries) {
|
||||
FeedEntryStatus status = new FeedEntryStatus(user, sub,
|
||||
entry);
|
||||
status.setRead(false);
|
||||
status.setSubscription(sub);
|
||||
statuses.add(status);
|
||||
}
|
||||
feedEntryStatusDAO.saveOrUpdate(statuses);
|
||||
} catch (Exception e) {
|
||||
log.error(
|
||||
"could not fetch initial statuses when importing {} : {}",
|
||||
feed.getUrl(), e.getMessage());
|
||||
}
|
||||
}
|
||||
taskGiver.add(feed);
|
||||
return feed;
|
||||
}
|
||||
|
||||
@@ -8,17 +8,16 @@ import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import com.commafeed.backend.MetricsBean;
|
||||
import com.commafeed.backend.cache.CacheService;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO.EntryWithFeed;
|
||||
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
import com.commafeed.backend.feeds.FeedUtils;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryContent;
|
||||
import com.commafeed.backend.model.FeedFeedEntry;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.google.common.collect.Lists;
|
||||
@@ -43,41 +42,35 @@ public class FeedUpdateService {
|
||||
|
||||
@Inject
|
||||
CacheService cache;
|
||||
|
||||
@Inject
|
||||
FeedEntryContentService feedEntryContentService;
|
||||
|
||||
/**
|
||||
* this is NOT thread-safe
|
||||
*/
|
||||
public void updateEntry(Feed feed, FeedEntry entry,
|
||||
List<FeedSubscription> subscriptions) {
|
||||
|
||||
EntryWithFeed existing = feedEntryDAO.findExisting(entry.getGuid(),
|
||||
FeedEntry existing = feedEntryDAO.findExisting(entry.getGuid(),
|
||||
entry.getUrl(), feed.getId());
|
||||
|
||||
FeedEntry update = null;
|
||||
FeedFeedEntry ffe = null;
|
||||
if (existing == null) {
|
||||
entry.setAuthor(FeedUtils.truncate(FeedUtils.handleContent(
|
||||
entry.getAuthor(), feed.getLink(), true), 128));
|
||||
FeedEntryContent content = entry.getContent();
|
||||
content.setTitle(FeedUtils.truncate(FeedUtils.handleContent(
|
||||
content.getTitle(), feed.getLink(), true), 2048));
|
||||
content.setContent(FeedUtils.handleContent(content.getContent(),
|
||||
feed.getLink(), false));
|
||||
|
||||
entry.setInserted(new Date());
|
||||
ffe = new FeedFeedEntry(feed, entry);
|
||||
|
||||
update = entry;
|
||||
} else if (existing.ffe == null) {
|
||||
ffe = new FeedFeedEntry(feed, existing.entry);
|
||||
update = existing.entry;
|
||||
if (existing != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (update != null) {
|
||||
List<User> users = Lists.newArrayList();
|
||||
for (FeedSubscription sub : subscriptions) {
|
||||
users.add(sub.getUser());
|
||||
}
|
||||
cache.invalidateUserData(users.toArray(new User[0]));
|
||||
feedEntryDAO.saveOrUpdate(update);
|
||||
em.persist(ffe);
|
||||
FeedEntryContent content = feedEntryContentService.findOrCreate(
|
||||
entry.getContent(), feed.getLink());
|
||||
entry.setGuidHash(DigestUtils.sha1Hex(entry.getGuid()));
|
||||
entry.setContent(content);
|
||||
entry.setInserted(new Date());
|
||||
entry.setFeed(feed);
|
||||
|
||||
List<User> users = Lists.newArrayList();
|
||||
for (FeedSubscription sub : subscriptions) {
|
||||
User user = sub.getUser();
|
||||
users.add(user);
|
||||
}
|
||||
feedEntryDAO.saveOrUpdate(entry);
|
||||
cache.invalidateUserData(users.toArray(new User[0]));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user