Added queue to handle sending of subscribers notifications

This commit is contained in:
thnilsen
2020-06-16 20:11:25 +02:00
parent 62e6f08e4f
commit e3d8e3ecf8
5 changed files with 351 additions and 64 deletions
+142 -20
View File
@@ -1,5 +1,6 @@
<?php
require_once(__DIR__ . "/queue.php");
/**
* Class that encapsulates everything that can be done with notifications
*/
@@ -46,19 +47,77 @@ class Notification
}
/**
* Loop over the list of subscribers to notify depending on impacted service(s) and
* Loop over the list of subscribers to notify depending on impacted service(s) and
* call the differnet notification handles.
* @return void
*/
*/
public function notify_subscribers()
{
global $mysqli;
// Fetch list of unique subscribers for given service
// Direct inclusion of variable withour using prepare justified by the fact that
// Direct inclusion of variable without using prepare justified by the fact that
// this->serviceids are not user submitted
$sql = "SELECT DISTINCT subscriberIDFK FROM services_subscriber WHERE serviceIDFK IN (" . $this->serviceids . ")";
$query = $mysqli->query($sql);
// Create the queue tasks for email/telegram notifications
$queue = new Queue();
$queue->status = $queue->all_status['populating'];
$queue->user_id = $_SESSION['user'];
$arr_data = array();
if ( SUBSCRIBE_EMAIL ) {
$arr_data = $this->prepare_email(); // Make up the base message and subject for email
$queue->type_id = $queue->all_type_id['notify_email'];
$queue->template_data1 = $arr_data['subject'];
$queue->template_data2 = $arr_data['body'];
$task_id_email = $queue->add_task();
syslog(1, "queue email: ". $task_id_email);
$arr_email = array();
}
if ( SUBSCRIBE_TELEGRAM ) {
$arr_data = $this->prepare_telegram();
$queue->type_id = $queue->all_type_id['notify_telegram'];
$queue->template_data1 = null;
$queue->template_data2 = $arr_data['body'];
$task_id_telegram = $queue->add_task();
syslog(1, "queue telegram: ". $task_id_telegram);
$arr_telegram = array();
}
while ($subscriber = $query->fetch_assoc()) {
// Fetch list of subscriber details for already found subscriber IDs
$stmt = $mysqli->prepare("SELECT typeID FROM subscribers WHERE subscriberID = ? AND active=1");
$stmt->bind_param("i", $subscriber['subscriberIDFK']);
$stmt->execute();
$subscriberQuery = $stmt->get_result();
while ($subscriberData = $subscriberQuery->fetch_assoc()) {
$typeID = $subscriberData['typeID']; // Telegram = 1, email = 2
// Handle telegram
if ($typeID == 1 && SUBSCRIBE_TELEGRAM) {
$arr_telegram[] = $subscriber['subscriberIDFK'];
}
// Handle email
if ($typeID == 2 && SUBSCRIBE_EMAIL) {
$arr_email[] = $subscriber['subscriberIDFK'];
}
}
}
if ( SUBSCRIBE_TELEGRAM) {
$queue->task_id = $task_id_telegram;
$queue->add_notification($arr_telegram); // Add array of Telegram users to the notification queue list
}
if ( SUBSCRIBE_EMAIL ) {
$queue->task_id = $task_id_email;
$queue->add_notification($arr_email); // Add array of Email users to the notification queue list
}
/* OLD CODE to get user email/telegram data) - Move to queue handler...
while ($subscriber = $query->fetch_assoc()) {
// Fetch list of subscriber details for already found subscriber IDs
$stmt = $mysqli->prepare("SELECT typeID, userID, firstname, token FROM subscribers WHERE subscriberID = ? AND active=1");
@@ -82,27 +141,33 @@ class Notification
$this->submit_email($userID, $token);
}
}
}
}*/
}
/**
* Sends Telegram notification message using their web api.
* @param string $userID The Telegram userid to send to
* @param string $firstname The users firstname
* @return boolean
* @param string $msg Body of message
* @return boolean true = Sent / False = failed
*/
public function submit_telegram($userID, $firstname)
{
public function submit_queue_telegram($userID, $firstname, $msg)
{
// TODO Handle limitations (Max 30 different subscribers per second)
// TODO Error handling
$msg = _("Hi %s!\nThere is a status update for service(s): %s\nThe new status is: %s\nTitle: %s\n\n%s\n\n<a href='%s'>View online</a>");
$msg = sprintf($msg, $firstname, $this->servicenames, $this->status, $this->title, $this->text, WEB_URL);
$tg_message = urlencode($msg);
$response = json_decode(file_get_contents("https://api.telegram.org/bot" . TG_BOT_API_TOKEN . "/sendMessage?chat_id=" . $userID . "&parse_mode=HTML&text=" . $tg_message), true);
if (! array_key_exists("ok", $response) || $response['ok'] != 1 ) {
$msg = sprintf($msg, $firstname);
$tg_message = array('text' => $msg, 'chat_id' => $userID, 'parse_mode' => 'HTML');
$json = @file_get_contents("https://api.telegram.org/bot" . TG_BOT_API_TOKEN . "/sendMessage?" . http_build_query($tg_message) );
$response = json_decode($json, true);
if (!is_array($response) || ! array_key_exists("ok", $response) || $response['ok'] != 1 ) {
syslog(1, "telegram failed: ".$userID);
return false;
}
syslog(1,"telegram ok: " .$userID);
return true;
}
@@ -113,12 +178,58 @@ class Notification
* @param String $uthkey Users token for managing subscription
* @return void
*/
public function submit_email($userID, $token)
public function submit_queue_email($subscriber, $subject, $msg)
{
// TODO Error handling
$Parsedown = new Parsedown();
//$Parsedown = new Parsedown();
$mailer = new Mailer();
if ( ! $mailer->send_mail($subscriber, $subject, $msg, true) ) {
syslog(1, "email failed: " .$subscriber);
return false;
}
syslog(1, "email ok: " .$subscriber);
return true;
}
// /**
// * Sends email notifications to a subscriber.
// * Function depends on Parsedown and Mailer class being loaded.
// * @param String $userID The email address to send to
// * @param String $uthkey Users token for managing subscription
// * @return void
// */
// public function submit_email_old($userID, $token)
// {
// // TODO Error handling
// //$Parsedown = new Parsedown();
// $mailer = new Mailer();
//
// $str_mail = file_get_contents("../libs/templates/email_status_update.html");
// $str_mail = str_replace("%name%", NAME, $str_mail);
// // $smtp_mail = str_replace("%email%", $userID, $smtp_mail);
// $str_mail = str_replace("%url%", WEB_URL, $str_mail);
// $str_mail = str_replace("%service%", $this->servicenames, $str_mail);
// $str_mail = str_replace("%status%", $this->status, $str_mail);
// $str_mail = str_replace("%time%", date("c", $this->time), $str_mail);
// $str_mail = str_replace("%comment%", $Parsedown->setBreaksEnabled(true)->text($this->text), $str_mail);
// $str_mail = str_replace("%token%", $token, $str_mail);
//
// $str_mail = str_replace("%service_status_update_from%", _("Service status update from"), $str_mail);
// $str_mail = str_replace("%services_impacted%", _("Service(s) Impacted"), $str_mail);
// $str_mail = str_replace("%status_label%", _("Status"), $str_mail);
// $str_mail = str_replace("%time_label%", _("Time"), $str_mail);
// $str_mail = str_replace("%manage_subscription%", _("Manage subscription"), $str_mail);
// $str_mail = str_replace("%unsubscribe%", _("Unsubscribe"), $str_mail);
// $str_mail = str_replace("%powered_by%", _("Powered by"), $str_mail);
//
// $subject = _('Status update from') . ' - ' . NAME . ' [ ' . $this->status . ' ]';
// $mailer->send_mail($userID, $subject, $str_mail);
// }
//
public function prepare_email(){
$Parsedown = new Parsedown();
$str_mail = file_get_contents("../libs/templates/email_status_update.html");
$str_mail = str_replace("%name%", NAME, $str_mail);
// $smtp_mail = str_replace("%email%", $userID, $smtp_mail);
@@ -127,8 +238,8 @@ class Notification
$str_mail = str_replace("%status%", $this->status, $str_mail);
$str_mail = str_replace("%time%", date("c", $this->time), $str_mail);
$str_mail = str_replace("%comment%", $Parsedown->setBreaksEnabled(true)->text($this->text), $str_mail);
$str_mail = str_replace("%token%", $token, $str_mail);
//$str_mail = str_replace("%token%", $token, $str_mail);
$str_mail = str_replace("%service_status_update_from%", _("Service status update from"), $str_mail);
$str_mail = str_replace("%services_impacted%", _("Service(s) Impacted"), $str_mail);
$str_mail = str_replace("%status_label%", _("Status"), $str_mail);
@@ -136,7 +247,18 @@ class Notification
$str_mail = str_replace("%manage_subscription%", _("Manage subscription"), $str_mail);
$str_mail = str_replace("%unsubscribe%", _("Unsubscribe"), $str_mail);
$str_mail = str_replace("%powered_by%", _("Powered by"), $str_mail);
$subject = _('Status update from') . ' - ' . NAME . ' [ ' . $this->status . ' ]';
$mailer->send_mail($userID, $subject, $str_mail);
$val = array();
$val['subject'] = $subject;
$val['body'] = $str_mail;
return $val;
}
}
public function prepare_telegram(){
$msg = _("Hi #s!\nThere is a status update for service(s): %s\nThe new status is: %s\nTitle: %s\n\n%s\n\n<a href='%s'>View online</a>");
$val['body'] = sprintf($msg, $this->servicenames, $this->status, $this->title, $this->text, WEB_URL);
return $val;
}
}