예제 #1
0
        //TODO : можно сделать лучше
        public void SynchronizeAddingFriend(NotificationReceiveModel notificationReceiveModel)
        {
            if (notificationReceiveModel.IsAccepted == false)
            {
                _mainLogic.DeleteNotification(notificationReceiveModel);
            }
            else
            {
                //binding users
                _mainLogic.AddFriend(new FriendReceiveModel()
                {
                    UserId   = notificationReceiveModel.FromUserId,
                    FriendId = notificationReceiveModel.ToUserId
                });

                _logger.Info($"Sync adding to friends users: {notificationReceiveModel.FromUserId},{notificationReceiveModel.ToUserId}");

                var user1 = _mainLogic.GetUser(new UserReceiveModel()
                {
                    Id = notificationReceiveModel.FromUserId
                })?.JsonData as UserResponseModel;
                var user2 = _mainLogic.GetUser(new UserReceiveModel()
                {
                    Id = notificationReceiveModel.ToUserId
                })?.JsonData as UserResponseModel;

                if (user1 != null && user2 != null)
                {
                    var connectedUser1 = _connectedClients.FirstOrDefault(c => c.Id == user1.UserId);
                    if (connectedUser1 != null)
                    {
                        Task.Run(() =>
                        {
                            _logger.Info($"Sync send info about adding to friends for user {connectedUser1.Id}");
                            connectedUser1.SendMessage(_encoder.Encryption(_jsonStringSerializer.Serialize(new OperationResultInfo()
                            {
                                ErrorInfo       = string.Empty,
                                OperationResult = OperationsResults.Successfully,
                                ToListener      = ListenerType.FriendListListener,
                                JsonData        = _jsonStringSerializer.Serialize(new UserListResponseModel()
                                {
                                    UserId   = user2.UserId,
                                    UserName = user2.UserName,
                                    Picture  = user2.File,
                                    IsOnline = user2.IsOnline
                                })
                            })));

                            //connectedUser1.SendMessage(_jsonStringSerializer.Serialize(new OperationResultInfo()
                            //{
                            //    ErrorInfo = string.Empty,
                            //    OperationResult = OperationsResults.Successfully,
                            //    ToListener = ListenerType.FriendListListener,
                            //    JsonData = _jsonStringSerializer.Serialize(new UserListResponseModel()
                            //    {
                            //        UserId = user2.UserId,
                            //        UserName = user2.UserName,
                            //        Picture = user2.File,
                            //        IsOnline = user2.IsOnline
                            //    })
                            //}));
                        });
                    }

                    var connectedUser2 = _connectedClients.FirstOrDefault(c => c.Id == user2.UserId);
                    if (connectedUser2 != null)
                    {
                        _logger.Info($"Sync send info about adding to friends for user {connectedUser2.Id}");
                        Task.Run(() =>
                        {
                            connectedUser2.SendMessage(_encoder.Encryption(_jsonStringSerializer.Serialize(new OperationResultInfo()
                            {
                                ErrorInfo       = string.Empty,
                                OperationResult = OperationsResults.Successfully,
                                ToListener      = ListenerType.FriendListListener,
                                JsonData        = _jsonStringSerializer.Serialize(new UserListResponseModel()
                                {
                                    UserId   = user1.UserId,
                                    UserName = user1.UserName,
                                    Picture  = user1.File,
                                    IsOnline = user1.IsOnline
                                })
                            })));

                            //connectedUser2.SendMessage(_jsonStringSerializer.Serialize(new OperationResultInfo()
                            //{
                            //    ErrorInfo = string.Empty,
                            //    OperationResult = OperationsResults.Successfully,
                            //    ToListener = ListenerType.FriendListListener,
                            //    JsonData = _jsonStringSerializer.Serialize(new UserListResponseModel()
                            //    {
                            //        UserId = user1.UserId,
                            //        UserName = user1.UserName,
                            //        Picture = user1.File,
                            //        IsOnline = user1.IsOnline
                            //    })
                            //}));
                        });
                    }
                }

                _mainLogic.DeleteNotification(notificationReceiveModel);
            }
        }