From 455b7264486d35278b3f8da7784cc26be7accbb9 Mon Sep 17 00:00:00 2001 From: Daniel Gruno Date: Sun, 7 Jan 2018 17:34:56 +0100 Subject: [PATCH] Do less aggressive subject nesting This addresses #427 by only allowing sort-by-subject to happen if we can establish that an email is a reply. --- site/api/stats.lua | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/site/api/stats.lua b/site/api/stats.lua index b9c913f7..b766584b 100644 --- a/site/api/stats.lua +++ b/site/api/stats.lua @@ -41,6 +41,12 @@ end -- findSubject: match an email with an earlier one with the same topic -- used for orphaned emails local function findSubject(gblob, blob, subject, epoch, maxAge) + -- If subject does not start with Re:, Fwd:, AW: etc, we + -- should not assume it's a reply. If we can't find via IRT etc + -- then just disregard it as a new thread. + if not subject:match("^[A-Za-z]+:") then + return nil + end local subj = subject:gsub("^[A-Za-z]:%s+", "") for k, v in pairs(blob) do if v.subject and v.subject == subj and v.epoch < epoch and (not maxAge or (maxAge and v.epoch >= (epoch - (maxAge*86400)))) then @@ -619,12 +625,15 @@ function handle(r) end end - if not irt or irt == JSON.null or #irt == 0 then + -- If this is a reply to a previous email, try assuming the subject can + -- be used to find the parent. + if not irt or irt == JSON.null or #irt == 0 and email.subject:match("^[A-Za-z]+:") then irt = email.subject:gsub("^[a-zA-Z]+:%s+", "") end -- If we can't match by in-reply-to or references, match/group by subject, ignoring Re:/Fwd:/etc - if not emails[irt] then + -- Only try this (just as above) if we spot a reply. + if not emails[irt] and email.subject:match("^[A-Za-z]+:") then irt = email.subject:gsub("^[a-zA-Z]+:%s+", "") while irt:match("^[a-zA-Z]+:%s+") do irt = irt:gsub("^[a-zA-Z]+:%s+", "")