views:

192

answers:

2

Hi,

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!

A: 

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).

Jakub Hampl
A: 

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).

Anthony Briggs
thanks anthony, the problem with that approach is that if I want to get the last activities from a user (with comments) I grab the last 30 records of the user (activities and comments) order by created_at: - However there would be comments inside those 30 (so there wouldn't be 30 activities).- Or there could be comments with no parent activity (because it has been left out those 30)
fesja
In the case you're talking about, you'd probably either want to grab the entire 'tree' for each activity (eg. one of your 30 is id=125 above, so you grab all four). You can do this in one query, but it may get quite large. Your other option is to grab things on demand - eg. an AJAX 'read more...' link or something like that.Which you choose really depends on what you're doing. For an RSS activity feed, option #2 is probably best, but for (eg.) the front profile page, you're more likely to need to request everything.
Anthony Briggs