Skip to content

Commit

Permalink
Updated algorithm to account for title
Browse files Browse the repository at this point in the history
  • Loading branch information
lordsarcastic committed Jul 22, 2024
1 parent ef97f32 commit 4daf202
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
31 changes: 28 additions & 3 deletions generators/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,30 @@ def _generate_entities_list_with_level_and_link(
if not entities:
continue

level_title = f"*{level}*\n\n"

# we must account for the length of the level title
# in the max size of a slack message block
MAX_MESSAGE_BLOCK_SIZE_FOR_TITLE = (
SlackMessageGenerator.SLACK_MAX_MESSAGE_BLOCK_SIZE
- len(level_title)
)
# let's us know if this is the first batch of entities
# which will contain the title of the level
batch_count = 0
current_count = 0
# the entities that will be written to slack at this current
# iteration
current_entities = []

while current_count < len(entities):
# we will keep adding entities to the current_entities list
# until we reach the max size of a slack message block
text_length = 0
entities_text = ""
while text_length < SlackMessageGenerator.SLACK_MAX_MESSAGE_BLOCK_SIZE and current_count < len(entities):
while text_length < (
MAX_MESSAGE_BLOCK_SIZE_FOR_TITLE
) and current_count < len(entities):
current_entities.append(entities[current_count])
entities_text = " \n".join(
SlackMessageGenerator._generate_text_for_entity(blueprint, entity)
Expand All @@ -335,22 +351,31 @@ def _generate_entities_list_with_level_and_link(
text_length = len(entities_text)
current_count += 1

# if we reach the end of the entities list,
# we will generate the block
if current_count == len(entities):
block.append(
SlackMessageGenerator._generate_block_for_entities(
current_entities,
blueprint,
f"*{level}*\n\n" if batch_count == 0 else ""
level_title if batch_count == 0 else ""
)
)
break

# the only way we can reach this point is if the current_entities
# list is above the max size of a slack message block
# if we simply make the request, the API call will fail
# therefore, we will remove the last entity from the list
# to stay within the limit.
current_entities.pop()
# update the current count to reflect the removal of the last entity
current_count -= 1
block.append(
SlackMessageGenerator._generate_block_for_entities(
current_entities,
blueprint,
f"*{level}*\n\n" if batch_count == 0 else ""
level_title if batch_count == 0 else ""
)
)
current_entities = []
Expand Down
3 changes: 0 additions & 3 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import itertools
from typing import Any

import inflect


Expand Down

0 comments on commit 4daf202

Please sign in to comment.