|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from aiohttp import web |
| 6 | +from slack import methods |
| 7 | +from slack import actions |
| 8 | +from slack.events import Message |
| 9 | +from slack.exceptions import SlackAPIError, RateLimited |
| 10 | + |
| 11 | +from pyslackersweb.sirbot import settings, models |
| 12 | +from pyslackersweb.util.log import ContextAwareLoggerAdapter |
| 13 | + |
| 14 | +logger = ContextAwareLoggerAdapter(logging.getLogger(__name__)) |
| 15 | + |
| 16 | + |
| 17 | +async def topic_change_revert(request: web.Request, action: actions.Action) -> None: |
| 18 | + response = Message() |
| 19 | + response["channel"] = action["channel"]["id"] |
| 20 | + response["ts"] = action["message_ts"] |
| 21 | + response["attachments"] = action["original_message"]["attachments"] |
| 22 | + response["attachments"][0]["color"] = "danger" |
| 23 | + response["attachments"][0]["text"] = f'Change reverted by <@{action["user"]["id"]}>' |
| 24 | + del response["attachments"][0]["actions"] |
| 25 | + |
| 26 | + data = json.loads(action["actions"][0]["value"]) |
| 27 | + await request.app["slack_client"].query( |
| 28 | + url=methods.CHANNELS_SET_TOPIC, |
| 29 | + data={"channel": data["channel"], "topic": data["old_topic"]}, |
| 30 | + ) |
| 31 | + |
| 32 | + await request.app["slack_client"].query(url=action["response_url"], data=response) |
| 33 | + |
| 34 | + |
| 35 | +async def topic_change_validate(request: web.Request, action: actions.Action) -> None: |
| 36 | + response = Message() |
| 37 | + response["channel"] = action["channel"]["id"] |
| 38 | + response["ts"] = action["message_ts"] |
| 39 | + response["attachments"] = action["original_message"]["attachments"] |
| 40 | + response["attachments"][0]["color"] = "good" |
| 41 | + response["attachments"][0]["text"] = f'Change validated by <@{action["user"]["id"]}>' |
| 42 | + del response["attachments"][0]["actions"] |
| 43 | + |
| 44 | + await request.app["slack_client"].query(url=action["response_url"], data=response) |
| 45 | + |
| 46 | + |
| 47 | +async def purpose_change_revert(request: web.Request, action: actions.Action) -> None: |
| 48 | + response = Message() |
| 49 | + response["channel"] = action["channel"]["id"] |
| 50 | + response["ts"] = action["message_ts"] |
| 51 | + response["attachments"] = action["original_message"]["attachments"] |
| 52 | + response["attachments"][0]["color"] = "danger" |
| 53 | + response["attachments"][0]["text"] = f'Change reverted by <@{action["user"]["id"]}>' |
| 54 | + del response["attachments"][0]["actions"] |
| 55 | + |
| 56 | + data = json.loads(action["actions"][0]["value"]) |
| 57 | + await request.app["slack_client"].query( |
| 58 | + url=methods.CHANNELS_SET_PURPOSE, |
| 59 | + data={"channel": data["channel"], "purpose": data["old_purpose"]}, |
| 60 | + ) |
| 61 | + |
| 62 | + await request.app["slack_client"].query(url=action["response_url"], data=response) |
| 63 | + |
| 64 | + |
| 65 | +async def purpose_change_validate(request: web.Request, action: actions.Action) -> None: |
| 66 | + response = Message() |
| 67 | + response["channel"] = action["channel"]["id"] |
| 68 | + response["ts"] = action["message_ts"] |
| 69 | + response["attachments"] = action["original_message"]["attachments"] |
| 70 | + response["attachments"][0]["color"] = "good" |
| 71 | + response["attachments"][0]["text"] = f'Change validated by <@{action["user"]["id"]}>' |
| 72 | + del response["attachments"][0]["actions"] |
| 73 | + |
| 74 | + await request.app["slack_client"].query(url=action["response_url"], data=response) |
| 75 | + |
| 76 | + |
| 77 | +async def pin_added_validate(request: web.Request, action: actions.Action) -> None: |
| 78 | + response = Message() |
| 79 | + response["channel"] = action["channel"]["id"] |
| 80 | + response["ts"] = action["message_ts"] |
| 81 | + response["attachments"] = action["original_message"]["attachments"] |
| 82 | + response["attachments"][0]["color"] = "good" |
| 83 | + response["attachments"][0]["pretext"] = f'Pin validated by <@{action["user"]["id"]}>' |
| 84 | + del response["attachments"][0]["actions"] |
| 85 | + |
| 86 | + await request.app["slack_client"].query(url=action["response_url"], data=response) |
| 87 | + |
| 88 | + |
| 89 | +async def pin_added_revert(request: web.Request, action: actions.Action) -> None: |
| 90 | + response = Message() |
| 91 | + |
| 92 | + response["channel"] = action["channel"]["id"] |
| 93 | + response["ts"] = action["message_ts"] |
| 94 | + response["attachments"] = action["original_message"]["attachments"] |
| 95 | + response["attachments"][0]["color"] = "danger" |
| 96 | + response["attachments"][0]["pretext"] = f'Pin reverted by <@{action["user"]["id"]}>' |
| 97 | + del response["attachments"][0]["actions"] |
| 98 | + |
| 99 | + action_data = json.loads(action["actions"][0]["value"]) |
| 100 | + remove_data = {"channel": action_data["channel"]} |
| 101 | + |
| 102 | + if action_data["item_type"] == "message": |
| 103 | + remove_data["timestamp"] = action_data["item_id"] |
| 104 | + elif action_data["item_type"] == "file": |
| 105 | + remove_data["file"] = action_data["item_id"] |
| 106 | + elif action_data["item_type"] == "file_comment": |
| 107 | + remove_data["file_comment"] = action_data["item_id"] |
| 108 | + else: |
| 109 | + raise TypeError(f'Unknown pin type: {action_data["type"]}') |
| 110 | + |
| 111 | + try: |
| 112 | + await request.app["slack_client"].query(url=methods.PINS_REMOVE, data=remove_data) |
| 113 | + except SlackAPIError as e: |
| 114 | + if e.error != "no_pin": |
| 115 | + raise |
| 116 | + |
| 117 | + await request.app["slack_client"].query(url=action["response_url"], data=response) |
| 118 | + |
| 119 | + |
| 120 | +async def admin_msg(request: web.Request, action: actions.Action) -> None: |
| 121 | + admin_msg = Message() |
| 122 | + admin_msg["channel"] = settings.SLACK_ADMIN_CHANNEL |
| 123 | + admin_msg["attachments"] = [ |
| 124 | + { |
| 125 | + "fallback": f'Message from {action["user"]["name"]}', |
| 126 | + "title": f'Message from <@{action["user"]["id"]}>', |
| 127 | + "color": "good", |
| 128 | + "text": action["submission"]["message"], |
| 129 | + } |
| 130 | + ] |
| 131 | + |
| 132 | + await request.app["slack_client"].query(url=methods.CHAT_POST_MESSAGE, data=admin_msg) |
| 133 | + |
| 134 | + response = Message() |
| 135 | + response["response_type"] = "ephemeral" |
| 136 | + response["text"] = "Thank you for your message." |
| 137 | + |
| 138 | + await request.app["slack_client"].query(url=action["response_url"], data=response) |
| 139 | + |
| 140 | + |
| 141 | +async def user_cleanup(request: web.Request, action: actions.Action) -> None: |
| 142 | + user_id = action["actions"][0]["value"] |
| 143 | + |
| 144 | + response = Message() |
| 145 | + response["text"] = f"Cleanup of <@{user_id}> triggered by <@{action['user']['id']}>" |
| 146 | + |
| 147 | + await request.app["slack_client"].query(url=action["response_url"], data=response) |
| 148 | + |
| 149 | + asyncio.create_task(_cleanup_user(request.app, user_id)) |
| 150 | + |
| 151 | + |
| 152 | +async def _cleanup_user(app: web.Application, user: str) -> None: |
| 153 | + try: |
| 154 | + async with app["pg"].acquire() as conn: |
| 155 | + messages = await conn.fetch( |
| 156 | + """SELECT id, channel FROM slack_messages WHERE "user" = $1 ORDER BY send_at DESC""", |
| 157 | + user, |
| 158 | + ) |
| 159 | + |
| 160 | + for message in messages: |
| 161 | + await _delete_message(app["slack_client"], message) |
| 162 | + except Exception: |
| 163 | + logger.exception("Unexpected exception cleaning up user %s", user) |
| 164 | + |
| 165 | + |
| 166 | +async def _delete_message(slack, message: dict) -> None: |
| 167 | + data = {"channel": message["channel"], "ts": message["id"]} |
| 168 | + try: |
| 169 | + await slack.query(url=methods.CHAT_DELETE, data=data) |
| 170 | + except RateLimited: |
| 171 | + logger.debug("sleeping") |
| 172 | + await asyncio.sleep(20) |
| 173 | + await _delete_message(slack, message) |
| 174 | + except SlackAPIError as e: |
| 175 | + if e.error == "message_not_found": |
| 176 | + return |
| 177 | + else: |
| 178 | + logger.exception( |
| 179 | + "Failed to cleanup message %s in channel %s", message["id"], message["channel"], |
| 180 | + ) |
| 181 | + except Exception: |
| 182 | + logger.exception( |
| 183 | + "Failed to cleanup message %s in channel %s", message["id"], message["channel"], |
| 184 | + ) |
0 commit comments