how to make friends based on follow/follower , Swi

2019-04-17 04:42发布

Currently my Firebase Database looks like this below. I have three users :usersI want to make feature like this, when one user follow another user and the user follow him back, they will be friends.They wont be friends until they are both follower of each other. So far I have managed to make the followers/following like this :user1 enter image description here enter image description hereNow I am out of clue what to do next.

1条回答
我命由我不由天
2楼-- · 2019-04-17 05:36

Small disclaimer: I didn't get a chance to test this, let me know if it does what you want.

Under followers and following you seem to be using push keys to add the userID of the person that's being followed or that is following. Instead, I would simply add the uid as a child and set the value of that node to something random. Then when you want an user's followers you'd get all the keys instead of all the values.

DB Structure: I omitted anything irrelevant for the sake of brevity.

root: {
  user_profile: {
    $user1: {
      followers: {
        $user2: true,
        $user3: true
      },
      following: {
        $user2: true,
        $user3: true
      },
      friends: {
        $user2: true,
        $user3: true
      }
    },
    $user2: {
      followers: {
        $user3: true,
        $user1: true
      },
      following: {
        $user3: true,
        $user1: true
      },
      friends: {
        $user3: true,
        $user1: true
      }
    },
    $user3: : {
      followers: {
        $user2: true,
        $user1: true
      },
      following: {
        $user1: true,
        $user2: true
      },
      friends: {
        $user1: true,
        $user2: true
      }
    }
  }
}

I think using DB Rules would be the easiest way to do this. Using these rules, a user would only be able to write to friends/$friend if the two users in question are following each other.

{
  "rules": {
    "user_profile": {
      ".read": true,
      "$user_profile": {
        ".write": "$user_profile === auth.uid",
        "friends": {
          "$friend": {
            ".write": "(data.parent().parent().child('followers/'+auth.uid).exists() && data.parent().parent().child('following/'+auth.uid).exists()) && $friend === auth.uid"
          }
        },
        "followers" : {
          "$follower": {
            ".write": "$follower === auth.uid"
          }
        }
      }
    }
  }
}

Small example on how to follow someone.

func follow(uid: String) -> Void {
    // Obv you'd want to do some extra checks here such as whether the user is logged in or not, but for the sake of brevity they're omitted.
    let dbRef = FIRDatabase.database().reference().child("user_profile")
    let userID = FIRAuth.auth()?.currentUser?.uid
    dbRef.child("\(userID)/following/\(uid)").setValue(true);
    dbRef.child("\(uid)/followers/\(userID)").setValue(true);
    dbRef.child("\(userID)/friends/\(uid)").setValue(true); // These should fail if the users aren't following each other.
    dbRef.child("\(uid)/friends/\(userID)").setValue(true); // These should fail if the users aren't following each other.
}

And for unfollowing an user you'd do exactly the same only with .remove() instead of .setValue(true).

查看更多
登录 后发表回答