From f4a9ec897b603b1664d9b28db7bb539e31fe533a Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Sat, 16 May 2020 23:00:57 +0200 Subject: Refactor message id handling This creates a common method for getting a message from a message hash or count. Since all commands now use the same method, all commands that accepts a hash now also accepts a count. --- wee_slack.py | 184 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 93 insertions(+), 91 deletions(-) (limited to 'wee_slack.py') diff --git a/wee_slack.py b/wee_slack.py index ca16626..68bb3e9 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -329,6 +329,14 @@ def print_error(message, buffer='', warning=False): w.prnt(buffer, '{}{}: {}'.format(w.prefix('error'), prefix, message)) +def print_message_not_found_error(msg_id): + if msg_id: + print_error("Invalid id given, must be an existing id or a number greater " + + "than 0 and less than the number of messages in the channel") + else: + print_error("No messages found in channel") + + def token_for_print(token): return '{}...{}'.format(token[:15], token[-10:]) @@ -847,24 +855,15 @@ def buffer_input_callback(signal, buffer_ptr, data): if not channel: return w.WEECHAT_RC_ERROR - def get_id(message_id): - if not message_id: - return 1 - elif message_id[0] == "$": - return message_id[1:] - else: - return int(message_id) - reaction = re.match(r"{}{}\s*$".format(REACTION_PREFIX_REGEX_STRING, EMOJI_CHAR_OR_NAME_REGEX_STRING), data) substitute = re.match("{}?s/".format(MESSAGE_ID_REGEX_STRING), data) if reaction: emoji = reaction.group("emoji_char") or reaction.group("emoji_name") if reaction.group("reaction_change") == "+": - channel.send_add_reaction(get_id(reaction.group("msg_id")), emoji) + channel.send_add_reaction(reaction.group("msg_id"), emoji) elif reaction.group("reaction_change") == "-": - channel.send_remove_reaction(get_id(reaction.group("msg_id")), emoji) + channel.send_remove_reaction(reaction.group("msg_id"), emoji) elif substitute: - msg_id = get_id(substitute.group("msg_id")) try: old, new, flags = re.split(r'(? ') return w.WEECHAT_RC_OK_EAT - msg = get_msg_from_id(channel, msg_id) + message = channel.message_from_hash_or_index(msg_id) - if msg: - if isinstance(msg, SlackThreadMessage): - parent_id = str(msg.parent_message.ts) - else: - parent_id = str(msg.ts) - elif msg_id.isdigit() and int(msg_id) >= 1: - mkeys = reversed(channel.visible_messages) - parent_id = str(next(islice(mkeys, int(msg_id) - 1, None))) - else: - w.prnt('', 'ERROR: Invalid id given, must be a number greater than 0 or an existing id') - return w.WEECHAT_RC_OK_EAT + if not message: + print_message_not_found_error(args) + return w.WEECHAT_RC_OK_EAT + + if isinstance(message, SlackThreadMessage): + parent_id = str(message.parent_message.ts) + elif message: + parent_id = str(message.ts) channel.send_message(text, request_dict_ext={'thread_ts': parent_id, 'reply_broadcast': broadcast}) return w.WEECHAT_RC_OK_EAT @@ -4704,18 +4711,13 @@ def command_linkarchive(data, current_buffer, args): if isinstance(channel, SlackChannelCommon): url += 'archives/{}/'.format(channel.identifier) if args: - if args[0] == '$': - message_id = args[1:] - else: - message_id = args - ts = channel.hashed_messages.get(message_id) - message = channel.messages.get(ts) + message = channel.message_from_hash_or_index(args) if message: url += 'p{}{:0>6}'.format(message.ts.majorstr(), message.ts.minorstr()) if isinstance(message, SlackThreadMessage): url += "?thread_ts={}&cid={}".format(message.parent_message.ts, channel.identifier) else: - w.prnt('', 'ERROR: Invalid id given, must be an existing id') + print_message_not_found_error(args) return w.WEECHAT_RC_OK_EAT w.command(current_buffer, "/input insert {}".format(url)) @@ -4882,7 +4884,7 @@ def line_event_cb(data, signal, hashtable): reaction = EMOJI_CHAR_OR_NAME_REGEX.match(hashtable["_chat_eol"]) if reaction: emoji = reaction.group("emoji_char") or reaction.group("emoji_name") - channel.send_change_reaction("toggle", message_hash[1:], emoji) + channel.send_change_reaction("toggle", message_hash, emoji) else: data = "message" if data == "message": @@ -4892,7 +4894,7 @@ def line_event_cb(data, signal, hashtable): w.command(buffer_pointer, "/input send {}s///".format(message_hash)) elif data == "linkarchive": w.command(buffer_pointer, "/cursor stop") - w.command(buffer_pointer, "/slack linkarchive {}".format(message_hash[1:])) + w.command(buffer_pointer, "/slack linkarchive {}".format(message_hash)) elif data == "reply": w.command(buffer_pointer, "/cursor stop") w.command(buffer_pointer, "/input insert /reply {}\\x20".format(message_hash)) -- cgit