Ejemplo n.º 1
0
        private static async Task <bool> AddOrUpdateFeedAsync(string streamName, string entry, FeedEntryType entryType,
                                                              string reason, KeylolDbContext dbContext)
        {
            var added     = false;
            var startTime = DateTime.Now - TimeSpan.FromDays(1);
            var feed      = await dbContext.Feeds.Where(
                f => f.StreamName == streamName && f.Entry == entry &&
                f.EntryType == entryType &&
                f.Time > startTime)
                            .FirstOrDefaultAsync();

            if (feed == null)
            {
                feed = new Feed
                {
                    StreamName = streamName,
                    EntryType  = entryType,
                    Entry      = entry
                };
                dbContext.Feeds.Add(feed);
                added = true;
            }
            if (!string.IsNullOrWhiteSpace(reason))
            {
                var properties =
                    Helpers.SafeDeserialize <SubscriptionStream.FeedProperties>(feed.Properties) ??
                    new SubscriptionStream.FeedProperties();
                properties.Reasons = properties.Reasons ?? new List <string>();
                bool   reasonExisted;
                string likeOperatorId = null;
                var    reasonParts    = reason.Split(':');
                if (reasonParts.Length == 2 && reasonParts[0] == "like")
                {
                    likeOperatorId = reasonParts[1];
                }
                if (likeOperatorId == null)
                {
                    reasonExisted = properties.Reasons.Contains(reason);
                }
                else
                {
                    if (properties.Reasons.Count > 0)
                    {
                        var indexToRemove = -1;
                        for (var i = 0; i < properties.Reasons.Count; i++)
                        {
                            var currentReason = properties.Reasons[i];
                            var parts         = currentReason.Split(':');
                            if (parts.Length != 2 || parts[0] != "like")
                            {
                                continue;
                            }
                            var currentSubscriberId    = parts[1];
                            var currentSubscriberCount = await dbContext.Subscriptions.LongCountAsync(
                                s => s.TargetId == currentSubscriberId && s.TargetType == SubscriptionTargetType.User);

                            var newSubscriberCount = await dbContext.Subscriptions.LongCountAsync(
                                s => s.TargetId == likeOperatorId && s.TargetType == SubscriptionTargetType.User);

                            if (newSubscriberCount >= currentSubscriberCount)
                            {
                                indexToRemove = i;
                            }
                            break;
                        }
                        if (indexToRemove >= 0)
                        {
                            properties.Reasons.RemoveAt(indexToRemove);
                            reasonExisted = false;
                        }
                        else
                        {
                            reasonExisted = true;
                        }
                    }
                    else
                    {
                        reasonExisted = false;
                    }
                }
                if (!reasonExisted)
                {
                    properties.Reasons.Add(reason);
                    feed.Properties = JsonConvert.SerializeObject(properties);
                }
            }
            await dbContext.SaveChangesAsync();

            return(added);
        }
Ejemplo n.º 2
0
        public async Task<ActionResult> Submit(CreateFeedEntryViewModel feedEntryViewModel, FeedEntryType submitType)
        {
            Log.Info("Received new feedentry:" + feedEntryViewModel.Title);
            if (ModelState.IsValid)
            {
                //create
                var feedEntry = Mapper.Map<CreateFeedEntryDto>(feedEntryViewModel);
                if (submitType == FeedEntryType.Attachment)
                {
                    feedEntry.FeedEntryType = WebApi.Public.FeedEntryType.Attachment;
                    if (feedEntryViewModel.Attachment == null)
                    {
                        ModelState.AddModelError("Attachment", "Invalid attachment");
                        return View(feedEntryViewModel);
                    }

                    var createdEntryViewModel = await WebApiService.Instance.PostAsync<FeedEntryDto>("/FeedEntries/Create", UserManager.User.AccessToken, feedEntry);

                    await WebApiService.Instance.PostFileAsync<FeedEntryDto>("/Documents/Upload", UserManager.User.AccessToken, new { feedName = feedEntryViewModel.FeedName, guid = createdEntryViewModel.DocumentId },
                         feedEntryViewModel.Attachment);

                    await WebApiService.Instance.PostAsync<FeedEntryDto>("/FeedEntries/AttachDocument", UserManager.User.AccessToken, new { feedName = feedEntryViewModel.FeedName, guid = createdEntryViewModel.Guid, documentId = createdEntryViewModel.DocumentId });


                    return RedirectToAction("Show", createdEntryViewModel.GetRouteValues());
                }
                if (submitType == FeedEntryType.Link)
                {
                    feedEntry.FeedEntryType = WebApi.Public.FeedEntryType.Link;

                    if (string.IsNullOrEmpty(feedEntryViewModel.Link))
                    {
                        ModelState.AddModelError("Link", "Missing link");
                        return View(feedEntryViewModel);
                    }
                    var createdEntryViewModel = await WebApiService.Instance.PostAsync<FeedEntryDto>("/FeedEntries/Create", UserManager.User.AccessToken, feedEntry);
                    return RedirectToAction("Show", createdEntryViewModel.GetRouteValues());
                }
                ModelState.AddModelError("submitType", "Invalid submit type!");
                return View(feedEntryViewModel);
            }
            string messages = string.Join("; ", ModelState.Values
                .SelectMany(x => x.Errors)
                .Select(x => x.ErrorMessage));
            Log.Error("Invalid state: " + messages);


            var feedEntryDto = await WebApiService.Instance.GetAsync<IEnumerable<string>>("/FeedEntries/Tags", UserManager.User.AccessToken, new { feedEntryViewModel.FeedName });
            ViewBag.Tags = feedEntryDto;

            return View(feedEntryViewModel);
        }