Esempio n. 1
0
        public async Task <IActionResult> GetMesagesForUser(
            int userId,
            [FromQuery] MessagePaginationParams messageParams)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Forbid());
            }

            messageParams.UserId = userId;

            var messagesFromRepo = await this._repo.GetMessagesForUser(messageParams);

            var messages = this._mapper.Map <IEnumerable <MessageToReturnDto> >(messagesFromRepo);

            Response.AddPagination(messagesFromRepo.CurrentPage,
                                   messagesFromRepo.PageSize,
                                   messagesFromRepo.TotalRecords,
                                   messagesFromRepo.TotalPages);

            return(Ok(messages));
        }
Esempio n. 2
0
        public async Task <PagedList <Message> > GetMessagesForUser(
            MessagePaginationParams paginationParams
            )
        {
            var messages = this._context.Messages.AsQueryable();

            if (!string.IsNullOrEmpty(paginationParams.MessageContainer))
            {
                switch (paginationParams.MessageContainer.ToLower())
                {
                case "inbox":
                    messages = messages.Where(m => m.RecipientId == paginationParams.UserId &&
                                              m.RecipientDeleted == false);
                    break;

                case "outbox":
                    messages = messages.Where(m => m.SenderId == paginationParams.UserId &&
                                              m.SenderDeleted == false);
                    break;

                default:
                    messages = messages.Where(m => m.RecipientId == paginationParams.UserId &&
                                              m.RecipientDeleted == false &&
                                              m.IsRead == false);
                    break;
                }
            }
            else
            {
                messages = messages.Where(m => m.RecipientId == paginationParams.UserId &&
                                          m.IsRead == false);
            }

            messages = messages.OrderByDescending(m => m.MessageSent);

            return(await PagedList <Message>
                   .CreateASync(messages, paginationParams.PageNumber, paginationParams.PageSize));
        }
Esempio n. 3
0
        public async Task TestGetReplies()
        {
            var user1 = new User()
            {
                ID   = Guid.NewGuid().ToString(),
                Role = Role.Admin,
            };

            await this._client.Users.Update(user1);

            var channel = _client.Channel("messaging", Guid.NewGuid().ToString());

            await channel.Create(user1.ID);

            var inMsg = new MessageInput()
            {
                Text = Guid.NewGuid().ToString()
            };

            var msg1 = await channel.SendMessage(inMsg, user1.ID);

            inMsg = new MessageInput()
            {
                Text = Guid.NewGuid().ToString()
            };

            var reply1 = await channel.SendMessage(inMsg, user1.ID, msg1.ID);

            inMsg = new MessageInput()
            {
                Text = Guid.NewGuid().ToString()
            };

            var reply2 = await channel.SendMessage(inMsg, user1.ID, msg1.ID);

            inMsg = new MessageInput()
            {
                Text = Guid.NewGuid().ToString()
            };

            var reply3 = await channel.SendMessage(inMsg, user1.ID, msg1.ID);

            var replies = await channel.GetReplies(msg1.ID, MessagePaginationParams.Default);

            Assert.AreEqual(3, replies.Count);
            Assert.AreEqual(3, replies.FindAll(m => m.ParentID == msg1.ID).Count);
            Assert.AreEqual(reply1.ID, replies[0].ID);
            Assert.AreEqual(reply2.ID, replies[1].ID);
            Assert.AreEqual(reply3.ID, replies[2].ID);

            var pagination = new MessagePaginationParams()
            {
                Limit = 1
            };

            replies = await channel.GetReplies(msg1.ID, pagination);

            Assert.AreEqual(1, replies.Count);
            Assert.AreEqual(reply3.ID, replies[0].ID);

            pagination.IDLT = reply2.ID;
            replies         = await channel.GetReplies(msg1.ID, pagination);

            Assert.AreEqual(1, replies.Count);
            Assert.AreEqual(reply1.ID, replies[0].ID);
        }