1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00

Merge pull request #1413 from profanity-im/feature/mam-addtodb-nodup

MAM: Only add each message once to database
This commit is contained in:
Michael Vetter 2020-07-24 10:53:03 +02:00 committed by GitHub
commit 3e7776a9fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 10 deletions

View File

@ -110,8 +110,8 @@ log_database_init(ProfAccount* account)
// message is the message text
// timestamp the timestamp like "2020/03/24 11:12:14"
// type is there to distinguish: message (chat), MUC message (muc), muc pm (mucpm)
// stanza_id is the ID from XEP-0359: Unique and Stable Stanza IDs
// archive_id is the ID from XEP-0313: Message Archive Management
// stanza_id is the ID in <message>
// archive_id is the stanza-id from from XEP-0359: Unique and Stable Stanza IDs used for XEP-0313: Message Archive Management
// replace_id is the ID from XEP-0308: Last Message Correction
// encryption is to distinguish: none, omemo, otr, pgp
// marked_read is 0/1 whether a message has been marked as read via XEP-0333: Chat Markers
@ -335,7 +335,7 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
char* escaped_message = str_replace(message->plain, "'", "''");
if (asprintf(&query, "INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `replace_id`, `type`, `encryption`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
if (asprintf(&query, "INSERT INTO `ChatLogs` (`from_jid`, `from_resource`, `to_jid`, `to_resource`, `message`, `timestamp`, `stanza_id`, `archive_id`, `replace_id`, `type`, `encryption`) SELECT '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' WHERE NOT EXISTS (SELECT 1 FROM `ChatLogs` WHERE `archive_id` = '%s')",
from_jid->barejid,
from_jid->resourcepart ? from_jid->resourcepart : "",
to_jid->barejid,
@ -343,9 +343,11 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
escaped_message,
date_fmt,
message->id ? message->id : "",
message->stanzaid ? message->stanzaid : "",
message->replace_id ? message->replace_id : "",
type,
enc)
enc,
message->stanzaid)
== -1) {
log_error("log_database_add(): SQL query. could not allocate memory");
return;

View File

@ -85,7 +85,7 @@ static void _handle_muc_private_message(xmpp_stanza_t* const stanza);
static void _handle_conference(xmpp_stanza_t* const stanza);
static void _handle_captcha(xmpp_stanza_t* const stanza);
static void _handle_receipt_received(xmpp_stanza_t* const stanza);
static void _handle_chat(xmpp_stanza_t* const stanza, gboolean is_mam, gboolean is_carbon);
static void _handle_chat(xmpp_stanza_t* const stanza, gboolean is_mam, gboolean is_carbon, const char *result_id);
static void _handle_ox_chat(xmpp_stanza_t* const stanza, ProfMessage* message, gboolean is_mam);
static xmpp_stanza_t* _handle_carbons(xmpp_stanza_t* const stanza);
static void _send_message_stanza(xmpp_stanza_t* const stanza);
@ -232,7 +232,7 @@ _message_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* con
}
if (msg_stanza) {
_handle_chat(msg_stanza, FALSE, is_carbon);
_handle_chat(msg_stanza, FALSE, is_carbon, NULL);
}
} else {
// none of the allowed types
@ -281,6 +281,7 @@ message_init(void)
message->to_jid = NULL;
message->id = NULL;
message->originid = NULL;
message->stanzaid = NULL;
message->replace_id = NULL;
message->body = NULL;
message->encrypted = NULL;
@ -313,6 +314,10 @@ message_free(ProfMessage* message)
xmpp_free(ctx, message->originid);
}
if (message->stanzaid) {
xmpp_free(ctx, message->stanzaid);
}
if (message->replace_id) {
xmpp_free(ctx, message->replace_id);
}
@ -1217,7 +1222,7 @@ _handle_carbons(xmpp_stanza_t* const stanza)
}
static void
_handle_chat(xmpp_stanza_t* const stanza, gboolean is_mam, gboolean is_carbon)
_handle_chat(xmpp_stanza_t* const stanza, gboolean is_mam, gboolean is_carbon, const char *result_id)
{
// some clients send the mucuser namespace with private messages
// if the namespace exists, and the stanza contains a body element, assume its a private message
@ -1280,6 +1285,26 @@ _handle_chat(xmpp_stanza_t* const stanza, gboolean is_mam, gboolean is_carbon)
message->id = strdup(id);
}
if (is_mam) {
// MAM has XEP-0359 stanza-id as <result id="">
if (result_id) {
message->stanzaid = strdup(result_id);
} else {
log_warning("MAM received with no result id");
}
} else {
// live messages use XEP-0359 <stanza-id>
// TODO: add to muc too
char* stanzaid = NULL;
xmpp_stanza_t* stanzaidst = stanza_get_child_by_name_and_ns(stanza, STANZA_NAME_STANZA_ID, STANZA_NS_STABLE_ID);
if (stanzaidst) {
stanzaid = (char*)xmpp_stanza_get_attribute(stanzaidst, STANZA_ATTR_ID);
if (stanzaid) {
message->stanzaid = strdup(stanzaid);
}
}
}
// replace id for XEP-0308: Last Message Correction
xmpp_stanza_t* replace_id_stanza = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_LAST_MESSAGE_CORRECTION);
if (replace_id_stanza) {
@ -1379,7 +1404,7 @@ _handle_ox_chat(xmpp_stanza_t* const stanza, ProfMessage* message, gboolean is_m
static gboolean
_handle_mam(xmpp_stanza_t* const stanza)
{
xmpp_stanza_t* result = stanza_get_child_by_name_and_ns(stanza, "result", STANZA_NS_MAM2);
xmpp_stanza_t* result = stanza_get_child_by_name_and_ns(stanza, STANZA_NAME_RESULT, STANZA_NS_MAM2);
if (!result) {
return FALSE;
}
@ -1390,8 +1415,12 @@ _handle_mam(xmpp_stanza_t* const stanza)
return FALSE;
}
// <result xmlns='urn:xmpp:mam:2' queryid='f27' id='5d398-28273-f7382'>
// same as <stanza-id> from XEP-0359 for live messages
const char* result_id = xmpp_stanza_get_id(result);
xmpp_stanza_t* message_stanza = xmpp_stanza_get_child_by_ns(forwarded, "jabber:client");
_handle_chat(message_stanza, TRUE, FALSE);
_handle_chat(message_stanza, TRUE, FALSE, result_id);
return TRUE;
}

View File

@ -110,6 +110,8 @@
#define STANZA_NAME_COMMAND "command"
#define STANZA_NAME_CONFIGURE "configure"
#define STANZA_NAME_ORIGIN_ID "origin-id"
#define STANZA_NAME_STANZA_ID "stanza-id"
#define STANZA_NAME_RESULT "result"
#define STANZA_NAME_MINIMIZE "minimize"
// error conditions

View File

@ -155,7 +155,10 @@ typedef struct prof_message_t
char* originid;
/* <replace id> XEP-0308 LMC */
char* replace_id;
/* for MAM we will need archive_id (stanza-id in XEP-0359) (see database.c) */
/* stanza-id from XEP 0359. Used for MAM. archive_id in our database (see database.c)
* coming in as <stanza-id> for live messages
* coming in as <result id=""> for MAM messages*/
char *stanzaid;
/* The raw body from xmpp message, either plaintext or OTR encrypted text */
char* body;
/* The encrypted message as for PGP */