Exemplo n.º 1
0
        public IViewComponentResult Invoke(string user1Id, string user2Id, int adId)
        {
            var currentUserId = usersService.GetCurrentUserId();

            var senderId    = string.Empty;
            var recipientId = string.Empty;

            if (currentUserId == user2Id)
            {
                senderId    = user2Id;
                recipientId = user1Id;
            }
            else
            {
                senderId    = user1Id;
                recipientId = user2Id;
            }

            var inputModel = new SendMessageInputModel
            {
                SenderId    = senderId,
                RecipientId = recipientId,
                AdId        = adId
            };

            return(View(inputModel));
        }
Exemplo n.º 2
0
        public async Task AddNewMessageAsync(string senderId, SendMessageInputModel model)
        {
            if (senderId == null)
            {
                throw new ArgumentNullException("Sender Id is null");
            }

            bool isNull = model.GetType().GetProperties()
                          .All(p => p.GetValue(model) != null);

            if (isNull == false)
            {
                throw new ArgumentNullException("Model param is null");
            }

            Message message = new Message()
            {
                Content     = model.Content,
                RecipientId = model.RecipientId,
                SenderId    = senderId,
                OrderId     = model.OrderId,
                ModifiedOn  = DateTime.UtcNow,
            };

            await this.repository.AddAsync(message);

            await this.repository.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public async Task Send(SendMessageInputModel inputModel)
        {
            var sanitizer = new HtmlSanitizer();
            var message   = sanitizer.Sanitize(inputModel.Message);

            if (string.IsNullOrEmpty(message) ||
                string.IsNullOrWhiteSpace(message) ||
                string.IsNullOrEmpty(message))
            {
                return;
            }

            var caller = this.userManager
                         .Users
                         .First(x => x.Id == inputModel.CallerId)
                         .ProfilePictureUrl;

            await this.Clients
            .User(inputModel.UserId)
            .SendAsync("RecieveMessage", message, caller);

            await this.Clients
            .Caller
            .SendAsync("SendMessage", message);
        }
Exemplo n.º 4
0
        public async Task SendMessage(SendMessageInputModel inputModel)
        {
            var messageViewModel = await messagesService.CreateMessageAsync(inputModel.SenderId, inputModel.RecipientId, inputModel.AdId, inputModel.Content);

            await Clients.Users(inputModel.RecipientId)
            .SendAsync("SendMessage", messageViewModel);
        }
Exemplo n.º 5
0
        public IHttpActionResult SendMessage(SendMessageInputModel model)
        {
            if (!this.CheckIfUserExist(model.ReceiverId))
            {
                return(this.BadRequest($"User with id {model.ReceiverId} does not exist."));
            }

            int           userId        = this.GetUserId();
            Communication communication = this.communicationService.GetCommunicationByUsers(userId, model.ReceiverId);

            if (communication == null)
            {
                return(this.BadRequest($"Communication between this users(me:{userId};receiver:{model.ReceiverId}) does not exist."));
            }
            int communicationId = communication.Id;

            try
            {
                this.messageService.SendMessage(communicationId, model.Content, userId, model.ReceiverId);
            }
            catch (Exception)
            {
                return(this.BadRequest("Fail to send message."));
            }

            return(this.Ok("Message send successfully."));
        }
        public async Task <IActionResult> Send(SendMessageInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                var sendMessageBindingModel =
                    await this.messagesService.GetMessageBindingModelByOfferIdAsync(inputModel.OfferId);

                sendMessageBindingModel.InputModel = inputModel;

                return(this.View(sendMessageBindingModel));
            }

            var unreadMessagesCount = await this.messagesService.GetUnreadMessagesCountAsync(inputModel.RecipientId);

            await this.hubContext.Clients.User(inputModel.RecipientId).SendAsync("MessageCount", unreadMessagesCount);

            if (this.ModelState.IsValid)
            {
                var messageViewModel = await this.messagesService.CreateMessageAsync(inputModel.SenderId, inputModel.RecipientId, inputModel.OfferId, inputModel.Content);

                await this.hubContext.Clients.User(inputModel.RecipientId)
                .SendAsync("SendMessage", messageViewModel);
            }

            return(this.RedirectToAction("Details", new { offerId = inputModel.OfferId, senderId = inputModel.RecipientId, recipientId = inputModel.SenderId }));
        }
        public IActionResult SendMessage([FromBody] SendMessageInputModel sendMessageInputModel)
        {
            // #somostodosdto

            using (var connection = _factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    // Declara a fila para que caso ela não exista ainda, eu crie ela
                    channel.QueueDeclare(
                        queue: QUEUE_NAME,
                        durable: false,
                        exclusive: false,
                        autoDelete: false,
                        arguments: null
                        );

                    // Formatar os dados para envio para a fila
                    var stringMessage = JsonSerializer.Serialize(sendMessageInputModel);
                    var byteArray     = Encoding.UTF8.GetBytes(stringMessage);

                    channel.BasicPublish(
                        exchange: "",
                        routingKey: QUEUE_NAME,
                        basicProperties: null,
                        body: byteArray
                        );
                }
            }

            return(Accepted());
        }
