Skip to content

Commit

Permalink
[FIX] Fixed jmap quota display
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Oct 22, 2024
1 parent e61afbb commit cddcc84
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
38 changes: 30 additions & 8 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,20 +752,39 @@ public function process() {
$prefetched[] = $form['imap_server_id'];
$this->session->set('imap_prefetched_ids', array_unique($prefetched, SORT_STRING));
}

$with_subscription = isset($this->request->post['subscription_state']) && $this->request->post['subscription_state'];
$cache = Hm_IMAP_List::get_cache($this->cache, $form['imap_server_id']);
$imap = Hm_IMAP_List::connect($form['imap_server_id'], $cache);
$this->out('can_share_folders', stripos($imap->get_capability(), 'ACL') !== false);
if (imap_authed($imap)) {
$quota_root = $imap->get_quota_root($folder ? $folder : 'INBOX');
if ($quota_root && isset($quota_root[0]['name'])) {
$quota = $imap->get_quota($quota_root[0]['name']);
if ($quota) {
$current = floatval($quota[0]['current']);
$max = floatval($quota[0]['max']);
if ($max > 0) {
$this->out('quota', ceil(($current / $max) * 100));
$this->out('quota_max', $max / 1024);
if($imap instanceof Hm_JMAP) {
if (!empty($quota_root["methodResponses"][0][1]["list"])) {
$quota = $quota_root["methodResponses"][0][1]["list"][0];

if ($quota) {
$used = floatval($quota['used']);
$max = floatval($quota['hardLimit']);
if ($max > 0) {
$quotaPercentage = ceil(($used / $max) * 100);
$quotaMaxInMB = $max / (1024 * 1024);

$this->out('quota', $quotaPercentage);
$this->out('quota_max', $quotaMaxInMB);
}
}
}
}else {
if ($quota_root && isset($quota_root[0]['name'])) {
$quota = $imap->get_quota($quota_root[0]['name']);
if ($quota) {
$current = floatval($quota[0]['current']);
$max = floatval($quota[0]['max']);
if ($max > 0) {
$this->out('quota', ceil(($current / $max) * 100));
$this->out('quota_max', $max / 1024);
}
}
}
}
Expand All @@ -778,12 +797,15 @@ public function process() {
$this->out('folder', $folder);
return;
}

if (imap_authed($imap)) {
$only_subscribed = $this->user_config->get('only_subscribed_folders_setting', false);
if ($with_subscription) {
$only_subscribed = false;
}

$msgs = $imap->get_folder_list_by_level(hex2bin($folder), $only_subscribed, $with_subscription);

if (isset($msgs[$folder])) {
unset($msgs[$folder]);
}
Expand Down
28 changes: 26 additions & 2 deletions modules/imap/hm-jmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class Hm_JMAP {
);
private $default_caps = array(
'urn:ietf:params:jmap:core',
'urn:ietf:params:jmap:mail'
'urn:ietf:params:jmap:mail',
'urn:ietf:params:jmap:quota'
);

public $selected_mailbox;
Expand Down Expand Up @@ -376,6 +377,28 @@ public function get_folder_list_by_level($level=false) {
return $this->parse_folder_list_by_level($level);
}

public function get_quota_root($mailbox) {
if (!is_array($this->session)) {
throw new Exception("Not authenticated. Please authenticate first.");
}
$methods = [
[
"Quota/get",
[
"accountId"=> (string)$this->account_id,
"name" => $this->session['username'],
"scope" => "folder",
"folder" => $mailbox
],
"0"
]
];
$response = $this->send_command($this->session['apiUrl'], $methods, 'POST');
return $response;
}
public function get_capability() {
//TODO: Implement
}
/**
* Return cached data
* @return array
Expand Down Expand Up @@ -452,7 +475,8 @@ public function get_folder_list() {
$methods = array(array(
'Mailbox/get',
array(
'accountId' => $this->account_id,
'accountId' => (string)$this->account_id,
"properties" => ["name", "role", "parentId", "totalEmails", "unreadEmails"],
'ids' => NULL
),
'fl'
Expand Down

0 comments on commit cddcc84

Please sign in to comment.