therealbigpepe
therealbigpepe

Reputation: 1559

Merge activities in activity feed

I'm building an app with activity feed, just like Facebook or Instagram. When for instance a user uploads a picture, this picture automatically creates an entry in activities table. There is a polymorphic relationship between the picture uploaded and the activity. The activity model will handle follows, likes and other things. The Activity model looks like that

Activity

model_id       12
model_type     picture
user_id        1313
activity_type  picture_upload

If the user decides to upload again another picture in a short period of time, I would like to merge these similar activities related to the same model type into one just like Instagram does. This activity would have a relation with both pictures uploaded. For this I created an additional field in activities table called multiple_ids, and there I was inserting the consequent picture id's

Activity

model_id       12
model_type     picture
user_id        1313
activity_type  picture_upload
multiple_ids   13,14,15

This is completely working, I created methods for the activity in order to get those "multiple models". But for example if the user decides to remove a picture included in the multiple_ids field I would have to make a LIKE query in multiple_ids. I think that there should be better ways of doing this. How would you do that? A scope merging "similar activities" when retrieving activities would be a good practice?

Upvotes: 2

Views: 203

Answers (2)

HyperSkilledMan
HyperSkilledMan

Reputation: 19

I think its good idea to generate group id for activity, and if activity repeats you could assign same group id to it.

Activity

model_id       12
model_type     picture
user_id        1313
activity_type  picture_upload
group_id       picture_upload_1313_14505049334
created_at     2017-02-01 11:55:00

When activity is creating, you could subscribe on event and check if in last five minutes this user did this activity - then you assign same group id it could look like this on Laravel 4

Activity::creating(function($activity) {
  $originatingActivity = Activity::where('created_at', '>', Date::subMinutes(5))->where('activity_type', $activity->activity_type)->where('user_id',$activity->user_id)->first();
  if ($originatingActivity) {
     $activity->group_id = $originatingActivity->group_id;
  }
  else {
    $activity->group_id = $activity->activity_type . $activity->user_id . time()
  }

})

And then in code you could get activities by group_id, so if you will delete one activity it doesn't break anything, you can write methods with groupBy statement to easily count them. Also this way, with some code changes, you can group different relation types if needed

Upvotes: 2

Tom
Tom

Reputation: 4292

Personally, I'd have a cross-reference table that simply stores the relationship of ID's.

parent | related
----------------
12     |  13
12     |  14
12     |  15

Then if the user deleted 13, 14 or 15, you'd simply delete this relationship from the table.

The tricky part comes if someone deletes 12. Here, you'll have to make a decision on how you want to handle it. You could delete all with a parent of 12, essentially levaing you with orphan activities. OR, you could with a little bit of work assosicate 14 and 15 with activity 13 instead.

Upvotes: 2

Related Questions