mediawiki-extensions-Echo/Notifier.php
Kaldari ab784f649b Making sure email notifications are based on categories as well
Change-Id: Ib7cee67c940a4bb1b770f509edd995cb0631950d
2013-03-07 10:32:29 -08:00

90 lines
2.8 KiB
PHP

<?php
// @todo Fill in
class EchoNotifier {
/**
* Record an EchoNotification for an EchoEvent
* Currently used for web-based notifications.
*
* @param $user User to notify.
* @param $event EchoEvent to notify about.
*/
public static function notifyWithNotification( $user, $event ) {
global $wgEchoConfig, $wgEchoNotifications;
EchoNotification::create( array( 'user' => $user, 'event' => $event ) );
// Attempt event logging if Echo schema is enabled
if ( $wgEchoConfig['eventlogging']['Echo']['enabled'] ) {
$agent = $event->getAgent();
// Typically an event should always have an agent, but agent could be
// null if the data is corrupted
if ( $agent ) {
$sender = $agent->isAnon() ? $agent->getName() : $agent->getId();
} else {
$sender = 0;
}
if ( isset( $wgEchoNotifications[$event->getType()]['group'] ) ) {
$group = $wgEchoNotifications[$event->getType()]['group'];
} else {
$group = 'neutral';
}
$data = array (
'version' => $wgEchoConfig['version'],
'eventId' => $event->getId(),
'notificationType' => $event->getType(),
'notificationGroup' => $group,
'sender' => (string)$sender,
'recipientUserId' => $user->getId(),
'recipientEditCount' => (int)$user->getEditCount()
);
EchoHooks::logEvent( 'Echo', $data );
}
EchoNotificationController::resetNotificationCount( $user, DB_MASTER );
}
/**
* Send a Notification to a user by email
*
* @param $user User to notify.
* @param $event EchoEvent to notify about.
* @return bool
*/
public static function notifyWithEmail( $user, $event ) {
if ( !$user->isEmailConfirmed() ) {
// No valid email address
return false;
}
// See if the user wants to receive emails for this category of event
$category = EchoNotificationController::getNotificationCategory( $event->getType() );
if ( $user->getOption( 'echo-subscriptions-email-' . $category ) ) {
global $wgEchoEnableEmailBatch, $wgPasswordSender, $wgPasswordSenderName;
// batched email notification
if ( $wgEchoEnableEmailBatch && $user->getOption( 'echo-email-frequency' ) > 0 ) {
$priority = EchoNotificationController::getNotificationPriority( $event->getType() );
MWEchoEmailBatch::addToQueue( $user->getId(), $event->getId(), $priority );
return true;
}
// no email notification
if ( $user->getOption( 'echo-email-frequency' ) < 0 ) {
return false;
}
// instant email notification
$adminAddress = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
$address = new MailAddress( $user );
$email = EchoNotificationController::formatNotification( $event, $user, 'email' );
$subject = $email['subject'];
$body = $email['body'];
UserMailer::send( $address, $adminAddress, $subject, $body );
}
return true;
}
}