matrix: Change the way the topic is changed.

This commit is contained in:
poljar (Damir Jelić) 2018-07-05 15:46:45 +02:00
parent 38d6a14a33
commit 24b412d73c
3 changed files with 61 additions and 90 deletions

View file

@ -139,7 +139,6 @@ class WeechatChannelBuffer(object):
self.name = "" self.name = ""
self.users = {} # type: Dict[str, RoomUser] self.users = {} # type: Dict[str, RoomUser]
self.topic = ""
self.topic_author = "" self.topic_author = ""
self.topic_date = None self.topic_date = None
@ -425,15 +424,21 @@ class WeechatChannelBuffer(object):
nick=user.nick, nick=user.nick,
chan_color=W.color("chat_channel"), chan_color=W.color("chat_channel"),
ncolor=W.color("reset"), ncolor=W.color("reset"),
room=self.name, room=self.short_name,
topic=topic topic=topic
) )
self.print_date_tags(data, date, tags) self.print_date_tags(data, date, tags)
def topic(self, nick, topic, date, message=True): @property
def topic(self):
return W.buffer_get_string(self._ptr, "title")
@topic.setter
def topic(self, topic):
W.buffer_set(self._ptr, "title", topic) W.buffer_set(self._ptr, "title", topic)
def change_topic(self, nick, topic, date, message=True):
if message: if message:
self._print_topic(nick, topic, date) self._print_topic(nick, topic, date)
@ -451,3 +456,11 @@ class WeechatChannelBuffer(object):
tags = self._message_tags(user, "self_message") tags = self._message_tags(user, "self_message")
tags.append(SCRIPT_NAME + "_action") tags.append(SCRIPT_NAME + "_action")
self.action(nick, message, date, tags) self.action(nick, message, date, tags)
@property
def short_name(self):
return W.buffer_get_string(self._ptr, "short_name")
@short_name.setter
def short_name(self, name):
W.buffer_set(self._ptr, "short_name", name)

View file

@ -566,44 +566,6 @@ class RoomMemberJoin(RoomEvent):
return cls(event_id, sender, timestamp, display_name) return cls(event_id, sender, timestamp, display_name)
def execute(self, server, room, buff, tags):
short_name = shorten_sender(self.sender)
if self.sender in room.users:
user = room.users[self.sender]
if self.display_name:
user.display_name = self.display_name
else:
user = MatrixUser(short_name, self.display_name)
if not user.nick_color:
if self.sender == server.user_id:
highlight_words = [self.sender, user.name]
if self.display_name:
highlight_words.append(self.display_name)
user.nick_color = "weechat.color.chat_nick_self"
W.buffer_set(buff, "highlight_words", ",".join(highlight_words))
else:
user.nick_color = W.info_get("nick_color_name", user.name)
room.users[self.sender] = user
nick_pointer = W.nicklist_search_nick(buff, "", self.sender)
if not nick_pointer:
add_user_to_nicklist(buff, self.sender, user)
# calculate room display name and set it as the buffer list name
room_name = room.display_name(server.user_id)
# A user has joined an encrypted room, we need to check for new devices
if room.encrypted:
server.device_check_timestamp = None
W.buffer_set(buff, "short_name", room_name)
class RoomMemberLeave(RoomEvent): class RoomMemberLeave(RoomEvent):
@ -675,43 +637,6 @@ class RoomPowerLevels(RoomEvent):
self._set_power_level(room, buff, level) self._set_power_level(room, buff, level)
class RoomTopiceMessage(RoomEvent):
def __init__(self, event_id, sender, timestamp, topic):
self.topic = topic
RoomEvent.__init__(self, event_id, sender, timestamp)
def execute(self, server, room, buff, tags):
topic = self.topic
nick, color_name = sender_to_nick_and_color(room, self.sender)
author = ("{nick_color}{user}{ncolor}").format(
nick_color=W.color(color_name), user=nick, ncolor=W.color("reset"))
# TODO print old topic if configured so
if room.is_named():
message = ("{prefix}{nick} has changed "
"the topic for {chan_color}{room}{ncolor} "
"to \"{topic}\"").format(
prefix=W.prefix("network"),
nick=author,
chan_color=W.color("chat_channel"),
ncolor=W.color("reset"),
room=room.named_room_name(),
topic=topic)
else:
message = ('{prefix}{nick} has changed the topic to '
'"{topic}"').format(
prefix=W.prefix("network"),
nick=author,
topic=topic)
tags = ["matrix_topic", "log3", "matrix_id_{}".format(self.event_id)]
date = server_ts_to_weechat(self.timestamp)
W.prnt_date_tags(buff, date, ",".join(tags), message)
class RoomTopicEvent(RoomEvent): class RoomTopicEvent(RoomEvent):
def __init__(self, event_id, sender, timestamp, topic): def __init__(self, event_id, sender, timestamp, topic):
@ -728,16 +653,6 @@ class RoomTopicEvent(RoomEvent):
return cls(event_id, sender, timestamp, topic) return cls(event_id, sender, timestamp, topic)
def execute(self, server, room, buff, tags):
topic = self.topic
W.buffer_set(buff, "title", topic)
room.topic = topic
room.topic_author = self.sender
room.topic_date = datetime.fromtimestamp(
server_ts_to_weechat(self.timestamp))
class RoomRedactionEvent(RoomEvent): class RoomRedactionEvent(RoomEvent):

