2014-08-08 16:49:02 +02:00
|
|
|
package com.commafeed.backend.service;
|
2013-04-11 20:49:08 +02:00
|
|
|
|
|
|
|
|
import java.util.List;
|
2013-07-03 12:42:01 +02:00
|
|
|
import java.util.Map;
|
2013-04-11 20:49:08 +02:00
|
|
|
|
2014-08-17 14:16:30 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2013-08-11 11:45:32 +02:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
2014-10-28 16:36:09 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2013-06-01 21:00:10 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
import com.commafeed.CommaFeedConfiguration;
|
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.FeedEntryStatusDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
2014-08-08 16:49:02 +02:00
|
|
|
import com.commafeed.backend.feed.FeedQueues;
|
|
|
|
|
import com.commafeed.backend.feed.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.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;
|
2013-08-08 17:00:35 +02:00
|
|
|
import com.commafeed.frontend.model.UnreadCount;
|
2013-08-01 13:35:04 +02:00
|
|
|
import com.google.common.collect.Maps;
|
2013-04-11 20:49:08 +02:00
|
|
|
|
2013-08-11 11:45:32 +02:00
|
|
|
@Slf4j
|
2014-08-17 14:16:30 +02:00
|
|
|
@RequiredArgsConstructor(onConstructor = @__({ @Inject }))
|
|
|
|
|
@Singleton
|
2013-04-11 20:49:08 +02:00
|
|
|
public class FeedSubscriptionService {
|
2013-05-20 21:53:13 +02:00
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
|
public static class FeedSubscriptionException extends RuntimeException {
|
2014-08-14 11:40:30 +02:00
|
|
|
private FeedSubscriptionException(String msg) {
|
2013-05-20 22:33:45 +03:00
|
|
|
super(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-11 20:49:08 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
private final FeedEntryStatusDAO feedEntryStatusDAO;
|
|
|
|
|
private final FeedSubscriptionDAO feedSubscriptionDAO;
|
|
|
|
|
private final FeedService feedService;
|
|
|
|
|
private final FeedQueues queues;
|
|
|
|
|
private final CacheService cache;
|
|
|
|
|
private final CommaFeedConfiguration config;
|
2013-07-03 12:42:01 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
public Feed subscribe(User user, String url, String title, FeedCategory category) {
|
2013-04-11 20:49:08 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
final String pubUrl = config.getApplicationSettings().getPublicUrl();
|
2013-06-01 21:00:10 +02:00
|
|
|
if (StringUtils.isBlank(pubUrl)) {
|
2013-07-25 09:17:33 +02:00
|
|
|
throw new FeedSubscriptionException("Public URL of this CommaFeed instance is not set");
|
2013-05-20 22:10:44 +03:00
|
|
|
}
|
|
|
|
|
if (url.startsWith(pubUrl)) {
|
2013-07-25 09:17:33 +02:00
|
|
|
throw new FeedSubscriptionException("Could not subscribe to a feed from this CommaFeed instance");
|
2013-04-30 22:05:38 +02:00
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
if (sub == null) {
|
|
|
|
|
sub = new FeedSubscription();
|
|
|
|
|
sub.setFeed(feed);
|
|
|
|
|
sub.setUser(user);
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
queues.add(feed, false);
|
2013-07-25 10:38:23 +02:00
|
|
|
cache.invalidateUserRootCategory(user);
|
2013-04-16 07:26:24 +02:00
|
|
|
return feed;
|
2013-04-11 20:49:08 +02:00
|
|
|
}
|
2013-07-25 10:38:23 +02:00
|
|
|
|
|
|
|
|
public boolean unsubscribe(User user, Long subId) {
|
2013-07-25 10:21:11 +02:00
|
|
|
FeedSubscription sub = feedSubscriptionDAO.findById(user, subId);
|
|
|
|
|
if (sub != null) {
|
|
|
|
|
feedSubscriptionDAO.delete(sub);
|
2013-07-25 10:38:23 +02:00
|
|
|
cache.invalidateUserRootCategory(user);
|
2013-07-25 10:21:11 +02:00
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 06:33:16 +01:00
|
|
|
public void refreshAll(User user) {
|
|
|
|
|
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
|
|
|
|
|
for (FeedSubscription sub : subs) {
|
|
|
|
|
Feed feed = sub.getFeed();
|
2014-08-08 16:49:02 +02:00
|
|
|
queues.add(feed, true);
|
2013-11-26 06:33:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 17:00:35 +02:00
|
|
|
public Map<Long, UnreadCount> getUnreadCount(User user) {
|
|
|
|
|
Map<Long, UnreadCount> map = Maps.newHashMap();
|
2013-07-25 10:38:23 +02:00
|
|
|
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
|
2013-07-25 10:21:11 +02:00
|
|
|
for (FeedSubscription sub : subs) {
|
2013-10-13 12:15:41 +02:00
|
|
|
map.put(sub.getId(), getUnreadCount(user, sub));
|
2013-07-03 12:42:01 +02:00
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
2013-07-25 10:21:11 +02:00
|
|
|
|
2014-08-14 11:40:30 +02:00
|
|
|
private UnreadCount getUnreadCount(User user, FeedSubscription sub) {
|
|
|
|
|
UnreadCount count = cache.getUnreadCount(sub);
|
|
|
|
|
if (count == null) {
|
|
|
|
|
log.debug("unread count cache miss for {}", Models.getId(sub));
|
|
|
|
|
count = feedEntryStatusDAO.getUnreadCount(user, sub);
|
|
|
|
|
cache.setUnreadCount(sub, count);
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
}
|