0

I work on chat app one to one chat and my database structure is

"rules": {
    "Messages" :{
      "$chatId" : {
        ".read": "root.child('Chat_members').child(chatId).child(auth.uid).exists()",
        ".write": "root.child('Chat_members').child(chatId).child(auth.uid).exists()"
      }

I want to implement delete message so if user1 delete message i will hide it and display it only for user2 ,i can do this from client side but it is very inefficient way to request all data where there is for example just one message that will display for user1 how to do this from firebase rules? can I find any help?

Hamdy
  • 25
  • 8
  • I am not sure the question is clear but this sounds like you simply need it add an `isVisible` flag and only read messages where `isVisible == true` OR denormalize your data so there are two sets, one for each user and if user 1 delete's a message, remove it from his 'visible messages' but leave it in user 2's 'visible messages' – Jay Feb 11 '21 at 18:10
  • @Jay what is the best way to do it that take care of performance? – Hamdy Feb 16 '21 at 11:23
  • There should be no performance impact with either solution - in fact, lowering the number of nodes read would actually be faster. That being said, Firebase is very fast to start with so it most likely won't make any performance difference. – Jay Feb 16 '21 at 16:48
  • @Jay you mean that i have to add a node for visibility and check this for every message if visibility is true i'll show the message. would this check for every message slow down message's display and effect on performance? – Hamdy Feb 18 '21 at 07:57
  • 1
    Hmm. No, that's not what my comment said. You would add a child node isVisible and only read nodes that has isVisible == true. Therefore if there are 1000 nodes and the user 'deletes' 900 of them (that's also called a 'soft delete' by the way) those nodes would have their isVisible set to false so they no longer appear in the UI. The query would only read nodes where isVisible == true. – Jay Feb 18 '21 at 22:43
  • @Jay thank you very much on your response .can you please take a look in my new question ? thanks again [https://stackoverflow.com/questions/66264647/how-to-create-pagination-in-chat-app-using-firebase-real-time-database/66265450#66265450] – Hamdy Feb 19 '21 at 18:04

1 Answers1

0

As the documentation says security rules don't filter data. Instead you need to ensure that the client only reads the data you want to show.

I don't seen an easy way to do that to your structure without adding more data. In fact, the simplest way I can think of is to simply replicate the chat for each user and then actually delete the message from their data structure when needed.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807