fix(api-messages-intro): Take intro from HTML if possible ZMS-112 (#672)

* if message has html part, use intro from it

* move intro creration into a separate function
This commit is contained in:
NickOvt 2024-04-15 10:58:45 +03:00 committed by GitHub
parent 02a43c6330
commit 9d9fbd25c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,6 +16,7 @@ const parseDate = require('../imap-core/lib/parse-date');
const log = require('npmlog'); const log = require('npmlog');
const packageData = require('../package.json'); const packageData = require('../package.json');
const { SettingsHandler } = require('./settings-handler'); const { SettingsHandler } = require('./settings-handler');
const { htmlToText } = require('html-to-text');
// index only the following headers for SEARCH // 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']; const INDEXED_HEADERS = ['to', 'cc', 'subject', 'from', 'sender', 'reply-to', 'message-id', 'thread-index', 'list-id', 'delivered-to'];
@ -317,29 +318,7 @@ class MessageHandler {
? messageData.text ? messageData.text
: messageData.text.substr(0, consts.MAX_PLAINTEXT_CONTENT); : messageData.text.substr(0, consts.MAX_PLAINTEXT_CONTENT);
messageData.intro = messageData.text messageData.intro = this.createIntro(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 + '…';
}
} }
if (maildata.html && maildata.html.length) { if (maildata.html && maildata.html.length) {
@ -360,6 +339,9 @@ class MessageHandler {
return html; return html;
}) })
.filter(html => 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( this.users.collection('users').findOneAndUpdate(
@ -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) { encryptMessage(pubKey, raw, callback) {
this.encryptMessageAsync(pubKey, raw) this.encryptMessageAsync(pubKey, raw)
.then(res => callback(null, res)) .then(res => callback(null, res))