mediawiki-extensions-Echo/includes/model/Notification.php

280 lines
6.5 KiB
PHP
Raw Normal View History

<?php
class EchoNotification extends EchoAbstractEntity implements Bundleable {
/**
* @var User
*/
protected $user;
/**
* @var EchoEvent
*/
protected $event;
/**
* The target page object for the notification if there is one. Null means
* the information has not been loaded.
*
* @var EchoTargetPage[]|null
*/
protected $targetPages;
/**
* @var string
*/
protected $timestamp;
/**
* @var string
*/
protected $readTimestamp;
/**
* The hash used to determine if a set of event could be bundled
* @var string
*/
protected $bundleHash = '';
/**
* @var EchoNotification[]
*/
protected $bundledNotifications;
/**
* Do not use this constructor.
*/
protected function __construct() {
}
/**
* Creates an EchoNotification object based on event and user
* @param array $info The following keys are required:
* - 'event' The EchoEvent being notified about.
* - 'user' The User being notified.
* @throws MWException
* @return EchoNotification
*/
public static function create( array $info ) {
$obj = new EchoNotification();
static $validFields = [ 'event', 'user' ];
foreach ( $validFields as $field ) {
if ( isset( $info[$field] ) ) {
$obj->$field = $info[$field];
} else {
throw new MWException( "Field $field is required" );
}
}
if ( !$obj->user instanceof User ) {
throw new InvalidArgumentException( 'Invalid user parameter, expected: User object' );
}
if ( !$obj->event instanceof EchoEvent ) {
throw new InvalidArgumentException( 'Invalid event parameter, expected: EchoEvent object' );
}
// Notification timestamp should be the same as event timestamp
$obj->timestamp = $obj->event->getTimestamp();
// Safe fallback
if ( !$obj->timestamp ) {
$obj->timestamp = wfTimestampNow();
}
// @Todo - Database insert logic should not be inside the model
$obj->insert();
return $obj;
}
/**
* Adds this new notification object to the backend storage.
*/
protected function insert() {
global $wgEchoNotifications;
$notifMapper = new EchoNotificationMapper();
// Get the bundle key for this event if web bundling is enabled
$bundleKey = '';
if ( !empty( $wgEchoNotifications[$this->event->getType()]['bundle']['web'] ) ) {
Hooks::run( 'EchoGetBundleRules', [ $this->event, &$bundleKey ] );
}
if ( $bundleKey ) {
$hash = md5( $bundleKey );
$this->bundleHash = $hash;
}
$notifUser = MWEchoNotifUser::newFromUser( $this->user );
Split alerts and messages in Echo Split the notifications into 'alert' and 'message' badget with two different flyouts. Also clean up styling and module behavior. ** Depends on ooui change Id4bbe14ba0bf6c for footers in popups. ** Depends on ooui change Ie93e4d6ed5637c for fixing a bug in inverted icons. ** MobileFrontend must also be updated to support the new modules in this patch I168f485d6e54cb4067 In this change: * Split notifcations into alert and messages and display those in two different badges. * Create two separate flyout/popups for each category with their notifications. * Create a view-model to control notification state and emit events for both the popup and the badge to intercept and react to. * Clean up module load and distribution: * Create an ext.echo.ui module for javascript-ui support and ooui widgets. * Create an ext.echo.nojs module that unifies all base classes that are needed for both nojs and js support, that the js version builds upon. * Create a separate ext.echo.logger module as a singleton that can be called to perform all logging. * Clean up style uses * Move the special page LESS file into nojs module so all styles load properly even in nojs mode. * Transfer some of the styling from JS to LESS for consistency. * Make the 'read more' button load already with the styles it needs to look like a button, since its behavior is similar in nojs and js vesions, but before its classes were applied only by the js, making it inconsistent and also making its appearance 'jump' from a link to a button. * Delete and clean up all old and unused files. * Moved 'Help.png' icon from modules/overlay to modules/icons for later use. Bug: T108190 Change-Id: I55f440ed9f64c46817f620328a6bb522d44c9ca9
2015-08-13 00:54:16 +00:00
$section = $this->event->getSection();
// Add listener to refresh notification count upon insert
$notifMapper->attachListener( 'insert', 'refresh-notif-count',
function () use ( $notifUser, $section ) {
$notifUser->resetNotificationCount();
}
);
$notifMapper->insert( $this );
if ( $this->event->getCategory() === 'edit-user-talk' ) {
$this->user->setNewtalk( true );
}
Hooks::run( 'EchoCreateNotificationComplete', [ $this ] );
}
/**
* Load a notification record from std class
* @param stdClass $row
* @param EchoTargetPage[]|null $targetPages An array of EchoTargetPage instances, or null if not loaded.
* @return EchoNotification|false False if failed to load/unserialize
*/
public static function newFromRow( $row, $targetPages = null ) {
$notification = new EchoNotification();
if ( property_exists( $row, 'event_type' ) ) {
$notification->event = EchoEvent::newFromRow( $row );
} else {
$notification->event = EchoEvent::newFromID( $row->notification_event );
}
if ( $notification->event === false ) {
return false;
}
$notification->targetPages = $targetPages;
$notification->user = User::newFromId( $row->notification_user );
// 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 );
}
$notification->bundleHash = $row->notification_bundle_hash;
return $notification;
}
/**
* Convert object property to database row array
* @return array
*/
public function toDbArray() {
return [
'notification_event' => $this->event->getId(),
'notification_user' => $this->user->getId(),
'notification_timestamp' => $this->timestamp,
'notification_read_timestamp' => $this->readTimestamp,
'notification_bundle_hash' => $this->bundleHash,
];
}
/**
* 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;
}
/**
* 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;
}
public function isRead() {
return $this->getReadTimestamp() !== null;
}
/**
* Getter method
* @return string|null Notification bundle hash
*/
public function getBundleHash() {
return $this->bundleHash;
}
/**
* Getter method. Returns an array of EchoTargetPages, or null if they have
* not been loaded.
*
* @return EchoTargetPage[]|null
*/
public function getTargetPages() {
return $this->targetPages;
}
public function setBundledNotifications( $notifications ) {
$this->bundledNotifications = $notifications;
}
public function getBundledNotifications() {
return $this->bundledNotifications;
}
/**
* @inheritDoc
*/
public function canBeBundled() {
return !$this->isRead();
}
/**
* @inheritDoc
*/
public function getBundlingKey() {
return $this->getBundleHash();
}
/**
* @inheritDoc
*/
public function setBundledElements( $bundleables ) {
$this->setBundledNotifications( $bundleables );
}
/**
* @inheritDoc
*/
public function getSortingKey() {
return ( $this->isRead() ? '0' : '1' ) . '_' . $this->getTimestamp();
}
/**
* 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',
] );
}
}