From 88569167e13951c29f87488269b42da90e99fd67 Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot] /join-active --- modules/CommandJoinActive.py | 89 ++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 modules/CommandJoinActive.py diff --git a/modules/CommandJoinActive.py b/modules/CommandJoinActive.py new file mode 100644 index 0000000..9b04fd7 --- /dev/null +++ b/modules/CommandJoinActive.py @@ -0,0 +1,89 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License + +import datetime +import threading +from inspect import cleandoc + +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"] = "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(): + print("_index_channels(): thread mismatch") + return + + print("_index_channels(): started") + new_dict = dict() + for c in self.bot.api.get_team_channels(self.TEAM_ID): + if c["id"] in self.skip_chann_ids: + continue + + posts = [] + for p in self.bot.api.get_posts_for_channel(c["id"]): + if p["type"] in ["", "slack_attachement"]: + posts.append(p) + + if len(posts) == 5: + break + + new_dict[c["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() + print("_index_channels(): done") + + + def on_POST(self, request, data): + if self.channel_index is None: + request.cmd_respond_text_temp("### ``Failed`` :(\nThe 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 c 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 not c["id"] in self.skip_chann_ids and self.channel_index[c["id"]] > timestamp: + self.bot.api.add_user_to_channel(c["id"], data["user_id"]) + msg += "\n + ~"+c["name"] + + request.cmd_respond_text_temp(cleandoc(""" + ### ``Done`` :) + 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