fesja
fesja

Reputation: 3313

How to do comments in Activity Stream? (like Facebook)

I'm starting to develop an activity stream. I've read both How to implement the activity stream in a social network and What’s the best manner of implementing a social activity stream?. What I haven't found is the best way to add comments to the activities. As in facebook, each comment can be commented by another person.

If each activity comment is saved as another activity, then I would not be able to get the activity of that comment without doing a query. So the solution I'm thinking is to save the comments inside the serialize data field of each activity. If the user wants to delete his comment, I would have to update that activity.

Is this the correct solution? Is there a better approach?

Thanks!

Upvotes: 2

Views: 2757

Answers (2)

Anthony Briggs
Anthony Briggs

Reputation: 3455

I agree with Jakub - it depends on your backend, but what you're after is an efficient method for storing a tree. If you're using an SQL database, I'd store both the parent activity, and the root of the tree (master id?). That way you'll be able to grab an entire tree of comments quickly, given any comment or activity id.

Your lookup code would then go something like:

  • To reproduce an entire activity stream with comments, grab all records with the same master id. You'll probably want to represent master records as having a null parent id.
  • When adding a comment, you'll need to update two fields - the master id and the parent id. You can get both of them directly from the parent activity without having to do any lookups.
  • When deleting a comment, just delete it and it'll disappear from the tree.
  • If you just want 'master' activities, then select where parent = null and you're done.

An example, if that helps:

  • Master Activity (id=123, p=null, m=123)
    • Comment (id=124, p=123, m=123)
    • Another comment (id=125, p=123, m=123)
      • A sub-comment (id=126, p=125, m=123)

I remember reading an example of this using a document based store with Couch-DB. I can't find it now, but from memory it used something similar (master ids for parent records).

Upvotes: 1

Jakub Hampl
Jakub Hampl

Reputation: 40543

This kind of depends on what type of backend are you using and what are your constraints.

Serializing the comments seems a reasonable approach or I guess you could just simply use a polymorphic comments table (if you're using a relational db).

Upvotes: 0

Related Questions