如何根据关注/追随者,Swift Firebase交友 [英] how to make friends based on follow/follower , Swift Firebase

查看:146
本文介绍了如何根据关注/追随者,Swift Firebase交友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的Firebase数据库如下所示。我有三个用户:我想做这样的功能,当一个用户跟随另一个用户,用户跟着他回来,他们将成为朋友。他们不会是朋友,直到他们都是追随者彼此。到目前为止,我已经设法让追随者/以下是这样的: 现在我是出于线索,下一步该做什么。

解决方案

小声明:我没有机会测试这个,让我知道如果它是你想要的。



在追随者和下面你似乎正在使用按键来添加被跟踪或正在关注的人的用户ID。相反,我只是简单地将uid添加为小孩,并将该节点的值设置为随机的值。然后,当你想要一个用户的追随者,你会得到所有的键,而不是所有的价值。

数据库结构:为了简洁起见,我省略了任何不相关的东西。 p>

  root:{
user_profile:{
$ user1:{
关注者:{
$ user2:true,
$ user3:true
},
如下:{
$ user2:true,
$ user3:true
},
朋友:{
$ user2:true,
$ user3:true
}
},
$ user2:{
followers:{
$ user3:true,
$ user1:true
},
以下内容:{
$ user3:true,
$ user1:true
$ b $,b $ b朋友:{
$ user3:true,
$ user1:true
}
},
$ user3::{
关注者:{
$ user2:true,
$ user1:true
},
以下内容:{
$ user1:tru e,
$ user2:true
},
好​​友:{
$ user1:true,
$ user2:true
}
}




$ b我认为使用数据库规则是最简单的方法做这个。
使用这些规则,如果两个用户之间相互关联,用户只能写信给朋友/ $朋友。

  {
rules:{
user_profile:{
.read :
$ user_profile:{$ b $.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
}
}
}
}
}
}

如何关注某人的小例子 p>

  func follow(uid:String) - >无效{
// Obv你想在这里做一些额外的检查,例如用户是否登录,但为了简洁起见,他们被省略了。
let dbRef = FIRDatabase.database()。reference()。child(user_profile)
let userID = FIRAuth.auth()?。currentUser?.uid
dbRef.child( \(用户ID)/以下/ \(UID))的setValue(真)。
dbRef.child(\(uid)/ followers / \(userID))。setValue(true);
dbRef.child(\(userID)/ friends / \(uid))。setValue(true); //如果用户没有关注,这些应该会失败。
dbRef.child(\(uid)/ friends / \(userID))。setValue(true); //如果用户没有关注,这些应该会失败。

$ / code>

而对于一个用户来说,你只能用 .remove()而不是 .setValue(true)


Currently my Firebase Database looks like this below. I have three users :I 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 : Now I am out of clue what to do next.

解决方案

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).

这篇关于如何根据关注/追随者,Swift Firebase交友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