From 5ecf972d8bf9b412ebf3ff04a08672cb914b5de3 Mon Sep 17 00:00:00 2001 From: Weather Date: Mon, 6 Apr 2026 17:38:15 -0400 Subject: [PATCH 1/2] fix: Logs error --- src/core/wikithoughts.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/wikithoughts.py b/src/core/wikithoughts.py index ea430c1..0d9eb59 100644 --- a/src/core/wikithoughts.py +++ b/src/core/wikithoughts.py @@ -160,7 +160,8 @@ async def auth_bot() -> None: logger.info("Bot was authenticated successfully!") else: bot_authenticated = False - logger.warning("Bot was unable to authenticate!") + logger.warning(f"Bot was unable to authenticate! Response: {returned_json}") + def headers_formatting( @@ -287,6 +288,8 @@ async def fetch_category_pages(response: httpx.Response) -> list[str]: logger.warning( f"Failed to reauthenticate the bot! Attempt: {failed_authentication_attempts}" ) + else: + logger.info("Bot was able to re-auth during runtime!") failed_authentication_attempts += 1 continue From a24ee0cf0de11eb2c81ea8d0c275e8cf2ae51a78 Mon Sep 17 00:00:00 2001 From: Weather Date: Mon, 6 Apr 2026 21:02:24 -0400 Subject: [PATCH 2/2] feat: Slack announcments displays username --- src/api/endpoints.py | 5 +++-- src/core/slack.py | 43 +++++++++++++++++++++++++++++++++++----- src/core/wikithoughts.py | 1 - src/static/js/main.js | 3 ++- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/api/endpoints.py b/src/api/endpoints.py index 34b10b1..b0f0421 100644 --- a/src/api/endpoints.py +++ b/src/api/endpoints.py @@ -48,7 +48,7 @@ def get_announcement() -> JSONResponse: JSONResponse: A JSON response containing the announcement data. """ - return JSONResponse({"data": slack.get_announcement()}) + return JSONResponse(slack.get_announcement()) @router.post("/slack/events") @@ -127,7 +127,8 @@ async def message_actions(payload: str = Form(...)) -> JSONResponse: form_json.get("actions", [{}])[0].get("value", '{text:""}') ).get("text", None) - slack.add_announcement(message_object) + user_id = form_json.get("user", {}).get("id") + slack.add_announcement(message_object, user_id) if response_url: async with httpx.AsyncClient() as client: diff --git a/src/core/slack.py b/src/core/slack.py index 885d53a..e85070e 100644 --- a/src/core/slack.py +++ b/src/core/slack.py @@ -15,12 +15,15 @@ client: AsyncWebClient | None = None + try: client = AsyncWebClient(token=SLACK_API_TOKEN) except Exception as e: logger.error(f"Failed to initialize Slack client: {e}") -announcements: list[str] = ["Welcome to Jumpstart!"] +announcements: list[dict[str, str]] = [ + {"content": "Welcome to Jumpstart!", "user": "Jumpstart"} +] def clean_text(raw: str) -> str: @@ -63,6 +66,31 @@ async def gather_emojis() -> dict: return emojis +async def get_username(user_id: str) -> str: + """ + Attempts to retrieve a slack username relating to a user id + + Args: + user_id (str): The ID of the user to retrieve + + Returns: + str: The username, or an empty string if not applicable + """ + + response = await client.users_info(user=user_id) + user = response.get("user", None) + + if user is None: + logger.warning(f"Unable to find a user under the id {user_id}") + return "Unknown" + + display_name = user.get("profile", {}).get("display_name", None) + real_name = user.get("real_name", None) + username = user.get("name", None) + + return real_name or display_name or username or "Unknown" + + async def request_upload_via_dm(user_id: str, announcement_text: str) -> None: """ Sends a DM to the user with the announcement text and a prompt to post it to Jumpstart. @@ -119,12 +147,12 @@ def convert_user_response_to_bool(message_data: dict) -> bool: return user_response -def get_announcement() -> str | None: +def get_announcement() -> dict[str, str] | None: """ Returns the next announcement in the queue. Returns: - str | None: The next announcement text, or None if there are no announcements. + dict | None: The next announcement text and user, or None if there are no announcements. """ if len(announcements) == 0: @@ -136,16 +164,21 @@ def get_announcement() -> str | None: return announcements.pop(0) -def add_announcement(announcement_text: str) -> None: +def add_announcement(announcement_text: str, user_id: str) -> None: """ Adds an announcement to the queue. Args: announcement_text (str): The text of the announcement to be added. + user_id (str): The user_id of the person """ if announcement_text is None or announcement_text.strip() == "": logger.warning("Attempted to add empty announcement, skipping!") return - announcements.append(announcement_text) + username: str = get_username(user_id=user_id) + + new_addition: dict[str, str] = {"content": announcement_text, "user": username} + + announcements.append(new_addition) diff --git a/src/core/wikithoughts.py b/src/core/wikithoughts.py index 0d9eb59..c0110ab 100644 --- a/src/core/wikithoughts.py +++ b/src/core/wikithoughts.py @@ -163,7 +163,6 @@ async def auth_bot() -> None: logger.warning(f"Bot was unable to authenticate! Response: {returned_json}") - def headers_formatting( new_etag: str | None = None, new_last_modified: str | None = None ) -> dict[str, str]: diff --git a/src/static/js/main.js b/src/static/js/main.js index 8c76e5e..c7e0af4 100644 --- a/src/static/js/main.js +++ b/src/static/js/main.js @@ -123,7 +123,8 @@ async function mediumUpdate() { const announcementData = await announcementRes.json(); $("#wikipageheader").text(wikiData.page + " - csh/Wikithoughts") $("#wikipagetext").text(wikiData.content); - $("#announcement").text(announcementData.data.substring(0, 910)); + $("#announcement").text(announcementData.content.substring(0, 910)); + $("#announcements-text-header").text("Announcements - " + announcementData.user) } catch (err) { console.log(err); }