about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-07-23 15:19:13 +0200
committerMichael Vetter <jubalh@iodoru.org>2020-07-23 15:23:18 +0200
commitc9b154b1a24bd3e96142ae826851538f2ef725d7 (patch)
treeca2a4acc6aa9442e22b039e9dbc84b63494c3f41 /src
parent8852db03d623f1920814fc8ff2c822a6353dd6e3 (diff)
downloadprofani-tty-c9b154b1a24bd3e96142ae826851538f2ef725d7.tar.gz
database: Only insert if there is no entry with same archive_id
archive_is is <stanza-id> or <result id=""> and should identify one
message stable and uniquely.

See XEP-0359: Unique and Stable Stanza IDs.

We need this for example for this situation:
* we go online with Profanity
* we fetch all messages since yesterday
* we add them to the db
* we go offline
* we go online with Profanity
* we fetch all messages since yesterday
* we only want to add the new ones

So far we don't ask MAM "give me all since last 'id'" but since a
certain date.

In case no archive_id will be set, it will be `(null)` and thus should
be inserted anyways because it won't find a value with (null) in that
row.

Because when adding we use `message->stanzaid ? message->stanzaid : "",`
so it will be empty in such a case.

Regards MAM: https://github.com/profanity-im/profanity/issues/660
Regards Stable IDs: https://github.com/profanity-im/profanity/issues/1207
Diffstat (limited to 'src')
-rw-r--r--src/database.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/database.c b/src/database.c
index 45112d50..8424b398 100644
--- a/src/database.c
+++ b/src/database.c
@@ -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;