Activity stream design with RavenDb

2019-08-04 13:48发布

问题:

This is more of a design pattern / document design question than a technical one...

I want to display a activity feed on my website which will list all the latest happenings users have been doing on my site...here are some of the activities i would like to display:

  • New media uploaded (Bob has uploaded a new track)
  • Comments on a profile (Paul has commented on Bob's profile)
  • Comments on media (Steve has commented on Paul's track 'my track name')
  • Status updates (Steve can write any status update he wishes)

Each activity will need to have it's own set of data, such as the new media uploaded activity I would like to include details about the media such as image, title, description etc).

This activity will mostly be used as a global feed so it's the same for all users, although I need the option for users to only show feed items from users they are following (like twitter).

I think I have 2 options:

1) Pull in all the data Ad-Hoc with an index so the information is always up to date, even if a user alters his media title name...i'm not sure how well this will scale though??

2) Have an ActivityFeed document which contains a Sub-Document such as NewMediaUploadActivity (shown below).

ActivityFeed
 - DateTime
 - AccountId
 - ActivityType
 - Activity (this is a polymorphic object)

NewMediaUploadActivity : Activity
 - MediaTitle
 - MediaDescription
 - GenreName

StatusUpdateActivity : Activity
 - StatusText

ProfileCommentActivity : Activity
 - CommentText
 - ProfileAccountId
 - ProfileUsername

Etc...

If anybody has any experience, or any input on the best way to do this in RavenDB I would be grateful, my live website built with SQL Server currently does what I need using a slightly modified option 2.

Paul

回答1:

I would model this as:

public class ActivityTracking<TActivity>
{
   public string[] AffectedUsers {get;set;}
   public TActivity Activity {get;set;}
}

You can have different activities (not required to be in an inheritance hierarchy) that are "attached" to different users. For example, Bob commenting on Jane's photo would show up in both streams. You then can just query for activities for that user.