Skip to content

Commit

Permalink
[FIX]Telegram webhook notification: Set an configurable interval for …
Browse files Browse the repository at this point in the history
…notifications
  • Loading branch information
Baraka24 committed Jun 9, 2024
1 parent 391e40d commit c9792fe
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
14 changes: 14 additions & 0 deletions modules/core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,20 @@ function max_source_setting_callback($val) {
return $val;
}}

/**
* Sanitize a default value for interval webhook notifications
* @subpackage core/functions
* @param int $val request interval
* @return sanitized interval
*/
if (!hm_exists('interval_webhook_notification_setting_callback')) {
function interval_webhook_notification_setting_callback($val) {
if ($val < 1) {
return DEFAULT_INTERVAL_WEBHOOK_NOTIFICATION;
}
return $val;
}}

/**
* Save user settings from the session to permanent storage
* @subpackage core/functions
Expand Down
1 change: 1 addition & 0 deletions modules/core/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
define('MAX_PER_SOURCE', 1000);
define('DEFAULT_PER_SOURCE', 20);
define('DEFAULT_MAX_GOOGLE_CONTACTS_NUMBER', 500);
define('DEFAULT_INTERVAL_WEBHOOK_NOTIFICATION', 5);
define('DEFAULT_SINCE', '-1 week');
define('DEFAULT_SEARCH_FLD', 'TEXT');

Expand Down
27 changes: 27 additions & 0 deletions modules/core/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,32 @@ var reset_default_value_input = function() {
}
};

var reset_default_value_input_interval_webhook_notification = function() {
let field = this.parentElement.parentElement.firstChild;
const defaultValue = this.getAttribute("default-value");

if (this.style.transform == "scaleX(1)") {
this.style.transform = "scaleX(-1)";
this.parentElement.setAttribute("restore_aria_label",hm_trans("Restore default value"))
field.value = field.getAttribute("current_value");
field.style.backgroundColor = "#fff";
field.style.pointerEvents = "auto";
field.style.touchAction = "auto";
}
else {
this.style.transform = "scaleX(1)";
this.parentElement.setAttribute("restore_aria_label",hm_trans("Restore current value"));
field.setAttribute("current_value", field.value);
field.value = 5;
if(defaultValue) {
field.value = defaultValue;
}
field.style.backgroundColor = "#eee";
field.style.pointerEvents = "none";
field.style.touchAction = "none";
}
};

var decrease_servers = function(section) {
const element = document.querySelector(`.server_count .${section}_server_count`);
const value = parseInt(element.textContent);
Expand Down Expand Up @@ -1961,6 +1987,7 @@ $(function() {
$('.reset_default_value_select').on("click", reset_default_value_select);
$('.reset_default_value_input').on("click", reset_default_value_input);
$('.reset_default_value_input_webhook_token').on("click", reset_default_value_input_webhook_token);
$('.reset_default_value_input_interval_webhook_notification').on("click", reset_default_value_input_interval_webhook_notification);
}

if (hm_check_dirty_flag()) {
Expand Down
22 changes: 20 additions & 2 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,26 @@ public function process() {
}
}

/**
* Process input from the interval webhook notification setting in the settings page
* @subpackage imap/handler
*/
class Hm_Handler_process_interval_webhook_notification_setting extends Hm_Handler_Module {
/**
* Allowed values are greater than zero
*/
public function process() {
process_site_setting('interval_webhook_notification', $this, 'interval_webhook_notification_setting_callback', DEFAULT_INTERVAL_WEBHOOK_NOTIFICATION);
}
}

