Merge branch 'lmc-new' into 'master'

Read newer LMC rules

See merge request poezio/poezio!79
This commit is contained in:
Maxime Buquet 2020-04-05 02:21:57 +02:00
commit bcd96cc8a4
3 changed files with 28 additions and 14 deletions

View file

@ -609,7 +609,7 @@ class ChatTab(Tab):
message = self._text_buffer.modify_message( message = self._text_buffer.modify_message(
txt, old_id, new_id, time=time, user=user, jid=jid) txt, old_id, new_id, time=time, user=user, jid=jid)
if message: if message:
self.text_win.modify_message(old_id, message) self.text_win.modify_message(message.identifier, message)
self.core.refresh_window() self.core.refresh_window()
return True return True
return False return False

View file

@ -1129,7 +1129,7 @@ class MucTab(ChatTab):
user=user, user=user,
jid=jid) jid=jid)
if message: if message:
self.text_win.modify_message(old_id, message) self.text_win.modify_message(message.identifier, message)
return highlight return highlight
return False return False

View file

@ -11,7 +11,7 @@ independently by their TextWins.
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
from typing import Union, Optional, List, Tuple from typing import Dict, Union, Optional, List, Tuple
from datetime import datetime from datetime import datetime
from poezio.config import config from poezio.config import config
from poezio.theming import get_theme, dump_tuple from poezio.theming import get_theme, dump_tuple
@ -121,6 +121,8 @@ class TextBuffer:
self._messages_nb_limit = messages_nb_limit # type: int self._messages_nb_limit = messages_nb_limit # type: int
# Message objects # Message objects
self.messages = [] # type: List[Message] self.messages = [] # type: List[Message]
# Correction id -> Original message id
self.correction_ids = {} # type: Dict[str, str]
# we keep track of one or more windows # we keep track of one or more windows
# so we can pass the new messages to them, as they are added, so # so we can pass the new messages to them, as they are added, so
# they (the windows) can build the lines from the new message # they (the windows) can build the lines from the new message
@ -186,15 +188,20 @@ class TextBuffer:
return min(ret_val, 1) return min(ret_val, 1)
def _find_message(self, old_id: str) -> int: def _find_message(self, orig_id: str) -> Tuple[str, int]:
""" """
Find a message in the text buffer from its message id Find a message in the text buffer from its message id
""" """
# When looking for a message, ensure the id doesn't appear in a
# message we've removed from our message list. If so return the index
# of the corresponding id for the original message instead.
orig_id = self.correction_ids.get(orig_id, orig_id)
for i in range(len(self.messages) - 1, -1, -1): for i in range(len(self.messages) - 1, -1, -1):
msg = self.messages[i] msg = self.messages[i]
if msg.identifier == old_id: if msg.identifier == orig_id:
return i return (orig_id, i)
return -1 return (orig_id, -1)
def ack_message(self, old_id: str, jid: str) -> Union[None, bool, Message]: def ack_message(self, old_id: str, jid: str) -> Union[None, bool, Message]:
"""Mark a message as acked""" """Mark a message as acked"""
@ -211,7 +218,7 @@ class TextBuffer:
Edit the ack status of a message, and optionally Edit the ack status of a message, and optionally
append some text. append some text.
""" """
i = self._find_message(old_id) _, i = self._find_message(old_id)
if i == -1: if i == -1:
return None return None
msg = self.messages[i] msg = self.messages[i]
@ -228,7 +235,7 @@ class TextBuffer:
def modify_message(self, def modify_message(self,
txt: str, txt: str,
old_id: str, orig_id: str,
new_id: str, new_id: str,
highlight: bool = False, highlight: bool = False,
time: Optional[datetime] = None, time: Optional[datetime] = None,
@ -236,14 +243,19 @@ class TextBuffer:
jid: Optional[str] = None): jid: Optional[str] = None):
""" """
Correct a message in a text buffer. Correct a message in a text buffer.
Version 1.1.0 of Last Message Correction (0308) added clarifications
that break the way poezio handles corrections. Instead of linking
corrections to the previous correction/message as we were doing, we
are now required to link all corrections to the original messages.
""" """
i = self._find_message(old_id) orig_id, i = self._find_message(orig_id)
if i == -1: if i == -1:
log.debug( log.debug(
'Message %s not found in text_buffer, abort replacement.', 'Message %s not found in text_buffer, abort replacement.',
old_id) orig_id)
raise CorrectionError("nothing to replace") raise CorrectionError("nothing to replace")
msg = self.messages[i] msg = self.messages[i]
@ -258,10 +270,12 @@ class TextBuffer:
elif not msg.user and msg.jid != jid: elif not msg.user and msg.jid != jid:
raise CorrectionError( raise CorrectionError(
'Messages %s and %s have not been ' 'Messages %s and %s have not been '
'sent by the same fullJID' % (old_id, new_id)) 'sent by the same fullJID' % (orig_id, new_id))
if not time: if not time:
time = msg.time time = msg.time
self.correction_ids[new_id] = orig_id
message = Message( message = Message(
txt, txt,
time, time,
@ -269,13 +283,13 @@ class TextBuffer:
msg.nick_color, msg.nick_color,
False, False,
msg.user, msg.user,
new_id, orig_id,
highlight=highlight, highlight=highlight,
old_message=msg, old_message=msg,
revisions=msg.revisions + 1, revisions=msg.revisions + 1,
jid=jid) jid=jid)
self.messages[i] = message self.messages[i] = message
log.debug('Replacing message %s with %s.', old_id, new_id) log.debug('Replacing message %s with %s.', orig_id, new_id)
return message return message
def del_window(self, win) -> None: def del_window(self, win) -> None: