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 3 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
32 changes: 27 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,41 @@ 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

brackets_regex = re.compile(r"""
^\[ # starts with [
.* # any number of any characters
[^\s] # not a whitespace
[^\]] # not a closing bracket
\] # ends with ]
""", re.VERBOSE)

"""If the message includes text in square brackets, use that as name for the thread. Otherwise, use the name template."""
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
if (match:= re.match(brackets_regex, message.content)) is not None:
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
content = match.group()[1:-1]
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
content = content.split(",")
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
processed_content = []
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
for word in content:
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
processed_content.append(word.lstrip().strip())
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
name = ", ".join(processed_content)
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
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
59 changes: 59 additions & 0 deletions tests/test_lib_thread_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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

# test if brackets are not closed and if there is no text in brackets
dariagrudzien marked this conversation as resolved.
Show resolved Hide resolved
@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

# test if there is no text in brackets
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

# test if there is no text in brackets
@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