Daniel Brötzmann pushed to branch mainwindow at gajim / gajim
Commits: d249841a by wurstsalat at 2021-11-22T20:50:35+01:00 Remote: Add TODO
- - - - - 9891ff3d by wurstsalat at 2021-11-22T20:54:23+01:00 Store message corrections in database
Original text is preserved in additional_data, message text is overwritten with corrected text.
- - - - -
10 changed files:
- gajim/common/modules/mam.py - gajim/common/modules/message.py - gajim/common/storage/archive.py - gajim/gtk/chat_list.py - gajim/gtk/controls/base.py - gajim/gtk/controls/chat.py - gajim/gtk/controls/groupchat.py - gajim/gtk/conversation/rows/message.py - gajim/gtk/conversation/view.py - gajim/remote_control.py
Changes:
===================================== gajim/common/modules/mam.py ===================================== @@ -257,6 +257,33 @@ def _mam_message_received(self, _con, stanza, properties): return stanza_id = message_id
+ event_attr = { + 'account': self._account, + 'jid': str(properties.jid.bare), + 'msgtxt': properties.body, + 'properties': properties, + 'additional_data': additional_data, + 'unique_id': properties.id, + 'stanza_id': stanza_id, + 'archive_jid': properties.mam.archive, + 'kind': kind, + } + + correct_id = parse_correction(properties) + if correct_id is not None: + event_attr.update({ + 'correct_id': correct_id, + }) + app.nec.push_incoming_event( + NetworkEvent('message-updated', **event_attr)) + app.storage.archive.store_message_correction( + self._account, + properties.jid.bare, + correct_id, + properties.body, + properties.type.is_groupchat) + return + app.storage.archive.insert_into_logs( self._account, with_, @@ -269,18 +296,7 @@ def _mam_message_received(self, _con, stanza, properties): message_id=properties.id)
app.nec.push_incoming_event( - NetworkEvent('mam-message-received', - account=self._account, - additional_data=additional_data, - correct_id=parse_correction(properties), - stanza_id=stanza_id, - archive_jid=properties.mam.archive, - msgtxt=properties.body, - properties=properties, - kind=kind, - jid=str(properties.jid.bare) - ) - ) + NetworkEvent('mam-message-received', **event_attr))
def _is_valid_request(self, properties): valid_id = self._mam_query_ids.get(properties.mam.archive, None)
===================================== gajim/common/modules/message.py ===================================== @@ -180,17 +180,30 @@ def _message_received(self, _con, stanza, properties): 'resource': resource, 'stanza_id': stanza_id, 'unique_id': stanza_id or message_id, - 'correct_id': parse_correction(properties), 'msgtxt': msgtxt, 'session': session, 'delayed': properties.user_timestamp is not None, - 'gc_control': None, - 'popup': False, 'msg_log_id': None, 'displaymarking': displaymarking, 'properties': properties, }
+ correct_id = parse_correction(properties) + if correct_id is not None: + event_attr.update({ + 'correct_id': correct_id, + }) + event = NetworkEvent('message-updated', **event_attr) + if should_log(self._account, jid): + app.storage.archive.store_message_correction( + self._account, + jid, + correct_id, + msgtxt, + properties.type.is_groupchat) + app.nec.push_incoming_event(event) + return + if type_.is_groupchat: if not msgtxt: return @@ -381,6 +394,15 @@ def log_message(self, message): if message.message is None: return
+ if message.correct_id is not None: + app.storage.archive.store_message_correction( + self._account, + message.jid, + message.correct_id, + message.message, + message.is_groupchat) + return + app.storage.archive.insert_into_logs( self._account, message.jid,
===================================== gajim/common/storage/archive.py ===================================== @@ -29,6 +29,7 @@ import sqlite3 as sqlite from collections import namedtuple
+from nbxmpp import JID from nbxmpp.structs import MessageProperties
from gajim.common import app @@ -431,7 +432,7 @@ def get_last_conversation_line(self, account, jid): KindConstant.ERROR])
sql = ''' - SELECT contact_name, time, kind, message, stanza_id, + SELECT contact_name, time, kind, message, stanza_id, message_id, additional_data FROM logs NATURAL JOIN jids WHERE jid IN ({jids}) AND account_id = {account_id} @@ -923,6 +924,51 @@ def find_stanza_id(self, account, archive_jid, stanza_id, origin_id=None, return True return False
+ @timeit + def store_message_correction(self, + account: str, + jid: JID, + correct_id: str, + corrected_text: str, + is_groupchat: bool) -> None: + type_ = JIDConstant.NORMAL_TYPE + if is_groupchat: + type_ = JIDConstant.ROOM_TYPE + + jid_id = self.get_jid_id(str(jid), type_=type_) + account_id = self.get_account_id(account) + sql = ''' + SELECT log_line_id, message, additional_data + FROM logs + WHERE +jid_id = ? + AND account_id = ? + AND message_id = ? + ''' + row = self._con.execute( + sql, (jid_id, account_id, correct_id)).fetchone() + if row is None: + return + + if row.additional_data is None: + additional_data = AdditionalDataDict() + else: + additional_data = row.additional_data + + original_text = additional_data.get_value( + 'corrected', 'original_text') + if original_text is None: + # Only set original_text for the first correction + additional_data.set_value( + 'corrected', 'original_text', row.message) + serialized_dict = json.dumps(additional_data.data) + + sql = ''' + UPDATE logs SET message = ?, additional_data = ? + WHERE log_line_id = ? + ''' + self._con.execute( + sql, (corrected_text, serialized_dict, row.log_line_id)) + @timeit def update_additional_data(self, account: str,
===================================== gajim/gtk/chat_list.py ===================================== @@ -358,6 +358,7 @@ def _on_message_received(self, event): if event.properties.stanza_id: stanza_id = event.properties.stanza_id.id row.set_stanza_id(stanza_id) + row.set_message_id(event.unique_id) row.set_message_text( event.msgtxt, additional_data=event.additional_data)
@@ -369,6 +370,10 @@ def _on_message_updated(self, event): if row is None: return
+ if hasattr(event, 'correct_id'): + if event.correct_id == row.message_id: + row.set_message_text(event.msgtxt) + if event.properties.is_moderation: if event.properties.moderation.stanza_id == row.stanza_id: text = get_retraction_text( @@ -483,6 +488,7 @@ def __init__(self, workspace_id: str, account: str, jid: JID, type_: str, self.contact_name: str = self.contact.name self.timestamp: int = 0 self.stanza_id: Optional[str] = None + self.message_id: Optional[str] = None self._unread_count: int = 0 self._pinned: bool = pinned
@@ -542,6 +548,7 @@ def __init__(self, workspace_id: str, account: str, jid: JID, type_: str, self._ui.timestamp_label.set_text(uf_timestamp)
self.stanza_id = line.stanza_id + self.message_id = line.message_id
if line.kind in (KindConstant.FILE_TRANSFER_INCOMING, KindConstant.FILE_TRANSFER_OUTGOING): @@ -685,6 +692,9 @@ def set_timestamp(self, timestamp: int) -> None: def set_stanza_id(self, stanza_id: str) -> None: self.stanza_id = stanza_id
+ def set_message_id(self, message_id: str) -> None: + self.message_id = message_id + def update_time(self) -> None: if self.timestamp == 0: return
===================================== gajim/gtk/controls/base.py ===================================== @@ -296,6 +296,11 @@ def process_event(self, event): getattr(self, method_name)(event)
def _on_message_updated(self, event): + if hasattr(event, 'correct_id'): + self.conversation_view.correct_message( + event.correct_id, event.msgtxt) + return + if event.properties.is_moderation: text = get_retraction_text( self.account, @@ -1122,7 +1127,6 @@ def add_message(self, msg_log_id=None, message_id=None, stanza_id=None, - correct_id=None, additional_data=None):
if additional_data is None: @@ -1139,7 +1143,6 @@ def add_message(self, display_marking=displaymarking, message_id=message_id, stanza_id=stanza_id, - correct_id=correct_id, log_line_id=msg_log_id, additional_data=additional_data)
===================================== gajim/gtk/controls/chat.py ===================================== @@ -412,7 +412,6 @@ def _on_mam_message_received(self, event): self.add_message(event.msgtxt, kind, tim=event.properties.mam.timestamp, - correct_id=event.correct_id, message_id=event.properties.id, stanza_id=event.stanza_id, additional_data=event.additional_data, @@ -433,7 +432,6 @@ def _on_message_received(self, event): msg_log_id=event.msg_log_id, message_id=event.properties.id, stanza_id=event.stanza_id, - correct_id=event.correct_id, additional_data=event.additional_data)
if kind == 'outgoing': @@ -460,12 +458,16 @@ def _on_message_sent(self, event): self.msg_textview.get_style_context().remove_class( 'gajim-msg-correcting')
+ if event.correct_id: + self.conversation_view.correct_message( + event.correct_id, event.message) + return + self.add_message(event.message, 'outgoing', tim=event.timestamp, displaymarking=displaymarking, message_id=message_id, - correct_id=event.correct_id, additional_data=event.additional_data)
def _on_receipt_received(self, event): @@ -606,7 +608,6 @@ def add_message(self, displaymarking=None, msg_log_id=None, stanza_id=None, - correct_id=None, message_id=None, additional_data=None, notify=True): @@ -629,7 +630,6 @@ def add_message(self, msg_log_id=msg_log_id, message_id=message_id, stanza_id=stanza_id, - correct_id=correct_id, additional_data=additional_data)
def shutdown(self):
===================================== gajim/gtk/controls/groupchat.py ===================================== @@ -671,7 +671,6 @@ def _on_mam_message_received(self, event): self.add_message(event.msgtxt, contact=event.properties.muc_nickname, tim=event.properties.mam.timestamp, - correct_id=event.correct_id, message_id=event.properties.id, stanza_id=event.stanza_id, additional_data=event.additional_data, @@ -694,7 +693,6 @@ def _on_gc_message_received(self, event): contact=event.properties.muc_nickname, tim=event.properties.timestamp, displaymarking=event.displaymarking, - correct_id=event.correct_id, message_id=event.properties.id, stanza_id=stanza_id, additional_data=event.additional_data) @@ -708,7 +706,6 @@ def add_message(self, contact='', tim=None, displaymarking=None, - correct_id=None, message_id=None, stanza_id=None, additional_data=None, @@ -733,7 +730,6 @@ def add_message(self, tim, notify, displaymarking=displaymarking, - correct_id=correct_id, message_id=message_id, stanza_id=stanza_id, additional_data=additional_data)
===================================== gajim/gtk/conversation/rows/message.py ===================================== @@ -70,7 +70,6 @@ def __init__(self, self.kind = kind self.name = name or '' self.text = text - self.additional_data = additional_data
self._contact = contact @@ -78,13 +77,11 @@ def __init__(self, if contact is not None and contact.is_groupchat: self._is_groupchat = True
- self._corrections = [] self._has_receipt = marker == 'received' self._has_displayed = marker == 'displayed'
- if additional_data is not None: - if additional_data.get_value('retracted', 'by') is not None: - self.get_style_context().add_class('retracted-message') + # Keep original text for message correction + self._original_text: str = text
is_previewable = app.interface.preview_manager.is_previewable( text, additional_data) @@ -144,6 +141,21 @@ def __init__(self,
self._message_icons = MessageIcons()
+ if additional_data is not None: + if additional_data.get_value('retracted', 'by') is not None: + self.get_style_context().add_class('retracted-message') + + correction_original = additional_data.get_value( + 'corrected', 'original_text') + if correction_original is not None: + self._original_text = correction_original + self._message_icons.set_correction_icon_visible(True) + original_text = reduce_chars_newlines( + correction_original, max_chars=150, max_lines=10) + self._message_icons.set_correction_tooltip( + _('Message corrected. Original message:' + '\n%s') % original_text) + if error is not None: self.set_error(to_user_string(error))
@@ -326,7 +338,6 @@ def set_retracted(self, text: str) -> None: self.get_style_context().add_class('retracted-message')
def set_correction(self, text: str) -> None: - self._corrections.append(self._message_widget.get_text()) result = process(text) self._message_widget.add_content(result)
@@ -334,11 +345,10 @@ def set_correction(self, text: str) -> None: self._message_icons.set_receipt_icon_visible(False) self._message_icons.set_correction_icon_visible(True)
- corrections = '\n'.join(line for line in self._corrections) - corrections = reduce_chars_newlines( - corrections, max_chars=150, max_lines=10) + original_text = reduce_chars_newlines( + self._original_text, max_chars=150, max_lines=10) self._message_icons.set_correction_tooltip( - _('Message corrected. Original message:\n%s') % corrections) + _('Message corrected. Original message:\n%s') % original_text)
def set_error(self, tooltip): self._message_icons.set_error_icon_visible(True)
===================================== gajim/gtk/conversation/view.py ===================================== @@ -212,7 +212,6 @@ def add_message(self, log_line_id=None, message_id=None, stanza_id=None, - correct_id=None, display_marking=None, additional_data=None, marker=None, @@ -221,10 +220,6 @@ def add_message(self, if not timestamp: timestamp = time.time()
- if correct_id is not None: - self.correct_message(correct_id, text) - return - message = MessageRow( self._account, self._contact,
===================================== gajim/remote_control.py ===================================== @@ -348,6 +348,7 @@ def on_unsubscribed_presence_received(self, obj):
def on_gc_message_received(self, obj): if not hasattr(obj, 'needs_highlight'): + # TODO: # event has not been handled at GUI level return self.raise_signal('GCMessage', (obj.conn.name, [obj.fjid, obj.msgtxt,
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/4dc79ca45426e17f247a11e0aeb404d1...