2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database gateway which handles direct database interaction with the
|
|
|
|
* echo_notification & echo_event for a user, that wouldn't require
|
|
|
|
* loading data into models
|
|
|
|
*/
|
|
|
|
class EchoUserNotificationGateway {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
protected $dbFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The tables for this gateway
|
|
|
|
*/
|
|
|
|
protected static $eventTable = 'echo_event';
|
|
|
|
protected static $notificationTable = 'echo_notification';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User
|
|
|
|
* @param MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
public function __construct( User $user, MWEchoDbFactory $dbFactory ) {
|
|
|
|
$this->user = $user;
|
|
|
|
$this->dbFactory = $dbFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark notifications as read
|
|
|
|
* @param $eventIDs array
|
2017-07-26 19:34:44 +00:00
|
|
|
* @return bool
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
public function markRead( array $eventIDs ) {
|
|
|
|
if ( !$eventIDs ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
|
|
|
|
return $dbw->update(
|
|
|
|
self::$notificationTable,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_event' => $eventIDs,
|
|
|
|
'notification_read_timestamp' => null,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-18 03:58:21 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-04 22:44:22 +00:00
|
|
|
/**
|
|
|
|
* Mark notifications as unread
|
|
|
|
* @param $eventIDs array
|
2017-07-26 19:34:44 +00:00
|
|
|
* @return bool
|
2016-03-04 22:44:22 +00:00
|
|
|
*/
|
|
|
|
public function markUnRead( array $eventIDs ) {
|
|
|
|
if ( !$eventIDs ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
|
|
|
|
return $dbw->update(
|
|
|
|
self::$notificationTable,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'notification_read_timestamp' => null ],
|
|
|
|
[
|
2016-03-04 22:44:22 +00:00
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_event' => $eventIDs,
|
|
|
|
'notification_read_timestamp IS NOT NULL'
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-03-04 22:44:22 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
2014-08-13 22:00:25 +00:00
|
|
|
* 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()
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
|
|
|
public function markAllRead() {
|
|
|
|
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
|
|
|
|
|
|
|
|
return $dbw->update(
|
|
|
|
self::$notificationTable,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'notification_read_timestamp' => $dbw->timestamp( wfTimestampNow() ) ],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_user' => $this->user->getId(),
|
2015-10-01 13:48:52 +00:00
|
|
|
'notification_read_timestamp' => null,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-18 03:58:21 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-22 21:33:22 +00:00
|
|
|
* Get notification count for the types specified
|
2016-03-14 13:37:20 +00:00
|
|
|
* @param int $dbSource use master or slave storage to pull count
|
|
|
|
* @param array $eventTypesToLoad event types to retrieve
|
|
|
|
* @param int $cap Max count
|
2014-07-18 03:58:21 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
public function getCappedNotificationCount( $dbSource, array $eventTypesToLoad = [], $cap = MWEchoNotifUser::MAX_BADGE_COUNT ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
// double check
|
2017-09-24 05:23:47 +00:00
|
|
|
if ( !in_array( $dbSource, [ DB_REPLICA, DB_MASTER ] ) ) {
|
|
|
|
$dbSource = DB_REPLICA;
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$eventTypesToLoad ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = $this->dbFactory->getEchoDb( $dbSource );
|
2016-03-14 13:37:20 +00:00
|
|
|
return $db->selectRowCount(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
self::$notificationTable,
|
|
|
|
self::$eventTable
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[ '1' ],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_read_timestamp' => null,
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_type' => $eventTypesToLoad,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-18 03:58:21 +00:00
|
|
|
__METHOD__,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'LIMIT' => $cap ],
|
|
|
|
[
|
|
|
|
'echo_event' => [ 'LEFT JOIN', 'notification_event=event_id' ],
|
|
|
|
]
|
2014-07-18 03:58:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IMPORTANT: should only call this function if the number of unread notification
|
|
|
|
* is reasonable, for example, unread notification count is less than the max
|
2016-03-09 04:50:31 +00:00
|
|
|
* display defined in MWEchoNotifUser::MAX_BADGE_COUNT
|
2014-07-18 03:58:21 +00:00
|
|
|
* @param string
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getUnreadNotifications( $type ) {
|
2017-09-24 05:23:47 +00:00
|
|
|
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA );
|
2014-07-18 03:58:21 +00:00
|
|
|
$res = $dbr->select(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
self::$notificationTable,
|
|
|
|
self::$eventTable
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[ 'notification_event' ],
|
|
|
|
[
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_read_timestamp' => null,
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => 0,
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_type' => $type,
|
|
|
|
'notification_event = event_id'
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-18 03:58:21 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$eventIds = [];
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $res ) {
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$eventIds[$row->notification_event] = $row->notification_event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $eventIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|