Skip to content

Commit 26bc417

Browse files
committed
feat(backend): allow search flagged in all folders if enabled in settings
1 parent b7e02c9 commit 26bc417

File tree

9 files changed

+106
-21
lines changed

9 files changed

+106
-21
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ DEFAULT_SETTING_SHOW_LIST_ICONS=true
115115
DEFAULT_SETTING_START_PAGE=none
116116
DEFAULT_SETTING_DISABLE_DELETE_PROMPT=false
117117
DEFAULT_SETTING_FLAGGED_PER_SOURCE=20
118+
DEFAULT_SETTING_FLAGGED_SEARCH_IN_ALL_FOLDER=false
118119
DEFAULT_SETTING_NO_FOLDER_ICONS=false
119120
DEFAULT_SETTING_ALL_EMAIL_PER_SOURCE=20
120121
DEFAULT_SETTING_ALL_EMAIL_SINCE='-1 week'

config/app.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,12 @@
11951195
*/
11961196
'default_setting_flagged_per_source' => env('DEFAULT_SETTING_FLAGGED_PER_SOURCE', 20),
11971197

1198+
/*
1199+
| allow searching all in all folders
1200+
| Defaults to false
1201+
*/
1202+
'default_setting_flagged_search_in_all_folder' => env('DEFAULT_SETTING_FLAGGED_SEARCH_IN_ALL_FOLDER', false),
1203+
11981204
/*
11991205
|
12001206
| Per source time limit for the flagged combined view

lib/environment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function define_default_constants($config) {
4343
define('DEFAULT_UNREAD_PER_SOURCE', $config->get('default_setting_unread_per_source', 20));
4444
define('DEFAULT_FLAGGED_SINCE', $config->get('default_setting_flagged_since', '-1 week'));
4545
define('DEFAULT_FLAGGED_PER_SOURCE', $config->get('default_setting_flagged_per_source', 20));
46+
define('DEFAULT_FLAGGED_SEARCH_IN_ALL_FOLDERS', $config->get('default_setting_flagged_search_in_all_folder', false));
4647
define('DEFAULT_ALL_SINCE', $config->get('default_setting_all_since', '-1 week'));
4748
define('DEFAULT_ALL_PER_SOURCE', $config->get('default_setting_all_per_source', 20));
4849
define('DEFAULT_ALL_EMAIL_SINCE', $config->get('default_setting_all_email_since', '-1 week'));

modules/core/handler_modules.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,24 @@ function delete_disabled_callback($val) {
344344
}
345345
}
346346

347+
/**
348+
* Process input from the allow searching flagged in all folders setting
349+
* @subpackage core/handler
350+
*/
351+
class Hm_Handler_process_allow_search_all_flagged_folder_setting extends Hm_Handler_Module {
352+
/**
353+
* Allowed vals are bool true/false
354+
*/
355+
public function process() {
356+
function allow_search_all_flagged_folder_callback($val) {
357+
return $val;
358+
}
359+
// exit(var_dump($this->request->post['allow_search_all_flagged_folder']));
360+
process_site_setting('allow_search_all_flagged_folder', $this, 'allow_search_all_flagged_folder_callback', DEFAULT_FLAGGED_SEARCH_IN_ALL_FOLDERS, true);
361+
}
362+
}
363+
364+
347365
/**
348366
* Process input from the disable delete attachment setting
349367
* @subpackage core/handler

modules/core/js_modules/Hm_MessagesStore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class Hm_MessagesStore {
271271
if (this.path != 'combined_inbox' && this.path != 'search') {
272272
sources = sources.filter(s => s.type != 'feeds');
273273
}
274+
console.log('Sources:', sources);
274275
sources.forEach((ds) => {
275276
const cfg = config.slice();
276277
if (ds.type == 'feeds') {

modules/core/output_modules.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,34 @@ protected function output() {
10551055
}
10561056
}
10571057

1058+
/**
1059+
* Option for the "allow search in all flagged folders" setting
1060+
* This setting allows searching flagged messages in all folders, not just the INBOX one.
1061+
* @subpackage core/output
1062+
*/
1063+
class Hm_Output_allow_search_all_flagged_folder_setting extends Hm_Output_Module {
1064+
/**
1065+
* Processed by Hm_Handler_process_allow_search_all_flagged_folder_setting
1066+
*/
1067+
protected function output() {
1068+
$settings = $this->get('user_settings', array());
1069+
if (array_key_exists('allow_search_all_flagged_folder', $settings) && $settings['allow_search_all_flagged_folder']) {
1070+
$checked = ' checked="checked"';
1071+
if($settings['allow_search_all_flagged_folder'] !== DEFAULT_FLAGGED_SEARCH_IN_ALL_FOLDERS) {
1072+
$reset = '<span class="tooltip_restore" restore_aria_label="Restore default value"><i class="bi bi-arrow-counterclockwise fs-6 cursor-pointer refresh_list reset_default_value_checkbox"></i></span>';
1073+
}
1074+
}
1075+
else {
1076+
$checked = '';
1077+
$reset='';
1078+
}
1079+
return '<tr class="flagged_setting"><td><label class="form-check-label" for="allow_search_all_flagged_folder">'.
1080+
$this->trans('Allow searching flagged in all folders').'</label></td>'.
1081+
'<td><input class="form-check-input" type="checkbox" '.$checked.
1082+
' value="1" id="allow_search_all_flagged_folder" name="allow_search_all_flagged_folder" data-default-value="'.(DEFAULT_FLAGGED_SEARCH_IN_ALL_FOLDERS ? 'true' : 'false') . '"/>'.$reset.'</td></tr>';
1083+
}
1084+
}
1085+
10581086
/**
10591087
* Option for the "received since" date range for the Flagged page
10601088
* @subpackage core/output

modules/core/setup.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
add_handler('settings', 'save_user_settings', true, 'core', 'save_user_data', 'before');
6969
add_handler('settings', 'reload_folder_cookie', true, 'core', 'save_user_settings', 'after');
7070
add_handler('settings', 'privacy_settings', true, 'core', 'date', 'after');
71+
add_handler('settings', 'process_allow_search_all_flagged_folder_setting', true, 'core', 'save_user_settings', 'before');
7172

7273
add_output('settings', 'start_settings_form', true, 'core', 'content_section_start', 'after');
7374
add_output('settings', 'start_general_settings', true, 'core', 'start_settings_form', 'after');
@@ -108,6 +109,7 @@
108109
add_output('settings', 'all_email_source_max_setting', true, 'core', 'all_email_since_setting', 'after');
109110
add_output('settings', 'end_settings_form', true, 'core', 'content_section_end', 'before');
110111
add_output('settings', 'privacy_settings', 'true', 'core', 'start_unread_settings', 'before');
112+
add_output('settings', 'allow_search_all_flagged_folder_setting', true, 'core', 'flagged_source_max_setting', 'after');
111113

112114
/* message list page */
113115
setup_base_page('message_list');
@@ -300,6 +302,7 @@
300302
'language' => FILTER_UNSAFE_RAW,
301303
'flagged_per_source' => FILTER_VALIDATE_INT,
302304
'flagged_since' => FILTER_UNSAFE_RAW,
305+
'allow_search_all_flagged_folder' => FILTER_VALIDATE_INT,
303306
'unread_per_source' => FILTER_VALIDATE_INT,
304307
'unread_since' => FILTER_UNSAFE_RAW,
305308
'all_email_per_source' => FILTER_VALIDATE_INT,

modules/imap/handler_modules.php

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,8 @@ public function process() {
12801280
$folders = array_map(function($ds) { return $ds['folder']; }, $data_sources);
12811281
}
12821282

1283+
$allow_all_folders = false;
1284+
12831285
list($sort, $reverse) = process_sort_arg($this->request->get['sort'], $this->user_config->get('default_sort_order_setting', 'arrival'));
12841286

12851287
switch ($this->get('list_path')) {
@@ -1294,6 +1296,10 @@ public function process() {
12941296
$date = process_since_argument($this->user_config->get('all_since_setting', DEFAULT_SINCE));
12951297
break;
12961298
case 'flagged':
1299+
$allow_all_folders = (bool)$this->user_config->get('allow_search_all_flagged_folder_setting', DEFAULT_FLAGGED_SEARCH_IN_ALL_FOLDERS);
1300+
$filter = 'FLAGGED';
1301+
$date = process_since_argument($this->user_config->get('flagged_since_setting', DEFAULT_FLAGGED_SINCE));
1302+
$limit = $this->user_config->get('flagged_per_source_setting', DEFAULT_FLAGGED_PER_SOURCE);
12971303
case 'unread':
12981304
$filter = $this->get('list_path') == 'unread' ? 'UNSEEN' : mb_strtoupper($this->get('list_path'));
12991305
default:
@@ -1317,33 +1323,54 @@ public function process() {
13171323

13181324
$messages = [];
13191325
$status = [];
1326+
$uids = [];
13201327
foreach ($ids as $key => $id) {
13211328
$details = Hm_IMAP_List::dump($id);
13221329
$mailbox = Hm_IMAP_List::get_connected_mailbox($id, $this->cache);
13231330
if($this->get('list_path') == 'snoozed' && !$mailbox->folder_exists('Snoozed')) {
13241331
continue;
13251332
}
1326-
$uids = $mailbox->search(hex2bin($folders[$key]), $filter, $terms, $sort, $reverse);
1327-
1328-
$total = count($uids);
1329-
$uids = array_slice($uids, 0, $limit);
1330-
1331-
$headers = $mailbox->get_message_list(hex2bin($folders[$key]), $uids);
1332-
foreach ($uids as $uid) {
1333-
if (isset($headers[$uid])) {
1334-
$msg = $headers[$uid];
1335-
} elseif (isset($headers[bin2hex($uid)])) {
1336-
$msg = $headers[bin2hex($uid)];
1337-
} else {
1338-
continue;
1333+
if($allow_all_folders) {
1334+
$all_folders = $mailbox->get_folders();
1335+
foreach (array_keys($all_folders) as $folder) {
1336+
$uids = $mailbox->search($folder, $filter, $terms, $sort, $reverse);
1337+
$headers = $mailbox->get_message_list($folder, $uids);
1338+
$uids = array_slice($uids, 0, $limit);
1339+
foreach ($uids as $uid) {
1340+
if (isset($headers[$uid])) {
1341+
$msg = $headers[$uid];
1342+
} elseif (isset($headers[bin2hex($uid)])) {
1343+
$msg = $headers[bin2hex($uid)];
1344+
} else {
1345+
continue;
1346+
}
1347+
$msg['server_id'] = $id;
1348+
$msg['server_name'] = $details['name'];
1349+
$msg['folder'] = $folders[$key];
1350+
$messages[] = $msg;
1351+
}
1352+
$status['imap_'.$id.'_'.bin2hex($folder)] = $mailbox->get_folder_status($folder);
13391353
}
1340-
$msg['server_id'] = $id;
1341-
$msg['server_name'] = $details['name'];
1342-
$msg['folder'] = $folders[$key];
1343-
$messages[] = $msg;
1354+
}else {
1355+
$uids = $mailbox->search(hex2bin($folders[$key]), $filter, $terms, $sort, $reverse);
1356+
$headers = $mailbox->get_message_list(hex2bin($folders[$key]), $uids);
1357+
$uids = array_slice($uids, 0, $limit);
1358+
// $total = count($uids);
1359+
foreach ($uids as $uid) {
1360+
if (isset($headers[$uid])) {
1361+
$msg = $headers[$uid];
1362+
} elseif (isset($headers[bin2hex($uid)])) {
1363+
$msg = $headers[bin2hex($uid)];
1364+
} else {
1365+
continue;
1366+
}
1367+
$msg['server_id'] = $id;
1368+
$msg['server_name'] = $details['name'];
1369+
$msg['folder'] = $folders[$key];
1370+
$messages[] = $msg;
1371+
}
1372+
$status['imap_'.$id.'_'.$folders[$key]] = $mailbox->get_folder_status(hex2bin($folders[$key]));
13441373
}
1345-
1346-
$status['imap_'.$id.'_'.$folders[$key]] = $mailbox->get_folder_status(hex2bin($folders[$key]));
13471374
}
13481375

13491376
$this->out('folder_status', $status);

modules/imap/hm-imap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,8 +1307,8 @@ public function get_message_content($uid, $message_part, $max=false, $struct=tru
13071307
* use IMAP SEARCH or ESEARCH
13081308
* @param string $target message types to search. can be ALL, UNSEEN, ANSWERED, etc
13091309
* @param mixed $uids an array of uids or a valid IMAP sequence set as a string (or false for ALL)
1310-
* @param string $fld optional field to search
1311-
* @param string $term optional search term
1310+
* @param array $terms optional search term
1311+
* @param array $esearch optional ESEARCH parameters
13121312
* @param bool $exclude_deleted extra argument to exclude messages with the deleted flag
13131313
* @param bool $exclude_auto_bcc don't include auto-bcc'ed messages
13141314
* @param bool $only_auto_bcc only include auto-bcc'ed messages

0 commit comments

Comments
 (0)