Skip to content

Commit

Permalink
feat(backend): change Hm_IMAP_List to Hm_Mailbox
Browse files Browse the repository at this point in the history
  • Loading branch information
amaninyumu1 authored and josaphatim committed Jan 22, 2025
1 parent 9ac9f53 commit 95fdb6a
Show file tree
Hide file tree
Showing 8 changed files with 676 additions and 637 deletions.
897 changes: 454 additions & 443 deletions modules/core/site.js

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,80 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima
}
}

function getCombinedMessagesLists($sources, $cache, $searchTerms, $listPage, $limit, $offsets = [], $defaultOffset = 0, $filter = 'ALL') {
$totalMessages = 0;
$offset = $defaultOffset;
$messagesLists = [];
$status = [];
foreach ($sources as $index => $dataSource) {

if ($offsets && $listPage > 1) {
if (isset($offsets[$index]) && (int) $offsets[$index] > 0) {
$offset = (int) $offsets[$index] * ($listPage - 1);
}
}

$mailbox = Hm_IMAP_List::get_connected_mailbox($dataSource['id'], $cache);
if ($mailbox && $mailbox->authed()) {
$folder = $dataSource['folder'];
$state = $mailbox->get_connection()->get_mailbox_status(hex2bin($folder));
$status['imap_'.$dataSource['id'].'_'.$folder] = $state;

$uids = $mailbox->search(hex2bin($folder), $filter, false, $searchTerms);
$total = count($uids);
// most recent messages at the top
$uids = array_reverse($uids);
$uids = array_slice($uids, $offset, $limit);

$headers = $mailbox->get_message_list(hex2bin($folder), $uids);
$messages = [];
foreach ($uids as $uid) {
if (isset($headers[$uid])) {
$messages[] = $headers[$uid];
}
}

$messagesLists[] = array_map(function($msg) use ($dataSource, $folder) {
$msg['server_id'] = $dataSource['id'];
$msg['server_name'] = $dataSource['name'];
$msg['folder'] = $folder;
return $msg;
}, $messages);
$totalMessages += $total;
}
}

return ['lists' => $messagesLists, 'total' => $totalMessages, 'status' => $status];
}

function flattenMessagesLists($messagesLists, $listSize) {
$endList = [];
$sizesTaken = [];

$max = $listSize * count($messagesLists);

while (count($endList) < $listSize * count($messagesLists) && count(array_filter($messagesLists, fn ($list) => count($list) > 0)) > 0) {
foreach ($messagesLists as $index => $list) {
if (count($list) > 0) {
$part = array_slice($list, 0, $listSize);
$endList = array_merge($endList, $part);
$messagesLists[$index] = array_slice($list, $listSize);
$sizesTaken[$index] = isset($sizesTaken[$index]) ? $sizesTaken[$index] + count($part) : count($part);
$totalTakens = array_sum(array_values($sizesTaken));
if ($totalTakens > $max) {
$sizesTaken[$index] = $sizesTaken[$index] - ($totalTakens - $max);
}
} else {
$sizesTaken[$index] = isset($sizesTaken[$index]) ? $sizesTaken[$index] : 0;
}
}
}

$endList = array_slice($endList, 0, $max);

return ['messages' => $endList, 'offsets' => $sizesTaken];
}

