How to group notifications in a feed

I use a feed in it, some notifications should appear as a group (i.e. a user posted a comment on your video) and others should appear as offline notifications (your video presented this week). Underlining new notifications is also a requirement

For grouped notifications, when a user consults their channel, the entry should be something like

Joe and other 5 posted a comment on your video "Cooking with fire"

      

The problem is how to group events when notifications alternate.

For example, imagine the following log:

 1 min ago           Joe posted comment on video 1
 10 mins ago         Video 1 featured
 11 mins ago         Helen posted comment on video 1
 11 mins ago         Michael posted comment on video 1
 14 mins ago         David posted comment on video 1
 14 mins ago         Robert posted comment on video 1

      

The feed channel is grouped in several ways. Even new notifications can change groups that break the selection.

Where can I learn more about general solutions to this problem and how to store and return notifications for my web service?

+3


source to share


1 answer


Personally, I would record the notification times exactly and then do something like this (I have not tested these specific solutions, but have done similar things before):

If you want notifications to be grouped by date :

SELECT about_resource_id,DATE(notification_time) AS notification_date,COUNT(*) FROM notification_tbl GROUP BY about_resource_id,DATE(notification_time);

      

By the clock (with division boundaries relative to the current time), you can do this:

SELECT 
    about_resource_id,
    TIMESTAMPDIFF(HOUR,NOW(),notification_time) AS hours_ago,
    COUNT(*) 
FROM notification_tbl 
GROUP BY about_resource_id,TIMESTAMPDIFF(HOUR,NOW(),notification_time);

      



For notifications grouped in other ways, I would write a suitable formula in the GROUP BY clause.

To indicate who last commented, I would do something like this:

SELECT 
    about_resource_id,
    DATE(notification_time) AS notification_date,
    SUBSTRING_INDEX(GROUP_CONCAT(counterparty_id,";"),";",1) AS last_commenter_id,
    COUNT(*) AS commenters 
FROM notification_tbl 
GROUP BY 
    about_resource_id,
    DATE(notification_time) 
ORDER BY notification_time DESC;

      

I used the MySQL-specific GROUP_CONCAT () function with the SUBSTRING_INDEX string function: Some alternative JOIN / aggregate methods for solving this problem can be difficult to refine because MySQL does not seem to support LIMIT in a subquery . You may need to consider the format of your commenter_id, commenter_name, or equivalent field; when adapting my solution.

0


source







All Articles