2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
class EchoNotification extends EchoAbstractEntity implements Bundleable {
|
2013-03-05 01:31:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EchoEvent
|
|
|
|
*/
|
|
|
|
protected $event;
|
|
|
|
|
2014-08-07 00:55:24 +00:00
|
|
|
/**
|
2015-03-16 15:47:13 +00:00
|
|
|
* The target page object for the notification if there is one. Null means
|
|
|
|
* the information has not been loaded.
|
|
|
|
*
|
|
|
|
* @var EchoTargetPage[]|null
|
2014-08-07 00:55:24 +00:00
|
|
|
*/
|
2015-03-16 15:47:13 +00:00
|
|
|
protected $targetPages;
|
2014-08-07 00:55:24 +00:00
|
|
|
|
2013-03-05 01:31:41 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $timestamp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $readTimestamp;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* The hash used to determine if a set of event could be bundled
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $bundleHash = '';
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
/**
|
|
|
|
* @var EchoNotification[]
|
|
|
|
*/
|
|
|
|
protected $bundledNotifications;
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* Do not use this constructor.
|
|
|
|
*/
|
2015-10-01 13:48:52 +00:00
|
|
|
protected function __construct() {
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
/**
|
2013-03-05 01:31:41 +00:00
|
|
|
* Creates an EchoNotification object based on event and user
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param array $info The following keys are required:
|
2013-03-05 01:31:41 +00:00
|
|
|
* - 'event' The EchoEvent being notified about.
|
|
|
|
* - 'user' The User being notified.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
|
|
|
* @return EchoNotification
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2013-03-05 01:31:41 +00:00
|
|
|
public static function create( array $info ) {
|
|
|
|
$obj = new EchoNotification();
|
2016-12-05 18:51:07 +00:00
|
|
|
static $validFields = [ 'event', 'user' ];
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $validFields as $field ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( isset( $info[$field] ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj->$field = $info[$field];
|
|
|
|
} else {
|
|
|
|
throw new MWException( "Field $field is required" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 22:39:23 +00:00
|
|
|
if ( !$obj->user instanceof User ) {
|
|
|
|
throw new InvalidArgumentException( 'Invalid user parameter, expected: User object' );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$obj->event instanceof EchoEvent ) {
|
2017-10-03 22:39:23 +00:00
|
|
|
throw new InvalidArgumentException( 'Invalid event parameter, expected: EchoEvent object' );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
// Notification timestamp should be the same as event timestamp
|
|
|
|
$obj->timestamp = $obj->event->getTimestamp();
|
|
|
|
// Safe fallback
|
|
|
|
if ( !$obj->timestamp ) {
|
|
|
|
$obj->timestamp = wfTimestampNow();
|
|
|
|
}
|
|
|
|
|
2015-10-29 11:23:31 +00:00
|
|
|
// @Todo - Database insert logic should not be inside the model
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj->insert();
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-15 23:21:39 +00:00
|
|
|
* Adds this new notification object to the backend storage.
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
protected function insert() {
|
2014-07-18 03:58:21 +00:00
|
|
|
global $wgEchoNotifications;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper();
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
// Get the bundle key for this event if web bundling is enabled
|
|
|
|
$bundleKey = '';
|
|
|
|
if ( !empty( $wgEchoNotifications[$this->event->getType()]['bundle']['web'] ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
Hooks::run( 'EchoGetBundleRules', [ $this->event, &$bundleKey ] );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
2014-08-07 19:10:37 +00:00
|
|
|
|
2019-11-07 19:35:03 +00:00
|
|
|
// @phan-suppress-next-line PhanImpossibleCondition May be set by hook
|
2013-01-15 23:21:39 +00:00
|
|
|
if ( $bundleKey ) {
|
|
|
|
$hash = md5( $bundleKey );
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->bundleHash = $hash;
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 17:38:12 +00:00
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $this->user );
|
2015-06-16 00:35:05 +00:00
|
|
|
|
2014-08-12 20:37:04 +00:00
|
|
|
// Add listener to refresh notification count upon insert
|
|
|
|
$notifMapper->attachListener( 'insert', 'refresh-notif-count',
|
2019-12-27 13:16:45 +00:00
|
|
|
function () use ( $notifUser ) {
|
2018-05-25 17:45:49 +00:00
|
|
|
$notifUser->resetNotificationCount();
|
2014-08-12 20:37:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
$notifMapper->insert( $this );
|
2013-05-24 22:51:47 +00:00
|
|
|
|
2016-10-13 18:55:34 +00:00
|
|
|
if ( $this->event->getCategory() === 'edit-user-talk' ) {
|
2016-10-18 02:53:10 +00:00
|
|
|
$this->user->setNewtalk( true );
|
2015-06-16 00:35:05 +00:00
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
Hooks::run( 'EchoCreateNotificationComplete', [ $this ] );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2013-03-01 00:26:59 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Load a notification record from std class
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param stdClass $row
|
|
|
|
* @param EchoTargetPage[]|null $targetPages An array of EchoTargetPage instances, or null if not loaded.
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return EchoNotification|false False if failed to load/unserialize
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public static function newFromRow( $row, array $targetPages = null ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$notification = new EchoNotification();
|
|
|
|
|
|
|
|
if ( property_exists( $row, 'event_type' ) ) {
|
|
|
|
$notification->event = EchoEvent::newFromRow( $row );
|
|
|
|
} else {
|
|
|
|
$notification->event = EchoEvent::newFromID( $row->notification_event );
|
|
|
|
}
|
2014-08-07 00:55:24 +00:00
|
|
|
|
2016-05-13 23:39:17 +00:00
|
|
|
if ( $notification->event === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-16 15:47:13 +00:00
|
|
|
$notification->targetPages = $targetPages;
|
2014-07-18 03:58:21 +00:00
|
|
|
$notification->user = User::newFromId( $row->notification_user );
|
2014-08-18 22:49:26 +00:00
|
|
|
// Notification timestamp should never be empty
|
|
|
|
$notification->timestamp = wfTimestamp( TS_MW, $row->notification_timestamp );
|
|
|
|
// Only convert to MW format if it is not empty, otherwise
|
|
|
|
// wfTimestamp would use current timestamp for empty cases
|
|
|
|
if ( $row->notification_read_timestamp ) {
|
|
|
|
$notification->readTimestamp = wfTimestamp( TS_MW, $row->notification_read_timestamp );
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
$notification->bundleHash = $row->notification_bundle_hash;
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert object property to database row array
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toDbArray() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2014-07-18 03:58:21 +00:00
|
|
|
'notification_event' => $this->event->getId(),
|
|
|
|
'notification_user' => $this->user->getId(),
|
|
|
|
'notification_timestamp' => $this->timestamp,
|
|
|
|
'notification_read_timestamp' => $this->readTimestamp,
|
|
|
|
'notification_bundle_hash' => $this->bundleHash,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 00:26:59 +00:00
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return EchoEvent The event for this notification
|
|
|
|
*/
|
|
|
|
public function getEvent() {
|
|
|
|
return $this->event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return User The recipient of this notification
|
|
|
|
*/
|
|
|
|
public function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
2013-03-05 01:31:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return string Notification creation timestamp
|
|
|
|
*/
|
|
|
|
public function getTimestamp() {
|
|
|
|
return $this->timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return string|null Notification read timestamp
|
|
|
|
*/
|
|
|
|
public function getReadTimestamp() {
|
|
|
|
return $this->readTimestamp;
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
public function isRead() {
|
|
|
|
return $this->getReadTimestamp() !== null;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Getter method
|
|
|
|
* @return string|null Notification bundle hash
|
|
|
|
*/
|
|
|
|
public function getBundleHash() {
|
|
|
|
return $this->bundleHash;
|
|
|
|
}
|
|
|
|
|
2014-08-07 00:55:24 +00:00
|
|
|
/**
|
2015-03-16 15:47:13 +00:00
|
|
|
* Getter method. Returns an array of EchoTargetPages, or null if they have
|
|
|
|
* not been loaded.
|
|
|
|
*
|
|
|
|
* @return EchoTargetPage[]|null
|
2014-08-07 00:55:24 +00:00
|
|
|
*/
|
2015-03-16 15:47:13 +00:00
|
|
|
public function getTargetPages() {
|
|
|
|
return $this->targetPages;
|
2014-08-07 00:55:24 +00:00
|
|
|
}
|
2016-03-04 19:23:02 +00:00
|
|
|
|
2019-10-23 10:44:35 +00:00
|
|
|
public function setBundledNotifications( array $notifications ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->bundledNotifications = $notifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBundledNotifications() {
|
|
|
|
return $this->bundledNotifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function canBeBundled() {
|
|
|
|
return !$this->isRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function getBundlingKey() {
|
|
|
|
return $this->getBundleHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public function setBundledElements( array $bundleables ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->setBundledNotifications( $bundleables );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function getSortingKey() {
|
2016-07-06 16:48:46 +00:00
|
|
|
return ( $this->isRead() ? '0' : '1' ) . '_' . $this->getTimestamp();
|
2016-03-04 19:23:02 +00:00
|
|
|
}
|
2019-03-02 20:25:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the list of fields that should be selected to create
|
|
|
|
* a new event with EchoNotification::newFromRow
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public static function selectFields() {
|
|
|
|
return array_merge( EchoEvent::selectFields(), [
|
|
|
|
'notification_event',
|
|
|
|
'notification_user',
|
|
|
|
'notification_timestamp',
|
|
|
|
'notification_read_timestamp',
|
|
|
|
'notification_bundle_hash',
|
|
|
|
] );
|
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|