예제 #1
0
        public void UpdateFeeds()
        {
            var subscriptions = _dbContext.Subscriptions;

            foreach (var subscription in subscriptions)
            {
                try
                {
                    var feed       = GenericSyndicationFeed.Create(new Uri(subscription.FeedUrl));
                    var itemsAdded = 0;
                    switch (feed.Format)
                    {
                    case SyndicationContentFormat.Atom:
                        itemsAdded = GetAtomFeedItems((AtomFeed)feed.Resource, subscription);
                        break;

                    case SyndicationContentFormat.Rss:
                        itemsAdded = GetRssFeedItems((RssFeed)feed.Resource, subscription);
                        break;

                    case SyndicationContentFormat.None:
                        subscription.IsWorking = false;
                        subscription.LastError = "Feed url is not a recognized format";
                        break;

                    default:
                        throw new Exception(String.Format("Format {0}, not supported for feed items", feed.Format));
                    }
                    foreach (var userSubscription in subscription.UserSubscriptions)
                    {
                        userSubscription.UnreadItems += itemsAdded;
                    }
                    subscription.IsWorking          = true;
                    subscription.LastFeedUpdatesUTC = DateTime.UtcNow;
                }
                catch (WebException webException)
                {
                    subscription.IsWorking = false;
                    subscription.LastError = String.Format("WebException {0}", webException.Message);
                }
                catch (XmlException xmlException)
                {
                    subscription.IsWorking = false;
                    subscription.LastError = String.Format("XmlException {0}", xmlException.Message);
                }
            }

            _dbContext.SaveChanges();
        }
예제 #2
0
        public void ImportOpml(Stream opmlFile, string userEmail)
        {
            var user = _dbContext.Users.Single(x => x.Email == userEmail);

            using (var textReader = new XmlTextReader(opmlFile))
            {
                const string rss      = "rss";
                var          xs       = new XmlSerializer(typeof(Opml));
                var          opmlData = (Opml)xs.Deserialize(textReader);

                foreach (var outline in opmlData.Body.Outline.Where(x => x.Type == rss))
                {
                    var xmlUrlHash   = outline.XmlUrl.GetHashCode();
                    var subscription = _dbContext.Subscriptions.SingleOrDefault(x => x.Hash == xmlUrlHash);

                    if (subscription == null)
                    {
                        subscription = new Subscription
                        {
                            FeedUrl    = outline.XmlUrl,
                            Hash       = xmlUrlHash,
                            SiteUrl    = outline.HtmlUrl,
                            AddedBy    = user,
                            AddedOnUTC = DateTime.UtcNow,
                            Title      = outline.Title,
                        };
                        _dbContext.Subscriptions.Add(subscription);
                    }

                    var userSubscription = new UserSubscription
                    {
                        Subscription = subscription,
                        User         = user,
                        AddedOnUTC   = DateTime.UtcNow,
                        UpdatedOnUTC = DateTime.UtcNow
                    };
                    _dbContext.UserSubscriptions.Add(userSubscription);
                }
                _dbContext.SaveChanges();
            }
        }
예제 #3
0
        public ActionResult Alerts()
        {
            var resourceManager = new ResourceManager("Treenks.Bralek.Web.Resources.Alerts", typeof(Alerts).Assembly);

            var resources = resourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true);
            var model     = string.Empty;
            var user      = _dbContext.Users.Single(x => x.Email == WebSecurity.CurrentUserName);

            foreach (DictionaryEntry resource in resources)
            {
                bool isNewAlert = user.AlertsSeen.All(x => x.Key != (string)resource.Key);
                if (isNewAlert)
                {
                    var value = (string)resource.Value;
                    model = value;
                    var actionStart = value.IndexOf('$', 0);
                    while (actionStart != -1)
                    {
                        var actionEnd = value.IndexOf('$', actionStart + 1);
                        var action    = value.Substring(actionStart, actionEnd - actionStart + 1);
                        var actionUrl = GetActionUrl(action.Replace('$', ' ').Trim());
                        model       = model.Replace(action, actionUrl);
                        actionStart = value.IndexOf('$', actionEnd + 1);
                    }

                    var alertSeen = new AlertInfo
                    {
                        Key         = (string)resource.Key,
                        ViewedBy    = user,
                        ViewedOnUTC = DateTime.UtcNow
                    };
                    user.AlertsSeen.Add(alertSeen);
                    _dbContext.SaveChanges();

                    return(PartialView("_Alerts", model));
                }
            }
            return(PartialView("_Alerts", model));
        }
예제 #4
0
        public ActionResult Get(int page          = 0, int?subscriptionId = null, bool?orderByOldest = null,
                                bool?showAllPosts = null)
        {
            var startFrom = page * PageSize;
            var user      = _dbContext.Users.FirstOrDefault(x => x.Email == WebSecurity.CurrentUserName);
            var owningSubscriptionPosts = _dbContext.SubscriptionPosts.Where(
                x => x.Subscription.UserSubscriptions.Any(y => y.User.Email == WebSecurity.CurrentUserName));

            if (subscriptionId != null && subscriptionId != -1)
            {
                owningSubscriptionPosts = owningSubscriptionPosts.Where(
                    x => x.Subscription.Id == subscriptionId);
            }
            else if (subscriptionId == -1)
            {
                owningSubscriptionPosts =
                    owningSubscriptionPosts.Where(
                        x => x.Subscription.UserSubscriptions.Any(
                            y => y.User.Email == WebSecurity.CurrentUserName &&
                            y.User.Bookmarks.Any(z => z.User.Email == y.User.Email && z.SubscriptionPost.Id == x.Id)));
            }

            if (showAllPosts.HasValue)
            {
                user.ShowAllItems = showAllPosts.Value;
                _dbContext.SaveChanges();
            }
            showAllPosts = showAllPosts ?? user.ShowAllItems;
            showAllPosts = showAllPosts.Value || subscriptionId == -1;
            if (!showAllPosts.Value)
            {
                startFrom = 0;
                owningSubscriptionPosts = owningSubscriptionPosts.Where(
                    x => !x.PostsRead.Any(y => y.UserSubscription.User.Email == WebSecurity.CurrentUserName &&
                                          y.SubscriptionPost.Id == x.Id));
            }

            if (orderByOldest.HasValue)
            {
                user.OrderByOldest = orderByOldest.Value;
                _dbContext.SaveChanges();
            }
            orderByOldest           = orderByOldest ?? user.OrderByOldest;
            owningSubscriptionPosts = orderByOldest.Value
                ? owningSubscriptionPosts.OrderBy(x => x.PublishDateUTC)
                : owningSubscriptionPosts.OrderByDescending(x => x.PublishDateUTC);

            var model = owningSubscriptionPosts.Skip(startFrom).Take(PageSize)
                        .Select(x => new SubscriptionItemViewModel
            {
                Author         = x.Authors,
                Content        = x.Content,
                OriginalUrl    = x.OriginalUrl,
                PublishDateUTC = x.PublishDateUTC,
                FeedId         = x.Subscription.Id,
                Title          = x.Title,
                Id             = x.Id,
                FeedName       = x.Subscription.Title,
                AlreadyRead    = x.PostsRead.Any(y => y.UserSubscription.User.Email == WebSecurity.CurrentUserName),
                InBookmarks    = x.Subscription.UserSubscriptions.Any(y => y.User.Email == WebSecurity.CurrentUserName &&
                                                                      y.User.Bookmarks.Any(z => z.User.Email == y.User.Email && z.SubscriptionPost.Id == x.Id))
            });

            return(PartialView("_SubscriptionItems", model));
        }