Skip to content

Commit

Permalink
* Added ability to count private messages when count a leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
korsakovs committed Feb 7, 2024
1 parent 8ed4876 commit f2f2b88
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 46 deletions.
1 change: 1 addition & 0 deletions tests/test_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def non_existing_company(non_existing_company_slack_team_id) -> Company:
weekly_thank_you_limit=5,
receivers_number_limit=5,
enable_leaderboard=True,
enable_private_message_counting_in_leaderboard=False,
enable_company_values=True,
enable_rich_text_in_thank_you_messages=True,
enable_attaching_files=True,
Expand Down
1 change: 1 addition & 0 deletions thankyou/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Company:
weekly_thank_you_limit: int
receivers_number_limit: int
enable_leaderboard: bool
enable_private_message_counting_in_leaderboard: bool
enable_company_values: bool
enable_rich_text_in_thank_you_messages: bool
enable_attaching_files: bool
Expand Down
6 changes: 4 additions & 2 deletions thankyou/dao/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ def delete_thank_you_type(self, company_uuid: str, thank_you_type_uuid: str): ..
@abstractmethod
def get_thank_you_sender_leaders(self, company_uuid: str, created_after: datetime = None,
created_before: datetime = None, thank_you_type: ThankYouType = None,
leaders_num: int = 3) -> List[Tuple[Slack_User_ID_Type, int]]: ...
leaders_num: int = 3, include_private: bool = False) \
-> List[Tuple[Slack_User_ID_Type, int]]: ...

@abstractmethod
def get_thank_you_receiver_leaders(self, company_uuid: str, created_after: datetime = None,
created_before: datetime = None, thank_you_type: ThankYouType = None,
leaders_num: int = 3) -> List[Tuple[Slack_User_ID_Type, int]]: ...
leaders_num: int = 3, include_private: bool = False) \
-> List[Tuple[Slack_User_ID_Type, int]]: ...

def create_employee(self, employee: Employee): ...

Expand Down
13 changes: 9 additions & 4 deletions thankyou/dao/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(self, encryption_secret_key: str = None, echo: bool = False):
Column("weekly_thank_you_limit", Integer, nullable=False),
Column("receivers_number_limit", Integer, nullable=False),
Column("enable_leaderboard", Boolean, nullable=False),
Column("enable_private_message_counting_in_leaderboard", Boolean, nullable=False),
Column("enable_company_values", Boolean, nullable=False),
Column("enable_rich_text_in_thank_you_messages", Boolean, nullable=False),
Column("enable_attaching_files", Boolean, nullable=False),
Expand Down Expand Up @@ -359,7 +360,8 @@ def delete_thank_you_type(self, company_uuid: str, thank_you_type_uuid: str):

