Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(other): add Junk button when Junk folder is configured for an IMAP Server #1420

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/core/message_list_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,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 @@ -1108,90 +1108,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 @@ -1200,6 +1146,53 @@ public function process() {
}
}
}

private function perform_action($mailbox, $action_type, $uids, $folder, $specials, $server_details) {
Baraka24 marked this conversation as resolved.
Show resolved Hide resolved
$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']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to check what happens in mailbox down the road - besides IMAP, it is passing this method to EWS if that's the account type and I am not sure EWS understands the Junk action unless you code it...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out! I'll look into implementing the JUNK action for the EWS side. If I run into any issues, I'll reach out for your help.

$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
36 changes: 36 additions & 0 deletions modules/imap/hm-ews.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ public function message_action($action, $itemIds, $folder=false, $keyword=false)
$status = false;
$responses = [];
switch ($action) {
case 'JUNK' :
$status = $this->move_items_to_junk($itemIds);
break;
case 'ARCHIVE':
$status = $this->archive_items($itemIds);
break;
Expand Down Expand Up @@ -971,6 +974,39 @@ protected function archive_items($itemIds) {
return $result;
}

protected function move_items_to_junk($itemIds) {
$result = true;
$folders = $this->get_parent_folders_of_items($itemIds);
foreach ($folders as $folder => $itemIds) {
if ($this->is_distinguished_folder($folder)) {
$folder = new Type\DistinguishedFolderIdType($folder);
} else {
$folder = new Type\FolderIdType($folder);
}

$junkFolder = new Type\DistinguishedFolderIdType(Type\DistinguishedFolderIdType::JUNK);
$request = [
'SourceFolderId' => $folder->toArray(true),
'DestinationFolderId' => $junkFolder->toArray(true),
'ItemIds' => [
'ItemId' => $itemIds = array_map(function($itemId) {
return (new Type\ItemIdType($itemId))->toArray();
}, $itemIds),
]
];

$request = Type::buildFromArray($request);

try {
$result = $result && $this->ews->MoveItem($request);
} catch (\Exception $e) {
Hm_Msgs::add('ERR' . $e->getMessage());
$result = false;
}
}
return $result;
}

protected function delete_items($itemIds, $hard = false) {
$result = true;
try {
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 @@ -1843,6 +1843,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
2 changes: 1 addition & 1 deletion 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 Down
Loading
Loading