1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-14 04:34:07 +02:00

[ticket/17135] Refactor messenger code to services [ci skip]

PHPBB3-17135
This commit is contained in:
rxu
2023-06-07 21:16:41 +07:00
parent 1f952ca6d8
commit 5873c72ca5
4 changed files with 390 additions and 8 deletions

View File

@@ -96,6 +96,15 @@ abstract class base
return;
}
/**
* get messenger method fie queue object name
* @return string
*/
abstract public function get_queue_object_name($user)
{
return '';
}
/**
* Sets the use of messenger queue flag
*
@@ -180,6 +189,16 @@ abstract class base
{
}
/**
* Send messages from the queue
*
* @param array $queue_data Queue data array
* @return void
*/
abstract public function process_queue(&$queue_data)
{
}
/**
* Set email template to use
*

View File

@@ -92,6 +92,15 @@ class email extends base
return NOTIFY_EMAIL;
}
/**
* get messenger method fie queue object name
* @return string
*/
abstract public function get_queue_object_name($user)
{
return 'email';
}
/**
* Check if the messenger method is enabled
* @return void
@@ -438,6 +447,71 @@ class email extends base
}
}
/**
* Send messages from the queue
*
* @param array $queue_data Queue data array
* @return void
*/
public function process_queue(&$queue_data)
{
$queue_object_name = $this->get_queue_object_name();
$messages_count = count($queue_data[$queue_object_name]['data'];
if (!$this->is_enabled() || !$messages_count)
{
unset($queue_data[$queue_object_name]);
return;
}
@set_time_limit(0);
$package_size = $queue_data[$queue_object_name]['package_size'] ?? 0;
$num_items = (!$package_size || $messages_count < $package_size) ? $messages_count : $package_size;
$mailer = new Mailer($this->transport);
for ($i = 0; $i < $num_items; $i++)
{
// Make variables available...
extract(array_shift($queue_data[$queue_object_name]['data']));
$break = false;
/**
* Event to send message via external transport
*
* @event core.notification_message_process
* @var bool break Flag indicating if the function return after hook
* @var Symfony\Component\Mime\Email email The Symfony Email object
* @since 3.2.4-RC1
* @changed 4.0.0-a1 Added vars: email. Removed vars: addresses, subject, msg.
*/
$vars = [
'break',
'email',
];
extract($this->dispatcher->trigger_event('core.notification_message_process', compact($vars)));
if (!$break)
{
try
{
$mailer->send($email);
}
catch (TransportExceptionInterface $e)
{
$this->error('EMAIL', $e->getDebug());
continue;
}
}
}
// No more data for this object? Unset it
if (!count($this->queue_data[$queue_object_name]['data']))
{
unset($this->queue_data[$queue_object_name]);
}
}
/**
* Get mailer transport object
*

View File

@@ -120,6 +120,15 @@ class jabber extends base
return NOTIFY_IM;
}
/**
* get messenger method fie queue object name
* @return string
*/
abstract public function get_queue_object_name($user)
{
return 'jabber';
}
/**
* Check if the messenger method is enabled
* @return void
@@ -127,10 +136,10 @@ class jabber extends base
public function is_enabled()
{
return
empty($this->config['jab_enable']) ||
empty($this->config['jab_host']) ||
empty($this->config['jab_username']) ||
empty($this->config['jab_password']);
!empty($this->config['jab_enable']) &&
!empty($this->config['jab_host']) &&
!empty($this->config['jab_username']) &&
!empty($this->config['jab_password']);
}
/**
@@ -416,6 +425,65 @@ class jabber extends base
$this->use_queue = !$this->config['jab_package_size'] ? false : $use_queue;
}
/**
* Send messages from the queue
*
* @param array $queue_data Queue data array
* @return void
*/
public function process_queue(&$queue_data)
{
$queue_object_name = $this->get_queue_object_name();
$messages_count = count($queue_data[$queue_object_name]['data'];
if (!$this->is_enabled() || !$messages_count)
{
unset($queue_data[$queue_object_name]);
return;
}
@set_time_limit(0);
$package_size = $queue_data[$queue_object_name]['package_size'] ?? 0;
$num_items = (!$package_size || $messages_count < $package_size) ? $messages_count : $package_size;
$mailer = new Mailer($this->transport);
for ($i = 0; $i < $num_items; $i++)
{
// Make variables available...
extract(array_shift($queue_data[$queue_object_name]['data']));
if (!$this->connect())
{
$this->error('JABBER', $this->user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->get_log());
return false;
}
if (!$this->login())
{
$this->error('JABBER', $this->user->lang['ERR_JAB_AUTH'] . '<br />' . $this->get_log());
return false;
}
foreach ($addresses as $address)
{
if ($this->send_message($address, $msg, $subject) === false)
{
$this->error('JABBER', $this->get_log());
continue;
}
}
}
// No more data for this object? Unset it
if (!count($this->queue_data[$queue_object_name]['data']))
{
unset($this->queue_data[$queue_object_name]);
}
$this->disconnect();
}
/**
* Send jabber message out
*/
@@ -452,7 +520,11 @@ class jabber extends base
foreach ($addresses as $address)
{
$this->send_message($address, $this->msg, $this->subject);
if ($this->send_message($address, $this->msg, $this->subject) === false)
{
$this->error('JABBER', $this->get_log());
continue;
}
}
$this->disconnect();
@@ -460,11 +532,11 @@ class jabber extends base
else
{
$this->queue->init('jabber', $this->config['jab_package_size']);
$this->queue->put('jabber', array(
$this->queue->put('jabber', [
'addresses' => $addresses,
'subject' => $this->subject,
'msg' => $this->msg)
);
'msg' => $this->msg,
]);
}
unset($addresses);