Firebase private chat schema and rules -
i trying setup private chat abilities in app working on , i'm having bit of trouble wrapping head around denormalizing data/setting rules properly.
after doing reading, realize rules or nothing using rules filter not option.
i've sketched out basic idea on paper, , have pasted below. there 2 main routes, users , chats.
users keyed list, each key matching authenticated user. inside each member of list have each chat said user in listed key.
for chats route i'd have list of of chats.
now rules.
users able read data in list key matched uid. write i'm less confident. i'm thinking have let authentication write, otherwise user starting chat not notify others of new chat playing chat id in chat list in users route.
for chats rules both read , write allowed if user authenticated , chat key located inside data in user route.
does seem i'm going in right direction?
users:{ user1:{ chat1: true, chat2: true ... }, user2:{ chat1: true, chat3: true .... } } chats:{ chat1:{ lastupdate: timestamp, messages:{ 0:{ from: user1 to: user2, message: message } ... } } } rules:{ .read: false, .write: false, users:{ $user_id:{ .read: auth != null && $user_id == auth.uid, .write: auth != null //not sure here other users need write here if start new chat } }, chats:{ $chat_id: { .read: auth != null && root.child('users').child($chat_id).contains(auth.id), .write: auth != null && root.child('users').child($chat_id).contains(auth.id) } } }
i've been playing more, here 1 option (by no means suggesting best way it)
rules:
{ "rules":{ ".read": false, ".write": false, "users":{ "$user_id":{ ".read": "auth != null && $user_id == auth.uid", ".write": "auth != null" //not sure here other users need write here if start new chat } }, "chats":{ "$chat_id": { ".read": "auth != null && root.child('users').child(auth.uid).child('chats').haschild($chat_id)", ".write": "auth != null && (root.child('users').child(auth.uid).child('chats').haschild($chat_id) || !data.exists())" } } } } users have structure this: users:{ someuserid:{ chats:{ //embedded second level can save firebaseobj.someuserid keys more somechatid: true //and repeat each chat } } }
chats this:
chats:{ somechatid:{ //chat data } //more chat objects }
i wouldn't surprised if there better way this, @ least may start stuck. i'll try remember update if/when better solution.
Comments
Post a Comment