Ejemplo n.º 1
0
        public ChannelDataModel CreatePrivateChannel(IDbConnection connection, ChannelMessageCreateModel messageModel)
        {
            var newChannel = CreateChannel(connection, new ChannelDataModel
            {
                ChannelIcon = messageModel.UserIcon,
                ChannelName = messageModel.UserName + " <==> " + messageModel.To.Name,
                CreatorName = messageModel.UserName,
                Password    = Guid.NewGuid().ToString(),
                ChannelType = messageModel.ChannelType,
                CreatorId   = messageModel.UserId
            }, messageModel.UserId);
            var secondUser =
                AddUserToConnectionChannel(connection, newChannel, messageModel.To.Id, true, true, newChannel.Password);

            newChannel.ChannelConnections.Add(secondUser);
            return(newChannel);
        }
        public async Task <bool> UserChannelsCreatePrivateChannel(ChannelMessageCreateModel messageModel)
        {
            _tryCatch(() => { messageModel.Validate(); });
            return(await _contextActionAsync(async connection =>
            {
                var cr = _getCurrentUser(connection);
                messageModel.DateCreate = UnixTime.UtcNow();
                messageModel.UserId = cr.UserId;
                messageModel.UserName = cr.Name;
                messageModel.ChannelType = ChannelTypes.Private;

                var channel = _channelService.GetPrivateChannel(connection, cr.UserId, messageModel.To.Id);
                if (channel != null && channel.Id != 0)
                {
                    if (channel.ChannelConnections.Count > 2)
                    {
                        throw new NotImplementedException("channel.ChannelConnections.Count > 2");
                    }
                    if (channel.ChannelConnections.Count == 0)
                    {
                        throw new NotImplementedException("channel.ChannelConnections.Count == 0");
                    }
                    if (channel.ChannelConnections.Count == 1)
                    {
                        var conn = channel.ChannelConnections[0];
                        if (conn.UserId == cr.UserId || conn.UserId == messageModel.To.Id)
                        {
                            _userChannelsCheckAndFixChannelCoonectionUser(connection, channel, conn);
                            var addUserConnectionId = conn.UserId == cr.UserId ? messageModel.To.Id : cr.UserId;
                            channel.ChannelConnections.Add(_channelService.AddUserToConnectionChannel(connection,
                                                                                                      channel, addUserConnectionId, true, true, channel.Password));
                        }
                        else
                        {
                            throw new ArgumentOutOfRangeException(nameof(conn.UserId), conn.UserId, Error.NotPermitted);
                        }
                    }
                    else
                    {
                        foreach (var conn in channel.ChannelConnections)
                        {
                            if (conn.UserId == cr.UserId || conn.UserId == messageModel.To.Id)
                            {
                                _userChannelsCheckAndFixChannelCoonectionUser(connection, channel, conn);
                            }
                            else
                            {
                                throw new SecurityException(Error.NotPermitted);
                            }
                        }
                    }
                }
                else
                {
                    channel = _channelService.CreatePrivateChannel(connection, messageModel);
                }

                messageModel.ChannelId = channel.Id;
                var newMessage = _channelService.CreateMessage(connection, messageModel);
                messageModel.UpdateByBase(newMessage);
                var channelOut = _channelService.GetPrivateChannelOut(connection, channel, cr.UserId);
                var privateGroupName = await cr.AddOrReplacePrivateChannelGroupNameAsync(Groups, channel.Id);
                var crUpdHubUser = _hubCache.AddOrUpdateLocal(cr, true);
                var targetUser = _getOnlineSingleUser(connection, messageModel.To.Id);
                if (targetUser != null)
                {
                    await targetUser.AddOrReplacePrivateChannelGroupNameAsync(Groups, channel.Id);
                    _hubCache.AddOrUpdateLocal(targetUser, true);
                }

                var iHubGroupItem = crUpdHubUser.GetUserChannelGroup(channel.Id);
                await Clients.Group(privateGroupName)
                .InvokeAsync("onUserChannelsCreatedPrivateChannel", channelOut, iHubGroupItem);
                return true;
            }));
        }