jenkins-bot has submitted this change and it was merged.
Change subject: Add method for fetching unread notifications
......................................................................
Add method for fetching unread notifications
* This will be used for the unread notifications in message overlay
* This will be used to "mark all as read" based on sections ( event types ),
since we can't do updateJoin. We could set a max update limit like 3000,
and use this to do "mark all as read", this would handle majority "mark all
as read"
use cases. This would also prevent updating big amount of data on a web
request
Change-Id: I2a9103a73d0aa91a52d5c0233e946a0ef979f96d
---
M includes/mapper/NotificationMapper.php
M tests/includes/mapper/NotificationMapperTest.php
2 files changed, 82 insertions(+), 22 deletions(-)
Approvals:
Jdlrobson: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/mapper/NotificationMapper.php
b/includes/mapper/NotificationMapper.php
index d179790..2e65cd9 100644
--- a/includes/mapper/NotificationMapper.php
+++ b/includes/mapper/NotificationMapper.php
@@ -72,6 +72,49 @@
}
/**
+ * Get unread notifications by user in the amount specified by limit.
Based on existing
+ * requirements, we just need x amount ( 100 ) unread notifications to
show on the
+ * overlay, so we don't need offset and ordering, we have an index to
retrieve unread
+ * notifications but it's not optimized for ordering
+ * @param User $user
+ * @param int $limit
+ * @param string[] $eventTypes
+ * @return EchoNotification[]
+ */
+ public function fetchUnreadByUser( User $user, $limit, array
$eventTypes = array() ) {
+ $data = array();
+
+ if ( !$eventTypes ) {
+ return $data;
+ }
+
+ $dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
+ $res = $dbr->select(
+ array( 'echo_notification', 'echo_event' ),
+ '*',
+ array(
+ 'notification_user' => $user->getID(),
+ 'event_type' => $eventTypes,
+ 'notification_bundle_base' => 1,
+ 'notification_read_timestamp' => NULL
+ ),
+ __METHOD__,
+ array(
+ 'LIMIT' => $limit,
+ ),
+ array(
+ 'echo_event' => array( 'LEFT JOIN',
'notification_event=event_id' ),
+ )
+ );
+ if ( $res ) {
+ foreach ( $res as $row ) {
+ $data[] = EchoNotification::newFromRow( $row );
+ }
+ }
+ return $data;
+ }
+
+ /**
* Get Notification by user in batch along with limit, offset etc
* @param User the user to get notifications for
* @param int The maximum number of notifications to return
@@ -79,10 +122,10 @@
* @param array Event types to load
* @return EchoNotification[]
*/
- public function fetchByUser( User $user, $limit, $continue, array
$eventTypesToLoad = array() ) {
+ public function fetchByUser( User $user, $limit, $continue, array
$eventTypes = array() ) {
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
- if ( !$eventTypesToLoad ) {
+ if ( !$eventTypes ) {
return array();
}
@@ -96,7 +139,7 @@
// Look for notifications with base = 1
$conds = array(
'notification_user' => $user->getID(),
- 'event_type' => $eventTypesToLoad,
+ 'event_type' => $eventTypes,
'notification_bundle_base' => 1
);
diff --git a/tests/includes/mapper/NotificationMapperTest.php
b/tests/includes/mapper/NotificationMapperTest.php
index a88dfd2..147edcd 100644
--- a/tests/includes/mapper/NotificationMapperTest.php
+++ b/tests/includes/mapper/NotificationMapperTest.php
@@ -9,17 +9,44 @@
$this->assertTrue( true );
}
- public function testFetchByUser() {
- global $wgEchoNotificationCategories;
- $previous = $wgEchoNotificationCategories;
+ public function fetchUnreadByUser( User $user, $limit, array
$eventTypes = array() ) {
+ // Unsuccessful select
+ $notifMapper = new EchoNotificationMapper(
$this->mockMWEchoDbFactory( array ( 'select' => false ) ) );
+ $res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10,
'' );
+ $this->assertEmpty( $res );
- // Alter the category group so the user is always elegible to
- // view some notification types.
- foreach ( $wgEchoNotificationCategories as &$value ) {
- $value['usergroups'] = array( 'echo_group' );
+ // Successful select
+ $dbResult = array(
+ (object)array (
+ 'event_id' => 1,
+ 'event_type' => 'test_event',
+ 'event_variant' => '',
+ 'event_extra' => '',
+ 'event_page_id' => '',
+ 'event_agent_id' => '',
+ 'event_agent_ip' => '',
+ 'notification_user' => 1,
+ 'notification_timestamp' => '20140615101010',
+ 'notification_read_timestamp' => '',
+ 'notification_bundle_base' => 1,
+ 'notification_bundle_hash' => 'testhash',
+ 'notification_bundle_display_hash' =>
'testdisplayhash'
+ )
+ );
+ $notifMapper = new EchoNotificationMapper(
$this->mockMWEchoDbFactory( array ( 'select' => $dbResult ) ) );
+ $res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10,
'', array() );
+ $this->assertEmpty( $res );
+
+ $notifMapper = new EchoNotificationMapper(
$this->mockMWEchoDbFactory( array ( 'select' => $dbResult ) ) );
+ $res = $notifMapper->fetchUnreadByUser( $this->mockUser(), 10,
'', array( 'test_event' ) );
+ $this->assertTrue( is_array( $res ) );
+ $this->assertGreaterThan( 0, count( $res ) );
+ foreach ( $res as $row ) {
+ $this->assertInstanceOf( 'EchoNotification', $row );
}
- unset( $value );
+ }
+ public function testFetchByUser() {
// Unsuccessful select
$notifMapper = new EchoNotificationMapper(
$this->mockMWEchoDbFactory( array ( 'select' => false ) ) );
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '' );
@@ -29,7 +56,7 @@
$dbResult = array(
(object)array (
'event_id' => 1,
- 'event_type' => 'test',
+ 'event_type' => 'test_event',
'event_variant' => '',
'event_extra' => '',
'event_page_id' => '',
@@ -55,19 +82,9 @@
$this->assertInstanceOf( 'EchoNotification', $row );
}
- // Alter the category group so the user is not elegible to
- // view any notification types.
- foreach ( $wgEchoNotificationCategories as &$value ) {
- $value['usergroups'] = array( 'sysop' );
- }
- unset( $value );
-
$notifMapper = new EchoNotificationMapper(
$this->mockMWEchoDbFactory( array() ) );
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '' );
$this->assertEmpty( $res );
-
- // Restore the default setting
- $wgEchoNotificationCategories = $previous;
}
public function testFetchNewestByUserBundleHash() {
--
To view, visit https://gerrit.wikimedia.org/r/153734
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I2a9103a73d0aa91a52d5c0233e946a0ef979f96d
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits