Skip to content

Commit

Permalink
feat(other): add Junk button when Junk folder is configured for an IM…
Browse files Browse the repository at this point in the history
…AP Server
  • Loading branch information
Baraka24 authored and christer77 committed Jan 7, 2025
1 parent b6ca24d commit d913a2b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 71 deletions.
4 changes: 3 additions & 1 deletion modules/core/message_list_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,16 @@ function message_controls($output_mod) {
'<li><a class="dropdown-item msg_unflag core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="unflag">'.$output_mod->trans('Unflag').'</a></li>'.
'<li><a class="dropdown-item msg_delete core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="delete">'.$output_mod->trans('Delete').'</a></li>'.
'<li><a class="dropdown-item msg_archive core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="archive">'.$output_mod->trans('Archive').'</a></li>'.
'<li><a class="dropdown-item msg_junk core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="junk">'.$output_mod->trans('Junk').'</a></li>'.
'</ul>'.
'</div>'.
'<a class="msg_read core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="read">'.$output_mod->trans('Read').'</a>'.
'<a class="msg_unread core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="unread">'.$output_mod->trans('Unread').'</a>'.
'<a class="msg_flag core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="flag">'.$output_mod->trans('Flag').'</a>'.
'<a class="msg_unflag core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="unflag">'.$output_mod->trans('Unflag').'</a>'.
'<a class="msg_delete core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="delete">'.$output_mod->trans('Delete').'</a>'.
'<a class="msg_archive core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="archive">'.$output_mod->trans('Archive').'</a>';
'<a class="msg_archive core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="archive">'.$output_mod->trans('Archive').'</a>'.
'<a class="msg_junk core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="junk">'.$output_mod->trans('Junk').'</a>';

if ($output_mod->get('msg_controls_extra')) {
$res .= $output_mod->get('msg_controls_extra');
Expand Down
121 changes: 57 additions & 64 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1113,90 +1113,36 @@ public function process() {
*/
class Hm_Handler_imap_message_action extends Hm_Handler_Module {
/**
* Read, unread, delete, flag, or unflag a set of message uids
* Read, unread, delete, flag, unflag, archive, or mark as junk a set of message uids
*/
public function process() {
list($success, $form) = $this->process_form(array('action_type', 'message_ids'));
if ($success) {
if (in_array($form['action_type'], array('delete', 'read', 'unread', 'flag', 'unflag', 'archive'))) {
if (in_array($form['action_type'], array('delete', 'read', 'unread', 'flag', 'unflag', 'archive', 'junk'))) {
$ids = process_imap_message_ids($form['message_ids']);
$errs = 0;
$msgs = 0;
$moved = array();
$status = array();
foreach ($ids as $server => $folders) {
$specials = get_special_folders($this, $server);
$trash_folder = false;
$archive_folder = false;
$mailbox = Hm_IMAP_List::get_connected_mailbox($server, $this->cache);
if ($mailbox && $mailbox->authed()) {
$server_details = $this->user_config->get('imap_servers')[$server];
if ($form['action_type'] == 'delete') {
if (array_key_exists('trash', $specials)) {
if ($specials['trash']) {
$trash_folder = $specials['trash'];
} elseif ($mailbox->is_imap()) {
Hm_Msgs::add(sprintf('ERRNo trash folder configured for %s', $server_details['name']));
}
}
}
if ($form['action_type'] == 'archive') {
if(array_key_exists('archive', $specials)) {
if($specials['archive']) {
$archive_folder = $specials['archive'];
} elseif ($mailbox->is_imap()) {
Hm_Msgs::add(sprintf('ERRNo archive folder configured for %s', $server_details['name']));
}
}
}

foreach ($folders as $folder => $uids) {
$status['imap_'.$server.'_'.$folder] = $imap->folder_state;

if ($mailbox->is_imap() && $form['action_type'] == 'delete' && $trash_folder && $trash_folder != hex2bin($folder)) {
if (! $mailbox->message_action(hex2bin($folder), 'MOVE', $uids, $trash_folder)['status']) {
$errs++;
}
else {
foreach ($uids as $uid) {
$moved[] = sprintf("imap_%s_%s_%s", $server, $uid, $folder);
}
}
}
elseif ($mailbox->is_imap() && $form['action_type'] == 'archive' && $archive_folder && $archive_folder != hex2bin($folder)) {
/* path according to original option setting */
if ($this->user_config->get('original_folder_setting', false)) {
$archive_folder .= '/' . hex2bin($folder);
$dest_path_exists = count($mailbox->get_folder_status($archive_folder));
if (!$dest_path_exists) {
$mailbox->create_folder($archive_folder);
}
}
if (! $mailbox->message_action(hex2bin($folder), 'MOVE', $uids, $archive_folder)['status']) {
$errs++;
}
else {
foreach ($uids as $uid) {
$moved[] = sprintf("imap_%s_%s_%s", $server, $uid, $folder);
}
}
}
else {
if (! $mailbox->message_action(hex2bin($folder), mb_strtoupper($form['action_type']), $uids)['status']) {
$errs++;
}
else {
$msgs += count($uids);
if ($form['action_type'] == 'delete') {
$mailbox->message_action(hex2bin($folder), 'EXPUNGE', $uids);
}
}
$status['imap_'.$server.'_'.$folder] = $mailbox->get_folder_state();
$action_result = $this->perform_action($mailbox, $form['action_type'], $uids, $folder, $specials, $server_details);
if ($action_result['error']) {
$errs++;
} else {
$msgs += count($uids);
$moved = array_merge($moved, $action_result['moved']);
}
}
}
}
if ($errs > 0) {
Hm_Msgs::add(sprintf('ERRAn error occurred trying to %s some messages!', $form['action_type'], $server));
Hm_Msgs::add(sprintf('ERRAn error occurred trying to %s some messages!', $form['action_type']));
}
$this->out('move_count', $moved);
if (count($status) > 0) {
Expand All @@ -1205,6 +1151,53 @@ public function process() {
}
}
}

private function perform_action($mailbox, $action_type, $uids, $folder, $specials, $server_details) {
$error = false;
$moved = array();
$folder_name = hex2bin($folder);
$special_folder = $this->get_special_folder($action_type, $specials, $server_details);

if ($special_folder && $special_folder != $folder_name) {
if ($this->user_config->get('original_folder_setting', false)) {
$special_folder .= '/' . $folder_name;
if (!count($mailbox->get_folder_status($special_folder))) {
$mailbox->create_folder($special_folder);
}
}
if (!$mailbox->message_action($folder_name, 'MOVE', $uids, $special_folder)['status']) {
$error = true;
} else {
foreach ($uids as $uid) {
$moved[] = sprintf("imap_%s_%s_%s", $server_details['id'], $uid, $folder);
}
}
} else {
if (!$mailbox->message_action($folder_name, mb_strtoupper($action_type), $uids)['status']) {
$error = true;
} else {
if ($action_type == 'delete') {
$mailbox->message_action($folder_name, 'EXPUNGE', $uids);
}
}
}
return ['error' => $error, 'moved' => $moved];
}

private function get_special_folder($action_type, $specials, $server_details) {
$folder = false;
if ($action_type == 'delete' && array_key_exists('trash', $specials)) {
$folder = $specials['trash'];
} elseif ($action_type == 'archive' && array_key_exists('archive', $specials)) {
$folder = $specials['archive'];
} elseif ($action_type == 'junk' && array_key_exists('junk', $specials)) {
$folder = $specials['junk'];
}
if (!$folder && $action_type != 'read' && $action_type != 'unread' && $action_type != 'flag' && $action_type != 'unflag') {
Hm_Msgs::add(sprintf('ERRNo %s folder configured for %s', $action_type, $server_details['name']));
}
return $folder;
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions modules/imap/hm-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,9 @@ public function message_action($action, $uids, $mailbox=false, $keyword=false) {
case 'ARCHIVE':
$command = "UID STORE $uid_string +FLAGS (\Archive)\r\n";
break;
case 'JUNK':
$command = "UID STORE $uid_string +FLAGS (\Junk)\r\n";
break;
case 'FLAG':
$command = "UID STORE $uid_string +FLAGS (\Flagged)\r\n";
break;
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/modules/core/message_list_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function test_icon_callback() {
*/
public function test_message_controls() {
$mod = new Hm_Output_Test(array('msg_controls_extra' => 'foo', 'foo' => 'bar', 'bar' => 'foo'), array('bar'));
$this->assertEquals('<a class="toggle_link" href="#"><i class="bi bi-check-square-fill"></i></a><div class="msg_controls fs-6 d-none gap-1 align-items-center"><div class="dropdown on_mobile"><button type="button" class="btn btn-outline-success btn-sm dropdown-toggle" id="coreMsgControlDropdown" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">Actions</button><ul class="dropdown-menu" aria-labelledby="coreMsgControlDropdown"><li><a class="dropdown-item msg_read core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="read">Read</a></li><li><a class="dropdown-item msg_unread core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="unread">Unread</a></li><li><a class="dropdown-item msg_flag core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="flag">Flag</a></li><li><a class="dropdown-item msg_unflag core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="unflag">Unflag</a></li><li><a class="dropdown-item msg_delete core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="delete">Delete</a></li><li><a class="dropdown-item msg_archive core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="archive">Archive</a></li></ul></div><a class="msg_read core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="read">Read</a><a class="msg_unread core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="unread">Unread</a><a class="msg_flag core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="flag">Flag</a><a class="msg_unflag core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="unflag">Unflag</a><a class="msg_delete core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="delete">Delete</a><a class="msg_archive core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="archive">Archive</a>foo</div>', message_controls($mod));
$this->assertEquals('<a class="toggle_link" href="#"><i class="bi bi-check-square-fill"></i></a><div class="msg_controls fs-6 d-none gap-1 align-items-center"><div class="dropdown on_mobile"><button type="button" class="btn btn-outline-success btn-sm dropdown-toggle" id="coreMsgControlDropdown" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">Actions</button><ul class="dropdown-menu" aria-labelledby="coreMsgControlDropdown"><li><a class="dropdown-item msg_read core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="read">Read</a></li><li><a class="dropdown-item msg_unread core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="unread">Unread</a></li><li><a class="dropdown-item msg_flag core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="flag">Flag</a></li><li><a class="dropdown-item msg_unflag core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="unflag">Unflag</a></li><li><a class="dropdown-item msg_delete core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="delete">Delete</a></li><li><a class="dropdown-item msg_archive core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="archive">Archive</a></li><li><a class="dropdown-item msg_junk core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="junk">Junk</a></li></ul></div><a class="msg_read core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="read">Read</a><a class="msg_unread core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="unread">Unread</a><a class="msg_flag core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="flag">Flag</a><a class="msg_unflag core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="unflag">Unflag</a><a class="msg_delete core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="delete">Delete</a><a class="msg_archive core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="archive">Archive</a><a class="msg_junk core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="junk">Junk</a>foo</div>', message_controls($mod));
}
/**
* @preserveGlobalState disabled
Expand All @@ -133,7 +133,7 @@ public function test_list_sources() {
*/
public function test_list_controls() {
$this->assertEquals('<div class="list_controls no_mobile d-flex gap-3 align-items-center">foobazbar</div>
<div class="list_controls on_mobile">
<div class="list_controls on_mobile">foobazbar
<i class="bi bi-filter-circle" onclick="listControlsMenu()"></i>
<div id="list_controls_menu" classs="list_controls_menu">foobazbar</div>
</div>', list_controls('foo', 'bar', 'baz'));
Expand Down
Loading

0 comments on commit d913a2b

Please sign in to comment.