buffer: Remember which name we display in the buffer.

This commit is contained in:
poljar (Damir Jelić) 2018-07-06 15:03:00 +02:00
parent 66507b23ce
commit 1e3e66e350
2 changed files with 34 additions and 27 deletions

View file

@ -629,6 +629,10 @@ class WeechatChannelBuffer(object):
class RoomBuffer(object): class RoomBuffer(object):
def __init__(self, room, server_name): def __init__(self, room, server_name):
self.room = room self.room = room
# This dict remembers the connection from a user_id to the name we
# displayed in the buffer
self.displayed_nicks = {}
user = shorten_sender(self.room.own_user_id) user = shorten_sender(self.room.own_user_id)
self.weechat_buffer = WeechatChannelBuffer( self.weechat_buffer = WeechatChannelBuffer(
room.room_id, room.room_id,
@ -636,12 +640,21 @@ class RoomBuffer(object):
user user
) )
def find_nick(self, user_id):
# type: (str) -> str
"""Find a suitable nick from a user_id"""
if user_id in self.displayed_nicks:
return self.displayed_nicks[user_id]
return user_id
def handle_membership_events(self, event, is_state): def handle_membership_events(self, event, is_state):
def join(event, date, is_state): def join(event, date, is_state):
user = self.room.users[event.sender] user = self.room.users[event.sender]
buffer_user = RoomUser(user.name, event.sender) buffer_user = RoomUser(user.name, event.sender, user.power_level)
# TODO remove this duplication # TODO we need to check that the displayed nick is unique if it
user.nick_color = buffer_user.color # isn't use the full user_id
self.displayed_nicks[event.sender] = user.name
if self.room.own_user_id == event.sender: if self.room.own_user_id == event.sender:
buffer_user.color = "weechat.color.chat_nick_self" buffer_user.color = "weechat.color.chat_nick_self"
@ -669,13 +682,15 @@ class RoomBuffer(object):
join(event, date, is_state) join(event, date, is_state)
elif isinstance(event, RoomMemberLeave): elif isinstance(event, RoomMemberLeave):
# TODO the nick can be a display name or a full sender name nick = self.find_nick(event.leaving_user)
nick = shorten_sender(event.sender)
if event.sender == event.leaving_user: if event.sender == event.leaving_user:
self.weechat_buffer.part(nick, date, not is_state) self.weechat_buffer.part(nick, date, not is_state)
else: else:
self.weechat_buffer.kick(nick, date, not is_state) self.weechat_buffer.kick(nick, date, not is_state)
if event.leaving_user in self.displayed_nicks:
del self.displayed_nicks[event.leaving_user]
elif isinstance(event, RoomMemberInvite): elif isinstance(event, RoomMemberInvite):
if is_state: if is_state:
return return
@ -713,8 +728,7 @@ class RoomBuffer(object):
# them all # them all
line = lines[0] line = lines[0]
# TODO the censor may not be in the room anymore censor = self.find_nick(event.sender)
censor = self.room.users[event.sender].name
message = line.message message = line.message
tags = line.tags tags = line.tags
@ -748,8 +762,7 @@ class RoomBuffer(object):
line.tags = tags line.tags = tags
def _handle_redacted_message(self, event): def _handle_redacted_message(self, event):
# TODO user doesn't have to be in the room anymore nick = self.find_nick(event.sender)
user = self.room.users[event.sender]
date = server_ts_to_weechat(event.timestamp) date = server_ts_to_weechat(event.timestamp)
tags = self.get_event_tags(event) tags = self.get_event_tags(event)
tags.append(SCRIPT_NAME + "_redacted") tags.append(SCRIPT_NAME + "_redacted")
@ -757,24 +770,20 @@ class RoomBuffer(object):
reason = (", reason: \"{reason}\"".format(reason=event.reason) reason = (", reason: \"{reason}\"".format(reason=event.reason)
if event.reason else "") if event.reason else "")
censor = self.room.users[event.censor] censor = self.find_nick(event.censor)
data = ("{del_color}<{log_color}Message redacted by: " data = ("{del_color}<{log_color}Message redacted by: "
"{censor}{log_color}{reason}{del_color}>{ncolor}").format( "{censor}{log_color}{reason}{del_color}>{ncolor}").format(
del_color=W.color("chat_delimiters"), del_color=W.color("chat_delimiters"),
ncolor=W.color("reset"), ncolor=W.color("reset"),
log_color=W.color("logger.color.backlog_line"), log_color=W.color("logger.color.backlog_line"),
censor=censor.name, censor=censor,
reason=reason) reason=reason)
self.weechat_buffer.message(user.name, data, date, tags) self.weechat_buffer.message(nick, data, date, tags)
def _handle_topic(self, event, is_state): def _handle_topic(self, event, is_state):
try: nick = self.find_nick(event.sender)
user = self.room.users[event.sender]
nick = user.name
except KeyError:
nick = event.sender
self.weechat_buffer.change_topic( self.weechat_buffer.change_topic(
nick, nick,
@ -801,22 +810,22 @@ class RoomBuffer(object):
elif isinstance(event, RoomTopicEvent): elif isinstance(event, RoomTopicEvent):
self._handle_topic(event, False) self._handle_topic(event, False)
elif isinstance(event, RoomMessageText): elif isinstance(event, RoomMessageText):
user = self.room.users[event.sender] nick = self.find_nick(event.sender)
data = (event.formatted_message.to_weechat() data = (event.formatted_message.to_weechat()
if event.formatted_message else event.message) if event.formatted_message else event.message)
date = server_ts_to_weechat(event.timestamp) date = server_ts_to_weechat(event.timestamp)
self.weechat_buffer.message( self.weechat_buffer.message(
user.name, nick,
data, data,
date, date,
self.get_event_tags(event) self.get_event_tags(event)
) )
elif isinstance(event, RoomMessageEmote): elif isinstance(event, RoomMessageEmote):
user = self.room.users[event.sender] nick = self.find_nick(event.sender)
date = server_ts_to_weechat(event.timestamp) date = server_ts_to_weechat(event.timestamp)
self.weechat_buffer.action( self.weechat_buffer.action(
user.name, nick,
event.message, event.message,
date, date,
self.get_event_tags(event) self.get_event_tags(event)
@ -827,15 +836,15 @@ class RoomBuffer(object):
self._handle_redacted_message(event) self._handle_redacted_message(event)
def self_message(self, message): def self_message(self, message):
user = self.room.users[self.room.own_user_id] nick = self.find_nick(self.room.own_user_id)
data = (message.formatted_message.to_weechat() data = (message.formatted_message.to_weechat()
if message.formatted_message if message.formatted_message
else message.message) else message.message)
date = server_ts_to_weechat(message.timestamp) date = server_ts_to_weechat(message.timestamp)
self.weechat_buffer.self_message(user.name, data, date) self.weechat_buffer.self_message(nick, data, date)
def self_action(self, message): def self_action(self, message):
user = self.room.users[self.room.own_user_id] nick = self.find_nick(self.room.own_user_id)
date = server_ts_to_weechat(message.timestamp) date = server_ts_to_weechat(message.timestamp)
self.weechat_buffer.self_action(user.name, message.message, date) self.weechat_buffer.self_action(nick, message.message, date)

View file

@ -180,8 +180,6 @@ class MatrixUser:
self.name = name # type: str self.name = name # type: str
self.display_name = display_name # type: str self.display_name = display_name # type: str
self.power_level = 0 # type: int self.power_level = 0 # type: int
self.nick_color = "" # type: str
self.prefix = "" # type: str
# yapf: enable # yapf: enable