From 8ccdba8904b4a1d374741db6cd2fea8b391b647b Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 30 Mar 2022 18:59:38 +0200 Subject: [PATCH] modules/CommandJoinActive.py --- modules/CommandJoinActive.py | 93 ------------------------------------ 1 file changed, 93 deletions(-) delete mode 100644 modules/CommandJoinActive.py diff --git a/modules/CommandJoinActive.py b/modules/CommandJoinActive.py deleted file mode 100644 index 172b3af..0000000 --- a/modules/CommandJoinActive.py +++ /dev/null @@ -1,93 +0,0 @@ -# Mattermost Bot module. -# Copyright (c) 2016-2022 by Someone (aka. Jan Vales ) -# published under MIT-License - -import datetime -import threading -from inspect import cleandoc - -import logging -logger = logging.getLogger(__name__) - -# pylint: disable=wrong-import-position -from AbstractCommand import AbstractCommand -class CommandJoinActive(AbstractCommand): - TRIGGER = "join-active" - CONFIG = {"display_name": "somebot-command", "auto_complete": True, - "auto_complete_hint": "[]" - } - USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PRIVATE+"Join active public channels. active = '10 posts+ in the last x months'. There is no undo." - - - def __init__(self, team_id, skip_chann_ids): - super().__init__(team_id) - - self.skip_chann_ids = skip_chann_ids - self.channel_index = None - self.channel_index_th = None - - - def on_register(self): - self._create_slash_command() - self.channel_index_th = threading.Timer(15.0, self._index_channels) - self.channel_index_th.setName("CommandJoinActive::_index_channels()") - self.channel_index_th.setDaemon(True) - self.channel_index_th.start() - - - def _index_channels(self): - if self.channel_index_th != threading.current_thread(): - logger.warning("_index_channels(): thread mismatch") - return - - logger.info("_index_channels(): started") - new_dict = {} - for chan in self.bot.api.get_team_channels(self.TEAM_ID): - if chan["id"] in self.skip_chann_ids: - continue - - posts = [] - for post in self.bot.api.get_posts_for_channel(chan["id"]): - if post["type"] in ["", "slack_attachement"]: - posts.append(post) - - if len(posts) == 5: - break - - new_dict[chan["id"]] = min([x["create_at"] for x in posts]) - - self.channel_index = new_dict - self.channel_index_th = threading.Timer(3600.0, self._index_channels) - self.channel_index_th.setName("CommandJoinActive::_index_channels()") - self.channel_index_th.setDaemon(True) - self.channel_index_th.start() - logger.info("_index_channels(): done") - - - def on_POST(self, request, data): - if self.channel_index is None: - request.respond_cmd_err("``/"+self.TRIGGER+"`` The channel indexer didnt finish yet. try again in a few minutes") - return - - try: - td = datetime.timedelta(weeks=int(data["text"].strip())) - except: - td = datetime.timedelta(weeks=2) - - timestamp = int((datetime.datetime.now()-td).timestamp())*1000 - - msg = "" - for chan in self.bot.api.get_team_channels(self.TEAM_ID): - #check against ban list and get the last 10 posts, if all of them are newer than min_timestamp: join - if chan["id"] not in self.channel_index and chan["id"] not in self.skip_chann_ids and self.channel_index[chan["id"]] > timestamp: - self.bot.api.add_user_to_channel(chan["id"], data["user_id"]) - msg += "\n + ~"+chan["name"] - - request.respond_cmd_temp(cleandoc(""" - ## :white_check_mark: Success! :) - By default it wont join you into channels that had less than 5 messages posted within the last 2 weeks. - If you want to be joined into less active channels give the command an integer number of weeks to to consider. - /join-active 8 will join you into channels that had less than 5 messages posted within the last 2 months. - Anyway: joined the following active public channels in the team (A reload might be needed to display the updated list of channels): - {} - """).format(msg)) -- 2.43.0