QnA application with flutter: Adding stream of comments

Viewed 963

I am building a forum application and so far i have structured the data into private posts (under each user as there is a section for users to view only their posts) and public posts (under collection 'public' so that everyone can view each other's posts). Now i would like to implement a commenting system. How should i structure the data? Should i create a subcollection 'comments' under each documentid (for each post) or create a collection 'comments' at the users level in firebase?

[![users level']

[![under collection 'public']

[![Inside subcollection of collection 'public', documentid represents each post's id under that category (forum, feedback and notes)']

1 Answers

This is more of an opinion based question, which means that there are more than 1 solutions. However, mine approach would be something similar to this and you can modify it or maybe get inspired for a better solution:

  1. You will have to add 2 additional fields under each post private (boolean ) and users (string array).
  2. Now you can query for posts using Simple queries like e.g. postRef.where('private', '==', false); and this will help you always load posts that are not private.
  3. Then you can query for private posts using additional Array membership, like e.g. postRef.where('private', '==', true).where('users', 'array-contains', 'UID'); and UID is the UID of the current logged in user that is viewing the forum page. When you query this post, you will be able to get all the private posts but only the ones that contain the UID of the user in the array.
  4. Each posts can have an array of JSON obj similar to { timestamp: number, comment: string, UID: string }. And this field will help you keep track of all the comments for the particular post. However, the users that are able to post and read them will be controlled based on the above fields users and private.
  5. On the other side, if the forum is expected to have thousands of comments, then it is suggested to have the comments in a separate database, such as Realtime Database for example. You can have them structured in a reference as posts/[POST_ID]/comments/ where the POST_ID is the one that is assigned for each document in your Firestore database. Then under you post, instead of having comments as JSON obj array, you can completely remove it. Therefore, when you will be querying for documents based on the above query fields, the API will also give you back the doc key (POST_ID) and you can use it to query for the comments from the Realtime Database. Further information can be found at Reading and writing data.
  6. Additionally you can implement rules in the database to make sure that you restrict access completely for users that query for private posts. Those rules are more complicated and more inforamtion can be found in Get started with Cloud Firestore Security Rules. However, you should be fine even without rules at the moment.

With that being said, please keep in mind that this is one of the many approaches to the use case scenario and probably there could be more and better ways to implement the same.

Related