コード例 #1
0
        public async Task <ChannelConnectionUserOut> UserChannelsGroupUpdateUser(ChannelConnectionUserOut tragetUser,
                                                                                 bool updatePasswordByChannel, int channelAdminUserId)
        {
            return(await _contextActionAsync(async connection =>
            {
                //ChannelConnectionUserOut
                var cr = _getCurrentUser(connection);

                if (cr.UserId != channelAdminUserId)
                {
                    throw new SecurityException(Error.NotPermitted);
                }
                GroupChannelOut targetChannel = null;
                ChannelConnectionDataModel oldModel = null;

                var channelId = tragetUser.ChannelId;
                var hasChange = _channelService.UpdateGroupUser(connection, tragetUser, updatePasswordByChannel,
                                                                channelAdminUserId,
                                                                targetUserChat => { targetChannel = targetUserChat; },
                                                                oldM => { oldModel = oldM; });

                if (!hasChange)
                {
                    return tragetUser;
                }
                var tu = _getOnlineSingleUser(connection, tragetUser.UserId);
                if (tu == null)
                {
                    return tragetUser;
                }
                var iHubGroupItem = cr.GetUserChannelGroup(channelId);
                // changes for target  user
                if (targetChannel != null)
                {
                    await tu.AddOrReplaceGroupChannelGroupNameAsync(Groups, channelId, iHubGroupItem.NativeName);
                    _hubCache.AddOrUpdateLocal(tu, true);
                    await Clients.Client(tu.ConnectionId).InvokeAsync("userChannelsAddOrUpdateGroupChannel",
                                                                      targetChannel, iHubGroupItem);
                    //todo Clients.Client(tu.ConnectionId).
                    return tragetUser;
                }
                if (oldModel.MessageRead && !tragetUser.MessageRead)
                {
                    await tu.RemoveGroupChannelNameAsync(Groups, channelId);
                    _hubCache.AddOrUpdateLocal(tu, true);
                    await Clients.Client(tu.ConnectionId).InvokeAsync("userChannelsGroupDropChannel", channelId, false,
                                                                      iHubGroupItem.GroupeName);
                    return tragetUser;
                }

                await Clients.Client(tu.ConnectionId)
                .InvokeAsync("onUserChannelsCrUserGroupUpdatedPermition", tragetUser);
                return tragetUser;
            }));
        }
        public GroupChannelOut CreateGroupChannel(IDbConnection connection, ChannelDataModel baseData)
        {
            GroupChannelOut result              = null;
            var             groupType           = (byte)ChannelTypes.Group;
            var             maxChannelsFromUser = 1;

            var channels = _channelRepo.GetPreCreateChannels(connection, groupType, baseData.CreatorId, baseData.ChannelName).ToList();

            if (channels.Any())
            {
                var existChannelByName = channels.SingleOrDefault(i => i.channelName == baseData.ChannelName);
                if (existChannelByName != null)
                {
                    throw new Exception(Error.ChannelNameNotValid);
                }
                if (channels.Count > maxChannelsFromUser)
                {
                    throw new Exception(Error.UserGroupChannelLimit);
                }
            }


            var userChannel  = CreateChannel(connection, baseData, baseData.CreatorId);
            var startMessage = CreateMessage(connection, new ChannelMessageDataModel
            {
                UserId     = userChannel.CreatorId,
                DateCreate = userChannel.DateCreate,
                ChannelId  = userChannel.Id,
                UserName   = userChannel.CreatorName,
                Message    = "Welcome to " + userChannel.ChannelName + "!",
                UserIcon   = userChannel.ChannelIcon,
            });


            var userChannelOut = new GroupChannelOut(userChannel, userChannel.CreatorId)
            {
                Messages = new Dictionary <long, ChannelMessageDataModel> {
                    { startMessage.Id, startMessage }
                }
            };

            userChannelOut.SetUsersIfCanMansge(connection, _channelConnRepo);
            userChannelOut.SetBtnSend(true);
            userChannelOut.SetComplexButtonView();

            result = userChannelOut;

            return(result);
        }
