mediawiki-extensions-Echo/includes/Notifier.php
Sam Smith f4765cdd25 Decommission Echo* instruments
Completely remove the EchoMail and EchoInteraction instruments,
supporting code, and config variables.  Note well that the
EchoEventLoggingSchemas config included an entry for the
already-decommissioned Echo instrument so remove that too.

Bug: T344167
Change-Id: Ic0c44737d2c4a78ec19e67b8b8cd4e6cfb8e14fa
2023-08-29 11:27:15 +01:00

156 lines
5.4 KiB
PHP

<?php
namespace MediaWiki\Extension\Notifications;
use MailAddress;
use MediaWiki\Extension\Notifications\Formatters\EchoHtmlEmailFormatter;
use MediaWiki\Extension\Notifications\Formatters\EchoPlainTextEmailFormatter;
use MediaWiki\Extension\Notifications\Hooks\HookRunner;
use MediaWiki\Extension\Notifications\Model\Event;
use MediaWiki\Extension\Notifications\Model\Notification;
use MediaWiki\MediaWikiServices;
use User;
use UserMailer;
class Notifier {
/**
* Record a Notification for an Event
* Currently used for web-based notifications.
*
* @param User $user User to notify.
* @param Event $event Event to notify about.
*/
public static function notifyWithNotification( $user, $event ) {
// Only create the notification if the user wants to receive that type
// of notification, and they are eligible to receive it. See bug 47664.
$attributeManager = Services::getInstance()->getAttributeManager();
$userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' );
if ( !in_array( $event->getType(), $userWebNotifications ) ) {
return;
}
Notification::create( [ 'user' => $user, 'event' => $event ] );
}
/**
* Send a Notification to a user by email
*
* @param User $user User to notify.
* @param Event $event Event to notify about.
* @return bool
*/
public static function notifyWithEmail( $user, $event ) {
global $wgEnableEmail, $wgBlockDisablesLogin;
$services = MediaWikiServices::getInstance();
$userOptionsLookup = $services->getUserOptionsLookup();
if (
// Email is globally disabled
!$wgEnableEmail ||
// User does not have a valid and confirmed email address
!$user->isEmailConfirmed() ||
// User has disabled Echo emails
$userOptionsLookup->getOption( $user, 'echo-email-frequency' ) < 0 ||
// User is blocked and cannot log in (T199993)
( $wgBlockDisablesLogin && $user->getBlock() )
) {
return false;
}
$hookRunner = new HookRunner( $services->getHookContainer() );
// Final check on whether to send email for this user & event
if ( !$hookRunner->onEchoAbortEmailNotification( $user, $event ) ) {
return false;
}
$attributeManager = Services::getInstance()->getAttributeManager();
$userEmailNotifications = $attributeManager->getUserEnabledEvents( $user, 'email' );
// See if the user wants to receive emails for this category or the user is eligible to receive this email
if ( in_array( $event->getType(), $userEmailNotifications ) ) {
global $wgEchoEnableEmailBatch, $wgEchoNotifications, $wgPasswordSender, $wgNoReplyAddress;
$priority = $attributeManager->getNotificationPriority( $event->getType() );
$bundleString = $bundleHash = '';
// We should have bundling for email digest as long as either web or email bundling is on,
// for example, talk page email bundling is off, but if a user decides to receive email
// digest, we should bundle those messages
if ( !empty( $wgEchoNotifications[$event->getType()]['bundle']['web'] ) ||
!empty( $wgEchoNotifications[$event->getType()]['bundle']['email'] )
) {
$hookRunner->onEchoGetBundleRules( $event, $bundleString );
}
if ( $bundleString ) {
$bundleHash = md5( $bundleString );
}
// email digest notification ( weekly or daily )
if ( $wgEchoEnableEmailBatch && $userOptionsLookup->getOption( $user, 'echo-email-frequency' ) > 0 ) {
// always create a unique event hash for those events don't support bundling
// this is mainly for group by
if ( !$bundleHash ) {
$bundleHash = md5( $event->getType() . '-' . $event->getId() );
}
EmailBatch::addToQueue( $user->getId(), $event->getId(), $priority, $bundleHash );
return true;
}
// instant email notification
$toAddress = MailAddress::newFromUser( $user );
$fromAddress = new MailAddress(
$wgPasswordSender,
wfMessage( 'emailsender' )->inContentLanguage()->text()
);
$replyAddress = new MailAddress( $wgNoReplyAddress );
// Since we are sending a single email, should set the bundle hash to null
// if it is set with a value from somewhere else
$event->setBundleHash( null );
$email = self::generateEmail( $event, $user );
if ( !$email ) {
return false;
}
$subject = $email['subject'];
$body = $email['body'];
$options = [ 'replyTo' => $replyAddress ];
UserMailer::send( $toAddress, $fromAddress, $subject, $body, $options );
}
return true;
}
/**
* @param Event $event
* @param User $user
* @return array|false An array of 'subject' and 'body', or false if things went wrong
*/
private static function generateEmail( Event $event, User $user ) {
$emailFormat = NotifUser::newFromUser( $user )->getEmailFormat();
$services = MediaWikiServices::getInstance();
$userOptionsLookup = $services->getUserOptionsLookup();
$lang = $services->getLanguageFactory()
->getLanguage( $userOptionsLookup->getOption( $user, 'language' ) );
$formatter = new EchoPlainTextEmailFormatter( $user, $lang );
$content = $formatter->format( $event, 'email' );
if ( !$content ) {
return false;
}
if ( $emailFormat === EmailFormat::HTML ) {
$htmlEmailFormatter = new EchoHtmlEmailFormatter( $user, $lang );
$htmlContent = $htmlEmailFormatter->format( $event, 'email' );
$multipartBody = [
'text' => $content['body'],
'html' => $htmlContent['body']
];
$content['body'] = $multipartBody;
}
return $content;
}
}
class_alias( Notifier::class, 'EchoNotifier' );