Exemplo n.º 8
0
        public async Task AddNewMessageAsync_WithCorrectData_CheckForExist()
        {
            var             context    = SteuDbContextInMemoryFactory.InitializeContext();
            MessagesService service    = this.IntializeMessagesService(context);
            var             repository = new EfDeletableEntityRepository <Message>(context);

            var model = new SendMessageInputModel()
            {
                Content       = "content",
                CountryFrom   = "Bulgaria",
                CountryTo     = "Serbia",
                OrderId       = "orderId",
                RecipientId   = "recipientId",
                Referer       = "/",
                TownFrom      = "Sofia",
                TownTo        = "Nis",
                TruckTypeIcon = "asd",
            };

            await service.AddNewMessageAsync("senderId", model);

            var actualArray = await repository.All().ToListAsync();

            Assert.Single(actualArray);
        }
        public async Task <IActionResult> Write(SendMessageInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.emailSender.SendEmailAsync(InfoConstant.SecondEmail, GlobalConstants.SystemName, input.Email, input.About, input.Description);

            await this.messagesService.AddSendMessageAsync(input);

            return(this.RedirectToAction("Sent"));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Post(SendMessageInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.NoContent());
            }

            var sanitizer = new HtmlSanitizer();
            var message   = sanitizer.Sanitize(inputModel.Message);
            var senderId  = this.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var model     = await this.messagesService.Create(senderId, inputModel.UserId, message);

            return(this.Json(model));
        }
Exemplo n.º 11
0
        public async Task TestGetSendMessagesById()
        {
            var sendMessageInputModel = new SendMessageInputModel
            {
                About       = "Test About",
                Description = "Test Description Test Description",
                Email       = "*****@*****.**",
            };

            var id = await this.messagesService.AddSendMessageAsync(sendMessageInputModel);

            var expectMessage = this.messagesService.GetSendMessageById(id);

            Assert.NotNull(expectMessage);
        }
Exemplo n.º 12
0
        public async Task TestAddSendMessageAsync()
        {
            var sendMessageInputModel = new SendMessageInputModel
            {
                About       = "Test About",
                Description = "Test Description Test Description",
                Email       = "*****@*****.**",
            };

            await this.messagesService.AddSendMessageAsync(sendMessageInputModel);

            var sendMessageToEmail = this.sendMessageRepository.All().FirstOrDefault().ToEmail;

            Assert.NotNull(sendMessageToEmail);
        }
Exemplo n.º 13
0
        public void SendMessage_returns_zero_when_incorrect()
        {
            var message = new SendMessageInputModel
            {
                Description  = TestsConstants.InvalidTestMessageDescription,
                ShowAll      = false,
                RecieverId   = TestsConstants.TestId1,
                RecieverName = TestsConstants.TestUsername2.ToUpper()
            };

            var expectedResult = 0;
            var actualResult   = this.messageService.SendMessage(message, TestsConstants.TestId);

            Assert.Equal(expectedResult, actualResult);
        }
        public async Task <string> AddSendMessageAsync(SendMessageInputModel input)
        {
            var newSendEmail = new SendMessage
            {
                About       = this.sanitizer.Sanitize(input.About),
                Description = this.sanitizer.Sanitize(input.Description),
                ToEmail     = this.sanitizer.Sanitize(input.Email),
            };

            await this.dbSendMessage.AddAsync(newSendEmail);

            await this.dbSendMessage.SaveChangesAsync();

            return(newSendEmail.Id);
        }
