2013-04-11 20:49:08 +02:00
|
|
|
package com.commafeed.backend.services;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
2013-07-03 12:42:01 +02:00
|
|
|
import java.util.Map;
|
2013-04-11 20:49:08 +02:00
|
|
|
|
2013-05-20 22:33:45 +03:00
|
|
|
import javax.ejb.ApplicationException;
|
2013-04-11 20:49:08 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-06-01 21:00:10 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2013-07-02 10:14:47 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2013-06-01 21:00:10 +02:00
|
|
|
|
2013-07-03 12:42:01 +02:00
|
|
|
import com.commafeed.backend.cache.CacheService;
|
2013-04-11 20:49:08 +02:00
|
|
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
2013-04-25 14:41:56 +02:00
|
|
|
import com.commafeed.backend.feeds.FeedRefreshTaskGiver;
|
2013-05-23 10:03:15 +02:00
|
|
|
import com.commafeed.backend.feeds.FeedUtils;
|
2013-04-11 20:49:08 +02:00
|
|
|
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;
|
2013-07-03 12:42:01 +02:00
|
|
|
import com.commafeed.backend.model.Models;
|
2013-04-11 20:49:08 +02:00
|
|
|
import com.commafeed.backend.model.User;
|
|
|
|
|
import com.google.api.client.util.Lists;
|
|
|
|
|
|
|
|
|
|
public class FeedSubscriptionService {
|
2013-05-20 21:53:13 +02:00
|
|
|
|
2013-07-03 12:42:01 +02:00
|
|
|
private static Logger log = LoggerFactory
|
|
|
|
|
.getLogger(FeedSubscriptionService.class);
|
2013-07-02 10:14:47 +02:00
|
|
|
|
2013-05-20 21:53:13 +02:00
|
|
|
@SuppressWarnings("serial")
|
2013-05-20 22:33:45 +03:00
|
|
|
@ApplicationException
|
2013-05-20 21:53:13 +02:00
|
|
|
public static class FeedSubscriptionException extends RuntimeException {
|
2013-05-20 22:33:45 +03:00
|
|
|
public FeedSubscriptionException(String msg) {
|
|
|
|
|
super(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-11 20:49:08 +02:00
|
|
|
|
|
|
|
|
@Inject
|
2013-04-21 11:24:45 +02:00
|
|
|
FeedService feedService;
|
2013-04-11 20:49:08 +02:00
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedEntryDAO feedEntryDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedEntryStatusDAO feedEntryStatusDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
2013-04-30 22:05:38 +02:00
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedRefreshTaskGiver taskGiver;
|
2013-04-11 20:49:08 +02:00
|
|
|
|
2013-07-03 12:42:01 +02:00
|
|
|
@Inject
|
|
|
|
|
CacheService cache;
|
|
|
|
|
|
2013-04-16 07:26:24 +02:00
|
|
|
public Feed subscribe(User user, String url, String title,
|
2013-04-11 20:49:08 +02:00
|
|
|
FeedCategory category) {
|
|
|
|
|
|
2013-05-20 22:10:44 +03:00
|
|
|
final String pubUrl = applicationSettingsService.get().getPublicUrl();
|
2013-06-01 21:00:10 +02:00
|
|
|
if (StringUtils.isBlank(pubUrl)) {
|
2013-05-20 22:33:45 +03:00
|
|
|
throw new FeedSubscriptionException(
|
2013-05-22 21:56:22 +02:00
|
|
|
"Public URL of this CommaFeed instance is not set");
|
2013-05-20 22:10:44 +03:00
|
|
|
}
|
|
|
|
|
if (url.startsWith(pubUrl)) {
|
2013-05-20 21:53:13 +02:00
|
|
|
throw new FeedSubscriptionException(
|
2013-04-30 22:05:38 +02:00
|
|
|
"Could not subscribe to a feed from this CommaFeed instance");
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-21 11:24:45 +02:00
|
|
|
Feed feed = feedService.findOrCreate(url);
|
2013-04-11 20:49:08 +02:00
|
|
|
|
|
|
|
|
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);
|
2013-06-04 10:11:16 +02:00
|
|
|
sub.setPosition(0);
|
2013-05-23 10:03:15 +02:00
|
|
|
sub.setTitle(FeedUtils.truncate(title, 128));
|
2013-04-11 20:49:08 +02:00
|
|
|
feedSubscriptionDAO.saveOrUpdate(sub);
|
|
|
|
|
|
|
|
|
|
if (newSubscription) {
|
2013-07-02 10:14:47 +02:00
|
|
|
try {
|
|
|
|
|
List<FeedEntryStatus> statuses = Lists.newArrayList();
|
2013-07-03 12:42:01 +02:00
|
|
|
List<FeedEntry> allEntries = feedEntryDAO.findByFeed(feed, 0,
|
|
|
|
|
10);
|
2013-07-02 10:14:47 +02:00
|
|
|
for (FeedEntry entry : allEntries) {
|
2013-07-14 05:48:47 +02:00
|
|
|
FeedEntryStatus status = new FeedEntryStatus(user, sub, entry);
|
2013-07-02 10:14:47 +02:00
|
|
|
status.setRead(false);
|
|
|
|
|
status.setSubscription(sub);
|
|
|
|
|
statuses.add(status);
|
|
|
|
|
}
|
|
|
|
|
feedEntryStatusDAO.saveOrUpdate(statuses);
|
|
|
|
|
} catch (Exception e) {
|
2013-07-03 12:42:01 +02:00
|
|
|
log.error(
|
|
|
|
|
"could not fetch initial statuses when importing {} : {}",
|
|
|
|
|
feed.getUrl(), e.getMessage());
|
2013-04-11 20:49:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-04-25 14:41:56 +02:00
|
|
|
taskGiver.add(feed);
|
2013-04-16 07:26:24 +02:00
|
|
|
return feed;
|
2013-04-11 20:49:08 +02:00
|
|
|
}
|
2013-07-03 12:42:01 +02:00
|
|
|
|
|
|
|
|
public Map<Long, Long> getUnreadCount(User user) {
|
|
|
|
|
Map<Long, Long> map = cache.getUnreadCounts(user);
|
|
|
|
|
if (map == null) {
|
|
|
|
|
log.debug("unread count cache miss for {}", Models.getId(user));
|
2013-07-22 16:31:29 +02:00
|
|
|
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
|
|
|
|
|
map = feedEntryStatusDAO.getUnreadCount(subs);
|
2013-07-03 12:42:01 +02:00
|
|
|
cache.setUnreadCounts(user, map);
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
2013-04-11 20:49:08 +02:00
|
|
|
}
|