Skip to content

Commit ca6cd9b

Browse files
committed
refactor: Return post if found via fetching active or archived threads
1 parent 9fab4c8 commit ca6cd9b

6 files changed

Lines changed: 28 additions & 22 deletions

File tree

src/bot.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
SimpleProjectItemEvent,
1414
)
1515
from src.utils.error import ForumChannelNotFound
16-
from src.utils.logging import bot_info
16+
from src.utils.logging import bot_error, bot_info
1717
from src.utils.utils import fetch_forum_channel, get_new_tag, get_post_id, retrieve_discord_id
1818

1919

@@ -44,11 +44,11 @@ async def process_update(
4444
):
4545
event = await state.get()
4646
bot_info(f"Processing event for item: {event.name}")
47-
post_id = await get_post_id(event.name, discord_guild_id, forum_channel_id, client)
47+
post_id_or_post = await get_post_id(event.name, discord_guild_id, forum_channel_id, client)
4848
author_discord_id = retrieve_discord_id(event.sender)
4949
user_mentions = [author_discord_id] if author_discord_id else []
5050
user_text_mention = f"<@{author_discord_id}>" if author_discord_id else "nieznany użytkownik"
51-
if post_id is None:
51+
if post_id_or_post is None:
5252
bot_info(f"Post not found, creating new post for item: {event.name}")
5353
message = f"Nowy task stworzony {event.name} przez: {user_text_mention}.>"
5454
post: GuildPublicThread = await client.create_forum_post(
@@ -58,10 +58,16 @@ async def process_update(
5858
auto_archive_duration=10080,
5959
user_mentions=user_mentions,
6060
)
61+
elif isinstance(post_id_or_post, int):
62+
post = await client.fetch_channel(post_id_or_post)
6163
else:
62-
post = await client.fetch_channel(post_id)
64+
post = post_id_or_post
6365

6466
if not isinstance(post, GuildPublicThread):
67+
try:
68+
bot_error(f"Post with ID {post.id} is not a GuildPublicThread.")
69+
except AttributeError:
70+
bot_error(f"Post with ID {post_id_or_post} is not a Discord channel object.")
6571
return
6672

6773
if isinstance(event, SimpleProjectItemEvent):

src/tests/test_integration/test_bot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ async def test_basic_event_only_creation(
6969
mock_fetch_public_archived_threads.return_value = []
7070
mock_create_forum_post.return_value = None
7171
mock_shelve_open.return_value = MockShelf({})
72+
mock_create_forum_post.return_value = "created_forum_post"
7273
update_queue = asyncio.Queue()
7374
await update_queue.put(ProjectItemEvent(name="Test Item", sender="test_sender"))
7475
await run(update_queue, stop_after_one_event=True)

src/tests/test_unit/test_bot.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async def test_process_update_already_exists(
7777
):
7878
state = asyncio.Queue()
7979
await state.put(SimpleProjectItemEvent("mmmocking", "norbiros", SimpleProjectItemEventType.CREATED))
80-
mock_get_post_id.return_value = "1"
80+
mock_get_post_id.return_value = 1
8181
mock_retrieve_discord_id.return_value = "2137696742041"
8282

8383
await process_update(rest_client_mock, 1, 1, forum_channel_mock, state)
@@ -101,7 +101,7 @@ async def test_process_update_archived(
101101
):
102102
state = asyncio.Queue()
103103
await state.put(SimpleProjectItemEvent("audacity4", "norbiros", SimpleProjectItemEventType.ARCHIVED))
104-
mock_get_post_id.return_value = "621"
104+
mock_get_post_id.return_value = 621
105105
user_id = 2137696742041
106106
mock_retrieve_discord_id.return_value = user_id
107107
mock_fetch_channel.return_value = post_mock
@@ -130,7 +130,7 @@ async def test_process_update_restored(
130130
):
131131
state = asyncio.Queue()
132132
await state.put(SimpleProjectItemEvent("audacity4", "norbiros", SimpleProjectItemEventType.RESTORED))
133-
mock_get_post_id.return_value = "621"
133+
mock_get_post_id.return_value = 621
134134
user_id = 2137696742041
135135
mock_retrieve_discord_id.return_value = user_id
136136
mock_fetch_channel.return_value = post_mock
@@ -157,7 +157,7 @@ async def test_process_update_deleted(
157157
):
158158
state = asyncio.Queue()
159159
await state.put(SimpleProjectItemEvent("audacity4", "norbiros", SimpleProjectItemEventType.DELETED))
160-
mock_get_post_id.return_value = "621"
160+
mock_get_post_id.return_value = 621
161161
mock_retrieve_discord_id.return_value = "niepodam@norbiros.dev"
162162
mock_fetch_channel.return_value = post_mock
163163

@@ -180,7 +180,7 @@ async def test_process_update_assignees(
180180
):
181181
state = asyncio.Queue()
182182
await state.put(ProjectItemEditedAssignees("audacity4", "norbiros", ["norbiros"]))
183-
mock_get_post_id.return_value = "621"
183+
mock_get_post_id.return_value = 621
184184
user_id = 2137696742041
185185
mock_retrieve_discord_id.return_value = user_id
186186
mock_fetch_channel.return_value = post_mock
@@ -206,7 +206,7 @@ async def test_process_update_body(
206206
state = asyncio.Queue()
207207
new_body = "Nowy opis taska"
208208
await state.put(ProjectItemEditedBody("audacity4", "norbiros", new_body))
209-
mock_get_post_id.return_value = "621"
209+
mock_get_post_id.return_value = 621
210210
user_id = 2137696742041
211211
mock_retrieve_discord_id.return_value = user_id
212212
mock_fetch_channel.return_value = post_mock
@@ -232,7 +232,7 @@ async def test_process_update_title(
232232
state = asyncio.Queue()
233233
new_title = "Nowy opis taska"
234234
await state.put(ProjectItemEditedTitle("audacity4", "norbiros", new_title))
235-
mock_get_post_id.return_value = "621"
235+
mock_get_post_id.return_value = 621
236236
user_id = 2137696742041
237237
mock_retrieve_discord_id.return_value = user_id
238238
mock_fetch_channel.return_value = post_mock
@@ -258,7 +258,7 @@ async def test_process_update_single_select(
258258
):
259259
state = asyncio.Queue()
260260
await state.put(ProjectItemEditedSingleSelect("audacity4", "norbiros", "big", SingleSelectType.SIZE))
261-
mock_get_post_id.return_value = "621"
261+
mock_get_post_id.return_value = 621
262262
user_id = 2137696742041
263263
mock_retrieve_discord_id.return_value = user_id
264264
mock_fetch_channel.return_value = post_mock
@@ -287,7 +287,7 @@ async def test_process_update_single_select_tag_unavailable(
287287
):
288288
state = asyncio.Queue()
289289
await state.put(ProjectItemEditedSingleSelect("audacity4", "norbiros", "big", SingleSelectType.SIZE))
290-
mock_get_post_id.return_value = "621"
290+
mock_get_post_id.return_value = 621
291291
user_id = 2137696742041
292292
mock_retrieve_discord_id.return_value = user_id
293293
mock_fetch_channel.return_value = post_mock

src/tests/test_unit/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ async def test_get_post_id_active_thread(mock_shelve_open, mock_fetch_active_thr
172172
mock_shelve_open.return_value = mock_shelf
173173
mock_fetch_active_threads.return_value = [post_mock]
174174

175-
assert await utils.get_post_id("audacity4", 1, 1, rest_client_mock) == 621
175+
assert await utils.get_post_id("audacity4", 1, 1, rest_client_mock) == post_mock
176176
assert mock_shelf.get("audacity4") == 621
177177

178178

@@ -187,7 +187,7 @@ async def test_get_post_id_archived_thread(
187187
mock_fetch_active_threads.return_value = []
188188
mock_fetch_public_archived_threads.return_value = [post_mock]
189189

190-
assert await utils.get_post_id("audacity4", 1, 1, rest_client_mock) == 621
190+
assert await utils.get_post_id("audacity4", 1, 1, rest_client_mock) == post_mock
191191
assert mock_shelf.get("audacity4") == 621
192192

193193

src/utils/logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def bot_info(text: str):
1313
# def bot_warning(text: str):
1414
# rich_print(f"[bold blue]BOT: [/bold blue][bold yellow]WARNING:[/bold yellow] {text}")
1515
#
16-
# def bot_error(text: str):
17-
# rich_print(f"[bold blue]BOT: [/bold blue][bold red]ERROR:[/bold red] {text}")
16+
def bot_error(text: str):
17+
rich_print(f"[bold blue]BOT: [/bold blue][bold red]ERROR:[/bold red] {text}")
1818

1919

2020
def server(text: str):

src/utils/utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import aiohttp
77
import yaml
8-
from hikari import ForumTag, GuildForumChannel
8+
from hikari import ForumTag, GuildForumChannel, GuildThreadChannel
99
from hikari.impl import RESTClientImpl
1010

1111

@@ -156,22 +156,21 @@ async def fetch_single_select_value(item_node_id: str | None, field_name: str |
156156

157157
async def get_post_id(
158158
name: str, discord_guild_id: int, forum_channel_id: int, rest_client: RESTClientImpl
159-
) -> int | None:
159+
) -> int | GuildThreadChannel | None:
160160
with shelve.open("post_id.db") as db:
161161
try:
162162
post_id: str = db[name]
163163
return int(post_id)
164164
except KeyError:
165165
pass
166-
# todo: return post if found in active threads or in archived threads
167166
for thread in await rest_client.fetch_active_threads(discord_guild_id):
168167
if thread.name == name:
169168
db[name] = thread.id
170-
return thread.id
169+
return thread
171170
for thread in await rest_client.fetch_public_archived_threads(forum_channel_id):
172171
if thread.name == name:
173172
db[name] = thread.id
174-
return thread.id
173+
return thread
175174

176175
return None
177176

0 commit comments

Comments
 (0)