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); }