Exemplo n.º 1
0
        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.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, _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();

            await _pusher.GroupJoinedEvent(user, createdGroup, null, 0);

            return(Json(new AiurValue <int>(createdGroup.Id)
            {
                Code = ErrorType.Success,
                Message = "You have successfully created a new group and joined it!"
            }));
        }
Exemplo n.º 2
0
        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.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 _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();

            await Task.WhenAll(
                _pusher.GroupJoinedEvent(user, group, latestMessage, messagesCount),
                group.ForEachUserAsync((eachUser, relation) => _pusher.NewMemberEvent(eachUser, user, group.Id))
                );

            return(Json(new AiurValue <int>(group.Id)
            {
                Code = ErrorType.Success,
                Message = $"You have successfully joint the group: {groupName}!"
            }));
        }