public async Task <IActionResult> CreateGroupConversation(CreateGroupConversationAddressModel model) { var user = await GetKahlaUser(); var exsists = _dbContext.GroupConversations.Exists(t => t.GroupName == model.GroupName); if (exsists) { return(this.Protocal(ErrorType.NotEnoughResources, $"A group with name: {model.GroupName} was already exists!")); } var createdGroup = await _dbContext.CreateGroup(model.GroupName); var newRelationship = new UserGroupRelation { UserId = user.Id, GroupId = createdGroup.Id, ReadTimeStamp = DateTime.MinValue }; _dbContext.UserGroupRelations.Add(newRelationship); await _dbContext.SaveChangesAsync(); return(Json(new AiurValue <int>(createdGroup.Id) { Code = ErrorType.Success, Message = "You have successfully created a new group and joined it!" })); }
public async Task <IActionResult> JoinGroup([Required] string groupName) { var user = await GetKahlaUser(); var group = await _dbContext.GroupConversations.SingleOrDefaultAsync(t => t.GroupName == groupName); if (group == null) { return(this.Protocal(ErrorType.NotFound, $"We can not find a group with name: {groupName}!")); } var joined = _dbContext.UserGroupRelations.Exists(t => t.UserId == user.Id && t.GroupId == group.Id); if (joined) { return(this.Protocal(ErrorType.HasDoneAlready, $"You have already joined the group: {groupName}!")); } // All checked and able to join him. // Warning: Currently we do not have invitation system for invitation control is too complicated. var newRelationship = new UserGroupRelation { UserId = user.Id, GroupId = group.Id }; _dbContext.UserGroupRelations.Add(newRelationship); await _dbContext.SaveChangesAsync(); return(this.Protocal(ErrorType.Success, $"You have successfully joint the group: {groupName}!")); }
public bool UpdateUserGroupRelation(UserGroupRelation model) { Db.user_group_relation.AddOrUpdate(model.ToEntity()); var saveOk = Db.SaveChanges() > 0; return(saveOk); }
public async Task <IActionResult> JoinGroup([Required] string groupName, string joinPassword) { var user = await _userManager.GetUserAsync(User); var group = await _dbContext.GroupConversations.SingleOrDefaultAsync(t => t.GroupName == groupName); if (group == null) { return(this.Protocol(ErrorType.NotFound, $"无法找到对应的群聊: {groupName}!")); } var joined = _dbContext.UserGroupRelations.Any(t => t.UserId == user.Id && t.GroupId == group.Id); if (joined) { return(this.Protocol(ErrorType.HasDoneAlready, $"您已经加入了这个群聊: {groupName}!")); } if (group.HasPassword && group.JoinPassword != joinPassword?.Trim()) { return(this.Protocol(ErrorType.WrongKey, "这个群聊需要密码, 但是您的密码不正确!")); } var newRelationship = new UserGroupRelation { UserId = user.Id, GroupId = group.Id }; _dbContext.UserGroupRelations.Add(newRelationship); await _dbContext.SaveChangesAsync(); return(this.Protocol(ErrorType.Success, $"您成功加入了这个群聊: {groupName}!")); }
public async Task <IActionResult> JoinGroup([Required] string groupName, string joinPassword) { var user = await GetKahlaUser(); if (!user.EmailConfirmed) { return(this.Protocol(ErrorType.Unauthorized, "You are not allowed to join groups without confirming your email!")); } GroupConversation group = null; lock (_obj) { group = _dbContext .GroupConversations .Include(t => t.Users) .ThenInclude(t => t.User) .SingleOrDefault(t => t.GroupName == groupName); if (group == null) { return(this.Protocol(ErrorType.NotFound, $"We can not find a group with name: {groupName}!")); } if (group.HasPassword && group.JoinPassword != joinPassword?.Trim()) { return(this.Protocol(ErrorType.WrongKey, "The group requires password and your password was not correct!")); } var joined = group.Users.Any(t => t.UserId == user.Id); if (joined) { return(this.Protocol(ErrorType.HasDoneAlready, $"You have already joined the group: {groupName}!")); } // All checked and able to join him. // Warning: Currently we do not have invitation system for invitation control is too complicated. var newRelationship = new UserGroupRelation { UserId = user.Id, GroupId = group.Id }; _dbContext.UserGroupRelations.Add(newRelationship); _dbContext.SaveChanges(); } await group.ForEachUserAsync((eachUser, relation) => _pusher.NewMemberEvent(eachUser, user, group.Id), _userManager); return(this.Protocol(ErrorType.Success, $"You have successfully joint the group: {groupName}!")); }
public async Task <IActionResult> CreateGroupConversation(CreateGroupConversationAddressModel model) { var user = await GetKahlaUser(); if (!user.EmailConfirmed) { return(this.Protocol(ErrorType.Unauthorized, "You are not allowed to join groups without confirming your email!")); } model.GroupName = model.GroupName.Trim(); var exists = _dbContext.GroupConversations.Any(t => t.GroupName.ToLower() == model.GroupName.ToLower()); if (exists) { return(this.Protocol(ErrorType.NotEnoughResources, $"A group with name: {model.GroupName} was already exists!")); } var limitedDate = DateTime.UtcNow - new TimeSpan(1, 0, 0, 0); var todayCreated = await _dbContext .GroupConversations .Where(t => t.OwnerId == user.Id) .Where(t => t.ConversationCreateTime > limitedDate) .CountAsync(); if (todayCreated > 4) { return(this.Protocol(ErrorType.NotEnoughResources, "You have created too many groups today. Try it tomorrow!")); } var createdGroup = await _dbContext.CreateGroup(model.GroupName, user.Id, model.JoinPassword); var newRelationship = new UserGroupRelation { UserId = user.Id, GroupId = createdGroup.Id, ReadTimeStamp = DateTime.MinValue }; _dbContext.UserGroupRelations.Add(newRelationship); await _dbContext.SaveChangesAsync(); return(Json(new AiurValue <int>(createdGroup.Id) { Code = ErrorType.Success, Message = "You have successfully created a new group and joined it!" })); }
public ActionResult Create(Group group) { if (WebSecurity.CurrentUserId > 0) { UserGroupRelation groupMember = new UserGroupRelation(); group.UserId = WebSecurity.CurrentUserId; _groupRepository.AddGroup(group); ViewBag.GroupId = group.Id; groupMember.UserId = WebSecurity.CurrentUserId; groupMember.Groupid = group.Id; _groupRelationRepository.Add(groupMember); return(RedirectToAction("AddMember", new { groupId = group.Id })); } return(RedirectToAction("Login", "Account")); }
public async Task <IActionResult> CreateGroupConversation(CreateGroupConversationAddressModel model) { var user = await GetKahlaUser(); model.GroupName = model.GroupName.Trim(); var exists = _dbContext.GroupConversations.Any(t => t.GroupName.ToLower() == model.GroupName.ToLower()); if (exists) { return(this.Protocol(ErrorType.Conflict, $"A group with name: {model.GroupName} was already exists!")); } var limitedDate = DateTime.UtcNow - new TimeSpan(1, 0, 0, 0); var todayCreated = await _dbContext .GroupConversations .Where(t => t.OwnerId == user.Id) .Where(t => t.ConversationCreateTime > limitedDate) .CountAsync(); if (todayCreated > 4) { return(this.Protocol(ErrorType.TooManyRequests, "You have created too many groups today. Try it tomorrow!")); } var createdGroup = await _dbContext.CreateGroup(model.GroupName, _configuration["GroupImagePath"], user.Id, model.JoinPassword); var newRelationship = new UserGroupRelation { UserId = user.Id, GroupId = createdGroup.Id, ReadTimeStamp = DateTime.MinValue }; await _dbContext.UserGroupRelations.AddAsync(newRelationship); await _dbContext.SaveChangesAsync(); _cannonQueue.QueueWithDependency <KahlaPushService>(pusher => pusher.GroupJoinedEvent(user, createdGroup, null, 0)); return(this.Protocol(new AiurValue <int>(createdGroup.Id) { Code = ErrorType.Success, Message = "You have successfully created a new group and joined it!" })); }
public static UpdateUserGroupRelation_M ToViewModel(UserGroupRelation model, List <SelectListItem> userListItems) { return(new UpdateUserGroupRelation_M() { _UserListItems = userListItems, user_group_relation_id = model.user_group_relation_id, user_id = model.user_id, nick_name = model.nick_name, user_group_id = model.user_group_id, create_user_id = model.create_user_id, create_user_name = model.create_user_name, create_date = model.create_date, }); }
public async Task <IActionResult> CreateGroupConversation(CreateGroupConversationAddressModel model) { var user = await _userManager.GetUserAsync(User); model.GroupName = model.GroupName.Trim().ToLower(); var exists = _dbContext.GroupConversations.Any(t => t.GroupName == model.GroupName); if (exists) { return(this.Protocol(ErrorType.NotEnoughResources, $"这个群名: {model.GroupName} 已经被占用了!")); } var limitedDate = DateTime.UtcNow - new TimeSpan(1, 0, 0, 0); var todayCreated = await _dbContext .GroupConversations .Where(t => t.OwnerId == user.Id) .Where(t => t.ConversationCreateTime > limitedDate) .CountAsync(); if (todayCreated > 100) { return(this.Protocol(ErrorType.NotEnoughResources, "您今天创建的群聊太多了!")); } var createdGroup = await _dbContext.CreateGroup(model.GroupName, user.Id, model.JoinPassword); var newRelationship = new UserGroupRelation { UserId = user.Id, GroupId = createdGroup.Id, ReadTimeStamp = DateTime.MinValue }; _dbContext.UserGroupRelations.Add(newRelationship); await _dbContext.SaveChangesAsync(); return(this.ChatJson(new AiurValue <int>(createdGroup.Id) { Code = ErrorType.Success, Message = "你已经成功的创建了一个群聊并加入了它!" })); }
public async Task <IActionResult> JoinGroup([Required] string groupName, string joinPassword) { var user = await GetKahlaUser(); if (!user.EmailConfirmed) { return(this.Protocol(ErrorType.Unauthorized, "You are not allowed to join groups without confirming your email!")); } GroupConversation group; lock (Obj) { group = _dbContext .GroupConversations .Include(t => t.Users) .ThenInclude(t => t.User) .SingleOrDefault(t => t.GroupName == groupName); if (group == null) { return(this.Protocol(ErrorType.NotFound, $"We can not find a group with name: {groupName}!")); } if (group.HasPassword && group.JoinPassword != joinPassword?.Trim()) { return(this.Protocol(ErrorType.WrongKey, "The group requires password and your password was not correct!")); } var joined = group.Users.Any(t => t.UserId == user.Id); if (joined) { return(this.Protocol(ErrorType.HasSuccessAlready, $"You have already joined the group: {groupName}!")); } // All checked and able to join him. // Warning: Currently we do not have invitation system for invitation control is too complicated. var newRelationship = new UserGroupRelation { UserId = user.Id, GroupId = group.Id }; _dbContext.UserGroupRelations.Add(newRelationship); _dbContext.SaveChanges(); } await _dbContext.Entry(user) .Collection(t => t.HisDevices) .LoadAsync(); var messagesCount = await _dbContext.Entry(group) .Collection(t => t.Messages) .Query() .CountAsync(); var latestMessage = await _dbContext .Messages .Include(t => t.Sender) .OrderByDescending(t => t.SendTime) .FirstOrDefaultAsync(); group.ForEachUser((eachUser, relation) => _cannonQueue.QueueWithDependency <KahlaPushService>(pusher => pusher.NewMemberEvent(eachUser, user, group.Id))); _cannonQueue.QueueWithDependency <KahlaPushService>(pusher => pusher.GroupJoinedEvent(user, group, latestMessage, messagesCount)); return(this.Protocol(new AiurValue <int>(group.Id) { Code = ErrorType.Success, Message = $"You have successfully joint the group: {groupName}!" })); }
public UserGroupRelation FindUserGroupRelation(string id) { return(UserGroupRelation.ToModel(Db.user_group_relation. FirstOrDefault(a => a.user_group_relation_id == id))); }
public bool Add(UserGroupRelation groupMember) { _db.UserGroupRelations.Add(groupMember); return(_db.SaveChanges() > 0); }