Esempio n. 1
0
        async Task <IDictionary <string, ReactionEntryGrouped> > GetReactionsAsync(int entityId, int entityReplyId)
        {
            // Get all reactions for entity (this includes entity replies for perf)
            var entityReactions = await _entityReactionStore.QueryAsync()
                                  .Take(int.MaxValue, false)
                                  .Select <EntityReactionsQueryParams>(q =>
            {
                q.EntityId.Equals(entityId);
            })
                                  .OrderBy("Id", OrderBy.Asc)
                                  .ToList();

            var output = new ConcurrentDictionary <string, ReactionEntryGrouped>();

            if (entityReactions != null)
            {
                // Get provided reactions
                var reactions = _reactionManager.GetReactions();
                if (reactions == null)
                {
                    return(null);
                }

                // No reactions have been provided
                var reactionsList = reactions.ToList();
                if (reactionsList.Count == 0)
                {
                    return(null);
                }

                // Iterate all entity reactions matching supplied entityId and entityReplyId
                foreach (var entityReaction in entityReactions?.Data.Where(r => r.EntityReplyId == entityReplyId))
                {
                    // Get provided reaction for entry
                    var reaction = reactionsList.FirstOrDefault(b => b.Name.Equals(entityReaction.ReactionName, StringComparison.OrdinalIgnoreCase));
                    if (reaction != null)
                    {
                        // Create a dictionary with all users who have reacted with this reaction
                        output.AddOrUpdate(reaction.Name,
                                           new ReactionEntryGrouped(new ReactionEntry(reaction)
                        {
                            CreatedBy   = entityReaction.CreatedBy,
                            CreatedDate = entityReaction.CreatedDate
                        }),
                                           (k, v) =>
                        {
                            v.Users.Add(entityReaction.CreatedBy);
                            return(v);
                        });
                    }
                }
            }

            return(output);
        }
Esempio n. 2
0
        public async Task <IActionResult> Post([FromBody] EntityReaction model)
        {
            // Get authenticated user (if any)
            var user = await base.GetAuthenticatedUserAsync();

            // Get client details
            var ipV4Address = _clientIpAddress.GetIpV4Address();
            var ipV6Address = _clientIpAddress.GetIpV6Address();
            var userAgent   = "";

            if (Request.Headers.ContainsKey("User-Agent"))
            {
                userAgent = Request.Headers["User-Agent"].ToString();
            }

            IEnumerable <EntityReaction> existingReactions = null;

            if (user != null)
            {
                // Has the user already reacted to the entity?
                existingReactions = await _entityReactionsStore.SelectEntityReactionsByUserIdAndEntityId(user.Id, model.EntityId);
            }
            else
            {
                // Delete all existing reactions for anonymous user using there
                // IP addresses & user agent as a unique identifier
                var anonymousReactions = await _entityReactionsStore.QueryAsync()
                                         .Select <EntityReactionsQueryParams>(q =>
                {
                    q.EntityId.Equals(model.EntityId);
                    q.CreatedUserId.Equals(0);
                    q.IpV4Address.Equals(ipV4Address);
                    q.IpV6Address.Equals(ipV6Address);
                    q.UserAgent.Equals(userAgent);
                })
                                         .ToList();

                existingReactions = anonymousReactions?.Data;
            }

            // Get reaction from existing reactions
            EntityReaction existingReaction = null;

            if (existingReactions != null)
            {
                foreach (var reaction in existingReactions.Where(r => r.EntityReplyId == model.EntityReplyId))
                {
                    if (reaction.ReactionName.Equals(model.ReactionName))
                    {
                        existingReaction = reaction;
                        break;
                    }
                }
            }

            // Delete any existing reaction
            if (existingReaction != null)
            {
                var delete = await _entityReactionManager.DeleteAsync(existingReaction);

                if (delete.Succeeded)
                {
                    // return 202 accepted to confirm delete
                    return(base.AcceptedDelete(await _simpleReactionsStore.GetSimpleReactionsAsync(model.EntityId, model.EntityReplyId)));
                }
            }

            // Set created by
            model.CreatedUserId = user?.Id ?? 0;
            model.CreatedDate   = DateTimeOffset.UtcNow;
            model.IpV4Address   = ipV4Address;
            model.IpV6Address   = ipV6Address;
            model.UserAgent     = userAgent;

            // Add and return results
            var result = await _entityReactionManager.CreateAsync(model);

            if (result.Succeeded)
            {
                // return 201 created
                return(base.Created(await _simpleReactionsStore.GetSimpleReactionsAsync(model.EntityId, model.EntityReplyId)));
            }

            // We should not reach here
            return(base.InternalServerError());
        }