Exemplo n.º 1
0
        protected override async Task ActionAsync()
        {
            var boxId = Input.Data.BoxId;

            if (boxId.Type == TypeBox.Site)
            {
                await UserSecurity.CheckAdministratorAsync(_userService, Input.UserId, boxId.Id);
            }
            else if (boxId.Type == TypeBox.User)
            {
                if (boxId.Id != Input.UserId)
                {
                    throw new NotAuthorizedException("You are not authorized to process this action.");
                }
            }

            await _messageService.SaveRead(Input.Data.ChatId, new Read()
            {
                CreateDate = DateTime.Now, Id = new BoxId()
                {
                    Id = Input.UserId, Type = TypeBox.User
                }
            });

            var chat = await _messageService.GetChatAsync(Input.Data.ChatId);

            var chatItem = await ListMessageCommand.MapChatItem(_logger, _siteMap, _userService, chat, Input.UserId);

            Result.Data = new GetMessageResult
            {
                Chat = chatItem
            };
        }
Exemplo n.º 2
0
        protected override async Task ActionAsync()
        {
            var data = Input.Data;

            var messageJson = data.MessageJson;
            var messageType = data.Type;

            var message     = GetMessage(messageType, messageJson);
            var createdDate = DateTime.Now;

            var messageDbModel = new MessageDbModel();

            messageDbModel.CreateDate  = createdDate;
            messageDbModel.FromId      = data.From;
            messageDbModel.Source      = data.Source;
            messageDbModel.Message     = data.MessageJson;
            messageDbModel.MessageType = data.Type;

            IList <To> toList = new List <To> {
                new To()
                {
                    Id = data.To
                }, new To()
                {
                    Id = data.From
                }
            };
            string      chatId;
            ChatDbModel chatDbModel;
            Read        read = null;

            // TODO control input
            if (!string.IsNullOrEmpty(Input.UserId))
            {
                read = new Read()
                {
                    CreateDate = createdDate, Id = data.From
                };
            }

            if (string.IsNullOrEmpty(data.ChatId))
            {
                var reads = new List <Read>();
                if (read != null)
                {
                    reads.Add(read);
                }

                chatDbModel            = new ChatDbModel();
                chatDbModel.CreateDate = createdDate;
                chatDbModel.UpdateDate = createdDate;
                chatDbModel.Title      = ((MessageSiteAuthenticated)message).Title;
                chatDbModel.Reads      = reads;
                chatDbModel.LastReads  = reads;
                chatDbModel.To         = toList;
                chatDbModel.Messages   = new List <MessageDbModel> {
                    messageDbModel
                };
                chatDbModel.NumberMessages = 1;

                await _messageService.SaveChatAsync(chatDbModel);

                chatId = chatDbModel.Id;
            }
            else
            {
                await _messageService.SaveMessageAsync(data.ChatId, messageDbModel, read);

                chatDbModel = await _messageService.GetChatAsync(data.ChatId);

                toList = chatDbModel.To;
                chatId = chatDbModel.Id;
            }

            Result.Data = new SendMessageResult()
            {
                Chat = await ListMessageCommand.MapChatItem(_logger, _siteMap, _userService, chatDbModel, Input.UserId)
            };

            ApplicationUser senderUserDb = null;

            if (!string.IsNullOrEmpty(data.From.Id) && data.From.Type == TypeBox.User)
            {
                senderUserDb = await _userService.FindApplicationUserByIdAsync(data.From.Id);
            }

            var siteId = toList.Where(t => t.Id.Type == TypeBox.Site).Select(t => t.Id.Id).FirstOrDefault();

            var site = await _siteMap.SiteUrlAsync(siteId);

            var siteName = site.Name;
            var siteUrl  = site.Url;
            var isReply  = data.Type == "Reply";

            foreach (var to in toList)
            {
                if (to.Id.Type == TypeBox.Site && !string.IsNullOrEmpty(to.Id.Id))
                {
                    var users = await _userService.UserByRoleAsync(siteId);

                    foreach (var userDb in users)
                    {
                        await
                        SendMessageReceiver(siteName, siteUrl, userDb, message, senderUserDb, messageType,
                                            chatDbModel, true);
                    }
                }
                else if (to.Id.Type == TypeBox.User && !string.IsNullOrEmpty(to.Id.Id))
                {
                    var userDb = await _userService.FindApplicationUserByIdAsync(to.Id.Id);
                    await
                    SendMessageReceiver(siteName, siteUrl, userDb, message, senderUserDb, messageType, chatDbModel, false);
                } /*else if (to.Id.Type == TypeBox.UserNotAuthenticated)
                   * {
                   * var messageUserNotAuthenticated = FindUserNotAUthenticatedInfo(chatDbModel);
                   * await
                   *    SendMessageReceiver(siteName, siteUrl, null, messageUserNotAuthenticated, senderUserDb, messageType, chatDbModel, false);
                   * }*/
            }

            await SendMessageSender(messageType, message, siteName, siteUrl, isReply, senderUserDb, data, chatId, chatDbModel);
        }