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

Add subreddits command to count submissions per subreddit #57

Merged
merged 1 commit into from
Feb 28, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bubbles/commands/debug.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from utonium import Payload, Plugin

from bubbles.commands.periodic.rule_monitoring import get_subreddit_stack
from bubbles.commands.periodic.transcription_check_ping import \
transcription_check_ping_callback
from bubbles.commands.periodic.transcription_check_ping import (
transcription_check_ping_callback,
)


def debug(payload: Payload) -> None:
Expand Down
4 changes: 3 additions & 1 deletion bubbles/commands/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def migrate():
except subprocess.CalledProcessError:
StatusMessage.step_failed()
revert_and_recover()
raise DeployError("Could not perform database migration! Unable to proceed!")
raise DeployError(
"Could not perform database migration! Unable to proceed!"
)
StatusMessage.step_succeeded()

StatusMessage: ContextStepMessage = ContextStepMessage(
Expand Down
40 changes: 19 additions & 21 deletions bubbles/commands/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@


def build_and_send_message(
convo_id: str = None,
message_id: str = None,
expand_message: bool = False,
update_message_data: dict = None
convo_id: str = None,
message_id: str = None,
expand_message: bool = False,
update_message_data: dict = None,
) -> None:
"""
Starting from a conversation id and a message ID, build the notification message.
Expand Down Expand Up @@ -40,7 +40,7 @@ def build_and_send_message(
participant = None

if len(convo.messages) == 1 and (
message.author == participant or isinstance(participant, Subreddit)
message.author == participant or isinstance(participant, Subreddit)
):
# WAIT! We _might_ have been sent a message, but the modmail API doesn't
# make that clear.
Expand All @@ -56,8 +56,8 @@ def build_and_send_message(
sender = message.author
recipient = participant
elif (
len(convo.messages) > 1
and len(set([m.author for m in convo.messages])) == 1 # see footnote
len(convo.messages) > 1
and len(set([m.author for m in convo.messages])) == 1 # see footnote
):
# It's one person sending multiple messages that haven't been responded
# to yet.
Expand Down Expand Up @@ -130,15 +130,11 @@ def build_and_send_message(
]

msg_blocks += [
blocks.ActionsBlock(
elements=action_elements
),
blocks.ActionsBlock(elements=action_elements),
blocks.ContextBlock(
elements=[
blocks.MarkdownTextObject(text=f"Conversation ID: {convo.id}"),
blocks.MarkdownTextObject(
text=f"Message ID: {message.id}"
),
blocks.MarkdownTextObject(text=f"Message ID: {message.id}"),
]
),
]
Expand All @@ -157,12 +153,12 @@ def build_and_send_message(

if update_message_data:
app.client.chat_update(
channel=update_message_data["channel"], ts=update_message_data["ts"], **chat_args
channel=update_message_data["channel"],
ts=update_message_data["ts"],
**chat_args,
)
else:
app.client.chat_postMessage(
channel=rooms_list["mod_messages"], **chat_args
)
app.client.chat_postMessage(channel=rooms_list["mod_messages"], **chat_args)


def process_modmail(message_state: str) -> None:
Expand All @@ -189,15 +185,17 @@ def handle_expansion_actions(payload: Payload) -> None:
action = payload.get_block_kit_action().split("_")

update_data = {
"channel": payload._slack_payload['container']['channel_id'],
"ts": payload._slack_payload['container']['message_ts']
"channel": payload._slack_payload["container"]["channel_id"],
"ts": payload._slack_payload["container"]["message_ts"],
}
build_and_send_message(
convo_id=action[2],
message_id=action[3],
update_message_data=update_data,
expand_message=action[1] == 'embiggen'
expand_message=action[1] == "embiggen",
)


PLUGIN = Plugin(block_kit_action_func=handle_expansion_actions, block_kit_action_regex=r"^modmail_")
PLUGIN = Plugin(
block_kit_action_func=handle_expansion_actions, block_kit_action_regex=r"^modmail_"
)
6 changes: 4 additions & 2 deletions bubbles/commands/periodic/rule_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

from praw.models import Rule

from bubbles.commands.periodic import (RULE_MONITORING_CHANNEL_ID,
RULE_MONITORING_DATA_PATH)
from bubbles.commands.periodic import (
RULE_MONITORING_CHANNEL_ID,
RULE_MONITORING_DATA_PATH,
)
from bubbles.config import app, reddit

# Newly-added subreddits that don't have their rules tracked yet
Expand Down
9 changes: 5 additions & 4 deletions bubbles/commands/periodic/transcription_check_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from enum import Enum
from typing import Dict, List, Optional, Tuple, TypedDict

from bubbles.commands.helper_functions_history.extract_author import \
extract_author
from bubbles.commands.periodic import (TRANSCRIPTION_CHECK_CHANNEL,
TRANSCRIPTION_CHECK_PING_CHANNEL)
from bubbles.commands.helper_functions_history.extract_author import extract_author
from bubbles.commands.periodic import (
TRANSCRIPTION_CHECK_CHANNEL,
TRANSCRIPTION_CHECK_PING_CHANNEL,
)
from bubbles.config import app, rooms_list, users_list

USERNAME_REGEX = re.compile(r"u/(?P<username>[^ *:\[\]()?!<>]+)")
Expand Down
6 changes: 2 additions & 4 deletions bubbles/commands/periodic/welcome_ping.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import datetime

from bubbles.commands.helper_functions_history.extract_author import \
extract_author
from bubbles.commands.periodic import (NEW_VOLUNTEER_CHANNEL,
NEW_VOLUNTEER_PING_CHANNEL)
from bubbles.commands.helper_functions_history.extract_author import extract_author
from bubbles.commands.periodic import NEW_VOLUNTEER_CHANNEL, NEW_VOLUNTEER_PING_CHANNEL
from bubbles.config import app, rooms_list


Expand Down
8 changes: 4 additions & 4 deletions bubbles/commands/plot_comments_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from numpy import flip
from utonium import Payload, Plugin

from bubbles.commands.helper_functions_history.extract_date_or_number import \
extract_date_or_number
from bubbles.commands.helper_functions_history.fetch_messages import \
fetch_messages
from bubbles.commands.helper_functions_history.extract_date_or_number import (
extract_date_or_number,
)
from bubbles.commands.helper_functions_history.fetch_messages import fetch_messages

# get rid of matplotlib's complaining
warnings.filterwarnings("ignore")
Expand Down
11 changes: 5 additions & 6 deletions bubbles/commands/plot_comments_historylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

from utonium import Payload, Plugin

from bubbles.commands.helper_functions_history.extract_author import \
extract_author
from bubbles.commands.helper_functions_history.extract_date_or_number import \
extract_date_or_number
from bubbles.commands.helper_functions_history.fetch_messages import \
fetch_messages
from bubbles.commands.helper_functions_history.extract_author import extract_author
from bubbles.commands.helper_functions_history.extract_date_or_number import (
extract_date_or_number,
)
from bubbles.commands.helper_functions_history.fetch_messages import fetch_messages

# get rid of matplotlib's complaining
warnings.filterwarnings("ignore")
Expand Down
11 changes: 5 additions & 6 deletions bubbles/commands/plot_comments_historywho.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
from numpy import cumsum, flip, zeros
from utonium import Payload, Plugin

from bubbles.commands.helper_functions_history.extract_author import \
extract_author
from bubbles.commands.helper_functions_history.extract_date_or_number import \
extract_date_or_number
from bubbles.commands.helper_functions_history.fetch_messages import \
fetch_messages
from bubbles.commands.helper_functions_history.extract_author import extract_author
from bubbles.commands.helper_functions_history.extract_date_or_number import (
extract_date_or_number,
)
from bubbles.commands.helper_functions_history.fetch_messages import fetch_messages
from bubbles.config import users_list

# get rid of matplotlib's complaining
Expand Down
8 changes: 6 additions & 2 deletions bubbles/commands/restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
from utonium import Payload, Plugin

from bubbles.config import COMMAND_PREFIXES
from bubbles.service_utils import (SERVICES, get_service_name, say_code,
verify_service_up)
from bubbles.service_utils import (
SERVICES,
get_service_name,
say_code,
verify_service_up,
)


def _restart_service(service: str, say: Callable) -> None:
Expand Down
8 changes: 6 additions & 2 deletions bubbles/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
from utonium import Payload, Plugin

from bubbles.config import COMMAND_PREFIXES
from bubbles.service_utils import (SERVICES, get_service_name, say_code,
verify_service_up)
from bubbles.service_utils import (
SERVICES,
get_service_name,
say_code,
verify_service_up,
)


def _start_service(service: str, say: Callable) -> None:
Expand Down
44 changes: 44 additions & 0 deletions bubbles/commands/subreddits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from utonium import Payload, Plugin

from bubbles.config import blossom
from bubbles.utils import parse_time_constraints, TimeParseError


def subreddits(payload: Payload) -> None:
"""
!subreddits [start time] [end time] - Get transcription counts by subreddit
Usage: `!subreddits` or `!subreddits 2023-02-20 2023-02-27
"""
parts = payload.get_text().split()

start_text = parts[1] if len(parts) >= 2 else "1 week"
end_text = parts[2] if len(parts) >= 3 else "now"

try:
start, end, time_str = parse_time_constraints(start_text, end_text)
except TimeParseError:
payload.say("Invalid time specified, please check the format.")
return

response = blossom.get(
"submission/subreddits/",
params={
"completed_by__isnull": False,
"complete_time__gte": start,
"complete_time__lte": end,
},
)

if not response.ok:
payload.say(
f"Sorry, but something went wrong. Please try again later.\n{response.text}"
)
return

data = response.json()
response_txt = "\n".join(f"- r/{sub}: {count}" for sub, count in data.item())

payload.say(response_txt)


PLUGIN = Plugin(func=subreddits, regex=r"^subreddits.*")
13 changes: 11 additions & 2 deletions bubbles/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
from utonium import Payload, PluginManager

from bubbles import __version__
from bubbles.config import (COMMAND_PREFIXES, DEFAULT_CHANNEL, app, rooms_list,
users_list)
from bubbles.config import (
COMMAND_PREFIXES,
DEFAULT_CHANNEL,
app,
rooms_list,
users_list,
)
from bubbles.interactive import InteractiveSession, MockClient
from bubbles.tl_commands import enable_tl_jobs
from bubbles.tl_utils import tl
Expand Down Expand Up @@ -76,8 +81,12 @@ def reaction_added(ack, payload, client, context, say):
# reaction_added_callback(payload)
plugin_manager.reaction_received(payload, client, context, say)


import re

pattern = re.compile(".*")


@app.action(pattern)
def handle_action(ack, body, client, context, say):
ack()
Expand Down
8 changes: 5 additions & 3 deletions bubbles/test/commands/periodic/test_rule_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import pytest

from bubbles.commands.periodic.rule_monitoring import (RuleChanges,
SubredditRule,
_compare_rules)
from bubbles.commands.periodic.rule_monitoring import (
RuleChanges,
SubredditRule,
_compare_rules,
)

EXAMPLE_RULE = {
"index": 0,
Expand Down
16 changes: 12 additions & 4 deletions bubbles/test/commands/periodic/test_transcription_check_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
import pytest

from bubbles.commands.periodic.transcription_check_ping import (
CheckData, CheckStatus, _aggregate_checks_by_mod,
_aggregate_checks_by_time, _extract_check_text, _get_check_data,
_get_check_reminder, _get_check_status, _get_check_username,
_is_check_message, _is_old_check)
CheckData,
CheckStatus,
_aggregate_checks_by_mod,
_aggregate_checks_by_time,
_extract_check_text,
_get_check_data,
_get_check_reminder,
_get_check_status,
_get_check_username,
_is_check_message,
_is_old_check,
)

EXAMPLE_USER_LIST = {
"UEEMDNC0K": "mod974",
Expand Down
19 changes: 12 additions & 7 deletions bubbles/tl_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
from bubbles.commands.modmail import modmail_callback
from bubbles.commands.periodic.banbot_check import banbot_check_callback
from bubbles.commands.periodic.etsy_sale_check import etsy_recent_sale_callback
from bubbles.commands.periodic.get_in_progress_posts import \
get_in_progress_callback
from bubbles.commands.periodic.get_in_progress_posts import get_in_progress_callback
from bubbles.commands.periodic.rule_monitoring import rule_monitoring_callback
from bubbles.commands.periodic.transcription_check_ping import \
transcription_check_ping_callback
from bubbles.commands.periodic.transcription_check_ping import (
transcription_check_ping_callback,
)
from bubbles.commands.periodic.welcome_ping import (
periodic_ping_in_progress_callback, welcome_ping_callback)
from bubbles.time_constants import (TRIGGER_4_HOURS_AGO, TRIGGER_12_HOURS_AGO,
TRIGGER_YESTERDAY)
periodic_ping_in_progress_callback,
welcome_ping_callback,
)
from bubbles.time_constants import (
TRIGGER_4_HOURS_AGO,
TRIGGER_12_HOURS_AGO,
TRIGGER_YESTERDAY,
)
from bubbles.tl_utils import TLJob

# class PeriodicCheck(TLJob):
Expand Down
Loading