def get_thank_you_sender_leaders(self, company_uuid: str, created_after: datetime = None,
created_before: datetime = None, thank_you_type: ThankYouType = None,
leaders_num: int = 3) -> List[Tuple[Slack_User_ID_Type, int]]:
leaders_num: int = 3, include_private: bool = False) \
-> List[Tuple[Slack_User_ID_Type, int]]:
with self._get_session() as session:
result = session.query(ThankYouMessage.author_slack_user_id, func.count())
result = result.filter(ThankYouMessage.deleted == False)
Expand All @@ -374,7 +376,8 @@ def get_thank_you_sender_leaders(self, company_uuid: str, created_after: datetim
if thank_you_type:
result = result.filter(ThankYouMessage.type == thank_you_type)

result = result.filter(ThankYouMessage.is_private == false())
if not include_private:
result = result.filter(ThankYouMessage.is_private == false())

result = result.group_by(ThankYouMessage.author_slack_user_id)
result = result.order_by(func.count().desc())
Expand All @@ -384,7 +387,8 @@ def get_thank_you_sender_leaders(self, company_uuid: str, created_after: datetim

def get_thank_you_receiver_leaders(self, company_uuid: str, created_after: datetime = None,
created_before: datetime = None, thank_you_type: ThankYouType = None,
leaders_num: int = 3) -> List[Tuple[Slack_User_ID_Type, int]]:
leaders_num: int = 3, include_private: bool = False) \
-> List[Tuple[Slack_User_ID_Type, int]]:
with self._get_session() as session:
result = session.query(ThankYouReceiver.slack_user_id, func.count()).join(ThankYouMessage)
result = result.filter(ThankYouMessage.deleted == False)
Expand All @@ -399,7 +403,8 @@ def get_thank_you_receiver_leaders(self, company_uuid: str, created_after: datet
if thank_you_type:
result = result.filter(ThankYouMessage.type == thank_you_type)

result = result.filter(ThankYouMessage.is_private == false())
if not include_private:
result = result.filter(ThankYouMessage.is_private == false())

result = result.group_by(ThankYouReceiver.slack_user_id)
result = result.order_by(func.count().desc())
Expand Down
15 changes: 10 additions & 5 deletions thankyou/slackbot/handlers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SendersReceiversStats:

@cached(cache=TTLCache(maxsize=1024, ttl=60))
def get_sender_and_receiver_leaders(company_uuid: str, leaderboard_time_settings: LeaderbordTimeSettings,
group_by_company_values: bool) -> SendersReceiversStats:
group_by_company_values: bool, include_private: bool) -> SendersReceiversStats:
if leaderboard_time_settings == LeaderbordTimeSettings.LAST_30_DAYS:
leaders_stats_from_datetime = datetime.utcnow() - timedelta(days=30)
leaders_stats_until_datetime = datetime.utcnow()
Expand Down Expand Up @@ -105,27 +105,31 @@ def get_sender_and_receiver_leaders(company_uuid: str, leaderboard_time_settings
sender_leaders.append((None, dao.get_thank_you_sender_leaders(
company_uuid=company_uuid,
created_after=leaders_stats_from_datetime,
created_before=leaders_stats_until_datetime
created_before=leaders_stats_until_datetime,
include_private=include_private
)))
receiver_leaders.append((None, dao.get_thank_you_receiver_leaders(
company_uuid=company_uuid,
created_after=leaders_stats_from_datetime,
created_before=leaders_stats_until_datetime
created_before=leaders_stats_until_datetime,
include_private=include_private
)))
else:
for thank_you_type in dao.read_thank_you_types(company_uuid=company_uuid):
type_leaders = dao.get_thank_you_sender_leaders(
company_uuid=company_uuid,
thank_you_type=thank_you_type,
created_after=leaders_stats_from_datetime,
created_before=leaders_stats_until_datetime
created_before=leaders_stats_until_datetime,
include_private=include_private
)
sender_leaders.append((thank_you_type, type_leaders))
type_leaders = dao.get_thank_you_receiver_leaders(
company_uuid=company_uuid,
thank_you_type=thank_you_type,
created_after=leaders_stats_from_datetime,
created_before=leaders_stats_until_datetime
created_before=leaders_stats_until_datetime,
include_private=include_private
)
receiver_leaders.append((thank_you_type, type_leaders))
return SendersReceiversStats(
Expand Down Expand Up @@ -157,6 +161,7 @@ def publish_configuration_view(client, company: Company, user_id: str):
enable_attaching_files=company.enable_attaching_files,
max_attached_files_num=company.max_attached_files_num,
enable_private_messages=company.enable_private_messages,
enable_private_message_counting_in_leaderboard=company.enable_private_message_counting_in_leaderboard
)

client.views_publish(
Expand Down
21 changes: 21 additions & 0 deletions thankyou/slackbot/handlers/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ def home_page_configuration_stats_time_period_value_changed_action_handler(body,
)


def handle_home_page_configuration_enable_private_message_counting_in_leaderboard_value_changed_action_handler(client, body, logger):
logger.info(body)
user_id = body["user"]["id"]
company = get_or_create_company_by_body(body)

new_enable_private_message_counting_in_leaderboard = (
"enable_private_message_counting_in_leaderboard"
in [option["value"] for option in body["actions"][0]["selected_options"]]
)

if company.enable_private_message_counting_in_leaderboard != new_enable_private_message_counting_in_leaderboard:
# ORM_WARNING: the following statement works because we use SQL Alchemy
company.enable_private_message_counting_in_leaderboard = new_enable_private_message_counting_in_leaderboard

publish_configuration_view(
client=client,
company=company,
user_id=user_id
)


def home_page_configuration_max_number_of_thank_you_receivers_value_changed_action_handler(body, client, logger):
logger.info(body)
user_id = body["user"]["id"]
Expand Down
9 changes: 3 additions & 6 deletions thankyou/slackbot/handlers/homepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def home_page_show_leaders_button_clicked_action_handler(body, client, logger):
senders_receivers_stats = get_sender_and_receiver_leaders(
company_uuid=company.uuid,
leaderboard_time_settings=company.leaderbord_time_settings,
group_by_company_values=company.enable_company_values
group_by_company_values=company.enable_company_values,
include_private=company.enable_private_message_counting_in_leaderboard
)

client.views_publish(
Expand Down Expand Up @@ -139,10 +140,6 @@ def home_page_say_thank_you_button_clicked_action_handler(body, client, logger):
else:
num_of_messages_a_user_can_send = None

display_private_message_option = (company.enable_sharing_in_a_slack_channel
and bool(company.share_messages_in_slack_channel)
and company.enable_private_messages)

try:
client.views_open(
trigger_id=body["trigger_id"],
Expand All @@ -154,7 +151,7 @@ def home_page_say_thank_you_button_clicked_action_handler(body, client, logger):
enable_attaching_files=company.enable_attaching_files,
max_attached_files_num=company.max_attached_files_num,
num_of_messages_a_user_can_send=num_of_messages_a_user_can_send,
display_private_message_option=display_private_message_option,
display_private_message_option=company.enable_private_messages,
),
)
except Exception as e:
Expand Down
12 changes: 2 additions & 10 deletions thankyou/slackbot/handlers/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ def say_thank_you_global_shortcut_action_handler(body, client, logger):
else:
num_of_messages_a_user_can_send = None

display_private_message_option = (company.enable_sharing_in_a_slack_channel
and bool(company.share_messages_in_slack_channel)
and company.enable_private_messages)

try:
client.views_open(
trigger_id=body["trigger_id"],
Expand All @@ -38,7 +34,7 @@ def say_thank_you_global_shortcut_action_handler(body, client, logger):
enable_attaching_files=company.enable_attaching_files,
max_attached_files_num=company.max_attached_files_num,
num_of_messages_a_user_can_send=num_of_messages_a_user_can_send,
display_private_message_option=display_private_message_option,
display_private_message_option=company.enable_private_messages,
),
)
except Exception as e:
Expand All @@ -63,10 +59,6 @@ def say_thank_you_message_shortcut_action_handler(body, client, logger):
else:
num_of_messages_a_user_can_send = None

display_private_message_option = (company.enable_sharing_in_a_slack_channel
and bool(company.share_messages_in_slack_channel)
and company.enable_private_messages)

try:
client.views_open(
trigger_id=body["trigger_id"],
Expand All @@ -78,7 +70,7 @@ def say_thank_you_message_shortcut_action_handler(body, client, logger):
enable_attaching_files=company.enable_attaching_files,
max_attached_files_num=company.max_attached_files_num,
num_of_messages_a_user_can_send=num_of_messages_a_user_can_send,
display_private_message_option=display_private_message_option,
display_private_message_option=company.enable_private_messages,
),
)
except Exception as e:
Expand Down
6 changes: 1 addition & 5 deletions thankyou/slackbot/handlers/slashcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ def merci_slash_command_action_handler(body, client, logger):
else:
num_of_messages_a_user_can_send = None

display_private_message_option = (company.enable_sharing_in_a_slack_channel
and bool(company.share_messages_in_slack_channel)
and company.enable_private_messages)

try:
client.views_open(
trigger_id=body["trigger_id"],
Expand All @@ -46,7 +42,7 @@ def merci_slash_command_action_handler(body, client, logger):
max_attached_files_num=company.max_attached_files_num,
num_of_messages_a_user_can_send=num_of_messages_a_user_can_send,
slash_command_slack_channel_id=channel_id,
display_private_message_option=display_private_message_option,
display_private_message_option=company.enable_private_messages,
),
)
except Exception as e:
Expand Down
22 changes: 16 additions & 6 deletions thankyou/slackbot/handlers/thankyoudialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ def thank_you_dialog_save_button_clicked_action_handler(body, client: WebClient,

if thank_you_message.slash_command_slack_channel_id:
try:
client.chat_postMessage(
channel=thank_you_message.slash_command_slack_channel_id,
blocks=thank_you_message_blocks(thank_you_message),
unfurl_links=False,
unfurl_media=False,
)
if thank_you_message.is_private:
for receiver in thank_you_message.receivers:
client.chat_postEphemeral(
user=receiver.slack_user_id,
channel=thank_you_message.slash_command_slack_channel_id,
blocks=thank_you_message_blocks(thank_you_message),
unfurl_links=False,
unfurl_media=False,
)
else:
client.chat_postMessage(
channel=thank_you_message.slash_command_slack_channel_id,
blocks=thank_you_message_blocks(thank_you_message),
unfurl_links=False,
unfurl_media=False,
)
except SlackApiError as e:
if e.response.data["error"] == "channel_not_found":
client.chat_postMessage(
Expand Down
9 changes: 8 additions & 1 deletion thankyou/slackbot/utils/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
home_page_configuration_max_attached_files_num_value_changed_action_handler, \
home_page_configuration_enable_weekly_thank_you_limit_value_changed_action_handler, \
home_page_configuration_enable_sharing_in_a_slack_channel_value_changed_action_handler, \
home_page_configuration_enable_private_messages_value_changed_action_handler
home_page_configuration_enable_private_messages_value_changed_action_handler, \
handle_home_page_configuration_enable_private_message_counting_in_leaderboard_value_changed_action_handler
from thankyou.slackbot.handlers.homepage import app_home_opened_action_handler, \
home_page_company_thank_you_button_clicked_action_handler, home_page_my_thank_you_button_clicked_action_handler, \
home_page_say_thank_you_button_clicked_action_handler, home_page_show_leaders_button_clicked_action_handler, \
Expand Down Expand Up @@ -163,6 +164,12 @@ def _home_page_configuration_stats_time_period_value_changed_action_handler(ack,
home_page_configuration_stats_time_period_value_changed_action_handler(body, client, logger)


@app.action("home_page_configuration_enable_private_message_counting_in_leaderboard_value_changed")
def _handle_home_page_configuration_enable_private_message_counting_in_leaderboard_value_changed_action_handler(ack, client, body, logger):
ack()
handle_home_page_configuration_enable_private_message_counting_in_leaderboard_value_changed_action_handler(client, body, logger)


@app.action("home_page_configuration_max_number_of_thank_you_receivers_value_changed")
def _home_page_configuration_max_number_of_thank_you_receivers_value_changed_action_handler(ack, client, body, logger):
ack()
Expand Down
1 change: 1 addition & 0 deletions thankyou/slackbot/utils/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get_or_create_company_by_slack_team_id(slack_team_id: str) -> Company:
weekly_thank_you_limit=5,
receivers_number_limit=10,
enable_leaderboard=True,
enable_private_message_counting_in_leaderboard=False,
enable_company_values=True,
enable_rich_text_in_thank_you_messages=False,
enable_attaching_files=True,
Expand Down
22 changes: 15 additions & 7 deletions thankyou/slackbot/views/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def configuration_view(admin_slack_user_ids: List[Slack_User_ID_Type], enable_sh
weekly_thank_you_limit: int,
enable_rich_text_in_thank_you_messages: bool, enable_company_values: bool,
enable_leaderboard: bool, max_thank_you_receivers_num: int, enable_attaching_files: bool,
enable_private_messages: bool, max_attached_files_num: int):
enable_private_messages: bool, max_attached_files_num: int,
enable_private_message_counting_in_leaderboard: bool):
stats_time_period_to_use_options = []
stats_time_period_to_use_selected_option = None
for enum, option in (
Expand Down Expand Up @@ -102,13 +103,13 @@ def configuration_view(admin_slack_user_ids: List[Slack_User_ID_Type], enable_sh
action_id="home_page_configuration_notification_slack_channel_value_changed"
)
),
checkbox_action_block(
element_action_id="home_page_configuration_enable_private_messages_value_changed",
checkbox_value="enable_private_messages",
checkbox_label="Allow users to send messages privately",
enabled=enable_private_messages
)
]),
checkbox_action_block(
element_action_id="home_page_configuration_enable_private_messages_value_changed",
checkbox_value="enable_private_messages",
checkbox_label="Allow users to send messages privately",
enabled=enable_private_messages
),
HeaderBlock(
text="Leaderboards Settings"
),
Expand All @@ -127,6 +128,13 @@ def configuration_view(admin_slack_user_ids: List[Slack_User_ID_Type], enable_sh
action_id="home_page_configuration_stats_time_period_value_changed"
)
),
checkbox_action_block(
element_action_id=
"home_page_configuration_enable_private_message_counting_in_leaderboard_value_changed",
checkbox_value="enable_private_message_counting_in_leaderboard",
checkbox_label="Count private messages when counting the leaderboard",
enabled=enable_private_message_counting_in_leaderboard
),
]),
HeaderBlock(
text="Weekly limits"
Expand Down

0 comments on commit f2f2b88

Please sign in to comment.