user = $user; $this->dbFactory = $dbFactory; } /** * Mark notifications as read * @param $eventIDs array * @return bool */ public function markRead( array $eventIDs ) { if ( !$eventIDs ) { return; } $dbw = $this->dbFactory->getEchoDb( DB_MASTER ); return $dbw->update( self::$notificationTable, [ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ], [ 'notification_user' => $this->user->getId(), 'notification_event' => $eventIDs, 'notification_read_timestamp' => null, ], __METHOD__ ); } /** * Mark notifications as unread * @param $eventIDs array * @return bool */ public function markUnRead( array $eventIDs ) { if ( !$eventIDs ) { return; } $dbw = $this->dbFactory->getEchoDb( DB_MASTER ); return $dbw->update( self::$notificationTable, [ 'notification_read_timestamp' => null ], [ 'notification_user' => $this->user->getId(), 'notification_event' => $eventIDs, 'notification_read_timestamp IS NOT NULL' ], __METHOD__ ); } /** * Mark all notification as read, use MWEchoNotifUer::markAllRead() instead * @deprecated may need this when running in a job or revive this when we * have updateJoin() */ public function markAllRead() { $dbw = $this->dbFactory->getEchoDb( DB_MASTER ); return $dbw->update( self::$notificationTable, [ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ], [ 'notification_user' => $this->user->getId(), 'notification_read_timestamp' => null, ], __METHOD__ ); } /** * Get notification count for the types specified * @param int $dbSource use master or slave storage to pull count * @param array $eventTypesToLoad event types to retrieve * @param int $cap Max count * @return int */ public function getCappedNotificationCount( $dbSource, array $eventTypesToLoad = [], $cap = MWEchoNotifUser::MAX_BADGE_COUNT ) { // double check if ( !in_array( $dbSource, [ DB_REPLICA, DB_MASTER ] ) ) { $dbSource = DB_REPLICA; } if ( !$eventTypesToLoad ) { return 0; } $db = $this->dbFactory->getEchoDb( $dbSource ); return $db->selectRowCount( [ self::$notificationTable, self::$eventTable ], [ '1' ], [ 'notification_user' => $this->user->getId(), 'notification_read_timestamp' => null, 'event_deleted' => 0, 'event_type' => $eventTypesToLoad, ], __METHOD__, [ 'LIMIT' => $cap ], [ 'echo_event' => [ 'LEFT JOIN', 'notification_event=event_id' ], ] ); } /** * IMPORTANT: should only call this function if the number of unread notification * is reasonable, for example, unread notification count is less than the max * display defined in MWEchoNotifUser::MAX_BADGE_COUNT * @param string * @return int[] */ public function getUnreadNotifications( $type ) { $dbr = $this->dbFactory->getEchoDb( DB_REPLICA ); $res = $dbr->select( [ self::$notificationTable, self::$eventTable ], [ 'notification_event' ], [ 'notification_user' => $this->user->getId(), 'notification_read_timestamp' => null, 'event_deleted' => 0, 'event_type' => $type, 'notification_event = event_id' ], __METHOD__ ); $eventIds = []; if ( $res ) { foreach ( $res as $row ) { $eventIds[$row->notification_event] = $row->notification_event; } } return $eventIds; } }