/// <summary> /// не тестил /// </summary> /// <param name="thisId">Кто удаляет из друзей</param> /// <param name="idToRemove">Кого удаляют из друзей</param> public void RemoveUserFromFriends(int thisId, int idToRemove) { if (thisId != idToRemove) { logger.Info($"user [id: {thisId}] remove [id: {idToRemove}] from friends"); var thisUserNickname = m_UserContext.UserTable.AsNoTracking() .Where(obj => obj.UserId == thisId) .FirstOrDefault()?.Nickname; var userToRemoveNicname = m_UserContext.UserTable.AsNoTracking() .Where(obj => obj.UserId == idToRemove) .FirstOrDefault()?.Nickname; var friend = m_UserFriendsContext.UserFriendsTable .Where(obj => obj.UserId == thisId) .Where(obj => obj.FriendId == idToRemove) .FirstOrDefault(); if (friend != null) { //friends logic //thisId.Friend.Delete(idToRemove); m_UserFriendsContext.UserFriendsTable.Remove(friend); //thisId.Followers.Add(idToRemove); m_UserFollowersContext.UserFollowersTable.Add(new UserFollowersTable() { UserId = thisId, FollowerId = idToRemove }); //idToRemove.Friend.Delete(thisId); var userToRemove = m_UserFriendsContext.UserFriendsTable .Where(obj => obj.UserId == idToRemove) .Where(obj => obj.FriendId == thisId) .FirstOrDefault(); if (userToRemove != null) { m_UserFriendsContext.UserFriendsTable.Remove(userToRemove); } //idToRemove.Followings.Add(thisId); m_UserFollowingContext.UserFollowingTable.Add(new UserFollowingTable() { UserId = idToRemove, FollowingId = thisId }); m_UserFollowingContext.SaveChanges(); m_UserFriendsContext.SaveChanges(); m_UserFollowersContext.SaveChanges(); EventLogger.AddLogForUser(thisId, LogType.Communication, $"You remove user [{userToRemoveNicname}] from friends list"); EventLogger.AddLogForUser(idToRemove, LogType.Communication, $"User {thisUserNickname} delete you from friends list"); } else { var follower = m_UserFollowersContext.UserFollowersTable.AsNoTracking() .Where(obj => obj.UserId == thisId) .Where(obj => obj.FollowerId == idToRemove) .FirstOrDefault(); if (follower != null) { //логика для follower EventLogger.AddLogForUser(thisId, LogType.Communication, $"User [{userToRemoveNicname}] is your follower, you can't remove him from that list"); return; } else { var following = m_UserFollowingContext.UserFollowingTable .Where(obj => obj.UserId == thisId) .Where(obj => obj.FollowingId == idToRemove) .FirstOrDefault(); if (following != null) { //логика followings //thisId.Followings.Delete(idToRemove) m_UserFollowingContext.UserFollowingTable.Remove(following); //idToRemove.Followers.Delete(thisId) var deleteThisFromFolowerList = m_UserFollowersContext.UserFollowersTable .Where(obj => obj.UserId == idToRemove) .Where(obj => obj.FollowerId == thisId) .FirstOrDefault(); m_UserFollowersContext.UserFollowersTable.Remove(deleteThisFromFolowerList); m_UserFollowersContext.SaveChanges(); m_UserFollowersContext.SaveChanges(); EventLogger.AddLogForUser(thisId, LogType.Communication, $"You stop following user [{userToRemoveNicname}]"); EventLogger.AddLogForUser(idToRemove, LogType.Communication, $"User {thisUserNickname} stop following you"); } else { //none logic return; } } } } else { logger.Warn("User trying to remove himself from a friends list"); EventLogger.AddLogForUser(thisId, LogType.Communication, "You can't remove yourself from a friends list!"); } }
/// <summary> /// Используется при инвайте в друзья [от thisId для idToAdd] /// </summary> /// <param name="thisId">Кто добавляет в друзья</param> /// <param name="idToAdd">Кого добавляют в друзья (кидают инвайт)</param> public void AddUserToFriends(int thisId, int idToAdd) { if (thisId != idToAdd) { logger.Info($"user [id: {thisId}] add [id: {idToAdd}] to friends"); var thisUser = m_UserContext.UserTable.AsNoTracking() .Where(obj => obj.UserId == thisId).FirstOrDefault(); var userToAdd = m_UserContext.UserTable.AsNoTracking() .Where(obj => obj.UserId == idToAdd) .FirstOrDefault(); var friend = m_UserFriendsContext.UserFriendsTable.AsNoTracking() .Where(obj => obj.UserId == thisId) .Where(obj => obj.FriendId == idToAdd) .FirstOrDefault(); if (friend != null) { //this user already in friend EventLogger.AddLogForUser(thisId, LogType.Communication, $"User [{userToAdd?.Nickname}] already in your friend list"); } else { var follower = m_UserFollowersContext.UserFollowersTable .Where(obj => obj.UserId == thisId) .Where(obj => obj.FollowerId == idToAdd) .FirstOrDefault(); if (follower != null) { //логика для follower //thisId.Follower.Delete(idToAdd) m_UserFollowersContext.UserFollowersTable.Remove(follower); //thisId.Friends.Add(idToAdd) m_UserFriendsContext.UserFriendsTable.Add(new UserFriendsTable() { UserId = thisId, FriendId = idToAdd }); //idToAdd.Friend.Add(thisId) m_UserFriendsContext.UserFriendsTable.Add(new UserFriendsTable() { UserId = idToAdd, FriendId = thisId }); //idToAdd.Following.Delete(thisId) var following = m_UserFollowingContext.UserFollowingTable .Where(obj => obj.UserId == idToAdd) .Where(obj => obj.FollowingId == thisId) .FirstOrDefault(); if (following != null) { m_UserFollowingContext.UserFollowingTable.Remove(following); } m_UserFollowingContext.SaveChanges(); m_UserFriendsContext.SaveChanges(); m_UserFollowersContext.SaveChanges(); EventLogger.AddLogForUser(thisId, LogType.Communication, $"You add user [{userToAdd?.Nickname}] to friends list"); EventLogger.AddLogForUser(idToAdd, LogType.Communication, $"User [{thisUser?.Nickname}] accept request for friendship"); } else { var following = m_UserFollowingContext.UserFollowingTable.AsNoTracking() .Where(obj => obj.UserId == thisId) .Where(obj => obj.FollowingId == idToAdd) .FirstOrDefault(); if (following != null) { //логика followings EventLogger.AddLogForUser(thisId, LogType.Communication, $"You already following user [{userToAdd?.Nickname}]"); } else { //логика none //thisId.Following.Add(idToAdd); m_UserFollowingContext.UserFollowingTable.Add(new UserFollowingTable() { UserId = thisId, FollowingId = idToAdd }); //idToAdd.Followers.Add(thisId); m_UserFollowersContext.UserFollowersTable.Add(new UserFollowersTable() { UserId = idToAdd, FollowerId = thisId }); m_UserFollowingContext.SaveChanges(); m_UserFollowersContext.SaveChanges(); EventLogger.AddLogForUser(thisId, LogType.Communication, $"You are following user [{userToAdd?.Nickname}]"); EventLogger.AddLogForUser(idToAdd, LogType.Communication, $"User [{thisUser?.Nickname}] now your follower"); } } } } else { logger.Warn("User trying to add himself in a friends list"); EventLogger.AddLogForUser(thisId, LogType.Communication, "You can't add yourself to a friends list!"); } }