Skip to:
Content

BuddyPress.org

Changeset 13383


Ignore:
Timestamp:
12/14/2022 09:42:06 PM (2 years ago)
Author:
imath
Message:

Improve consistency between private message status & notification

A private message can have a status set to read or unread by the user. When the Notifications component is active, a notification is generated each time a message is sent. When the user opens this message, 2 actions are performed:

  1. Set the message status as read.
  2. Mark the notification about this message as read.

When opening a message, even if the message has a read status, we still need to check if there's an existing notification about it as notifications can be marked unread from the user's notifications screen. If it's the case, then we need to make sure to mark this unread notification as read 🤪.

Props niftythree

Closes https://github.com/buddypress/buddypress/pull/43
See #8778 (trunk)

Location:
trunk/src/bp-messages
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/actions/view.php

    r13096 r13383  
    5454     */
    5555    if ( bp_is_my_profile() ) {
     56        // This is marking the messages as read inside the BP Messages component's recipient table.
    5657        messages_mark_thread_read( $thread_id );
    5758    }
  • trunk/src/bp-messages/bp-messages-notifications.php

    r13269 r13383  
    262262 * @param int $user_id   ID of the user who read the thread.
    263263 * @param int $num_rows  The number of affected rows by the "mark read" update query.
     264 * @return bool True on success. False otherwise.
    264265 */
    265266function bp_messages_mark_notification_on_mark_thread( $thread_id, $user_id = 0, $num_rows = 0 ) {
     267    $unread_messages = array();
     268
    266269    if ( ! $num_rows ) {
    267         return;
     270        /**
     271         * The thread might have been marked read, but a notification about the thread might also have been
     272         * marked back as unread from the BP Notifications screen. We then need to check a notification exists
     273         * about `new_message` action before checking found message ids are part of the thread.
     274         *
     275         * @see https://buddypress.trac.wordpress.org/ticket/8778
     276         */
     277        $notifications = bp_notifications_get_all_notifications_for_user( $user_id );
     278        if ( $notifications ) {
     279            $unread_messages = wp_filter_object_list(
     280                $notifications,
     281                array(
     282                    'component_action' => 'new_message',
     283                    'is_new'           => 1,
     284                ),
     285                'and',
     286                'item_id'
     287            );
     288
     289            $num_rows = count( $unread_messages );
     290            if ( $num_rows ) {
     291                $unread_messages = array_map( 'intval', $unread_messages );
     292            } else {
     293                return false;
     294            }
     295        }
    268296    }
    269297
     
    271299    $message_ids     = wp_list_pluck( $thread_messages, 'id' );
    272300
    273     bp_notifications_mark_notifications_by_item_ids( $user_id, $message_ids, 'messages', 'new_message', false );
     301    if ( $unread_messages && ! array_intersect( $message_ids, $unread_messages ) ) {
     302        return false;
     303    }
     304
     305    return bp_notifications_mark_notifications_by_item_ids( $user_id, $message_ids, 'messages', 'new_message', false );
    274306}
    275307add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread', 10, 3 );
Note: See TracChangeset for help on using the changeset viewer.