public async Task <IActionResult> FindMessageChats(int ID)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            var RequestedUser = unitOfWork.GGUsers.GetUser(ID);

            var chatGroup = await unitOfWork.db.ChatGroups
                            .Where(cg =>
                                   cg.UserChatGroups.Any(ucg => ucg.UserId == currentUser.Id) &&
                                   cg.UserChatGroups.Any(ucg => ucg.UserId == RequestedUser.Id))
                            .Include(cg => cg.Messages)
                            .FirstOrDefaultAsync();


            if (chatGroup == null)
            {
                chatGroup = new ChatGroup()
                {
                    Name           = RequestedUser.NickName,
                    UserChatGroups = new List <UserChatGroup>(),
                    Messages       = new List <Message>()
                };

                var chatGroupRelation1 = new UserChatGroup()
                {
                    User  = currentUser,
                    Group = chatGroup
                };

                var chatGroupRelation2 = new UserChatGroup()
                {
                    User  = RequestedUser,
                    Group = chatGroup
                };

                chatGroup.UserChatGroups.Add(chatGroupRelation1);
                chatGroup.UserChatGroups.Add(chatGroupRelation2);

                unitOfWork.db.ChatGroups.Add(chatGroup);
                await unitOfWork.Save();


                chatGroup = await unitOfWork.db.ChatGroups
                            .Where(cg =>
                                   cg.UserChatGroups.Any(ucg => ucg.UserId == currentUser.Id) &&
                                   cg.UserChatGroups.Any(ucg => ucg.UserId == RequestedUser.Id))
                            .Include(cg => cg.Messages)
                            .FirstOrDefaultAsync();
            }
            var viewModel = new ChatGroupVM {
                Group = chatGroup, CurrentUserNickName = currentUser.NickName, CurrentMessageChatID = chatGroup.Id
            };

            return(PartialView("_ChatBox", viewModel));
        }
Beispiel #2
0
        public ActionResultVM GroupNew([FromBody] ChatGroupVM cg)
        {
            var vm = new ActionResultVM();

            try
            {
                vm = ccs.HandelGroupNew(cg, Groups, db);
            }
            catch (Exception ex)
            {
                vm.Set(ex);
            }

            return(vm);
        }
        public async Task <IActionResult> GetPartial(int chatId)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            //db.Users.Where(u => u.Id == aspNetUserId).Select(u => u.UserAccount).SingleOrDefault();
            var chatGroup = await unitOfWork.db.ChatGroups.Where(cg => cg.Id == chatId)
                            .Include(cg => cg.Messages)
                            .FirstOrDefaultAsync();


            var viewModel = new ChatGroupVM {
                Group = chatGroup, CurrentUserNickName = currentUser.NickName, CurrentMessageChatID = chatGroup.Id
            };


            return(PartialView("_ChatBox", viewModel));
        }
Beispiel #4
0
        /// <summary>
        /// 处理新建组
        /// </summary>
        /// <param name="cg">组信息</param>
        /// <param name="Groups"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static ActionResultVM HandelGroupNew(ChatGroupVM cg, IGroupManager Groups, Data.ContextBase db)
        {
            var vm = new ActionResultVM();

            if (cg == null)
            {
                vm.Set(ARTag.lack);
                vm.Msg = "组信息不能为空";
            }
            else if (string.IsNullOrWhiteSpace(cg.GroupName))
            {
                vm.Set(ARTag.lack);
                vm.Msg = "组名称不能为空";
            }
            else if (cg.GroupUser.Count == 0)
            {
                vm.Set(ARTag.fail);
                vm.Msg = "组用户至少有一个(默认第一个用户为管理员)";
            }

            //管理员
            var users = FindUsers(cg.GroupUser.Keys.ToList());

            if (users?.Count > 0)
            {
                var now = DateTime.Now;

                cg.GroupId = Core.UniqueTo.LongId().ToString();

                //组成员
                var listgm = new List <Domain.NChatGroupMember>();

                //用户加入组
                foreach (var user in users)
                {
                    foreach (var conn in user.Conns.Keys)
                    {
                        Groups.AddToGroupAsync(conn, cg.GroupId);
                    }

                    //添加组成员
                    listgm.Add(new Domain.NChatGroupMember()
                    {
                        CgmId         = Core.UniqueTo.LongId().ToString(),
                        CgId          = cg.GroupId,
                        CuUserId      = user.UserId,
                        CgmCreateTime = now,
                        CgmStatus     = 1
                    });
                }

                //维护组
                OnlineGroup1.Add(cg.GroupId, cg);

                //写入表
                db.NChatGroup.Add(new Domain.NChatGroup()
                {
                    CgId         = cg.GroupId,
                    CgName       = cg.GroupName,
                    CgOwnerId    = users.FirstOrDefault().UserId,
                    CgCreateTime = DateTime.Now,
                    CcId         = string.IsNullOrWhiteSpace(cg.GroupClassify) ? "1" : cg.GroupClassify,
                    CgStatus     = 1
                });
                db.NChatGroupMember.AddRange(listgm);
                db.SaveChangesAsync();

                //成功
                vm.Data = cg.GroupId;
                vm.Set(ARTag.success);
            }
            else
            {
                vm.Set(ARTag.invalid);
                vm.Msg = "组用户无效";
            }

            return(vm);
        }