コード例 #3
0
        public Dictionary <int, GroupChannelOut> GetGroupChannelsOut(IDbConnection connection, int currentUserId, int skipMessages = 0)
        {
            var groupChannelType = (byte)ChannelTypes.Group;
            var groupChannels    = new Dictionary <int, GroupChannelOut>();
            var channels         = _channelConnRepo.GetUserConnectedChannls(connection, currentUserId, groupChannelType);

            foreach (var channel in channels)
            {
                var pChOut = new GroupChannelOut(channel.ChannelData.ConvertToWorkModel(), currentUserId);
                pChOut.SetMessages(connection, _channelMessageRepo, skipMessages);
                pChOut.SetComplexButtonView();
                pChOut.SetBtnSend(channel.MessageSend);
                pChOut.SetUsersIfCanMansge(connection, _channelConnRepo);
                groupChannels.Add(pChOut.ChannelId, pChOut);
                // break;
            }

            return(groupChannels);
        }
        public bool UpdateGroupUser(IDbConnection connection, ChannelConnectionUserOut tragetUser, bool updatePasswordByChannel, int channelAdminUserId, Action <GroupChannelOut> setChannelToTargetUserIfBeforeCantRead, Action <ChannelConnectionDataModel> setOldModel)
        {
            var channelData = _channelRepo.GetChannelWithConnectedUsers(connection, tragetUser.ChannelId, new List <int>(tragetUser.UserId));

            if (channelData == null)
            {
                throw new ArgumentNullException(nameof(channelData), Error.NoData);
            }
            if (channelData.creatorId != channelAdminUserId)
            {
                throw new SecurityException(Error.NotPermitted);
            }
            if (!channelData.HasConnections())
            {
                throw new SecurityException(Error.ChannelNotExist);
            }
            var channelConnections = channelData.GetConnections();

            if (channelConnections.Count > 1)
            {
                throw new NotImplementedException("channelConnections.Count >1");
            }
            var channelConnection = channelConnections[0];

            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            // ReSharper disable InvertIf

            setOldModel(_channelConnRepo.ConvertToWorkModel(channelConnection));
            if (!tragetUser.MessageRead && channelConnection.messageRead)
            {
                tragetUser.MessageSend        = false;
                channelConnection.messageRead = tragetUser.MessageRead;
                channelConnection.messageSend = tragetUser.MessageSend;

                return(_channelConnRepo.Update(connection, channelConnection));
            }

            if (tragetUser.MessageRead && !channelConnection.messageRead)
            {
                channelConnection.messageRead = true;
                channelConnection.messageSend = tragetUser.MessageSend;
                if (updatePasswordByChannel)
                {
                    channelConnection.password    = channelData.password;
                    tragetUser.HasCorrectPassword = true;
                }
                if (_channelConnRepo.Update(connection, channelConnection))
                {
                    var targetChat = new GroupChannelOut(channelData.ConvertToWorkModel(), tragetUser.UserId);
                    targetChat.SetMessages(connection, _channelMessageRepo);
                    targetChat.SetComplexButtonView();
                    targetChat.SetBtnSend(tragetUser.MessageSend);
                    setChannelToTargetUserIfBeforeCantRead(targetChat);
                    return(true);
                }
                return(false);
            }

            if (!channelConnection.messageRead)
            {
                return(false);
            }
            var hasChange = false;

            if (updatePasswordByChannel && channelConnection.password != channelData.password)
            {
                channelConnection.password    = channelData.password;
                tragetUser.HasCorrectPassword = true;

                var targetChat = new GroupChannelOut(channelData.ConvertToWorkModel(), tragetUser.UserId);
                targetChat.SetMessages(connection, _channelMessageRepo);
                targetChat.SetComplexButtonView();
                targetChat.SetBtnSend(channelConnection.messageSend);
                setChannelToTargetUserIfBeforeCantRead(targetChat);
                hasChange = true;
            }
            if (channelConnection.messageSend != tragetUser.MessageSend)
            {
                channelConnection.messageSend = tragetUser.MessageSend;
                hasChange = true;
            }
            if (hasChange)
            {
                var suc = _channelConnRepo.Update(connection, channelConnection);
                if (!suc)
                {
                    throw new NotImplementedException();
                }
            }
            return(hasChange);
        }
        public GroupChannelOut JoinUserToGroupChannel(IDbConnection connection, int channelId, string password, int userId, Action <NameIdInt, ChannelConnectionUserOut> setChannelOwnerAndUserOut)
        {
            GroupChannelOut result    = null;
            var             groupType = (byte)ChannelTypes.Group;

            var channel = _channelRepo.GetChannelWithConnectedUsers(connection, channelId, new List <int>());

            if (channel == null)
            {
                throw new Exception(Error.ChannelNotExist);
            }
            if (channel.creatorId == userId)
            {
                throw new NotImplementedException("is creator user channel must be exist before");
            }

            var admin = new NameIdInt(channel.creatorId, channel.creatorName);

            if (channel.password != password)
            {
                setChannelOwnerAndUserOut(admin, null);
                throw new SecurityException(Error.NotPermitted);
            }
            var maxLimit          = (int)MaxLenghtConsts.GroupChannelsLimit;
            var channelConnection = channel.GetConnections().SingleOrDefault(i => i.userId == userId && i.channelType == groupType);

            ChannelConnectionDataModel targetChannelConnectionData;

            if (channelConnection == null)
            {
                var chConnCount = _channelConnRepo.GetCountConectionsForUser(connection, userId);
                var canAdd      = chConnCount <= maxLimit - 1;
                if (!canAdd)
                {
                    throw new Exception(Error.MaxChannelsLimit);
                }
                targetChannelConnectionData = new ChannelConnectionDataModel
                {
                    UserId      = userId,
                    Password    = password,
                    MessageRead = true,
                    MessageSend = true,
                    ChannelType = ChannelTypes.Group,
                    ChannelId   = channel.Id
                };
                var entyty = _channelConnRepo.ConvertToEntity(targetChannelConnectionData);
                entyty = _channelConnRepo.AddOrUpdate(connection, entyty);
                targetChannelConnectionData.Id = entyty.Id;
            }
            else
            {
                if (!channelConnection.messageRead)
                {
                    setChannelOwnerAndUserOut(admin, null);
                    throw new SecurityException(Error.YouAreBlockedInThisChannel);
                }
                // ReSharper disable once InvertIf
                if (channelConnection.password != password)
                {
                    channelConnection.password = password;
                    var updated = _channelConnRepo.Update(connection, channelConnection);
                    if (!updated)
                    {
                        throw new NotImplementedException();
                    }
                }

                targetChannelConnectionData = _channelConnRepo.ConvertToWorkModel(channelConnection);
            }

            var channelData = channel.ConvertToWorkModel();

            setChannelOwnerAndUserOut(admin, new ChannelConnectionUserOut(targetChannelConnectionData, "", password));
            var channelOut = new GroupChannelOut(channelData, userId);

            channelOut.SetMessages(connection, _channelMessageRepo);
            channelOut.SetBtnSend(targetChannelConnectionData.MessageSend);
            channelOut.SetComplexButtonView();
            result = channelOut;
            return(result);
        }