View file

@ -40,7 +40,9 @@ from .rooms import (
RoomMessageText, RoomMessageText,
RoomMessageEmote, RoomMessageEmote,
MatrixUser, MatrixUser,
RoomMemberJoin RoomMemberJoin,
RoomMemberLeave,
RoomTopicEvent
) )
from matrix.api import ( from matrix.api import (
MatrixClient, MatrixClient,
@ -629,7 +631,13 @@ class MatrixServer:
server_buffer_prnt(self, pprint.pformat(message.request.payload)) server_buffer_prnt(self, pprint.pformat(message.request.payload))
server_buffer_prnt(self, pprint.pformat(message.response.body)) server_buffer_prnt(self, pprint.pformat(message.response.body))
def handle_room_event(self, room, room_buffer, event, is_state_event): def handle_room_membership_events(
self,
room,
room_buffer,
event,
is_state_event
):
if isinstance(event, RoomMemberJoin): if isinstance(event, RoomMemberJoin):
if event.sender in room.users: if event.sender in room.users:
user = room.users[event.sender] user = room.users[event.sender]
@ -639,6 +647,8 @@ class MatrixServer:
short_name = shorten_sender(event.sender) short_name = shorten_sender(event.sender)
user = MatrixUser(short_name, event.display_name) user = MatrixUser(short_name, event.display_name)
buffer_user = RoomUser(user.name, event.sender) buffer_user = RoomUser(user.name, event.sender)
# TODO remove this duplication
user.nick_color = buffer_user.color
room.users[event.sender] = user room.users[event.sender] = user
if self.user_id == event.sender: if self.user_id == event.sender:
@ -650,6 +660,39 @@ class MatrixServer:
server_ts_to_weechat(event.timestamp), server_ts_to_weechat(event.timestamp),
not is_state_event not is_state_event
) )
# calculate room display name and set it as the buffer list name
room_name = room.display_name(self.user_id)
room_buffer.short_name = room_name
# A user has joined an encrypted room, we need to check for
# new devices
if room.encrypted:
self.device_check_timestamp = None
elif isinstance(event, RoomMemberLeave):
pass
def handle_room_event(self, room, room_buffer, event, is_state_event):
if isinstance(event, (RoomMemberJoin, RoomMemberLeave)):
self.handle_room_membership_events(
room,
room_buffer,
event,
is_state_event
)
elif isinstance(event, RoomTopicEvent):
try:
user = room.users[event.sender]
nick = user.name
except KeyError:
nick = event.sender
room_buffer.change_topic(
nick,
event.topic,
server_ts_to_weechat(event.timestamp),
not is_state_event
)
else: else:
tags = tags_for_message("message") tags = tags_for_message("message")
event.execute(self, room, room_buffer._ptr, tags) event.execute(self, room, room_buffer._ptr, tags)