Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daria/thread names #27

Merged
merged 5 commits into from
Sep 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 29 additions & 5 deletions juniorguru_chick/lib/threads.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime

import discord
import re


DAYS = ["Pondělní", "Úterní", "Středeční",
Expand All @@ -23,20 +24,43 @@ async def fetch_starting_message(thread: discord.Thread) -> discord.Message | No
except discord.errors.NotFound:
return None

def name_thread(message: discord.Message, name_template) -> str | None:
honzajavorek marked this conversation as resolved.
Show resolved Hide resolved
"""If the message includes text in square brackets, use that as name for the thread. Otherwise, use the name template."""
brackets_regex = re.compile(r"""
^
\[ # starts with [
(?P<bracket_content>
.* # any number of any characters
[^\s] # not a whitespace
[^\]] # not a closing bracket
)
\] # ends with ]
""", re.VERBOSE)

if (match:= re.match(brackets_regex, message.content)) is not None:
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
content = match.group("bracket_content")
parts = content.split(",")
words = []
for part in parts:
words.append(part.strip())
name = ", ".join(words)
return name

else:
weekday = datetime.now().weekday()
name = name_template.format(weekday=DAYS[weekday], author=message.author.display_name)
return name

async def create_thread(message: discord.Message, name_template) -> discord.Thread:
"""Creates a new thread for given message"""
weekday = datetime.now().weekday()
name = name_template.format(weekday=DAYS[weekday], author=message.author.display_name)
name = name_thread(message, name_template)
return await message.create_thread(name=name)


async def ensure_thread_name(thread: discord.Thread, name_template) -> str | None:
"""Ensures given thread has a name"""
starting_message = await fetch_starting_message(thread)
if starting_message:
weekday = datetime.now().weekday()
name = name_template.format(weekday=DAYS[weekday], author=starting_message.author.display_name)
name = name_thread(starting_message, name_template)
if thread.name != name:
await thread.edit(name=name)
return name
Expand Down
56 changes: 56 additions & 0 deletions tests/test_lib_thread_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest
from datetime import datetime

from juniorguru_chick.lib.threads import name_thread

DAYS = ["Pondělní", "Úterní", "Středeční",
"Čtvrteční", "Páteční", "Sobotní", "Nedělní"]

class Message:
def __init__(self, display_name, message):
self.author = lambda: None
self.author.display_name = display_name
self.content = message

def test_name_thread_no_brackets():
content = "Hello this is my thread"
weekday = datetime.now().weekday()
name_template = "{weekday} objev od {author}"
expected_name = f"{DAYS[weekday]} objev od Jana"
message = Message("Jana", content)

assert name_thread(message, name_template) == expected_name

@pytest.mark.parametrize('content', [
pytest.param("[ Hello this is my thread", id="brackets not closed"),
pytest.param("[] Hello this is my thread", id="no text in brackets"),
pytest.param("Hello this is my thread]", id="brackets not opened")])
def test_brackets_used_incorrectly(content):
weekday = datetime.now().weekday()
name_template = "{weekday} objev od {author}"
expected_name = f"{DAYS[weekday]} objev od Jana"
message = Message("Jana", content)

assert name_thread(message, name_template) == expected_name

def test_no_text_in_brackets():
content = "[] Hello this is my thread"
weekday = datetime.now().weekday()
name_template = "{weekday} objev od {author}"
expected_name = f"{DAYS[weekday]} objev od Jana"
message = Message("Jana", content)

assert name_thread(message, name_template) == expected_name

@pytest.mark.parametrize('content, expected_name', [
pytest.param("[eslint, nextjs]", "eslint, nextjs", id="comma separated strings with spaces"),
pytest.param("[eslint,nextjs]", "eslint, nextjs", id="comma separated strings without spaces"),
pytest.param("[ Java ]", "Java", id="whitespaces around string"),
pytest.param("[:css: CSS]", ":css: CSS", id="starting emoji"),
pytest.param("[eslint,nextjs, :css: CSS, ruby]", "eslint, nextjs, :css: CSS, ruby", id="emoji in the middle"),
pytest.param("[ eslint ] Hello", "eslint", id="brackets at the beginning")])
def test_parse_message_in_brackets(content, expected_name):
message = Message("Jana", content)
name_template = "{weekday} objev od {author}"

assert name_thread(message, name_template) == expected_name