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 | |
parent | ceb921a0148f7fdc2a9df3e6b85143bf8c26c341 (diff) | |
download | dino-b0ff90a14a5d127e17f2371f87e7bb659de3a68f.tar.gz dino-b0ff90a14a5d127e17f2371f87e7bb659de3a68f.zip |
Add initial message markup (XEP-0394) support
-rw-r--r-- | libdino/CMakeLists.txt | 1 | ||||
-rw-r--r-- | libdino/meson.build | 1 | ||||
-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 | ||||
-rw-r--r-- | main/src/ui/chat_input/chat_input_controller.vala | 19 | ||||
-rw-r--r-- | main/src/ui/chat_input/chat_text_view.vala | 130 | ||||
-rw-r--r-- | main/src/ui/conversation_content_view/message_widget.vala | 105 | ||||
-rw-r--r-- | main/src/ui/util/helper.vala | 25 | ||||
-rw-r--r-- | xmpp-vala/CMakeLists.txt | 1 | ||||
-rw-r--r-- | xmpp-vala/meson.build | 1 | ||||
-rw-r--r-- | xmpp-vala/src/module/xep/0394_message_markup.vala | 81 |
13 files changed, 456 insertions, 78 deletions
diff --git a/libdino/CMakeLists.txt b/libdino/CMakeLists.txt index e4d786c9..34cf9575 100644 --- a/libdino/CMakeLists.txt +++ b/libdino/CMakeLists.txt @@ -68,6 +68,7 @@ SOURCES src/service/util.vala src/util/display_name.vala + src/util/send_message.vala src/util/util.vala src/util/weak_map.vala src/util/weak_timeout.vala diff --git a/libdino/meson.build b/libdino/meson.build index 17804d23..559a81b5 100644 --- a/libdino/meson.build +++ b/libdino/meson.build @@ -74,6 +74,7 @@ sources = files( 'src/service/stream_interactor.vala', 'src/service/util.vala', 'src/util/display_name.vala', + 'src/util/send_message.vala', 'src/util/util.vala', 'src/util/weak_map.vala', 'src/util/weak_timeout.vala', 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 diff --git a/main/src/ui/chat_input/chat_input_controller.vala b/main/src/ui/chat_input/chat_input_controller.vala index cf8e5a02..07499aa4 100644 --- a/main/src/ui/chat_input/chat_input_controller.vala +++ b/main/src/ui/chat_input/chat_input_controller.vala @@ -3,6 +3,7 @@ using Gdk; using Gtk; using Xmpp; +using Xmpp; using Dino.Entities; namespace Dino.Ui { @@ -136,6 +137,7 @@ public class ChatInputController : Object { string text = chat_input.chat_text_view.text_view.buffer.text; ContentItem? quoted_content_item_bak = quoted_content_item; + var markups = chat_input.chat_text_view.get_markups(); // Reset input state. Has do be done before parsing commands, because those directly return. chat_input.chat_text_view.text_view.buffer.text = ""; @@ -194,21 +196,8 @@ public class ChatInputController : Object { break; } } - Message out_message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(text, conversation); - if (quoted_content_item_bak != null) { - out_message.set_quoted_item(quoted_content_item_bak.id); - - // Store body with fallback - string fallback = FallbackBody.get_quoted_fallback_body(quoted_content_item_bak); - 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); - } - stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(out_message, conversation); + + Dino.send_message(conversation, text, quoted_content_item_bak != null ? quoted_content_item_bak.id : 0, null, markups); } private void on_text_input_changed() { diff --git a/main/src/ui/chat_input/chat_text_view.vala b/main/src/ui/chat_input/chat_text_view.vala index aa246d8d..c7429318 100644 --- a/main/src/ui/chat_input/chat_text_view.vala +++ b/main/src/ui/chat_input/chat_text_view.vala @@ -40,6 +40,10 @@ public class ChatTextView : Box { private uint wait_queue_resize; private SmileyConverter smiley_converter; + private TextTag italic_tag; + private TextTag bold_tag; + private TextTag strikethrough_tag; + construct { valign = Align.CENTER; scrolled_window.set_child(text_view); @@ -49,6 +53,15 @@ public class ChatTextView : Box { text_input_key_events.key_pressed.connect(on_text_input_key_press); text_view.add_controller(text_input_key_events); + italic_tag = text_view.buffer.create_tag("italic"); + italic_tag.style = Pango.Style.ITALIC; + + bold_tag = text_view.buffer.create_tag("bold"); + bold_tag.weight = Pango.Weight.BOLD; + + strikethrough_tag = text_view.buffer.create_tag("strikethrough"); + strikethrough_tag.strikethrough = true; + smiley_converter = new SmileyConverter(text_view); scrolled_window.vadjustment.changed.connect(on_upper_notify); @@ -60,6 +73,37 @@ public class ChatTextView : Box { }); } + public void set_text(Message message) { + // Get a copy of the markup spans, such that we can modify them + var markups = new ArrayList<Xep.MessageMarkup.Span>(); + foreach (var markup in message.get_markups()) { + markups.add(new Xep.MessageMarkup.Span() { types=markup.types, start_char=markup.start_char, end_char=markup.end_char }); + } + + text_view.buffer.text = Util.remove_fallbacks_adjust_markups(message.body, message.quoted_item_id > 0, message.get_fallbacks(), markups); + + foreach (var markup in markups) { + foreach (var ty in markup.types) { + TextTag tag = null; + switch (ty) { + case Xep.MessageMarkup.SpanType.EMPHASIS: + tag = italic_tag; + break; + case Xep.MessageMarkup.SpanType.STRONG_EMPHASIS: + tag = bold_tag; + break; + case Xep.MessageMarkup.SpanType.DELETED: + tag = strikethrough_tag; + break; + } + TextIter start_selection, end_selection; + text_view.buffer.get_iter_at_offset(out start_selection, markup.start_char); + text_view.buffer.get_iter_at_offset(out end_selection, markup.end_char); + text_view.buffer.apply_tag(tag, start_selection, end_selection); + } + } + } + public override void dispose() { base.dispose(); if (wait_queue_resize != 0) { @@ -95,6 +139,7 @@ public class ChatTextView : Box { } private bool on_text_input_key_press(EventControllerKey controller, uint keyval, uint keycode, Gdk.ModifierType state) { + // Enter pressed -> Send message (except if it was Shift+Enter) if (keyval in new uint[]{ Key.Return, Key.KP_Enter }) { // Allow the text view to process the event. Needed for IME. if (text_view.im_context_filter_keypress(controller.get_current_event())) { @@ -109,11 +154,96 @@ public class ChatTextView : Box { } return true; } + if (keyval == Key.Escape) { cancel_input(); } + + // Style text section bold (CTRL + b) or italic (CTRL + i) + if ((state & ModifierType.CONTROL_MASK) > 0) { + if (keyval in new uint[]{ Key.i, Key.b }) { + TextIter start_selection, end_selection; + text_view.buffer.get_selection_bounds(out start_selection, out end_selection); + + TextTag tag = null; + bool already_formatted = false; + var markup_types = get_markup_types_from_iter(start_selection); + if (keyval == Key.i) { + tag = italic_tag; + already_formatted = markup_types.contains(Xep.MessageMarkup.SpanType.EMPHASIS); + } else if (keyval == Key.b) { + tag = bold_tag; + already_formatted = markup_types.contains(Xep.MessageMarkup.SpanType.STRONG_EMPHASIS); + } else if (keyval == Key.s) { + tag = strikethrough_tag; + already_formatted = markup_types.contains(Xep.MessageMarkup.SpanType.DELETED); + } + if (tag != null) { + if (already_formatted) { + text_view.buffer.remove_tag(tag, start_selection, end_selection); + } else { + text_view.buffer.apply_tag(tag, start_selection, end_selection); + } + } + } + } + return false; } + + public Gee.List<Xep.MessageMarkup.Span> get_markups() { + var markups = new HashMap<Xep.MessageMarkup.SpanType, Xep.MessageMarkup.SpanType>(); + markups[Xep.MessageMarkup.SpanType.EMPHASIS] = Xep.MessageMarkup.SpanType.EMPHASIS; + markups[Xep.MessageMarkup.SpanType.STRONG_EMPHASIS] = Xep.MessageMarkup.SpanType.STRONG_EMPHASIS; + markups[Xep.MessageMarkup.SpanType.DELETED] = Xep.MessageMarkup.SpanType.DELETED; + + var ended_groups = new ArrayList<Xep.MessageMarkup.Span>(); + Xep.MessageMarkup.Span current_span = null; + + TextIter iter; + text_view.buffer.get_start_iter(out iter); + int i = 0; + do { + var char_markups = get_markup_types_from_iter(iter); + + // Not the same set of markups as last character -> end all spans + if (current_span != null && (!char_markups.contains_all(current_span.types) || !current_span.types.contains_all(char_markups))) { + ended_groups.add(current_span); + current_span = null; + } + + if (char_markups.size > 0) { + if (current_span == null) { + current_span = new Xep.MessageMarkup.Span() { types=char_markups, start_char=i, end_char=i + 1 }; + } else { + current_span.end_char = i + 1; + } + } + + i++; + } while (iter.forward_char()); + + if (current_span != null) { + ended_groups.add(current_span); + } + + return ended_groups; + } + + private Gee.List<Xep.MessageMarkup.SpanType> get_markup_types_from_iter(TextIter iter) { + var ret = new ArrayList<Xep.MessageMarkup.SpanType>(); + + foreach (TextTag tag in iter.get_tags()) { + if (tag.style == Pango.Style.ITALIC) { + ret.add(Xep.MessageMarkup.SpanType.EMPHASIS); + } else if (tag.weight == Pango.Weight.BOLD) { + ret.add(Xep.MessageMarkup.SpanType.STRONG_EMPHASIS); + } else if (tag.strikethrough) { + ret.add(Xep.MessageMarkup.SpanType.DELETED); + } + } + return ret; + } } } diff --git a/main/src/ui/conversation_content_view/message_widget.vala b/main/src/ui/conversation_content_view/message_widget.vala index 11b38286..376ef4bd 100644 --- a/main/src/ui/conversation_content_view/message_widget.vala +++ b/main/src/ui/conversation_content_view/message_widget.vala @@ -26,8 +26,8 @@ public class MessageMetaItem : ContentMetaItem { AdditionalInfo additional_info = AdditionalInfo.NONE; ulong realize_id = -1; - ulong style_updated_id = -1; ulong marked_notify_handler_id = -1; + uint pending_timeout_id = -1; public Label label = new Label("") { use_markup=true, xalign=0, selectable=true, wrap=true, wrap_mode=Pango.WrapMode.WORD_CHAR, hexpand=true, vexpand=true }; @@ -64,6 +64,7 @@ public class MessageMetaItem : ContentMetaItem { if (message.marked in Message.MARKED_RECEIVED) { binding.unbind(); this.disconnect(marked_notify_handler_id); + marked_notify_handler_id = -1; } }); } @@ -71,20 +72,72 @@ public class MessageMetaItem : ContentMetaItem { update_label(); } - private string generate_markup_text(ContentItem item) { + private void generate_markup_text(ContentItem item, Label label) { MessageItem message_item = item as MessageItem; Conversation conversation = message_item.conversation; Message message = message_item.message; - bool theme_dependent = false; + // Get a copy of the markup spans, such that we can modify them + var markups = new ArrayList<Xep.MessageMarkup.Span>(); + foreach (var markup in message.get_markups()) { + markups.add(new Xep.MessageMarkup.Span() { types=markup.types, start_char=markup.start_char, end_char=markup.end_char }); + } + + string markup_text = message.body; + + var attrs = new AttrList(); + label.set_attributes(attrs); - string markup_text = Dino.message_body_without_reply_fallback(message); + if (markup_text == null) return; // TODO remove + // Only process messages up to a certain size if (markup_text.length > 10000) { markup_text = markup_text.substring(0, 10000) + " [" + _("Message too long") + "]"; } - if (message.body.has_prefix("/me ")) { - markup_text = markup_text.substring(4); + + bool theme_dependent = false; + + markup_text = Util.remove_fallbacks_adjust_markups(markup_text, message.quoted_item_id > 0, message.get_fallbacks(), markups); + + var bold_attr = Pango.attr_weight_new(Pango.Weight.BOLD); + var italic_attr = Pango.attr_style_new(Pango.Style.ITALIC); + var strikethrough_attr = Pango.attr_strikethrough_new(true); + + // Prefix message with name instead of /me + if (markup_text.has_prefix("/me ")) { + string display_name = Util.get_participant_display_name(stream_interactor, conversation, message.from); + markup_text = display_name + " " + markup_text.substring(4); + + foreach (Xep.MessageMarkup.Span span in markups) { + int length = display_name.char_count() - 4 + 1; + span.start_char += length; + span.end_char += length; + } + + bold_attr.end_index = display_name.length; + italic_attr.end_index = display_name.length; + attrs.insert(bold_attr.copy()); + attrs.insert(italic_attr.copy()); + } + + foreach (var markup in markups) { + foreach (var ty in markup.types) { + Attribute attr = null; + switch (ty) { + case Xep.MessageMarkup.SpanType.EMPHASIS: + attr = Pango.attr_style_new(Pango.Style.ITALIC); + break; + case Xep.MessageMarkup.SpanType.STRONG_EMPHASIS: + attr = Pango.attr_weight_new(Pango.Weight.BOLD); + break; + case Xep.MessageMarkup.SpanType.DELETED: + attr = Pango.attr_strikethrough_new(true); + break; + } + attr.start_index = markup_text.index_of_nth_char(markup.start_char); + attr.end_index = markup_text.index_of_nth_char(markup.end_char); + attrs.insert(attr.copy()); + } } if (conversation.type_ == Conversation.Type.GROUPCHAT) { @@ -93,11 +146,6 @@ public class MessageMetaItem : ContentMetaItem { markup_text = Util.parse_add_markup_theme(markup_text, null, true, true, true, Util.is_dark_theme(this.label), ref theme_dependent); } - if (message.body.has_prefix("/me ")) { - string display_name = Util.get_participant_display_name(stream_interactor, conversation, message.from); - markup_text = @"<i><b>$(Markup.escape_text(display_name))</b> " + markup_text + "</i>"; - } - int only_emoji_count = Util.get_only_emoji_count(markup_text); if (only_emoji_count != -1) { string size_str = only_emoji_count < 5 ? "xx-large" : "large"; @@ -121,8 +169,10 @@ public class MessageMetaItem : ContentMetaItem { additional_info = AdditionalInfo.PENDING; } else { int time_diff = (- (int) message.time.difference(new DateTime.now_utc()) / 1000); - Timeout.add(10000 - time_diff, () => { + if (pending_timeout_id != -1) Source.remove(pending_timeout_id); + pending_timeout_id = Timeout.add(10000 - time_diff, () => { update_label(); + pending_timeout_id = -1; return false; }); } @@ -136,16 +186,14 @@ public class MessageMetaItem : ContentMetaItem { if (theme_dependent && realize_id == -1) { realize_id = label.realize.connect(update_label); -// style_updated_id = label.style_updated.connect(update_label); } else if (!theme_dependent && realize_id != -1) { label.disconnect(realize_id); - label.disconnect(style_updated_id); } - return markup_text; + label.label = markup_text; } public void update_label() { - label.label = generate_markup_text(content_item); + generate_markup_text(content_item, label); } public override Object? get_widget(Plugins.ConversationItemWidgetInterface outer, Plugins.WidgetType type) { @@ -209,16 +257,15 @@ public class MessageMetaItem : ContentMetaItem { outer.set_widget(label, Plugins.WidgetType.GTK4, 2); }); edit_mode.send.connect(() => { - if (((MessageItem) content_item).message.body != edit_mode.chat_text_view.text_view.buffer.text) { - on_edit_send(edit_mode.chat_text_view.text_view.buffer.text); - } else { -// edit_cancelled(); - } + string text = edit_mode.chat_text_view.text_view.buffer.text; + var markups = edit_mode.chat_text_view.get_markups(); + Dino.send_message(message_item.conversation, text, message_item.message.quoted_item_id, message_item.message, markups); + in_edit_mode = false; outer.set_widget(label, Plugins.WidgetType.GTK4, 2); }); - edit_mode.chat_text_view.text_view.buffer.text = message.body; + edit_mode.chat_text_view.set_text(message); outer.set_widget(edit_mode, Plugins.WidgetType.GTK4, 2); edit_mode.chat_text_view.text_view.grab_focus(); @@ -227,11 +274,6 @@ public class MessageMetaItem : ContentMetaItem { } } - private void on_edit_send(string text) { - stream_interactor.get_module(MessageCorrection.IDENTITY).send_correction(message_item.conversation, message_item.message, text); - this.in_edit_mode = false; - } - private void on_received_correction(ContentItem content_item) { if (this.content_item.id == content_item.id) { this.content_item = content_item; @@ -251,6 +293,15 @@ public class MessageMetaItem : ContentMetaItem { public override void dispose() { stream_interactor.get_module(MessageCorrection.IDENTITY).received_correction.disconnect(on_received_correction); this.notify["in-edit-mode"].disconnect(on_in_edit_mode_changed); + if (marked_notify_handler_id != -1) { + this.disconnect(marked_notify_handler_id); + } + if (realize_id != -1) { + label.disconnect(realize_id); + } + if (pending_timeout_id != -1) { + Source.remove(pending_timeout_id); + } if (label != null) { label.unparent(); label.dispose(); diff --git a/main/src/ui/util/helper.vala b/main/src/ui/util/helper.vala index 63288fc2..45b96b94 100644 --- a/main/src/ui/util/helper.vala +++ b/main/src/ui/util/helper.vala @@ -297,6 +297,31 @@ public static string parse_add_markup_theme(string s_, string? highlight_word, b return s; } + // Modifies `markups`. + public string remove_fallbacks_adjust_markups(string text, bool contains_quote, Gee.List<Xep.FallbackIndication.Fallback> fallbacks, Gee.List<Xep.MessageMarkup.Span> markups) { + string processed_text = text; + + foreach (var fallback in fallbacks) { + if (fallback.ns_uri == Xep.Replies.NS_URI && contains_quote) { + foreach (var fallback_location in fallback.locations) { + processed_text = processed_text[0:processed_text.index_of_nth_char(fallback_location.from_char)] + + processed_text[processed_text.index_of_nth_char(fallback_location.to_char):processed_text.length]; + + int length = fallback_location.to_char - fallback_location.from_char; + foreach (Xep.MessageMarkup.Span span in markups) { + if (span.start_char > fallback_location.to_char) { + span.start_char -= length; + } + if (span.end_char > fallback_location.to_char) { + span.end_char -= length; + } + } + } + } + } + return processed_text; + } + /** * This is a heuristic to count emojis in a string {@link http://example.com/} * diff --git a/xmpp-vala/CMakeLists.txt b/xmpp-vala/CMakeLists.txt index cfbc0aaf..fa0b08ef 100644 --- a/xmpp-vala/CMakeLists.txt +++ b/xmpp-vala/CMakeLists.txt @@ -55,6 +55,7 @@ SOURCES "src/module/xep/0048_bookmarks.vala" "src/module/xep/0048_conference.vala" + "src/module/xep/0394_message_markup.vala" "src/module/xep/0402_bookmarks2.vala" "src/module/xep/0004_data_forms.vala" diff --git a/xmpp-vala/meson.build b/xmpp-vala/meson.build index be5e96a8..7d062db3 100644 --- a/xmpp-vala/meson.build +++ b/xmpp-vala/meson.build @@ -55,6 +55,7 @@ sources = files( 'src/module/xep/0047_in_band_bytestreams.vala', 'src/module/xep/0048_bookmarks.vala', 'src/module/xep/0048_conference.vala', + 'src/module/xep/0394_message_markup.vala', 'src/module/xep/0049_private_xml_storage.vala', 'src/module/xep/0054_vcard/module.vala', 'src/module/xep/0059_result_set_management.vala', diff --git a/xmpp-vala/src/module/xep/0394_message_markup.vala b/xmpp-vala/src/module/xep/0394_message_markup.vala new file mode 100644 index 00000000..32b441af --- /dev/null +++ b/xmpp-vala/src/module/xep/0394_message_markup.vala @@ -0,0 +1,81 @@ +using Gee; + +namespace Xmpp.Xep.MessageMarkup { + + public const string NS_URI = "urn:xmpp:markup:0"; + + public enum SpanType { + EMPHASIS, + STRONG_EMPHASIS, + DELETED, + } + + public class Span : Object { + public Gee.List<SpanType> types { get; set; } + public int start_char { get; set; } + public int end_char { get; set; } + } + + public Gee.List<Span> get_spans(MessageStanza stanza) { + var ret = new ArrayList<Span>(); + + foreach (StanzaNode span_node in stanza.stanza.get_deep_subnodes(NS_URI + ":markup", NS_URI + ":span")) { + int start_char = span_node.get_attribute_int("start", -1, NS_URI); + int end_char = span_node.get_attribute_int("end", -1, NS_URI); + if (start_char == -1 || end_char == -1) continue; + + var types = new ArrayList<SpanType>(); + foreach (StanzaNode span_subnode in span_node.get_all_subnodes()) { + types.add(str_to_span_type(span_subnode.name)); + } + ret.add(new Span() { types=types, start_char=start_char, end_char=end_char }); + } + return ret; + } + + public void add_spans(MessageStanza stanza, Gee.List<Span> spans) { + if (spans.is_empty) return; + + StanzaNode markup_node = new StanzaNode.build("markup", NS_URI).add_self_xmlns(); + + foreach (var span in spans) { + StanzaNode span_node = new StanzaNode.build("span", NS_URI) + .put_attribute("start", span.start_char.to_string(), NS_URI) + .put_attribute("end", span.end_char.to_string(), NS_URI); + + foreach (var type in span.types) { + span_node.put_node(new StanzaNode.build(span_type_to_str(type), NS_URI)); + } + markup_node.put_node(span_node); + } + + stanza.stanza.put_node(markup_node); + } + + public static string span_type_to_str(Xep.MessageMarkup.SpanType span_type) { + switch (span_type) { + case Xep.MessageMarkup.SpanType.EMPHASIS: + return "emphasis"; + case Xep.MessageMarkup.SpanType.STRONG_EMPHASIS: + return "strong"; + case Xep.MessageMarkup.SpanType.DELETED: + return "deleted"; + default: + return ""; + } + } + + public static Xep.MessageMarkup.SpanType str_to_span_type(string span_str) { + switch (span_str) { + case "emphasis": + return Xep.MessageMarkup.SpanType.EMPHASIS; + case "strong": + return Xep.MessageMarkup.SpanType.STRONG_EMPHASIS; + case "deleted": + return Xep.MessageMarkup.SpanType.DELETED; + default: + return Xep.MessageMarkup.SpanType.EMPHASIS; + } + } + +}
\ No newline at end of file |