if (!hm_exists('save_sent_msg')) {
function save_sent_msg($handler, $imap_id, $imap, $imap_details, $msg, $msg_id, $show_errors = true) {
$specials = get_special_folders($handler, $imap_id);
Expand Down
38 changes: 3 additions & 35 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,41 +332,9 @@ public function process() {
$sent_folder = false;
$mailbox = Hm_IMAP_List::get_connected_mailbox($imap_id, $this->cache);
if ($mailbox && $mailbox->authed()) {
$specials = get_special_folders($this, $imap_id);
if (array_key_exists('sent', $specials) && $specials['sent']) {
$sent_folder = $specials['sent'];
}

if (!$sent_folder) {
$auto_sent = $mailbox->get_special_use_mailboxes('sent');
if (!array_key_exists('sent', $auto_sent)) {
return;
}
$sent_folder = $auto_sent['sent'];
}
if (!$sent_folder) {
Hm_Debug::add(sprintf("Unable to save sent message, no sent folder for IMAP %s", $imap_details['server']));
}
if ($sent_folder) {
Hm_Debug::add(sprintf("Attempting to save sent message for IMAP server %s in folder %s", $imap_details['server'], $sent_folder));
if (! $mailbox->store_message($sent_folder, $msg)) {
Hm_Msgs::add('ERRAn error occurred saving the sent message');
}
$uid = null;
$mailbox_page = $mailbox->get_messages($sent_folder, 'ARRIVAL', true, 'ALL', 0, 10);
foreach ($mailbox_page[1] as $mail) {
$msg_header = $mailbox->get_message_headers($sent_folder, $mail['uid']);
if ($msg_header['Message-Id'] === $mime->get_headers()['Message-Id']) {
$uid = $mail['uid'];
break;
}
}
}
$uid = save_sent_msg($this, $imap_id, $imap, $imap_details, $msg, $mime->get_headers()['Message-Id']);
if ($uid) {
if ($uid && $this->user_config->get('review_sent_email_setting', false)) {
$this->out('redirect_url', '?page=message&uid='.$uid.'&list_path=imap_'.$imap_id.'_'.bin2hex($sent_folder));
}
$uid = save_sent_msg($this, $imap_id, $mailbox, $imap_details, $msg, $mime->get_headers()['Message-Id']);
if ($uid && $this->user_config->get('review_sent_email_setting', false)) {
$this->out('redirect_url', '?page=message&uid='.$uid.'&list_path=imap_'.$imap_id.'_'.bin2hex($sent_folder));
}
}
}
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 @@ -1074,6 +1074,7 @@ public function get_message_list($uids, $raw=false, $include_content_body = fals
'google_thread_id' => $google_thread_id, 'google_labels' => $google_labels, 'list_archive' => $list_archive,
'references' => $references, 'message_id' => $message_id, 'x_auto_bcc' => $x_auto_bcc,
'x_snoozed' => $x_snoozed, 'x_schedule' => $x_schedule, 'x_profile_id' => $x_profile_id, 'x_delivery' => $x_delivery);
$headers[$uid]['preview_msg'] = $flds['body'] != "content_body" ? $flds['body'] : "";

if ($raw) {
$headers[$uid] = array_map('trim', $headers[$uid]);
Expand Down
4 changes: 2 additions & 2 deletions modules/imap/js_modules/route_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function applyImapMessageListPageHandlers(routeParams) {
if (window.inlineMessageMessageListAndSearchPageHandler) inlineMessageMessageListAndSearchPageHandler(routeParams);
if (window.wpMessageListPageHandler) wpMessageListPageHandler(routeParams);

return async function () {
return async function() {
const [refreshIntervalId, abortController] = await setupPageResult;
abortController.abort();
clearInterval(refreshIntervalId);
Expand All @@ -37,4 +37,4 @@ function applyImapMessageContentPageHandlers(routeParams) {

if (window.pgpMessageContentPageHandler) pgpMessageContentPageHandler();
if (window.wpMessageContentPageHandler) wpMessageContentPageHandler(routeParams);
}
}
1 change: 0 additions & 1 deletion modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,6 @@ $(function() {
setTimeout(search_selected_for_imap, 100);
});


if (hm_is_logged()) {
imap_unsnooze_messages();
setInterval(imap_unsnooze_messages, 60000);
Expand Down
62 changes: 29 additions & 33 deletions modules/smtp/js_modules/route_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ function applySmtpComposePageHandlers() {
}

var interval = Hm_Utils.get_from_global('compose_save_interval', 30);
Hm_Timer.add_job(function () { save_compose_state(); }, interval, true);
$('.draft_title').on("click", function () { $('.draft_list').toggle(); });
$('.toggle_recipients').on("click", function () { return toggle_recip_flds(); });
Hm_Timer.add_job(function() { save_compose_state(); }, interval, true);
$('.draft_title').on("click", function() { $('.draft_list').toggle(); });
$('.toggle_recipients').on("click", function() { return toggle_recip_flds(); });
$('.smtp_reset').on("click", reset_smtp_form);
$('.delete_draft').on("click", function () { smtp_delete_draft($(this).data('id')); });
$('.smtp_save').on("click", function () { save_compose_state(false, true); });
$('.smtp_send_archive').on("click", function () { send_archive(false, true); });
$('.delete_draft').on("click", function() { smtp_delete_draft($(this).data('id')); });
$('.smtp_save').on("click", function() { save_compose_state(false, true); });
$('.smtp_send_archive').on("click", function() { send_archive(false, true); });

const modal = new Hm_Modal({
modalId: 'emptySubjectBodyModal',
Expand Down Expand Up @@ -95,7 +95,7 @@ function applySmtpComposePageHandlers() {
========================================
*/
function showModal() {
if (!modal.modalContent.html()) {
if (! modal.modalContent.html()) {
modal.addFooterBtn(hm_trans('Send anyway'), 'btn-warning', handleSendAnyway);
if (showBtnSendAnywayDontWarnFuture) {
modal.addFooterBtn(hm_trans("Send anyway and don't warn in the future"), 'btn-warning', handleSendAnywayAndDontWarnMe);
Expand All @@ -109,22 +109,24 @@ function applySmtpComposePageHandlers() {
return new Promise((resolve) => {
const checkValue = () => {
if ($(selector).val() !== targetValue) {
resolve();
resolve();
} else {
setTimeout(checkValue, 100);
setTimeout(checkValue, 100);
}
};
checkValue();
checkValue();
});
}

async function handleSendAnyway() {

if ($('.compose_draft_id').val() == '0') {
Hm_Notices.show([hm_trans('Please wait, sending message...')]);
await waitForValueChange('.compose_draft_id', '0');
Hm_Notices.show([hm_trans('Please wait, sending message...')]);
await waitForValueChange('.compose_draft_id', '0');
}



if (handleMissingAttachment()) {
if (isScheduledMode == null) {
document.getElementsByClassName("smtp_send")[0].click();
Expand Down Expand Up @@ -172,53 +174,53 @@ function applySmtpComposePageHandlers() {
return true;
}
});
$('.compose_form').on('submit', function () {
$('.compose_form').on('submit', function() {
process_compose_form();
});
if ($('.compose_cc').val() || $('.compose_bcc').val()) {
toggle_recip_flds();
}
if (window.location.href.search('&reply=1') !== -1 || window.location.href.search('&reply_all=1') !== -1) {
replace_cursor_positon($('textarea[name="compose_body"]'));
replace_cursor_positon ($('textarea[name="compose_body"]'));
}
if (window.location.href.search('&forward=1') !== -1) {
setTimeout(function () {
setTimeout(function() {
save_compose_state();
}, 100);
}
if ($('.sys_messages').text() != 'Message Sent') {
get_smtp_profile($('.compose_server').val());
}
$('.compose_server').on('change', function () {
$('.compose_server').on('change', function() {
get_smtp_profile($('.compose_server').val());
});
if ($('.compose_attach_button').attr('disabled') == 'disabled') {
if($('.compose_attach_button').attr('disabled') == 'disabled'){
check_attachment_dir_access();
};

$('.compose_container').attr('ondrop', 'move_recipient_to_section(event)').attr('ondragover', 'allow_drop(event)');
$('.compose_to, .compose_cc, .compose_bcc').on('keypress', function (e) {
if (e.which == 13) {
$('.compose_to, .compose_cc, .compose_bcc').on('keypress', function(e) {
if(e.which == 13) {
e.preventDefault();
text_to_bubbles(this);
}
});
$('.compose_to, .compose_cc, .compose_bcc').on('blur', function (e) {
$('.compose_to, .compose_cc, .compose_bcc').on('blur', function(e) {
e.preventDefault();
text_to_bubbles(this);
});
$('.compose_subject, .compose_body, .compose_server, .smtp_send_placeholder, .smtp_send_archive').on('focus', function (e) {
$('.compose_to, .compose_cc, .compose_bcc').each(function () {
$('.compose_subject, .compose_body, .compose_server, .smtp_send_placeholder, .smtp_send_archive').on('focus', function(e) {
$('.compose_to, .compose_cc, .compose_bcc').each(function() {
bubbles_to_text(this);
});
});
$('.compose_to, .compose_cc, .compose_bcc').on('focus', function (e) {
$('.compose_to, .compose_cc, .compose_bcc').on('focus', function(e) {
text_to_bubbles(this);
});
$('.compose_container').on('click', function () {
$('.compose_container').on('click', function() {
$(this).find('input').focus();
});
$(document).on('click', '.bubble_close', function (e) {
$(document).on('click', '.bubble_close', function(e) {
e.stopPropagation();
$(".bubble_dropdown-content").remove();
$(this).parent().remove();
Expand All @@ -232,7 +234,7 @@ function applySmtpComposePageHandlers() {
var excludedEmail = null;

const excludeEmail = function () {
var newRecipients = recipientsInput.val().split(',').filter(function (email) {
var newRecipients = recipientsInput.val().split(',').filter(function(email) {
if (email.includes(selectedEmail)) {
excludedEmail = email;
return false;
Expand All @@ -244,7 +246,7 @@ function applySmtpComposePageHandlers() {

if (recipientsInput.val().includes(selectedEmail)) {
excludeEmail();
$(document).on('change', '#compose_smtp_id', function () {
$(document).on('change', '#compose_smtp_id', function() {
if ($(this).val() !== selectedVal) {
if (!recipientsInput.val().includes(selectedEmail)) {
recipientsInput.val(recipientsInput.val() + ', ' + excludedEmail);
Expand All @@ -255,12 +257,6 @@ function applySmtpComposePageHandlers() {
});
}


$('.compose_to').on('keyup', function (e) { autocomplete_contact(e, '.compose_to', '#to_contacts'); });
$('.compose_cc').on('keyup', function (e) { autocomplete_contact(e, '.compose_cc', '#cc_contacts'); });
$('.compose_bcc').on('keyup', function (e) { autocomplete_contact(e, '.compose_bcc', '#bcc_contacts'); });
$('.compose_to').focus();

if (window.pgpComposePageHandler) pgpComposePageHandler();
if (window.profilesComposePageHandler) profilesComposePageHandler();
}
Loading

0 comments on commit 95fdb6a

Please sign in to comment.