diff options
author | fiaxh <git@lightrise.org> | 2024-07-29 13:16:54 +0200 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2024-07-29 13:16:54 +0200 |
commit | b0ff90a14a5d127e17f2371f87e7bb659de3a68f (patch) | |
tree | a4b44a2837f94bef433adc318eb9a738425a494f /libdino/src | |
parent | ceb921a0148f7fdc2a9df3e6b85143bf8c26c341 (diff) | |
download | dino-b0ff90a14a5d127e17f2371f87e7bb659de3a68f.tar.gz dino-b0ff90a14a5d127e17f2371f87e7bb659de3a68f.zip |
Add initial message markup (XEP-0394) support
Diffstat (limited to 'libdino/src')
-rw-r--r-- | libdino/src/entity/message.vala | 52 | ||||
-rw-r--r-- | libdino/src/service/message_correction.vala | 28 | ||||
-rw-r--r-- | libdino/src/service/message_processor.vala | 34 | ||||
-rw-r--r-- | libdino/src/util/send_message.vala | 56 |
4 files changed, 134 insertions, 36 deletions
diff --git a/libdino/src/entity/message.vala b/libdino/src/entity/message.vala index 4e6c7f45..e5aad25f 100644 --- a/libdino/src/entity/message.vala +++ b/libdino/src/entity/message.vala @@ -70,6 +70,7 @@ public class Message : Object { public int quoted_item_id { get; private set; default=0; } private Gee.List<Xep.FallbackIndication.Fallback> fallbacks = null; + private Gee.List<Xep.MessageMarkup.Span> markups = null; private Database? db; @@ -160,16 +161,53 @@ public class Message : Object { public Gee.List<Xep.FallbackIndication.Fallback> get_fallbacks() { if (fallbacks != null) return fallbacks; + fetch_body_meta(); + return fallbacks; + } + + public Gee.List<Xep.MessageMarkup.Span> get_markups() { + if (markups != null) return markups; + fetch_body_meta(); + + return markups; + } + + public void persist_markups(Gee.List<Xep.MessageMarkup.Span> markups, int message_id) { + this.markups = markups; + + foreach (var span in markups) { + foreach (var ty in span.types) { + db.body_meta.insert() + .value(db.body_meta.info_type, Xep.MessageMarkup.NS_URI) + .value(db.body_meta.message_id, message_id) + .value(db.body_meta.info, Xep.MessageMarkup.span_type_to_str(ty)) + .value(db.body_meta.from_char, span.start_char) + .value(db.body_meta.to_char, span.end_char) + .perform(); + } + } + } + + private void fetch_body_meta() { var fallbacks_by_ns = new HashMap<string, ArrayList<Xep.FallbackIndication.FallbackLocation>>(); - foreach (Qlite.Row row in db.body_meta.select().with(db.body_meta.message_id, "=", id)) { - if (row[db.body_meta.info_type] != Xep.FallbackIndication.NS_URI) continue; + var markups = new ArrayList<Xep.MessageMarkup.Span>(); - string ns_uri = row[db.body_meta.info]; - if (!fallbacks_by_ns.has_key(ns_uri)) { - fallbacks_by_ns[ns_uri] = new ArrayList<Xep.FallbackIndication.FallbackLocation>(); + foreach (Qlite.Row row in db.body_meta.select().with(db.body_meta.message_id, "=", id)) { + switch (row[db.body_meta.info_type]) { + case Xep.FallbackIndication.NS_URI: + string ns_uri = row[db.body_meta.info]; + if (!fallbacks_by_ns.has_key(ns_uri)) { + fallbacks_by_ns[ns_uri] = new ArrayList<Xep.FallbackIndication.FallbackLocation>(); + } + fallbacks_by_ns[ns_uri].add(new Xep.FallbackIndication.FallbackLocation(row[db.body_meta.from_char], row[db.body_meta.to_char])); + break; + case Xep.MessageMarkup.NS_URI: + var types = new ArrayList<Xep.MessageMarkup.SpanType>(); + types.add(Xep.MessageMarkup.str_to_span_type(row[db.body_meta.info])); + markups.add(new Xep.MessageMarkup.Span() { types=types, start_char=row[db.body_meta.from_char], end_char=row[db.body_meta.to_char] }); + break; } - fallbacks_by_ns[ns_uri].add(new Xep.FallbackIndication.FallbackLocation(row[db.body_meta.from_char], row[db.body_meta.to_char])); } var fallbacks = new ArrayList<Xep.FallbackIndication.Fallback>(); @@ -177,7 +215,7 @@ public class Message : Object { fallbacks.add(new Xep.FallbackIndication.Fallback(ns_uri, fallbacks_by_ns[ns_uri].to_array())); } this.fallbacks = fallbacks; - return fallbacks; + this.markups = markups; } public void set_fallbacks(Gee.List<Xep.FallbackIndication.Fallback> fallbacks) { diff --git a/libdino/src/service/message_correction.vala b/libdino/src/service/message_correction.vala index 6d4137d4..e6401a05 100644 --- a/libdino/src/service/message_correction.vala +++ b/libdino/src/service/message_correction.vala @@ -39,27 +39,21 @@ public class MessageCorrection : StreamInteractionModule, MessageListener { }); } - public void send_correction(Conversation conversation, Message old_message, string correction_text) { - string stanza_id = old_message.edit_to ?? old_message.stanza_id; + public void set_correction(Conversation conversation, Message message, Message old_message) { + string reference_stanza_id = old_message.edit_to ?? old_message.stanza_id; - Message out_message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(correction_text, conversation); - out_message.edit_to = stanza_id; - out_message.set_quoted_item(old_message.quoted_item_id); - outstanding_correction_nodes[out_message.stanza_id] = stanza_id; - stream_interactor.get_module(MessageProcessor.IDENTITY).send_xmpp_message(out_message, conversation); + outstanding_correction_nodes[message.stanza_id] = reference_stanza_id; db.message_correction.insert() - .value(db.message_correction.message_id, out_message.id) - .value(db.message_correction.to_stanza_id, stanza_id) - .perform(); + .value(db.message_correction.message_id, message.id) + .value(db.message_correction.to_stanza_id, reference_stanza_id) + .perform(); db.content_item.update() - .with(db.content_item.foreign_id, "=", old_message.id) - .with(db.content_item.content_type, "=", 1) - .set(db.content_item.foreign_id, out_message.id) - .perform(); - - on_received_correction(conversation, out_message.id); + .with(db.content_item.foreign_id, "=", old_message.id) + .with(db.content_item.content_type, "=", 1) + .set(db.content_item.foreign_id, message.id) + .perform(); } public bool is_own_correction_allowed(Conversation conversation, Message message) { @@ -145,7 +139,7 @@ public class MessageCorrection : StreamInteractionModule, MessageListener { return false; } - private void on_received_correction(Conversation conversation, int message_id) { + public void on_received_correction(Conversation conversation, int message_id) { ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item_by_foreign(conversation, 1, message_id); if (content_item != null) { received_correction(content_item); diff --git a/libdino/src/service/message_processor.vala b/libdino/src/service/message_processor.vala index 620c93eb..d8ea3e2d 100644 --- a/libdino/src/service/message_processor.vala +++ b/libdino/src/service/message_processor.vala @@ -38,6 +38,7 @@ public class MessageProcessor : StreamInteractionModule, Object { received_pipeline.connect(new FilterMessageListener()); received_pipeline.connect(new StoreMessageListener(this, stream_interactor)); received_pipeline.connect(new StoreContentItemListener(stream_interactor)); + received_pipeline.connect(new MarkupListener(stream_interactor)); stream_interactor.account_added.connect(on_account_added); @@ -45,18 +46,6 @@ public class MessageProcessor : StreamInteractionModule, Object { stream_interactor.stream_resumed.connect(send_unsent_chat_messages); } - public Entities.Message send_text(string text, Conversation conversation) { - Entities.Message message = create_out_message(text, conversation); - return send_message(message, conversation); - } - - public Entities.Message send_message(Entities.Message message, Conversation conversation) { - stream_interactor.get_module(ContentItemStore.IDENTITY).insert_message(message, conversation); - send_xmpp_message(message, conversation); - message_sent(message, conversation); - return message; - } - private void convert_sending_to_unsent_msgs(Account account) { db.message.update() .with(db.message.account_id, "=", account.id) @@ -344,6 +333,25 @@ public class MessageProcessor : StreamInteractionModule, Object { } } + private class MarkupListener : MessageListener { + + public string[] after_actions_const = new string[]{ "STORE" }; + public override string action_group { get { return "Markup"; } } + public override string[] after_actions { get { return after_actions_const; } } + + private StreamInteractor stream_interactor; + + public MarkupListener(StreamInteractor stream_interactor) { + this.stream_interactor = stream_interactor; + } + + public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { + Gee.List<MessageMarkup.Span> markups = MessageMarkup.get_spans(stanza); + message.persist_markups(markups, message.id); + return false; + } + } + private class StoreContentItemListener : MessageListener { public string[] after_actions_const = new string[]{ "DEDUPLICATE", "DECRYPT", "FILTER_EMPTY", "STORE", "CORRECTION", "MESSAGE_REINTERPRETING" }; @@ -421,6 +429,8 @@ public class MessageProcessor : StreamInteractionModule, Object { } } + MessageMarkup.add_spans(new_message, message.get_markups()); + build_message_stanza(message, new_message, conversation); pre_message_send(message, new_message, conversation); if (message.marked == Entities.Message.Marked.UNSENT || message.marked == Entities.Message.Marked.WONTSEND) return; diff --git a/libdino/src/util/send_message.vala b/libdino/src/util/send_message.vala new file mode 100644 index 00000000..234a1644 --- /dev/null +++ b/libdino/src/util/send_message.vala @@ -0,0 +1,56 @@ +using Gee; + +using Dino.Entities; +using Xmpp; + +namespace Dino { + + public void send_message(Conversation conversation, string text, int reply_to_id, Message? correction_to, Gee.List<Xep.MessageMarkup.Span> markups) { + StreamInteractor stream_interactor = Application.get_default().stream_interactor; + + Message out_message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(text, conversation); + + if (correction_to != null) { + string correction_to_stanza_id = correction_to.edit_to ?? correction_to.stanza_id; + out_message.edit_to = correction_to_stanza_id; + stream_interactor.get_module(MessageCorrection.IDENTITY).set_correction(conversation, out_message, correction_to); + } + + if (reply_to_id != 0) { + ContentItem reply_to = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item_by_id(conversation, reply_to_id); + + out_message.set_quoted_item(reply_to.id); + + // Store body with fallback + string fallback = FallbackBody.get_quoted_fallback_body(reply_to); + out_message.body = fallback + out_message.body; + + // Store fallback location + var fallback_location = new Xep.FallbackIndication.FallbackLocation(0, (int)fallback.char_count()); + var fallback_list = new ArrayList<Xep.FallbackIndication.Fallback>(); + fallback_list.add(new Xep.FallbackIndication.Fallback(Xep.Replies.NS_URI, new Xep.FallbackIndication.FallbackLocation[] { fallback_location })); + out_message.set_fallbacks(fallback_list); + + // Adjust markups to new prefix + foreach (var span in markups) { + span.start_char += fallback.length; + span.end_char += fallback.length; + } + } + + if (!markups.is_empty) { + out_message.persist_markups(markups, out_message.id); + } + + + if (correction_to != null) { + stream_interactor.get_module(MessageCorrection.IDENTITY).on_received_correction(conversation, out_message.id); + stream_interactor.get_module(MessageProcessor.IDENTITY).send_xmpp_message(out_message, conversation); + return; + } + + stream_interactor.get_module(ContentItemStore.IDENTITY).insert_message(out_message, conversation); + stream_interactor.get_module(MessageProcessor.IDENTITY).send_xmpp_message(out_message, conversation); + stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent(out_message, conversation); + } +}
\ No newline at end of file |