public ActionResult Chat(ChatRequestModel request)
        {
            var response = new PartialViewModel();

            var user = UserService.GetUser(request.User.Id);

            if (user == null || user.Role == Models.User_Model.RoleLevel.Blocked)
            {
                response.Logout = true;
                return(Json(response));
            }

            int chatroomId = 0;

            if (request.RawChatroomIdValue != null)
            {
                chatroomId = request.RawChatroomIdValue.GetHashCode();
            }
            int parentChatroomId = chatroomId; //temporary during initial testing

            string chatroomName = request.ChatroomName;

            if (!ChatroomService.DoesChatroomExist(chatroomId))
            {
                ChatroomService.CreateChatroom(chatroomId, chatroomName);
            }

            var joinErrors = SecurityService.CanUserJoinChatroom(request);

            response.Errors.AddRange(joinErrors);

            if (joinErrors.Count == 0)
            {
                if (!ChatroomService.AddUserToChatroom(chatroomId, parentChatroomId, request.User.Id, request.UserHandle))
                {
                    response.AddError("Error adding user into chatroom.");
                }
            }

            var chatroomModel = new ChatroomModel()
            {
                ChatroomId       = chatroomId,
                ChatroomName     = chatroomName,
                ParentChatroomId = parentChatroomId,
                UserHandle       = request.UserHandle,
                UserId           = request.User.Id
            };

            //response.Data = PartialView("~/Views/Chatroom/_Chat.cshtml", chatroomModel);
            response.Data = RenderPartialViewToString(this.ControllerContext, "~/Views/Chatroom/_Chat.cshtml", chatroomModel);

            return(Json(response));
        }
Beispiel #2
0
        //Helper method for creating a chatroom since MVC uses controller contexts to route the user to the chat partial view page/
        //We don't need that routing here, all we need is the logic or 99% above that line so just replicate it here and
        //This will create a chatroom and add the user to it
        //The chatrrom only exists in memory while the tests are run. Each new build refreshes
        public ChatResponseTestModel createChatroomAndAddUser(ChatRequestModel request)
        {
            var response = new ChatResponseTestModel();

            int chatroomId = 0;

            if (request.RawChatroomIdValue != null)
            {
                chatroomId = request.RawChatroomIdValue.GetHashCode();
            }
            int parentChatroomId = chatroomId; //temporary during initial testing

            string chatroomName = request.ChatroomName;

            if (!ChatroomService.DoesChatroomExist(chatroomId))
            {
                ChatroomService.CreateChatroom(chatroomId, chatroomName);
            }

            var joinErrors = SecurityService.CanUserJoinChatroom(request);

            response.Errors.AddRange(joinErrors);

            if (joinErrors.Count == 0)
            {
                if (!ChatroomService.AddUserToChatroom(chatroomId, parentChatroomId, request.User.Id, request.UserHandle))
                {
                    response.AddError("Error adding user into chatroom.");
                }
            }

            var chatroomModel = new ChatroomModel()
            {
                ChatroomId       = chatroomId,
                ChatroomName     = chatroomName,
                ParentChatroomId = parentChatroomId,
                UserHandle       = request.UserHandle,
                UserId           = request.User.Id
            };

            response.ChatroomModel = chatroomModel;

            return(response);
        }