【问题标题】:How to allow at most two people to be in a chat room in Firebase rules?如何在 Firebase 规则中最多允许两个人进入聊天室?
【发布时间】:2017-05-03 22:57:08
【问题描述】:

我想使用 Firebase 设置 2 人聊天。 当他们“喜欢”对方时,用户将能够聊天。我正在自己的 SQL 服务器上处理“点赞”,但我想使用 Firebase 进行聊天。

这是虚拟数据,最终会是这个样子。

{
  "admins": {
    "chat1": {
      "user_id_1": {
        "active": true
      },
      "user_id_2": {
        "active": true
      }
    }
  },
  "chats": {
    "chat1": {
      "messages": {
        "random_id": {
          "message": "first message",
          "sender": "user_id_1"
        }
      }
    }
  }
}

这些是我的规则:

{
  "rules": {      
    "admins": {
      "$chatAdmin": {

      }
    },
    "chats": {
      "$chat": {
        ".read": "root.child('admins').child($chat).hasChild(auth.uid)",
        ".write": "root.child('admins').child($chat).hasChild(auth.uid)"
      }
    }
  }
}

所以我想要的是,每次两个用户“匹配”时,他们就会互相“喜欢”。我想在/chats/{sha1(uid1 + uid2)} 中创建他们的聊天,并只给这两个用户访问它,读写。

【问题讨论】:

  • 嘿西奥。通过快速扫描,您的结构看起来非常适合要求。你有什么问题?
  • 好了,现在每个人都可以创建管理员行了。我只想为用户每次聊天创建 2 条记录
  • 嗯,知道了。这确实更棘手。我能快速想到的最接近的方法是类似于 Andre 描述的 here 或保留一个单独的计数器,就像我描述的 here。
  • @FrankvanPuffelen 我真的不明白你的解决方案。我想到了一个解决方案,通过我的自定义服务器更新每场比赛的规则。但这违反了 Firebase 设计模式……
  • 拥有自定义服务器后,事情会变得容易得多。根据您当前的规则,用户不能更新admin 节点,而服务器可以。这样可以确保安全。这与“Firebase 设计模式”有何不同?

标签: json firebase firebase-realtime-database firebase-security nosql


【解决方案1】:

稍后谢谢我,破解>

只要firebase不改变uid格式

export async function sendMessage(myUid, otherUid, content) {
    const conversationId = createConversationId(myUid, otherUid)
    const data = { content, timestamp: new Date().getTime(), authorUid: myUid }
    await firebase.database().ref(`conversations/${conversationId}`).push(data)
    return
}

export function createConversationId(uid1, uid2) {
    return uid1 > uid2 ? `${uid2}${uid1}` : `${uid1}${uid2}`
}

另见

https://gist.github.com/katowulf/4741111

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2014-06-17
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多