/**
* Process input from the webhook token setting in the settings page
* @subpackage imap/handler
*/
class Hm_Handler_process_webhook_token_setting extends Hm_Handler_Module {
/**
* Allowed values are greater than zero and less than MAX_PER_SOURCE
* Allowed values for webhook token
*/
public function process() {
function webhook_token_callback($val) { return $val; }
Expand Down Expand Up @@ -1316,10 +1333,11 @@ public function process() {
$this->out('imap_server_ids', $form['imap_server_ids']);

$webhook_token = $this->user_config->get('webhook_token_setting');
$interval_webhook_notification = $this->user_config->get('interval_webhook_notification_setting');
$msg_count = count($msg_list);
$email_to = $msg_list[0]['to'];
if ($msg_count > 0) {
$interval = 5 * 60;
$interval = $interval_webhook_notification * 60;
set_time_limit(0);
while (true) {
if($webhook_token && !empty($webhook_token)) {
Expand Down
25 changes: 25 additions & 0 deletions modules/imap/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,31 @@ protected function output() {
}
}

/**
* Option to set the interval webhook notification for the settings page
* @subpackage imap/output
*/
class Hm_Output_interval_webhook_notification_setting extends Hm_Output_Module {
protected function output() {
$settings = $this->get('user_settings', array());
$per_page = 5;
$reset = '';
if (array_key_exists('interval_webhook_notification', $settings)) {
$per_page = $settings['interval_webhook_notification'];
}
if ($per_page != 5) {
$reset = '<span class="tooltip_restore" restore_aria_label="Restore default value"><i class="bi bi-arrow-repeat refresh_list reset_default_value_input_interval_webhook_notification"></i></span>';
}
return '<tr class="general_setting"><td><label for="interval_webhook_notification">'.
$this->trans('Interval webhook notification(minutes)').'</label></td><td><input class="form-control form-control-sm w-auto" type="text" id="interval_webhook_notification" '.
'name="interval_webhook_notification" value="'.$this->html_safe($per_page).'" />'.$reset.'</td></tr>';
}
}

/**
* Option to set the webhook token for the settings page
* @subpackage imap/output
*/
class Hm_Output_webhook_token_setting extends Hm_Output_Module {
protected function output() {
$settings = $this->get('user_settings', array());
Expand Down
3 changes: 3 additions & 0 deletions modules/imap/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
add_handler('settings', 'process_unread_on_open', true, 'imap', 'date', 'after');
add_handler('settings', 'process_imap_per_page_setting', true, 'imap', 'date', 'after');
add_handler('settings', 'process_webhook_token_setting', true, 'imap', 'save_user_settings', 'before');
add_handler('settings', 'process_interval_webhook_notification_setting', true, 'imap', 'date', 'after');
add_handler('settings', 'process_max_google_contacts_number', true, 'imap', 'date', 'after');
add_handler('settings', 'process_review_sent_email_setting', true, 'imap', 'date', 'after');
add_handler('settings', 'process_auto_advance_email_setting', true, 'imap', 'date', 'after');
Expand All @@ -53,6 +54,7 @@
add_output('settings', 'imap_unread_on_open', true, 'imap', 'imap_msg_icons_setting', 'after');
add_output('settings', 'imap_per_page_setting', true, 'imap', 'imap_pagination_links', 'after');
add_output('settings', 'webhook_token_setting', true, 'imap', 'imap_per_page_setting', 'after');
add_output('settings', 'interval_webhook_notification_setting', true, 'imap', 'webhook_token_setting', 'after');
add_output('settings', 'enable_simple_download_options', true, 'imap', 'imap_per_page_setting', 'after');
add_output('settings', 'max_google_contacts_number', true, 'imap', 'imap_per_page_setting', 'after');
add_output('settings', 'review_sent_email', true, 'imap', 'imap_pagination_links', 'after');
Expand Down Expand Up @@ -420,5 +422,6 @@
'imap_snooze_until' => FILTER_DEFAULT,
'auto_advance_email' => FILTER_VALIDATE_BOOLEAN,
'webhook_token' => FILTER_DEFAULT,
'interval_webhook_notification' => FILTER_VALIDATE_INT,
)
);

0 comments on commit c9792fe

Please sign in to comment.