public void ChekAndRestoreTargetPrivateChannel(IDbConnection connection, int activeUserId, int channeId, Func <int, bool> targetUserIsOnline, Action <PrivateChannelOut> notifyOtherUserIfTargetChannelNotExist) { PrivateChannelOut targetPrivateChannel = null; var c = connection; var channel = _channelRepo.GetById(c, channeId); if (channel == null) { throw new ArgumentNullException(nameof(channel), Error.ChannelNotExist); } var targetConnection = _channelConnRepo.GetOtherChannlConnecttions(c, channel.Id, activeUserId, (byte)ChannelTypes.Private).SingleOrDefault(i => !i.messageRead); if (targetConnection == null) { return; } targetConnection.messageRead = true; if (!targetConnection.messageSend) { targetConnection.messageSend = true; } if (targetConnection.password != channel.password) { targetConnection.password = channel.password; } _channelConnRepo.Update(connection, targetConnection); var isOnline = targetUserIsOnline(targetConnection.userId); if (isOnline) { var channelOut = channel.ConvertToWorkModel(); targetPrivateChannel = new PrivateChannelOut(channelOut); targetPrivateChannel.SetBtnSend(true); targetPrivateChannel.SetMessages(c, _channelMessageRepo); targetPrivateChannel.SetComplexButtonView(); } if (targetPrivateChannel != null) { notifyOtherUserIfTargetChannelNotExist(targetPrivateChannel); } }
public Dictionary <int, PrivateChannelOut> GetPrivateChannelsOut(IDbConnection connection, int currentUserId, int skipMessages = 0) { var privateChannelType = (byte)ChannelTypes.Private; var privateChannels = new Dictionary <int, PrivateChannelOut>(); var channels = _channelConnRepo.GetUserConnectedChannls(connection, currentUserId, privateChannelType); foreach (var channel in channels) { var pChOut = new PrivateChannelOut(channel.ChannelData.ConvertToWorkModel()); pChOut.SetMessages(connection, _channelMessageRepo, skipMessages); pChOut.SetComplexButtonView(); pChOut.SetBtnSend(channel.MessageSend); privateChannels.Add(pChOut.ChannelId, pChOut); //break; } return(privateChannels); }
public PrivateChannelOut GetPrivateChannelOut(IDbConnection connection, ChannelDataModel data, int currentUserId, int skip = 0) { PrivateChannelOut result = null; var pChOut = new PrivateChannelOut(data); if (data.ChannelConnections != null) { var count = data.ChannelConnections.Count; switch (count) { case 2: break; case 1: var ignoreId = data.ChannelConnections[0].UserId; var others = _channelConnRepo.GetOtherChannlConnecttions(connection, data.Id, ignoreId, (byte)ChannelTypes.Private).ToList(); if (others.Count != 1) { throw new ArgumentException(nameof(others), @" others.Count!=1 "); } data.ChannelConnections.Add(_channelConnRepo.ConvertToWorkModel(others[0])); break; default: throw new NotImplementedException("channel connection count is wrong"); } } else { data.ChannelConnections = _channelConnRepo .ConvertToWorkModel(_channelConnRepo.GetByChannelId(connection, data.Id).ToList()) .ToList(); } var curConn = data.ChannelConnections.Single(i => i.UserId == currentUserId); pChOut.SetBtnSend(curConn.MessageSend); pChOut.SetMessages(connection, _channelMessageRepo, skip); pChOut.SetComplexButtonView(); result = pChOut; return(result); }
public async Task <bool> UserChannelsSendMessage(ChannelMessageTransfer 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; //todo tmp //======== var newMessage = _channelService.CreateMessage(connection, messageModel); messageModel.UpdateByBase(newMessage); //======== switch (messageModel.ChannelType) { case ChannelTypes.Private: ConnectionUser targetUser = null; PrivateChannelOut targetChannel = null; var privateGroup = cr.CreatePrivateUserChannelGroupName(messageModel.ChannelId); _channelService.ChekAndRestoreTargetPrivateChannel(connection, cr.UserId, newMessage.ChannelId, targetUserId => { targetUser = _getOnlineSingleUser(connection, targetUserId); return targetUser != null; }, targetChannelDataModel => { targetChannel = targetChannelDataModel; }); if (targetUser != null) { await Clients.Client(cr.ConnectionId).InvokeAsync("onUserChannelsSended", messageModel); if (targetUser == null) { throw new NotImplementedException(nameof(targetUser)); } if (targetChannel == null) { throw new NotImplementedException(nameof(targetChannel)); } await targetUser.AddOrReplacePrivateChannelGroupNameAsync(Groups, targetChannel.ChannelId); var updTuHubUser = _hubCache.AddOrUpdateLocal(targetUser, true); var iHubGroupItem = updTuHubUser.GetUserChannelGroup(targetChannel.ChannelId); await Clients.Client(updTuHubUser.ConnectionId) .InvokeAsync("onUserChannelsCreatedPrivateChannel", targetChannel, iHubGroupItem); } else { await Clients.Group(privateGroup).InvokeAsync("onUserChannelsSended", messageModel); } return true; case ChannelTypes.Group: var groupChannelroup = cr.CreateGroupChannelGroupName(messageModel.ChannelId); await Clients.Group(groupChannelroup).InvokeAsync("onUserChannelsSended", messageModel); return true; case ChannelTypes.Alliance: var role = AllianceRoleHelper.GetByRoleId(cr.AllianceRoleId); if (!role.MessageSend) { throw new SecurityException(Error.NotPermitted); } var allianceGroup = cr.CreateAllianceGroupName(); await Clients.Group(allianceGroup).InvokeAsync("onUserChannelsSended", messageModel); return true; default: throw new ArgumentOutOfRangeException(); } })); }