Skip to content

Commit

Permalink
ews: remove pending todo items, remove debugging info, return folder …
Browse files Browse the repository at this point in the history
…and item ids when certain operations are performed; fix snooze/unsnooze operations
  • Loading branch information
kroky committed Nov 29, 2024
1 parent 30bd1f1 commit f250f56
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
8 changes: 6 additions & 2 deletions modules/core/hm-mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ public function create_folder($folder, $parent = null) {
}
if ($this->is_imap()) {
$new_folder = prep_folder_name($this->connection, $folder, false, $parent);
return $this->connection->create_mailbox($new_folder);
if ($this->connection->create_mailbox($new_folder)) {
return $new_folder;
} else {
return false;
}
} else {
return $this->connection->create_folder($folder, $parent);
}
Expand Down Expand Up @@ -249,7 +253,7 @@ public function get_special_use_mailboxes($folder = false) {
* Get messages in a folder applying filters, sorting and pagination
* @return array - [total results found, results for a single page]
*/
public function get_messages($folder, $sort, $reverse, $flag_filter, $offset=0, $limit=0, $keyword=false, $trusted_senders=[], $include_preview = false) {
public function get_messages($folder, $sort, $reverse, $flag_filter, $offset=0, $limit=50, $keyword=false, $trusted_senders=[], $include_preview = false) {
if (! $this->select_folder($folder)) {
return;
}
Expand Down
7 changes: 5 additions & 2 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1335,8 +1335,11 @@ function snooze_message($mailbox, $msg_id, $folder, $snooze_tag) {
$res = false;
$snooze_folder = 'Snoozed';
if ($snooze_tag) {
if (!count($mailbox->get_folder_status($snooze_folder))) {
$mailbox->create_folder($snooze_folder);
$status = $mailbox->get_folder_status($snooze_folder);
if (! count($status)) {
$snooze_folder = $mailbox->create_folder($snooze_folder);
} else {
$snooze_folder = $status['id'];
}
if ($mailbox->store_message($snooze_folder, $msg)) {
$deleteResult = $mailbox->message_action($folder, 'DELETE', array($msg_id));
Expand Down
4 changes: 3 additions & 1 deletion modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,11 @@ public function process() {
$mailbox = Hm_IMAP_List::get_connected_mailbox($server_id, $this->cache);
if ($mailbox && $mailbox->authed()) {
$folder = 'Snoozed';
if (! count($mailbox->get_folder_status($folder))) {
$status = $mailbox->get_folder_status($folder);
if (! count($status)) {
continue;
}
$folder = $status['id'];
$ret = $mailbox->get_messages($folder, 'DATE', false, 'ALL');
foreach ($ret[1] as $msg) {
$msg_headers = $mailbox->get_message_headers($folder, $msg['uid']);
Expand Down
33 changes: 21 additions & 12 deletions modules/imap/hm-ews.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ public function get_folders($folder = null, $only_subscribed = false, $unsubscri
'basename' => $name,
'realname' => $name,
'namespace' => '',
// TODO - flags
'marked' => false,
'noselect' => false,
'marked' => false, // doesn't seem to be used anywhere but imap returns it
'noselect' => false, // all EWS folders are selectable
'can_have_kids' => true,
'has_kids' => $folder->get('childFolderCount') > 0,
'children' => $folder->get('childFolderCount'),
Expand Down Expand Up @@ -145,11 +144,18 @@ public function get_folder_status($folder) {
try {
if ($this->is_distinguished_folder($folder)) {
$folder = new Type\DistinguishedFolderIdType($folder);
} else {
$result = $this->api->getFolder($folder->toArray(true));
} elseif (base64_encode(base64_decode($folder, true)) === $folder) {
$folder = new Type\FolderIdType($folder);
$result = $this->api->getFolder($folder->toArray(true));
} else {
$result = $this->api->getFolderByDisplayName($folder, Enumeration\DistinguishedFolderIdNameType::MESSAGE_ROOT);
if (! $result) {
throw new Exception('Folder not found.');
}
}
$result = $this->api->getFolder($folder->toArray(true));
return [
'id' => $result->get('folderId')->get('id'),
'name' => $result->get('displayName'),
'messages' => $result->get('totalCount'),
'uidvalidity' => false,
Expand All @@ -173,9 +179,14 @@ public function create_folder($folder, $parent = null) {
$parent = new Type\FolderIdType($parent);
}
try {
$result = $this->api->createFolders([$folder], $parent);
print_r($result);
exit;
$request = [
'Folders' => ['Folder' => [
'DisplayName' => $folder
]],
'ParentFolderId' => $parent->toArray(true),
];
$result = $this->ews->CreateFolder($request);
return $result->get('id');
} catch(\Exception $e) {
Hm_Msgs::add('ERR' . $e->getMessage());
return false;
Expand Down Expand Up @@ -288,9 +299,7 @@ public function store_message($folder, $message, $seen = true, $draft = false) {
'MessageDisposition' => 'SaveOnly',
'SavedItemFolderId' => $folder->toArray(true),
]);
print_r($result);
exit;
return true;
return $result->get('id');
} catch (\Exception $e) {
Hm_Msgs::add('ERR' . $e->getMessage());
return false;
Expand Down Expand Up @@ -386,7 +395,7 @@ public function search($folder, $sort, $reverse, $flag_filter, $offset, $limit,
$qs[] = "Subject:($term[1])";
break;
default:
// TODO: check for other types of terms
// noop
}
}
} elseif (! empty($keyword)) {
Expand Down
1 change: 1 addition & 0 deletions modules/imap/hm-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ public function get_mailbox_status($mailbox, $args=array('UNSEEN', 'UIDVALIDITY'
if ($this->check_response($response, true)) {
$attributes = $this->parse_status_response($response);
$this->check_mailbox_state_change($attributes);
$attributes['id'] = $mailbox;
}
return $attributes;
}
Expand Down

0 comments on commit f250f56

Please sign in to comment.