aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2017-10-15 22:35:05 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2017-10-15 22:42:18 +0200
commit5aeb8609d224eafef3f63b613dad88451ee9fd74 (patch)
tree93e6f94e559c7b27fac8009e2378a506894ae1ae
parentfad6f861f41846255b77754c269cbe75748d80eb (diff)
downloadwee-slack-5aeb8609d224eafef3f63b613dad88451ee9fd74.tar.gz
refactor: Make use of message keys more clear and efficient
All of the times we used sorted_message_keys, we were accessing some of the last elements in the list. Since OrderedDict is doubly linked, using reversed(keys) and next/islice is much more efficient than converting it to a list. Because of this the method now returns a generator in reverse order. It is renamed to main_message_keys_reversed to make it clear that it isn't returning thread messages, and that it is now reversed.
-rw-r--r--wee_slack.py22
1 files changed, 11 insertions, 11 deletions
diff --git a/wee_slack.py b/wee_slack.py
index 38eed77..d25f8df 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -4,6 +4,7 @@ from __future__ import unicode_literals
from collections import OrderedDict
from functools import wraps
+from itertools import islice
import time
import json
@@ -1363,8 +1364,8 @@ class SlackChannel(object):
self.eventrouter.receive(s)
def my_last_message(self, msgno):
- for message in reversed(self.sorted_message_keys()):
- m = self.messages[message]
+ for key in self.main_message_keys_reversed():
+ m = self.messages[key]
if "user" in m.message_json and "text" in m.message_json and m.message_json["user"] == self.team.myidentifier:
msgno -= 1
if msgno == 0:
@@ -1394,14 +1395,15 @@ class SlackChannel(object):
def send_change_reaction(self, method, msg_number, reaction):
if 0 < msg_number < len(self.messages):
- timestamp = self.sorted_message_keys()[-msg_number]
+ keys = self.main_message_keys_reversed()
+ timestamp = next(islice(keys, msg_number - 1, None))
data = {"channel": self.identifier, "timestamp": timestamp, "name": reaction}
s = SlackRequest(self.team.token, method, data)
self.eventrouter.receive(s)
- def sorted_message_keys(self):
- return [key for key, message in self.messages.items()
- if type(message) == SlackMessage]
+ def main_message_keys_reversed(self):
+ return (key for key in reversed(self.messages)
+ if type(self.messages[key]) == SlackMessage)
# Typing related
def set_typing(self, user):
@@ -1443,8 +1445,7 @@ class SlackChannel(object):
def mark_read(self, ts=None, update_remote=True, force=False):
if not ts:
- msg_timestamps = self.sorted_message_keys()
- ts = msg_timestamps[-1] if msg_timestamps else SlackTS()
+ ts = next(self.main_message_keys_reversed(), SlackTS())
if self.new_messages or force:
if self.channel_buffer:
w.buffer_set(self.channel_buffer, "unread", "")
@@ -2959,9 +2960,8 @@ def thread_command_callback(data, current_buffer, args):
elif args[0] == '/reply':
count = int(args[1])
msg = " ".join(args[2:])
- mkeys = channel.sorted_message_keys()
- mkeys.reverse()
- parent_id = str(mkeys[count - 1])
+ mkeys = channel.main_message_keys_reversed()
+ parent_id = str(next(islice(mkeys, count - 1, None)))
channel.send_message(msg, request_dict_ext={"thread_ts": parent_id})
return w.WEECHAT_RC_OK_EAT
w.prnt(current, "Invalid thread command.")