Exemple #1
0
        public UserSubscription AddNewFeed(string feedUrl, User user)
        {
            var feedHashCode = feedUrl.GetHashCode();
            var subscription = _dbContext.Subscriptions.SingleOrDefault(x => x.Hash == feedHashCode);
            if (subscription == null)
            {
                subscription = AddSubscription(feedUrl, user);
            }
            else
            {
                var alreadySubscribed = subscription.UserSubscriptions.Any(x => x.User.Email == user.Email);
                if (alreadySubscribed)
                {
                    throw new BralekTechnicalException("ALREADY_SUBSCRIBED", null);
                }
            }
            var userSubscription = new UserSubscription
            {
                AddedOnUTC = DateTime.UtcNow,
                Subscription = subscription,
                UpdatedOnUTC = DateTime.UtcNow,
                User = user,
                UnreadItems = subscription.SubscriptionPosts.Count()
            };
            _dbContext.UserSubscriptions.Add(userSubscription);
            _dbContext.SaveChanges();

            return userSubscription;
        }
Exemple #2
0
        private Subscription AddSubscription(string feedUrl, User user)
        {
            try
            {
                var uri = new Uri(feedUrl);
                var feed = GenericSyndicationFeed.Create(uri,
                    new SyndicationResourceLoadSettings { AutoDetectExtensions = true, Timeout = new TimeSpan(0, 0, 3) });
                var feedHashCode = feedUrl.GetHashCode();
                var subscription = new Subscription
                {
                    AddedOnUTC = DateTime.UtcNow,
                    AddedBy = user,
                    FeedUrl = feedUrl,
                    Hash = feedHashCode,
                    Title = feed.Title,
                };

                switch (feed.Format)
                {
                    case SyndicationContentFormat.Atom:
                        var atom = (AtomFeed)feed.Resource;
                        subscription.Title = GetAtomSubscriptionTitle(atom);
                        GetAtomFeedItems(atom, subscription);
                        break;
                    case SyndicationContentFormat.Rss:
                        var rss = (RssFeed)feed.Resource;
                        subscription.SiteUrl = rss.Channel.Link.ToString();
                        GetRssFeedItems(rss, subscription);
                        break;
                    case SyndicationContentFormat.None:
                        throw new BralekTechnicalException("NO_RSS_FORMAT", null);
                    default:
                        throw new BralekTechnicalException("RSS_UNKNOWN_FORMAT", null);
                }

                _dbContext.Subscriptions.Add(subscription);
                return subscription;
            }
            catch (UriFormatException ex)
            {
                throw new BralekTechnicalException("ADD_FEED_URI_ERROR", ex);
            }
            catch (WebException ex)
            {
                throw new BralekTechnicalException("FEED_READ_ERROR", ex);
            }
            catch (XmlException ex)
            {
                throw new BralekTechnicalException("FEED_XML_ERROR", ex);
            }
        }