Exemplo n.º 1
0
            public async Task <Guid> Handle(Add request, CancellationToken cancellationToken)
            {
                try
                {
                    using var unitOfWork = new ContactFormServerUnitOfWork(context);

                    if (!string.IsNullOrEmpty(request.message.Honeypot))
                    {
                        throw new Exception($"Spam detected{Environment.NewLine}Account: {request.accountId}{Environment.NewLine}Payolad: {JsonSerializer.Serialize(request.message)}");
                    }

                    var account = await unitOfWork.Accounts.GetAsync(request.accountId, cancellationToken).ConfigureAwait(false);

                    if (account is null)
                    {
                        throw new HttpException(StatusCodes.Status404NotFound, $"No account with Id {request.accountId} has been found");
                    }

                    if (!account.Enabled)
                    {
                        throw new HttpException(StatusCodes.Status403Forbidden, $"{account.Id} is not enabled yet");
                    }

                    var message = new Domain.Entities.Message
                    {
                        Id          = Guid.NewGuid(),
                        Name        = request.message.Name,
                        EMail       = request.message.EMail,
                        PhoneNumber = request.message.Phone,
                        Text        = request.message.Message,
                        AccountId   = account.Id,
                        Created     = DateTime.UtcNow,
                        CreatedBy   = "me",
                    };

                    await unitOfWork.Messages.AddAsync(message, cancellationToken).ConfigureAwait(false);

                    await unitOfWork.CompleteAsync(cancellationToken);

                    return(message.Id);
                }
                catch (Exception ex)
                {
                    string content = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    this.logger.LogError(content);
                    return(Guid.Empty);
                    // throw ex;
                }
            }
Exemplo n.º 2
0
        public async Task <Unit> Handle(SaveNewMessageCommand request, CancellationToken cancellationToken)
        {
            // Get User
            var viewer = _viewerRepository.Get().FirstOrDefault(x => x.DisplayName.Equals(request.UserName) && x.IsEnabled);

            // Save message
            var message = new Domain.Entities.Message
            {
                ViewerId     = viewer.Id,
                DateReceived = DateTime.Now,
                Content      = request.Content
            };

            await _messageRepository.Add(message);

            await _messageRepository.SaveChangesAsync();

            return(new Unit());
        }
Exemplo n.º 3
0
 public bool CreateMessage(string business_id, Domain.Entities.Message message, bool real_time_update)
 {
     try
     {
         _messageRepository.Upsert(message);
     }catch (Exception ex) { }
     if (real_time_update)
     {
         try
         {
             CopyMessageToRealtimeDB(business_id, message);
         }
         catch (Exception ex)
         {
             BackgroundJob.Enqueue <MessageService>(x => x.CopyMessageToRealtimeDB(business_id, message));
         }
     }
     return(true);
 }
Exemplo n.º 4
0
        public async Task <Message> BuildMessage(Channel channel, ZaloMessage msg, Customer customer)
        {
            var message_id       = MessageService.FormatId01(channel.business_id, msg.msgid);
            var sender_ext_id    = "";
            var recipient_ext_id = "";

            if (Convert.ToInt64(msg.fromuid) > 0 && msg.fromuid.ToString() != channel.ext_id)
            {
                sender_ext_id    = msg.fromuid.ToString();
                recipient_ext_id = msg.oaid.ToString();
            }
            else
            {
                sender_ext_id    = msg.oaid.ToString();
                recipient_ext_id = msg.touid.ToString();
            }
            if (sender_ext_id.Length < 5)
            {
                return(null);
            }

            var thread_id = ThreadService.FormatId01(channel.business_id, customer.ext_id);

            var timestamp    = msg.timestamp > 9999999999 ? msg.timestamp / 1000 : msg.timestamp;
            var created_time = Core.Helpers.CommonHelper.UnixTimestampToDateTime(timestamp);

            var imageUrl = msg.href;

            if (!string.IsNullOrWhiteSpace(imageUrl) && !imageUrl.Contains("hibaza") && !imageUrl.Contains("firebase"))
            {
                imageUrl = await DownloadToLocalAsync(imageUrl);
            }

            var message = new Domain.Entities.Message
            {
                //"sendmsg"  "sendimagemsg")
                id                  = message_id,
                parent_id           = "",
                parent_ext_id       = "",
                root_ext_id         = "",
                conversation_ext_id = sender_ext_id,
                ext_id              = msg.msgid,
                thread_id           = thread_id,
                thread_type         = "message",
                sender_id           = CustomerService.FormatId01(channel.business_id, sender_ext_id),
                sender_ext_id       = sender_ext_id,
                sender_name         = sender_ext_id == customer.ext_id ? customer.name : channel.name,
                sender_avatar       = sender_ext_id == customer.ext_id ? customer.avatar : "",
                recipient_id        = CustomerService.FormatId01(channel.business_id, recipient_ext_id),
                recipient_ext_id    = recipient_ext_id,
                recipient_name      = recipient_ext_id == channel.ext_id ? channel.name : customer.name,
                recipient_avatar    = recipient_ext_id == channel.ext_id ? "" : customer.avatar,
                author              = CustomerService.FormatId01(channel.business_id, sender_ext_id),
                customer_id         = customer.id,
                message             = msg.message,
                tag                 = "",
                template            = "",
                url                 = imageUrl,
                timestamp           = timestamp,
                updated_time        = created_time,
                created_time        = created_time,
                business_id         = channel.business_id,
                channel_id          = channel.id,
                channel_ext_id      = channel.ext_id,
                channel_type        = channel.type,
                owner_id            = sender_ext_id,
                agent_id            = customer.agent_id,
                type                = msg.@event == "sendimagemsg" ? "image" : "text",
                liked               = false,
                hidden              = false,
                deleted             = false,
                urls                = "[]"
            };

            return(message);
        }
Exemplo n.º 5
0
 public void PostMessage(Domain.Entities.Message message) => Post($"{_ServiceAddress}", message);