mu-store: use Mu::MessageContact
Use the new contact class. And a lot of whitespace cleanups.
This commit is contained in:
112
lib/mu-store.cc
112
lib/mu-store.cc
@ -37,6 +37,7 @@
|
||||
|
||||
#include "mu-message-flags.hh"
|
||||
#include "mu-msg-fields.h"
|
||||
#include "mu-msg.hh"
|
||||
#include "mu-store.hh"
|
||||
#include "mu-query.hh"
|
||||
#include "utils/mu-str.h"
|
||||
@ -956,79 +957,63 @@ add_terms_values(MuMsgFieldId mfid, MsgDoc* msgdoc)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const std::string&
|
||||
xapian_pfx(MuMsgContact* contact)
|
||||
xapian_pfx(const MessageContact& contact)
|
||||
{
|
||||
static const std::string empty;
|
||||
|
||||
/* use ptr to string to prevent copy... */
|
||||
switch (contact->type) {
|
||||
case MU_MSG_CONTACT_TYPE_TO:
|
||||
switch (contact.type) {
|
||||
case MessageContact::Type::To:
|
||||
return prefix(MU_MSG_FIELD_ID_TO);
|
||||
case MU_MSG_CONTACT_TYPE_FROM:
|
||||
case MessageContact::Type::From:
|
||||
return prefix(MU_MSG_FIELD_ID_FROM);
|
||||
case MU_MSG_CONTACT_TYPE_CC:
|
||||
case MessageContact::Type::Cc:
|
||||
return prefix(MU_MSG_FIELD_ID_CC);
|
||||
case MU_MSG_CONTACT_TYPE_BCC:
|
||||
case MessageContact::Type::Bcc:
|
||||
return prefix(MU_MSG_FIELD_ID_BCC);
|
||||
default:
|
||||
g_warning("unsupported contact type %u", (unsigned)contact->type);
|
||||
default: /* REPLY_TO not supported */
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
add_address_subfields(Xapian::Document& doc, const char* addr, const std::string& pfx)
|
||||
add_contacts_terms_values(Xapian::Document& doc, MuMsg *msg,
|
||||
Contacts& contacts_store)
|
||||
{
|
||||
const char *at, *domain_part;
|
||||
char* name_part;
|
||||
|
||||
/* add "foo" and "bar.com" as terms as well for
|
||||
* "foo@bar.com" */
|
||||
if (G_UNLIKELY(!(at = (g_strstr_len(addr, -1, "@")))))
|
||||
return;
|
||||
|
||||
name_part = g_strndup(addr, at - addr); // foo
|
||||
domain_part = at + 1;
|
||||
|
||||
add_term(doc, pfx + name_part);
|
||||
add_term(doc, pfx + domain_part);
|
||||
|
||||
g_free(name_part);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
each_contact_info(MuMsgContact* contact, MsgDoc* msgdoc)
|
||||
{
|
||||
/* for now, don't store reply-to addresses */
|
||||
if (mu_msg_contact_type(contact) == MU_MSG_CONTACT_TYPE_REPLY_TO)
|
||||
return TRUE;
|
||||
|
||||
const std::string pfx(xapian_pfx(contact));
|
||||
if (pfx.empty())
|
||||
return TRUE; /* unsupported contact type */
|
||||
|
||||
if (!mu_str_is_empty(contact->name)) {
|
||||
Xapian::TermGenerator termgen;
|
||||
termgen.set_document(*msgdoc->_doc);
|
||||
const auto flat = Mu::utf8_flatten(contact->name);
|
||||
termgen.set_document(doc);
|
||||
|
||||
for (auto&& contact: mu_msg_get_contacts(msg)) {
|
||||
|
||||
const std::string pfx{xapian_pfx(contact)};
|
||||
if (pfx.empty())
|
||||
continue; // not supported
|
||||
|
||||
if (!contact.name.empty()) {
|
||||
const auto flat = Mu::utf8_flatten(contact.name.c_str());
|
||||
termgen.index_text(flat, 1, pfx);
|
||||
}
|
||||
|
||||
if (!mu_str_is_empty(contact->email)) {
|
||||
const auto flat = Mu::utf8_flatten(contact->email);
|
||||
add_term(*msgdoc->_doc, pfx + flat);
|
||||
add_address_subfields(*msgdoc->_doc, contact->email, pfx);
|
||||
/* store it also in our contacts cache */
|
||||
auto& contacts{msgdoc->_priv->contacts_};
|
||||
contacts.add(Mu::ContactInfo(contact->full_address,
|
||||
contact->email,
|
||||
contact->name ? contact->name : "",
|
||||
msgdoc->_personal,
|
||||
mu_msg_get_date(msgdoc->_msg)));
|
||||
add_term(doc, pfx + contact.email);
|
||||
|
||||
// index name / domain separately, too.
|
||||
if (auto at = contact.email.find('@'); at != std::string::npos) {
|
||||
add_term(doc, pfx + contact.email.substr(0, at));
|
||||
add_term(doc, pfx + contact.email.substr(at));
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
termgen.index_text_without_positions(contact.email, 1, pfx);
|
||||
|
||||
/* and add to the contact store.*/
|
||||
contacts_store.add(ContactInfo{
|
||||
contact.display_name(),
|
||||
contact.email,
|
||||
contact.name,
|
||||
contacts_store.is_personal(contact.email),
|
||||
contact.message_date});
|
||||
}
|
||||
}
|
||||
|
||||
Xapian::Document
|
||||
@ -1037,29 +1022,8 @@ Store::Private::new_doc_from_message(MuMsg* msg)
|
||||
Xapian::Document doc;
|
||||
MsgDoc docinfo = {&doc, msg, this, 0, NULL};
|
||||
|
||||
add_contacts_terms_values(doc, msg, contacts_);
|
||||
mu_msg_field_foreach((MuMsgFieldForeachFunc)add_terms_values, &docinfo);
|
||||
|
||||
mu_msg_contact_foreach(
|
||||
msg,
|
||||
[](auto contact, gpointer msgdocptr) -> gboolean {
|
||||
auto msgdoc{reinterpret_cast<MsgDoc*>(msgdocptr)};
|
||||
|
||||
if (!contact->email)
|
||||
return FALSE; // invalid contact
|
||||
else if (msgdoc->_personal)
|
||||
return TRUE; // already deemed personal
|
||||
|
||||
if (msgdoc->_priv->contacts_.is_personal(contact->email))
|
||||
msgdoc->_personal = true; // this one's personal.
|
||||
|
||||
return TRUE;
|
||||
},
|
||||
&docinfo);
|
||||
|
||||
/* also store the contact-info as separate terms, and add it
|
||||
* to the cache */
|
||||
mu_msg_contact_foreach(msg, (MuMsgContactForeachFunc)each_contact_info, &docinfo);
|
||||
|
||||
// g_printerr ("\n--%s\n--\n", doc.serialise().c_str());
|
||||
|
||||
return doc;
|
||||
|
||||
Reference in New Issue
Block a user