简体   繁体   中英

How to group notifications in a feed

I'm implementing a feed in it some notifications should be displayed as a group (ie a user posted a comment on your video) and others as standalone notifications (Your video is featured this week). Highlighting new notifications is also a requirement

For grouped notifications, when the user consult his feed 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 the events when the notifications are interleaved.

In 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 coud be grouped in several ways. Even new notifications may alter the groups breaking the highlighting.

Where can I read more about common solutions for this problem and how to store and return the notifications for my web service?

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

If you wanted 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 hour (with subdivision boundaries relative to current time), you might 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.

For the indication of 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've used the MySQL-specific GROUP_CONCAT() function here, with the string function SUBSTRING_INDEX: Some alternative techniques for solving this problem with a JOIN/aggregate may be difficult to perfect because MySQL apparently does not support LIMIT within a subquery . You might need to consider the format of your commenter_id, commenter_name or equivalent field(s); when adapting my solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM