<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][7832] branches/1.9: Improve logic for marking at-mention notifications as read</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/7832">7832</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-02-08 18:26:52 +0000 (Sat, 08 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Improve logic for marking at-mention notifications as read

In previous iterations, at-mention notifications were marked as read in the
following cases:

- when a user visited his own activity page
- when a user visited his own mention page
- when a user visited a single-activity permalink page

When the new notifications component was introduced in BP 1.9, the old
notification functions were swapped out one-for-one with the new correlates.
However, this introduced a bug where a logged-out user visiting one of the
pages listed above would mark at-mention items as read for *all* users (due to
the user_id field being null, and the user_id WHERE clause being excluded from
the UPDATE query).

We take this bug as an opportunity to narrow the circumstances in which
at-mention notifications are marked as read. The new system:

- When a user visits his own mention page, mark all his at-mention notifications
  as read
- When a user visits the permalink page of a single activity item in which he
  is mentioned, mark that specific at-mention notification as read

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/5384">#5384</a>

Props SlothLoveChunk for an initial patch</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches19bpactivitybpactivitynotificationsphp">branches/1.9/bp-activity/bp-activity-notifications.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#branches19teststestcasesactivitynotificationsphp">branches/1.9/tests/testcases/activity/notifications.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches19bpactivitybpactivitynotificationsphp"></a>
<div class="modfile"><h4>Modified: branches/1.9/bp-activity/bp-activity-notifications.php (7831 => 7832)</h4>
<pre class="diff"><span>
<span class="info">--- branches/1.9/bp-activity/bp-activity-notifications.php   2014-02-08 18:25:54 UTC (rev 7831)
+++ branches/1.9/bp-activity/bp-activity-notifications.php      2014-02-08 18:26:52 UTC (rev 7832)
</span><span class="lines">@@ -336,18 +336,43 @@
</span><span class="cx"> add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );
</span><span class="cx"> 
</span><span class="cx"> /**
</span><del>- * Remove activity notifications when a user clicks on them.
</del><ins>+ * Mark at-mention notifications as read when users visit their Mentions page.
</ins><span class="cx">  *
</span><span class="cx">  * @since BuddyPress (1.5)
</span><span class="cx">  *
</span><span class="cx">  * @uses bp_notifications_mark_all_notifications_by_type()
</span><span class="cx">  */
</span><span class="cx"> function bp_activity_remove_screen_notifications() {
</span><del>-       if ( bp_is_active( 'notifications' ) ) {
-               bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->activity->id, 'new_at_mention' );
</del><ins>+        if ( ! bp_is_active( 'notifications' ) ) {
+               return;
</ins><span class="cx">   }
</span><ins>+
+       // Only mark read if you're looking at your own mentions
+       if ( ! bp_is_my_profile() ) {
+               return;
+       }
+
+       bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->activity->id, 'new_at_mention' );
</ins><span class="cx"> }
</span><del>-add_action( 'bp_activity_screen_my_activity',               'bp_activity_remove_screen_notifications' );
-add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications' );
-add_action( 'bp_activity_screen_mentions',                  'bp_activity_remove_screen_notifications' );
</del><ins>+add_action( 'bp_activity_screen_mentions', 'bp_activity_remove_screen_notifications' );
</ins><span class="cx"> 
</span><ins>+/**
+ * Mark at-mention notification as read when user visits the activity with the mention.
+ *
+ * @since BuddyPress (2.0.0)
+ */
+function bp_activity_remove_screen_notifications_single_activity_permalink( $activity ) {
+       if ( ! bp_is_active( 'notifications' ) ) {
+               return;
+       }
+
+       if ( ! is_user_logged_in() ) {
+               return;
+       }
+
+       // Mark as read any notifications for the current user related to this
+       // activity item
+       bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $activity->id, buddypress()->activity->id, 'new_at_mention' );
+}
+add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications_single_activity_permalink' );
+
</ins></span></pre></div>
<a id="branches19teststestcasesactivitynotificationsphp"></a>
<div class="addfile"><h4>Added: branches/1.9/tests/testcases/activity/notifications.php (0 => 7832)</h4>
<pre class="diff"><span>
<span class="info">--- branches/1.9/tests/testcases/activity/notifications.php                          (rev 0)
+++ branches/1.9/tests/testcases/activity/notifications.php     2014-02-08 18:26:52 UTC (rev 7832)
</span><span class="lines">@@ -0,0 +1,227 @@
</span><ins>+<?php
+
+/**
+ * @group activity
+ * @group notifications
+ */
+class BP_Tests_Activity_Notifications extends BP_UnitTestCase {
+       protected $current_user;
+       protected $u1;
+       protected $u2;
+       protected $a1;
+       protected $a2;
+
+       public function setUp() {
+               parent::setUp();
+               $this->current_user = get_current_user_id();
+               $this->u1 = $this->create_user();
+               $this->u2 = $this->create_user();
+               $this->set_current_user( $this->u1 );
+       }
+
+       public function tearDown() {
+               $this->set_current_user( $this->current_user );
+               parent::tearDown();
+       }
+
+       /**
+        * @group bp_activity_remove_screen_notifications
+        * @group mentions
+        */
+       public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink() {
+               $this->create_notifications();
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Double check it's there
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               // Go to the activity permalink page
+               $this->go_to( bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/p/' . $this->a1 . '/' );
+               $activity = bp_activity_get_specific( array( 'activity_ids' => $this->a1, 'show_hidden' => true, 'spam' => 'ham_only', ) );
+               do_action( 'bp_activity_screen_single_activity_permalink', $activity['activities'][0] );
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Should be empty
+               $this->assertEquals( array(), $notifications );
+       }
+
+       /**
+        * @group bp_activity_remove_screen_notifications
+        * @group mentions
+        */
+       public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink_logged_out() {
+               $this->create_notifications();
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Double check it's there
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               // Log out
+               $this->set_current_user( 0 );
+
+               // Go to the activity permalink page
+               $this->go_to( bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/p/' . $this->a1 . '/' );
+               $activity = bp_activity_get_specific( array( 'activity_ids' => $this->a1, 'show_hidden' => true, 'spam' => 'ham_only', ) );
+               do_action( 'bp_activity_screen_single_activity_permalink', $activity['activities'][0] );
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Should be untouched
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               $this->set_current_user( $this->u1 );
+       }
+
+       /**
+        * @group bp_activity_remove_screen_notifications
+        * @group mentions
+        */
+       public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink_wrong_user() {
+               $this->create_notifications();
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Double check it's there
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               // Switch user
+               $this->set_current_user( $this->u2 );
+
+               // Go to the activity permalink page
+               $this->go_to( bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/p/' . $this->a1 . '/' );
+               $activity = bp_activity_get_specific( array( 'activity_ids' => $this->a1, 'show_hidden' => true, 'spam' => 'ham_only', ) );
+               do_action( 'bp_activity_screen_single_activity_permalink', $activity['activities'][0] );
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Should be untouched
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               $this->set_current_user( $this->u1 );
+       }
+
+       /**
+        * @group bp_activity_remove_screen_notifications
+        * @group mentions
+        */
+       public function test_bp_activity_remove_screen_notifications_on_mentions() {
+               $this->create_notifications();
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Double check it's there
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               // Go to the My Activity page
+               $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' );
+               do_action( 'bp_activity_screen_mentions' );
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Should be empty
+               $this->assertEquals( array(), $notifications );
+       }
+
+       /**
+        * @group bp_activity_remove_screen_notifications
+        * @group mentions
+        */
+       public function test_bp_activity_remove_screen_notifications_on_mentions_logged_out() {
+               $this->create_notifications();
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Double check it's there
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               // Log out
+               $this->set_current_user( 0 );
+
+               // Go to the My Activity page
+               $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' );
+               do_action( 'bp_activity_screen_mentions' );
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Should be untouched
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               // clean up
+               $this->set_current_user( $this->u1 );
+       }
+
+       /**
+        * @group bp_activity_remove_screen_notifications
+        * @group mentions
+        */
+       public function test_bp_activity_remove_screen_notifications_on_mentions_wrong_user() {
+               $this->create_notifications();
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Double check it's there
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               // Log out
+               $this->set_current_user( $this->u2 );
+
+               // Go to the My Activity page
+               $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' );
+               do_action( 'bp_activity_screen_mentions' );
+
+               $notifications = BP_Notifications_Notification::get( array(
+                       'user_id' => $this->u1,
+               ) );
+
+               // Should be untouched
+               $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
+
+               // clean up
+               $this->set_current_user( $this->u1 );
+       }
+
+       /**
+        * Creates two notifications for $u1, one of which is for mentions
+        */
+       protected function create_notifications() {
+               $u1_mentionname = bp_activity_get_user_mentionname( $this->u1 );
+               $this->a1 = $this->factory->activity->create( array(
+                       'user_id' => $this->u2,
+                       'component' => buddypress()->activity->id,
+                       'type' => 'activity_update',
+                       'content' => sprintf( 'Hello! @%s', $u1_mentionname ),
+               ) );
+               $u2_mentionname = bp_activity_get_user_mentionname( $this->u2 );
+               $this->a2 = $this->factory->activity->create( array(
+                       'user_id' => $this->u1,
+                       'component' => buddypress()->activity->id,
+                       'type' => 'activity_update',
+                       'content' => sprintf( 'Hello! @%s', $u2_mentionname ),
+               ) );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>