Skip to content

Commit

Permalink
fix(api-messages-intro): Take intro from HTML if possible ZMS-112 (#672)
Browse files Browse the repository at this point in the history
* if message has html part, use intro from it

* move intro creration into a separate function
  • Loading branch information
NickOvt committed Apr 15, 2024
1 parent 02a43c6 commit 9d9fbd2
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions lib/message-handler.js
Expand Up @@ -16,6 +16,7 @@ const parseDate = require('../imap-core/lib/parse-date');
const log = require('npmlog');
const packageData = require('../package.json');
const { SettingsHandler } = require('./settings-handler');
const { htmlToText } = require('html-to-text');

// index only the following headers for SEARCH
const INDEXED_HEADERS = ['to', 'cc', 'subject', 'from', 'sender', 'reply-to', 'message-id', 'thread-index', 'list-id', 'delivered-to'];
Expand Down Expand Up @@ -317,29 +318,7 @@ class MessageHandler {
? messageData.text
: messageData.text.substr(0, consts.MAX_PLAINTEXT_CONTENT);

messageData.intro = messageData.text
// assume we get the intro text from first 2 kB
.substr(0, 2 * 1024)
// remove markdown urls
.replace(/\[[^\]]*\]/g, ' ')
// remove quoted parts
// "> quote from previous message"
.replace(/^>.*$/gm, '')
// remove lines with repetetive chars
// "---------------------"
.replace(/^\s*(.)\1+\s*$/gm, '')
// join lines
.replace(/\s+/g, ' ')
.trim();

if (messageData.intro.length > 128) {
let intro = messageData.intro.substr(0, 128);
let lastSp = intro.lastIndexOf(' ');
if (lastSp > 0) {
intro = intro.substr(0, lastSp);
}
messageData.intro = intro + '…';
}
messageData.intro = this.createIntro(messageData.text);
}

if (maildata.html && maildata.html.length) {
Expand All @@ -360,6 +339,9 @@ class MessageHandler {
return html;
})
.filter(html => html);

// if message has HTML content use it instead of text/plain content for intro
messageData.intro = this.createIntro(htmlToText(messageData.html.join('')));
}

this.users.collection('users').findOneAndUpdate(
Expand Down Expand Up @@ -1680,6 +1662,35 @@ class MessageHandler {
);
}

createIntro(text) {
// regexes
let intro = text
// assume we get the intro text from first 2 kB
.substr(0, 2 * 1024)
// remove markdown urls
.replace(/\[[^\]]*\]/g, ' ')
// remove quoted parts
// "> quote from previous message"
.replace(/^>.*$/gm, '')
// remove lines with repetetive chars
// "---------------------"
.replace(/^\s*(.)\1+\s*$/gm, '')
// join lines
.replace(/\s+/g, ' ')
.trim();

if (intro.length > 128) {
intro = intro.substr(0, 128);
let lastSp = intro.lastIndexOf(' ');
if (lastSp > 0) {
intro = intro.substr(0, lastSp);
}
intro = intro + '…';
}

return intro;
}

encryptMessage(pubKey, raw, callback) {
this.encryptMessageAsync(pubKey, raw)
.then(res => callback(null, res))
Expand Down

0 comments on commit 9d9fbd2

Please sign in to comment.