Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 38 additions & 5 deletions src/core/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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)
4 changes: 3 additions & 1 deletion src/core/wikithoughts.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ 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(
Expand Down Expand Up @@ -287,6 +287,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
Expand Down
3 changes: 2 additions & 1 deletion src/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading