diff options
author | fiaxh <git@lightrise.org> | 2019-12-18 18:23:31 +0100 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2019-12-18 20:35:04 +0100 |
commit | 321c3529f3fedbd5eaa9cd619ee408fb683921ca (patch) | |
tree | 3743e39ee2a8225ed8d1c2bb1259e17b22a1fa47 /libdino/src | |
parent | 9fe8450571e49aaf376d07b379968c575bc0445c (diff) | |
download | dino-321c3529f3fedbd5eaa9cd619ee408fb683921ca.tar.gz dino-321c3529f3fedbd5eaa9cd619ee408fb683921ca.zip |
Add (partial) support for unique stanza ids (XEP-0359)
Diffstat (limited to 'libdino/src')
-rw-r--r-- | libdino/src/entity/message.vala | 7 | ||||
-rw-r--r-- | libdino/src/service/database.vala | 13 | ||||
-rw-r--r-- | libdino/src/service/message_processor.vala | 39 |
3 files changed, 49 insertions, 10 deletions
diff --git a/libdino/src/entity/message.vala b/libdino/src/entity/message.vala index 9a25ac0c..4b16d12c 100644 --- a/libdino/src/entity/message.vala +++ b/libdino/src/entity/message.vala @@ -40,6 +40,7 @@ public class Message : Object { public Type type_ { get; set; default = Type.UNKNOWN; } public string? body { get; set; } public string? stanza_id { get; set; } + public string? server_id { get; set; } public DateTime? time { get; set; } /** UTC **/ public DateTime? local_time { get; set; } @@ -63,8 +64,9 @@ public class Message : Object { this.db = db; id = row[db.message.id]; - account = db.get_account_by_id(row[db.message.account_id]); // TODO don’t have to generate acc new + account = db.get_account_by_id(row[db.message.account_id]); stanza_id = row[db.message.stanza_id]; + server_id = row[db.message.server_id]; type_ = (Message.Type) row[db.message.type_]; counterpart = db.get_jid_by_id(row[db.message.counterpart_id]); @@ -108,6 +110,7 @@ public class Message : Object { .value(db.message.encryption, encryption) .value(db.message.marked, marked); if (stanza_id != null) builder.value(db.message.stanza_id, stanza_id); + if (server_id != null) builder.value(db.message.server_id, server_id); id = (int) builder.perform(); if (real_jid != null) { @@ -161,6 +164,8 @@ public class Message : Object { switch (sp.name) { case "stanza-id": update_builder.set(db.message.stanza_id, stanza_id); break; + case "server-id": + update_builder.set(db.message.server_id, server_id); break; case "counterpart": update_builder.set(db.message.counterpart_id, db.get_jid_id(counterpart)); update_builder.set(db.message.counterpart_resource, counterpart.resourcepart); break; diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index 60f1b52c..54854fd1 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -7,7 +7,7 @@ using Dino.Entities; namespace Dino { public class Database : Qlite.Database { - private const int VERSION = 9; + private const int VERSION = 10; public class AccountTable : Table { public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; @@ -55,6 +55,7 @@ public class Database : Qlite.Database { public class MessageTable : Table { public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; public Column<string> stanza_id = new Column.Text("stanza_id"); + public Column<string> server_id = new Column.Text("server_id") { min_version=10 }; public Column<int> account_id = new Column.Integer("account_id") { not_null = true }; public Column<int> counterpart_id = new Column.Integer("counterpart_id") { not_null = true }; public Column<string> counterpart_resource = new Column.Text("counterpart_resource"); @@ -69,7 +70,7 @@ public class Database : Qlite.Database { internal MessageTable(Database db) { base(db, "message"); - init({id, stanza_id, account_id, counterpart_id, our_resource, counterpart_resource, direction, + init({id, stanza_id, server_id, account_id, counterpart_id, our_resource, counterpart_resource, direction, type_, time, local_time, body, encryption, marked}); // get latest messages @@ -405,6 +406,14 @@ public class Database : Qlite.Database { return builder.count() > 0; } + public bool contains_message_by_server_id(Account account, Jid counterpart, string server_id) { + QueryBuilder builder = message.select() + .with(message.server_id, "=", server_id) + .with(message.counterpart_id, "=", get_jid_id(counterpart)) + .with(message.account_id, "=", account.id); + return builder.count() > 0; + } + public Message? get_message_by_id(int id) { Row? row = message.row_with(message.id, id).inner; if (row != null) { diff --git a/libdino/src/service/message_processor.vala b/libdino/src/service/message_processor.vala index 13278ee5..604578b5 100644 --- a/libdino/src/service/message_processor.vala +++ b/libdino/src/service/message_processor.vala @@ -91,7 +91,7 @@ public class MessageProcessor : StreamInteractionModule, Object { public async Entities.Message parse_message_stanza(Account account, Xmpp.MessageStanza message) { Entities.Message new_message = new Entities.Message(message.body); new_message.account = account; - new_message.stanza_id = message.id; + new_message.stanza_id = Xep.UniqueStableStanzaIDs.get_origin_id(message) ?? message.id; Jid? counterpart_override = null; if (message.from.equals(stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(message.from.bare_jid, account))) { @@ -105,7 +105,17 @@ public class MessageProcessor : StreamInteractionModule, Object { new_message.counterpart = counterpart_override ?? (new_message.direction == Entities.Message.DIRECTION_SENT ? message.to : message.from); new_message.ourpart = new_message.direction == Entities.Message.DIRECTION_SENT ? message.from : message.to; + XmppStream? stream = stream_interactor.get_stream(account); Xep.MessageArchiveManagement.MessageFlag? mam_message_flag = Xep.MessageArchiveManagement.MessageFlag.get_flag(message); + Xep.MessageArchiveManagement.Flag? mam_flag = stream != null ? stream.get_flag(Xep.MessageArchiveManagement.Flag.IDENTITY) : null; + if (mam_message_flag != null && mam_flag != null && mam_flag.ns_ver == Xep.MessageArchiveManagement.NS_URI && mam_message_flag.mam_id != null) { + new_message.server_id = mam_message_flag.mam_id; + } else if (message.type_ == Xmpp.MessageStanza.TYPE_GROUPCHAT) { + new_message.server_id = Xep.UniqueStableStanzaIDs.get_stanza_id(message, new_message.counterpart.bare_jid); + } else if (message.type_ == Xmpp.MessageStanza.TYPE_CHAT) { + new_message.server_id = Xep.UniqueStableStanzaIDs.get_stanza_id(message, account.bare_jid); + } + if (mam_message_flag != null) new_message.local_time = mam_message_flag.server_time; if (new_message.local_time == null || new_message.local_time.compare(new DateTime.now_utc()) > 0) new_message.local_time = new DateTime.now_utc(); @@ -167,11 +177,17 @@ public class MessageProcessor : StreamInteractionModule, Object { } public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { - bool is_uuid = message.stanza_id != null && Regex.match_simple("""[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}""", message.stanza_id); - bool new_uuid_msg = is_uuid && !db.contains_message_by_stanza_id(message, conversation.account); - bool new_misc_msg = !is_uuid && !db.contains_message(message, conversation.account); - bool new_msg = new_uuid_msg || new_misc_msg; - return !new_msg; + if (message.server_id != null) { + return db.contains_message_by_server_id(conversation.account, message.counterpart, message.server_id); + } else if (message.stanza_id != null) { + bool is_uuid = Regex.match_simple("""[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}""", message.stanza_id); + if (is_uuid) { + return db.contains_message_by_stanza_id(message, conversation.account); + } else { + return db.contains_message(message, conversation.account); + } + } + return false; } } @@ -268,8 +284,17 @@ public class MessageProcessor : StreamInteractionModule, Object { if (delayed) { Xmpp.Xep.DelayedDelivery.Module.set_message_delay(new_message, message.time); } + + // Set an origin ID if a MUC doen't guarantee to keep IDs + if (conversation.type_ == Conversation.Type.GROUPCHAT) { + Xep.Muc.Flag? flag = stream.get_flag(Xep.Muc.Flag.IDENTITY); + if (flag == null) return; + if(!flag.has_room_feature(conversation.counterpart, Xep.Muc.Feature.STABLE_ID)) { + Xep.UniqueStableStanzaIDs.set_origin_id(new_message, message.stanza_id); + } + } + stream.get_module(Xmpp.MessageModule.IDENTITY).send_message(stream, new_message); - message.stanza_id = new_message.id; } else { message.marked = Entities.Message.Marked.UNSENT; } |