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

[ENH]message list: Preview of message when viewing message list #1241

Merged
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
12 changes: 9 additions & 3 deletions modules/core/message_list_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,21 @@ function checkbox_callback($vals, $style, $output_mod) {
if (!hm_exists('subject_callback')) {
function subject_callback($vals, $style, $output_mod) {
$img = '';
if (count($vals) == 4 && $vals[3]) {
$subject = '';
$preview_msg = '';
if (isset($vals[3]) && $vals[3]) {
$img = '<i class="bi bi-filetype-'.$vals[3].'"></i>';
}
$subject = $output_mod->html_safe($vals[0]);
if (isset($vals[4]) && $vals[4]) {
$preview_msg = $output_mod->html_safe($vals[4]);
}

$hl_subject = preg_replace("/^(\[[^\]]+\])/", '<span class="s_pre">$1</span>', $subject);
if ($style == 'news') {
return sprintf('<div class="subject"><div class="%s" title="%s">%s <a href="%s">%s</a></div></div>', $output_mod->html_safe(implode(' ', $vals[2])), $subject, $img, $output_mod->html_safe($vals[1]), $hl_subject);
return sprintf('<div class="subject"><div class="%s" title="%s">%s <a href="%s">%s</a><p class="fw-light">%s</p></div></div>', $output_mod->html_safe(implode(' ', $vals[2])), $subject, $img, $output_mod->html_safe($vals[1]), $hl_subject, $preview_msg);
}
return sprintf('<td class="subject"><div class="%s"><a title="%s" href="%s">%s</a></div></td>', $output_mod->html_safe(implode(' ', $vals[2])), $subject, $output_mod->html_safe($vals[1]), $hl_subject);
return sprintf('<td class="subject"><div class="%s"><a title="%s" href="%s">%s</a><p class="fw-light">%s</p></div></td>', $output_mod->html_safe(implode(' ', $vals[2])), $subject, $output_mod->html_safe($vals[1]), $hl_subject, $preview_msg);
}}

/**
Expand Down
9 changes: 7 additions & 2 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false,
$msg['subject'] = '[No Subject]';
}
$subject = $msg['subject'];
$preview_msg = "";
if (isset($msg['preview_msg'])) {
$preview_msg = $msg['preview_msg'];
}

if ($parent_list == 'sent') {
$icon = 'sent';
$from = $msg['to'];
Expand Down Expand Up @@ -298,7 +303,7 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false,
$res[$id] = message_list_row(array(
array('checkbox_callback', $id),
array('icon_callback', $flags),
array('subject_callback', $subject, $url, $flags, $icon),
array('subject_callback', $subject, $url, $flags, $icon, $preview_msg),
array('safe_output_callback', 'source', $source),
array('safe_output_callback', 'from'.$nofrom, $from, null, str_replace(array($from, '<', '>'), '', $msg['from'])),
array('date_callback', $date, $timestamp),
Expand All @@ -314,7 +319,7 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false,
array('checkbox_callback', $id),
array('safe_output_callback', 'source', $source, $icon),
array('safe_output_callback', 'from'.$nofrom, $from, null, str_replace(array($from, '<', '>'), '', $msg['from'])),
array('subject_callback', $subject, $url, $flags),
array('subject_callback', $subject, $url, $flags, null, $preview_msg),
array('date_callback', $date, $timestamp, $is_snoozed),
array('icon_callback', $flags)
),
Expand Down
13 changes: 11 additions & 2 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ public function process() {
$offset = 0;
$msgs = array();
$list_page = 1;
$include_preview = $this->user_config->get('active_preview_message_setting', false);

list($success, $form) = $this->process_form(array('imap_server_id', 'folder'));
if ($success) {
Expand All @@ -847,9 +848,9 @@ public function process() {
$existingEmails = array_map(function($c){
return $c->value('email_address');
},$contact_list);
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit, $keyword, $existingEmails);
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit, $keyword, $existingEmails, $include_preview);
} else {
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit, $keyword);
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit, $keyword, null, $include_preview);
}
foreach ($results as $msg) {
$msg['server_id'] = $form['imap_server_id'];
Expand Down Expand Up @@ -2276,3 +2277,11 @@ function process_move_messages_in_screen_email_enabled_callback($val) { return $
process_site_setting('move_messages_in_screen_email', $this, 'process_move_messages_in_screen_email_enabled_callback', true, true);
}
}
class Hm_Handler_process_setting_active_preview_message extends Hm_Handler_Module {
public function process() {
function process_active_preview_message_callback($val) { return $val; }
process_site_setting('active_preview_message', $this, 'process_active_preview_message_callback', true, true);
}
}


30 changes: 25 additions & 5 deletions modules/imap/hm-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ public function poll() {
* @param bool $raw flag to disable decoding header values
* @return array list of headers and values for the specified uids
*/
public function get_message_list($uids, $raw=false) {
public function get_message_list($uids, $raw=false, $include_preview = false) {
christer77 marked this conversation as resolved.
Show resolved Hide resolved
if (is_array($uids)) {
sort($uids);
$sorted_string = implode(',', $uids);
Expand All @@ -890,7 +890,11 @@ public function get_message_list($uids, $raw=false) {
if ($this->is_supported( 'X-GM-EXT-1' )) {
$command .= 'X-GM-MSGID X-GM-THRID X-GM-LABELS ';
}
$command .= "BODY.PEEK[HEADER.FIELDS (SUBJECT X-AUTO-BCC FROM DATE CONTENT-TYPE X-PRIORITY TO LIST-ARCHIVE REFERENCES MESSAGE-ID X-SNOOZED)])\r\n";
$command .= "BODY.PEEK[HEADER.FIELDS (SUBJECT X-AUTO-BCC FROM DATE CONTENT-TYPE X-PRIORITY TO LIST-ARCHIVE REFERENCES MESSAGE-ID X-SNOOZED)]";
if ($include_preview) {
$command .= " BODY[0.1]";
}
$command .= ")\r\n";
$cache_command = $command.(string)$raw;
$cache = $this->check_cache($cache_command);
if ($cache !== false) {
Expand All @@ -901,8 +905,9 @@ public function get_message_list($uids, $raw=false) {
$status = $this->check_response($res, true);
$tags = array('X-GM-MSGID' => 'google_msg_id', 'X-GM-THRID' => 'google_thread_id', 'X-GM-LABELS' => 'google_labels', 'UID' => 'uid', 'FLAGS' => 'flags', 'RFC822.SIZE' => 'size', 'INTERNALDATE' => 'internal_date');
$junk = array('X-AUTO-BCC', 'MESSAGE-ID', 'REFERENCES', 'X-SNOOZED', 'LIST-ARCHIVE', 'SUBJECT', 'FROM', 'CONTENT-TYPE', 'TO', '(', ')', ']', 'X-PRIORITY', 'DATE');
$flds = array('x-auto-bcc' => 'x_auto_bcc', 'message-id' => 'message_id', 'references' => 'references', 'x-snoozed' => 'x_snoozed', 'list-archive' => 'list_archive', 'date' => 'date', 'from' => 'from', 'to' => 'to', 'subject' => 'subject', 'content-type' => 'content_type', 'x-priority' => 'x_priority');
$flds = array('x-auto-bcc' => 'x_auto_bcc', 'message-id' => 'message_id', 'references' => 'references', 'x-snoozed' => 'x_snoozed', 'list-archive' => 'list_archive', 'date' => 'date', 'from' => 'from', 'to' => 'to', 'subject' => 'subject', 'content-type' => 'content_type', 'x-priority' => 'x_priority', 'body' => 'content_body');
$headers = array();

foreach ($res as $n => $vals) {
if (isset($vals[0]) && $vals[0] == '*') {
$uid = 0;
Expand All @@ -926,6 +931,7 @@ public function get_message_list($uids, $raw=false) {
$count = count($vals);
for ($i=0;$i<$count;$i++) {
if ($vals[$i] == 'BODY[HEADER.FIELDS') {

$i++;
while(isset($vals[$i]) && in_array(mb_strtoupper($vals[$i]), $junk)) {
$i++;
Expand All @@ -943,6 +949,17 @@ public function get_message_list($uids, $raw=false) {
}
}
}
elseif ($vals[$i] == 'BODY[0.1') {
$content = '';
$i++;
$i++;
christer77 marked this conversation as resolved.
Show resolved Hide resolved
while(isset($vals[$i]) && $vals[$i] != ')') {
$content .= $vals[$i];
$i++;
}
$i++;
$flds['body'] = $content;
}
elseif (isset($tags[mb_strtoupper($vals[$i])])) {
if (isset($vals[($i + 1)])) {
if (($tags[mb_strtoupper($vals[$i])] == 'flags' || $tags[mb_strtoupper($vals[$i])] == 'google_labels' ) && $vals[$i + 1] == '(') {
Expand All @@ -960,6 +977,7 @@ public function get_message_list($uids, $raw=false) {
}
}
}

if ($uid) {
$cset = '';
if (mb_stristr($content_type, 'charset=')) {
Expand All @@ -973,6 +991,7 @@ public function get_message_list($uids, $raw=false) {
'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);
$headers[$uid]['preview_msg'] = $flds['body'] != "content_body" ? $flds['body'] : "";

if ($raw) {
$headers[$uid] = array_map('trim', $headers[$uid]);
Expand All @@ -981,6 +1000,7 @@ public function get_message_list($uids, $raw=false) {
$headers[$uid] = array_map(array($this, 'decode_fld'), $headers[$uid]);
}


}
}
}
Expand Down Expand Up @@ -2292,7 +2312,7 @@ public function get_first_message_part($uid, $type, $subtype=false, $struct=fals
* @return array list of headers
*/

public function get_mailbox_page($mailbox, $sort, $rev, $filter, $offset=0, $limit=0, $keyword=false, $trusted_senders=array()) {
public function get_mailbox_page($mailbox, $sort, $rev, $filter, $offset=0, $limit=0, $keyword=false, $trusted_senders=array(), $include_preview = false) {
$result = array();

/* select the mailbox if need be */
Expand Down Expand Up @@ -2332,7 +2352,7 @@ public function get_mailbox_page($mailbox, $sort, $rev, $filter, $offset=0, $lim

/* get the headers and build a result array by UID */
if (!empty($uids)) {
$headers = $this->get_message_list($uids);
$headers = $this->get_message_list($uids, false, $include_preview);
foreach($uids as $uid) {
if (isset($headers[$uid])) {
$result[$uid] = $headers[$uid];
Expand Down
15 changes: 15 additions & 0 deletions modules/imap/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1490,3 +1490,18 @@ protected function output() {
return $res;
}
}
class Hm_Output_setting_active_preview_message extends Hm_Output_Module {
protected function output() {
$settings = $this->get('user_settings', array());
$checked = "";
if (array_key_exists('active_preview_message', $settings) && $settings['active_preview_message']) {
if ($settings['active_preview_message']) {
$checked = "checked";
}
}
$res = '<tr class="general_setting"><td><label for="active_preview_message">'.
$this->trans('Active preview message').'</label></td><td><input class="form-check-input" type="checkbox" role="switch" id="active_preview_message" name="active_preview_message" '.$checked.' ></td></tr>';
return $res;
}
}

3 changes: 3 additions & 0 deletions modules/imap/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
add_handler('settings', 'process_auto_advance_email_setting', true, 'imap', 'date', 'after');
add_handler('settings', 'process_first_time_screen_emails_per_page_setting', true, 'imap', 'date', 'after');
add_handler('settings', 'process_setting_move_messages_in_screen_email', true, 'imap', 'process_first_time_screen_emails_per_page_setting', 'after');
add_handler('settings', 'process_setting_active_preview_message', true, 'imap', 'process_setting_move_messages_in_screen_email', 'after');
add_output('settings', 'imap_server_ids', true, 'imap', 'page_js', 'before');
add_output('settings', 'start_sent_settings', true, 'imap', 'end_settings_form', 'before');
add_output('settings', 'sent_since_setting', true, 'imap', 'start_sent_settings', 'after');
Expand All @@ -62,6 +63,7 @@
add_output('settings', 'imap_auto_advance_email', true, 'imap', 'imap_pagination_links', 'after');
add_output('settings', 'first_time_screen_emails_per_page_setting', true, 'imap', 'imap_auto_advance_email', 'after');
add_output('settings', 'setting_move_messages_in_screen_email', true, 'imap', 'first_time_screen_emails_per_page_setting', 'after');
add_output('settings', 'setting_active_preview_message', true, 'imap', 'setting_move_messages_in_screen_email', 'after');

/* compose page data */
add_output('compose', 'imap_server_ids', true, 'imap', 'page_js', 'before');
Expand Down Expand Up @@ -451,5 +453,6 @@
'identifier' => FILTER_DEFAULT,
'permissions' => FILTER_DEFAULT,
'action' => FILTER_DEFAULT,
'active_preview_message' => FILTER_VALIDATE_BOOLEAN,
)
);
2 changes: 1 addition & 1 deletion modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ var add_email_in_contact_trusted = function(list_email) {
}
);
}
};
};

$('.screen-email-unlike').on("click", function() { imap_screen_email(); return false; });

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 @@ -80,8 +80,8 @@ public function test_checkbox_callback() {
*/
public function test_subject_callback() {
$mod = new Hm_Output_Test(array('foo' => 'bar', 'bar' => 'foo'), array('bar'));
$this->assertEquals('<td class="subject"><div class=""><a title="foo" href="bar">foo</a></div></td>', subject_callback(array('foo', 'bar', array()), 'email', $mod));
$this->assertEquals('<div class="subject"><div class="" title="foo"><i class="bi bi-filetype-code"></i> <a href="bar">foo</a></div></div>', subject_callback(array('foo', 'bar', array(), 'code'), 'news', $mod));
$this->assertEquals('<td class="subject"><div class=""><a title="foo" href="bar">foo</a><p class="fw-light"></p></div></td>', subject_callback(array('foo', 'bar', array()), 'email', $mod));
$this->assertEquals('<div class="subject"><div class="" title="foo"><i class="bi bi-filetype-code"></i> <a href="bar">foo</a><p class="fw-light"></p></div></div>', subject_callback(array('foo', 'bar', array(), 'code'), 'news', $mod));
}
/**
* @preserveGlobalState disabled
Expand Down