Beispiel #1
0
        public async Task <IActionResult> CreateSingleChatRoom([FromBody] JObject jObject)
        {
            var result = new OperationResult <ChatRoom>();

            var username = jObject["username"].ToString();
            var greeting = jObject["message"] != null ? jObject["message"].ToString() : null;

            if (await _userManager.FindByNameAsync(username) == null)
            {
                return(new ObjectResult(await result.Fail(EnumHelper.GetStringValue(AuthError.UserNotFound))));
            }

            var chatRoom = new ChatRoom
            {
                Id    = ObjectId.GenerateNewId().ToString(),
                Users = new List <string> {
                    User.Identity.Name, username
                },
                Messages        = new List <ChatMessage>(),
                Type            = ChatRoomType.Single,
                LastMessageTime = DateTime.Now,
                Name            = username
            };

            if ((await _chatRoomService.GetChatRoomsForUserAsync(User.Identity.Name))
                .Any(r => r.Users.Contains(username)))
            {
                return(new ObjectResult(await result.Fail(EnumHelper.GetStringValue(ChatError.RoomAlreadyExists))));
            }
            await _chatRoomService.CreateChatRoomAsync(chatRoom);

            if (greeting != null)
            {
                var greetingMessage = new ChatMessage
                {
                    Id        = ObjectId.GenerateNewId().ToString(),
                    From      = User.Identity.Name,
                    To        = username,
                    Timestamp = DateTime.Now,
                    RoomId    = chatRoom.Id,
                    Status    = ChatMessageStatus.Sent,
                    Body      = greeting
                };

                var addHeaders = new Dictionary <string, object> {
                    { "greeting", "true" }
                };
                await _chatMessageService.PublishMessageAsync(greetingMessage, addHeaders);

                chatRoom.Messages.Add(greetingMessage);
            }
            return(new ObjectResult(await result.Success(chatRoom)));
        }
Beispiel #2
0
        public async Task <IActionResult> SendMessage([FromBody] SendMessageModel model)
        {
            var username = User.Identity.Name;

            var message = new ChatMessage()
            {
                Id        = model.Id ?? ObjectId.GenerateNewId().ToString(),
                RoomId    = model.Room.Id ?? ObjectId.GenerateNewId().ToString(),
                From      = User.Identity.Name,
                Timestamp = DateTime.Now,
                Status    = ChatMessageStatus.Sending,
                Body      = model.Body
            };

            switch (model.Room.Type)
            {
            case ChatRoomType.Single:
                message.To = model.Room.Users.FirstOrDefault(u => u != username);
                break;

            case ChatRoomType.Group:
                message.To = $"group_{model.Room.Id}";
                break;

            default:
                throw new InvalidOperationException();
            }

            var headers = new Dictionary <string, object>();

            if (model.Session != null)
            {
                headers["session"] = model.Session;
            }

            await _chatMessageService.PublishMessageAsync(message, headers);

            return(new ObjectResult(message));
        }