Exemplo n.º 15
0
        public async Task TestGetAllSendMessages()
        {
            var sendMessageInputModel = new SendMessageInputModel
            {
                About       = "Test About",
                Description = "Test Description Test Description",
                Email       = "*****@*****.**",
            };

            await this.messagesService.AddSendMessageAsync(sendMessageInputModel);

            await this.messagesService.AddSendMessageAsync(sendMessageInputModel);

            var sendMessageCount = this.messagesService.GetAllSendMessages().Count;

            Assert.Equal(2, sendMessageCount);
        }
Exemplo n.º 16
0
        public async Task TestDeleteSendMessageAsync()
        {
            var sendMessageInputModel = new SendMessageInputModel
            {
                About       = "Test About",
                Description = "Test Description Test Description",
                Email       = "*****@*****.**",
            };

            var id = await this.messagesService.AddSendMessageAsync(sendMessageInputModel);

            await this.messagesService.DeleteSendMessageAsync(id);

            var expectMessage = this.sendMessageRepository.AllWithDeleted().Where(m => m.Id == id).FirstOrDefault();

            Assert.True(expectMessage.IsDeleted);
        }
Exemplo n.º 17
0
        public void SendMessage_returns_one_when_correct()
        {
            var message = new SendMessageInputModel
            {
                Description  = TestsConstants.ValidTestMessageDescription,
                ShowAll      = false,
                RecieverId   = TestsConstants.TestId1,
                RecieverName = TestsConstants.TestUsername2
            };

            var expectedResult = 1;
            var actualResult   = this.messageService.SendMessage(message, TestsConstants.TestId);

            Assert.Equal(expectedResult, actualResult);

            this.SeedDb();
        }
Exemplo n.º 18
0
        public async Task<IActionResult> NewMessage(SendMessageInputModel model)
        {
            if (this.ModelState.IsValid == false)
            {
                return this.View(model);
            }

            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            await this.messagesService.AddNewMessageAsync(userId, model);

            var count = this.messagesService.GetCountOfAllUnReadMessagesByUserId(model.RecipientId);

            await this.hubContext.Clients.User(model.RecipientId).SendAsync("MessageCount", count);

            return this.Redirect(model.Referer);
        }
Exemplo n.º 19
0
        public async Task AddNewMessageAsync_WithNullSenderId_ShouldReturnArgumentNullException()
        {
            var             context    = SteuDbContextInMemoryFactory.InitializeContext();
            MessagesService service    = this.IntializeMessagesService(context);
            var             repository = new EfDeletableEntityRepository <Message>(context);

            var model = new SendMessageInputModel()
            {
                Content       = "Content",
                CountryFrom   = "Bulgaria",
                CountryTo     = "Serbia",
                OrderId       = "orderId",
                RecipientId   = "recipientId",
                Referer       = "/",
                TownFrom      = "Sofia",
                TownTo        = "Nis",
                TruckTypeIcon = "asda",
            };

            await Assert.ThrowsAsync <ArgumentNullException>(()
                                                             => service.AddNewMessageAsync(null, model));
        }
Exemplo n.º 20
0
        public PartialViewResult ChatWithSomebody([FromBody] SendMessageInputModel model)
        {
            var reciever = this.accountService.GetUserByName(model.RecieverName, this.ModelState);

            if (this.ModelState.IsValid)
            {
                var recieverId = reciever.Id;

                model.Messages   = this.messageService.GetConversationMessages(this.User.Identity.Name, model.RecieverName, model.ShowAll);
                model.RecieverId = recieverId;

                var result = this.PartialView("_ChatViewPartial", model);

                return(result);
            }
            else
            {
                var result = this.PartialView("_ErrorPartial", this.ModelState);

                return(result);
            }
        }
Exemplo n.º 21
0
 public async Task <IViewComponentResult> InvokeAsync(SendMessageInputModel viewModel)
 {
     return(this.View(viewModel));
 }
Exemplo n.º 22
0
        public void Send([FromBody] SendMessageInputModel model)
        {
            var author = this.accountService.GetUser(this.User);

            this.messageService.SendMessage(model, author.Id);
        }