如何在社交媒体应用中阻止Firebase上的用户?对于iOS

问题描述 投票:1回答:3

enter image description here我的应用程序约90%,并准备发布它,以便它可以发布进行测试。

我被苹果拒绝了,因为我没有一个非常重要的功能: - 用户阻止滥用用户的机制。

我已经有了一个跟随其他用户的功​​能,但是我仍然坚持如何阻止访问,因此当一个用户阻止另一个用户时,他们看不到任何与刚刚阻止他的人相关的内容。他们无法在搜索,新消息和帖子中看到他们的个人资料。

我不知道从哪里开始。如果您使用过用户内容生成的应用程序。我甚至不知道从哪里开始。我已经坚持了3个星期,现在我很绝望。

//我将如何实现这样的东西?

我的安全规则是基本的。

{
  "rules": {
".read": "auth != null",
".write": "auth != null"

} }

我现在显示信息的方式是,用户必须使用电子邮件和用户名和密码登录,并创建'和允许他们登录的用户,进入应用程序并查看与应用程序相关的所有内容。

我的节点也是

Messages

评论flagged-posts以下喜欢发布用户消息用户

swift firebase firebase-security
3个回答
4
投票

这个问题有很多解决方案。一种方法是在数据库中为被阻止的用户提供单独的节点。在每个用户下,您可以列出被阻止用户的uid,其值为“true”。这个值是什么并不重要 - 这样可以更容易地搜索被阻止的人。因此,例如,假设这是您的数据库结构:

users:
    uid1:
       my data: {
           // some stuff here
       }
    uid2:
       my data: {
           // some stuff here
       }
blocked:
     uid1:
         uid8: true,
         uid3: true
     uid2:
         uid1: true

在上面的例子中,uid1阻止了uid8uid3,依此类推。然后,您可以调整规则以验证用户是否可以读取他们是否经过身份验证,并且在该用户的阻止列表中找不到他们的uid

{
  "rules": {
    "users": {
      "$user_id": {
        // only messages from the last ten minutes can be read
        ".read": "auth != null && !root.child('blocked/'+$user_id+'/'+auth.uid+'/true').exists()",
        ".write": "$user_id === auth.uid"
      }
    }
  }
}

有关安全规则的一些示例,请查看有关安全规则的documentation


0
投票

我假设您的数据库中有某个用户模型正确吗?只需添加另一个名为“阻塞”或“其他”的字段,并根据该值验证某些用户是否可以看到该用户


0
投票

这就是我提出的,它对我来说很好。它不像Instagram那样被阻止的用户完全从您的Feed中消失,但它肯定会阻止Apple,因为一个用户可以阻止另一个用户,一旦发生这种情况,他们就不能互相发送消息。

它还为您提供了在显示操作表时显示“阻止”或“解除阻止”的选项,以便当前用户知道他们是否知道他们是否阻止了其他用户。

第一件事是我假设这是在某种聊天视图控制器里面有一个发送消息按钮,在聊天vc里面有发送者的id(当前用户)和接收者的id(其他用户)。

要阻止某人在Firebase中创建blocked引用,请为想要阻止其他用户的用户创建一个节点,然后最终将阻止的用户添加为该节点下的键/值对。 other user's idkey,价值可能是真的。

例如。 dog123想阻止cat456。当狗决定阻止猫时,参加阻止的将是:

let dogId = dog123 // current user
let catId = cat456 // other user

func actionSheetBlockedAction() {

    let dictValues = [String: Any]()
    dictValues.updateValue(true, forKey: catId)

    let ref = Database.database().reference().child("blocked").child(dogId)
    ref.updateChildValues(dictValues)
}

这将导致数据库看起来像:

root
  |
  @---blocked
         |
         |
         @----dog123
                |
                |----cat456: true  // cat123 is blocked from sending messages to dog123

在同一个聊天vc中你需要添加一些属性,在viewDidLoad或viewWillAppear中你需要添加2个独立的观察者来监听两个用户的被阻止的ref。

你必须对两个用户进行检查,因为是的dog123可能阻止了cat456,但作为回报,cat456也可能阻止了dog123。稍后,如果狗决定解锁猫,但猫仍然阻止了狗,那么消息仍将从狗到猫。

为了防止您添加观察者,他们将切换按下发送按钮时检查的某些属性。如果其中任何一个属性为true,则表示某人阻止了其他人,并且不会发送任何消息。这些属性会让你知道谁阻止了谁。

您还可以使用这些属性在操作表出现时显示“阻止”或“取消阻止”按钮。

var currentUserId = Auth.auth().currentUser?.uid
var otherUserId = "whatever" // you should already have this
var isCurrentUserBlocked = false
var isOtherUserBlocked = false

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    checkBlockedRefsForBothUsers() // this is where the isCurrentUserBlocked & isOtherUserBlocked properties get changed
}


@IBAction func sendMessageButtonTapped(sender: UIButton) {

    if isOtherUserBlocked {
        // alert the current user that they blocked the other user
        return
    }

    if isCurrentUserBlocked {
        // alert the current user that the other user blocked them
        return
    }

    // if neither are true then let the current user send their message to the other user
}


func checkBlockedRefsForBothUsers() {

    let currentUsersBlockedRef = Database.database().reference().child("blocked").child(currentUserId!)
    currentUsersBlockedRef?.observe( .value, with: { (snapshot) in

        // if the current user ISN'T under the blocked ref then the other user ISN'T blocked
        if !snapshot.exists() {
            self.isOtherUserBlocked = false
            return
        }

        for child in snapshot.children {
            let snap = child as! DataSnapshot

            // if the other user's uid IS under the current user's blocked ref then the other user IS blocked from them
            if snap.key == self.otherUsersId {
                self.isOtherUserBlocked = true
                break
            }

             // if the other user's uid ISN'T under the current user's blocked ref then the other user ISN'T blocked
            self.isOtherUserBlocked = false
        }
    })

    let otherUsersBlockedRef = Database.database().reference().child("blocked").child(otherUserId)
    otherUsersBlockedRef?.observe( .value, with: { (snapshot) in

        // if the other user's uid ISN'T under the blocked ref then the current user ISN'T blocked
        if !snapshot.exists() {
            self.isCurrentUserBlocked = false
            return
        }

        for child in snapshot.children {

            let snap = child as! DataSnapshot

            // if the current user's uid IS under the other user's blocked ref then the current user IS blocked from them
            if snap.key == self.currentUserId {
                self.isCurrentUserBlocked = true
                break
            }

             // if the current user's uid ISN'T under the other user's blocked ref then the current user ISN'T blocked
            self.isCurrentUserBlocked = false
        }
    })
}

并且在显示操作表时显示“阻止”或“解除阻止”按钮使用相同的2个属性:

@IBAction func presentActionSheet(sender: UIButton) {

    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    let blockAction = UIAlertAction(title: "Block", style: .default) { (action) in
         // run code to block the otherUserId
         self.actionSheetBlockedAction()
    }

    let unblockAction = UIAlertAction(title: "Unblock", style: .default) { (action) in
        // run code to unblock the otherUserId
    }

    if isOtherUserBlocked {
        actionSheet.addAction(unblockAction) // if the current user blocked the other user then show the "unblock" action
    } else {
        actionSheet.addAction(blockAction)  // if the current user didn't block the other user then show the "block" action
    }

    present(actionSheet, animated: true, completion: nil)
}

最后你可能会想到,当狗阻挡猫时,只更新它们的参考值是有意义的。问题是当猫的动作表出现时,使用上面的逻辑,猫似乎会阻塞狗,猫的动作表将显示“解锁”。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.