Skip to content

Commit

Permalink
Option for no notifications when not in room (closes #1636)
Browse files Browse the repository at this point in the history
  • Loading branch information
quartata committed Feb 22, 2018
1 parent 1991e9a commit 9d24a90
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
20 changes: 17 additions & 3 deletions chatcommands.py
Expand Up @@ -847,8 +847,8 @@ def allnotificationsites(msg, room_id):


# noinspection PyIncorrectDocstring,PyMissingTypeHints
@command(int, str, whole_msg=True)
def notify(msg, room_id, se_site):
@command(int, str, bool, whole_msg=True, arity=(2, 3))
def notify(msg, room_id, se_site, always_ping):
"""
Subscribe a user to events on a site in a single room
:param msg:
Expand All @@ -857,7 +857,8 @@ def notify(msg, room_id, se_site):
:return: A string
"""
# TODO: Add check whether smokey reports in that room
response, full_site = add_to_notification_list(msg.owner.id, msg._client.host, room_id, se_site)
response, full_site = add_to_notification_list(msg.owner.id, msg._client.host, room_id, se_site,
always_ping=(always_ping or True))

if response == 0:
return "You'll now get pings from me if I report a post on `{site}`, in room "\
Expand All @@ -870,6 +871,19 @@ def notify(msg, room_id, se_site):
raise CmdException("Unrecognized code returned when adding notification.")


# temp command
@command(privileged=True)
def migrate_notifications():
for i, notification in enumerate(GlobalVars.notifications):
if len(notification) == 4:
GlobalVars.notifications[i] = notification + (True,)

with open("notifications.p", "wb") as f:
pickle.dump(GlobalVars.notifications, f, protocol=pickle.HIGHEST_PROTOCOL)

return "shoutouts to simpleflips"


# noinspection PyIncorrectDocstring,PyMissingTypeHints
@command(whole_msg=True, aliases=["unnotify-all"])
def unnotify_all(msg):
Expand Down
31 changes: 24 additions & 7 deletions datahandling.py
Expand Up @@ -15,6 +15,11 @@
from blacklists import load_blacklists


class Any:
def __eq__(self, _):
return True


def _load_pickle(path, encoding='utf-8'):
with open(path, mode="rb") as f:
try:
Expand Down Expand Up @@ -330,15 +335,15 @@ def check_site_and_get_full_name(site):
# (that is, being pinged when Smokey reports something on a specific site)

# noinspection PyMissingTypeHints
def add_to_notification_list(user_id, chat_site, room_id, se_site):
def add_to_notification_list(user_id, chat_site, room_id, se_site, always_ping=True):
if se_site[0] != "/":
exists, se_site = check_site_and_get_full_name(se_site)
if not exists:
return -2, None
notification_tuple = (int(user_id), chat_site, int(room_id), se_site)
notification_tuple = (int(user_id), chat_site, int(room_id), se_site, Any())
if notification_tuple in GlobalVars.notifications:
return -1, None
GlobalVars.notifications.append(notification_tuple)
GlobalVars.notifications.append((int(user_id), chat_site, int(room_id), se_site, always_ping))
with open("notifications.p", "wb") as f:
pickle.dump(GlobalVars.notifications, f, protocol=pickle.HIGHEST_PROTOCOL)
return 0, se_site
Expand All @@ -350,7 +355,7 @@ def remove_from_notification_list(user_id, chat_site, room_id, se_site):
exists, se_site = check_site_and_get_full_name(se_site)
if not exists:
return False
notification_tuple = (int(user_id), chat_site, int(room_id), se_site)
notification_tuple = (int(user_id), chat_site, int(room_id), se_site, Any())
if notification_tuple not in GlobalVars.notifications:
return False
GlobalVars.notifications.remove(notification_tuple)
Expand All @@ -364,7 +369,7 @@ def will_i_be_notified(user_id, chat_site, room_id, se_site):
exists, site = check_site_and_get_full_name(se_site)
if not exists:
return False
notification_tuple = (int(user_id), chat_site, int(room_id), site)
notification_tuple = (int(user_id), chat_site, int(room_id), site, Any())
return notification_tuple in GlobalVars.notifications


Expand Down Expand Up @@ -393,12 +398,24 @@ def get_user_ids_on_notification_list(chat_site, room_id, se_site):
uids = []
for notification in GlobalVars.notifications:
if notification[1] == chat_site and notification[2] == int(room_id) and notification[3] == se_site:
uids.append(notification[0])
uids.append((notification[0], notification[4]))
return uids


def get_user_names_on_notification_list(chat_site, room_id, se_site, client):
return [client.get_user(i).name for i in get_user_ids_on_notification_list(chat_site, room_id, se_site)]
names = []
current_users = client._br.get_current_users_in_room(room_id)

for i, always in get_user_ids_on_notification_list(chat_site, room_id, se_site):
if always:
names.append(client.get_user(i).name)
else:
try:
names.append(current_users[current_users.index((i, Any()))][1])
except:
pass

return names


# noinspection PyMissingTypeHints
Expand Down
15 changes: 10 additions & 5 deletions test/test_chatcommands.py
Expand Up @@ -429,9 +429,9 @@ def test_notifications():
"You won't get notified for any sites in that room."
assert chatcommands.willbenotified("11540", "gaming", original_msg=msg1) == \
"No, you won't be notified for that site in that room."
assert chatcommands.notify("11540", "gaming", original_msg=msg1) == \
assert chatcommands.notify("11540", "gaming", None, original_msg=msg1) == \
"You'll now get pings from me if I report a post on `gaming`, in room `11540` on `chat.stackexchange.com`"
assert chatcommands.notify("11540", "codegolf.stackexchange.com", original_msg=msg1) == \
assert chatcommands.notify("11540", "codegolf.stackexchange.com", None, original_msg=msg1) == \
"You'll now get pings from me if I report a post on `codegolf.stackexchange.com`, in room `11540` on " \
"`chat.stackexchange.com`"
assert chatcommands.willbenotified("11540", "gaming.stackexchange.com", original_msg=msg1) == \
Expand All @@ -444,9 +444,9 @@ def test_notifications():
"You won't get notified for any sites in that room."
assert chatcommands.willbenotified("11540", "raspberrypi", original_msg=msg2) == \
"No, you won't be notified for that site in that room."
assert chatcommands.notify("11540", "raspberrypi", original_msg=msg2) == \
assert chatcommands.notify("11540", "raspberrypi", None, original_msg=msg2) == \
"You'll now get pings from me if I report a post on `raspberrypi`, in room `11540` on `chat.stackexchange.com`"
assert chatcommands.notify("11540", "raspberrypi", original_msg=msg2) == \
assert chatcommands.notify("11540", "raspberrypi", None, original_msg=msg2) == \
"That notification configuration is already registered."
assert chatcommands.willbenotified("11540", "raspberrypi.stackexchange.com", original_msg=msg2) == \
"Yes, you will be notified for that site in that room."
Expand All @@ -473,8 +473,13 @@ def test_notifications():
"No, you won't be notified for that site in that room."

assert chatcommands.allnotificationsites("asdf", original_msg=msg1) == "Invalid input type given for an argument"
assert chatcommands.notify("11540", "charcoalspam.stackexchange.com", original_msg=msg1) == \
assert chatcommands.notify("11540", "charcoalspam.stackexchange.com", None, original_msg=msg1) == \
"The given SE site does not exist."

assert chatcommands.notify("11540", "codegolf", "True", original_msg=msg1) == \
"You'll now get pings from me if I report a post on `codegolf`, in room `11540` on `chat.stackexchange.com`"
assert chatcommands.notify("11540", "codegolf", "False", original_msg=msg1) == \
"That notification configuration is already registered."
finally:
# Cleanup
os.remove("notifications.p")

0 comments on commit 9d24a90

Please sign in to comment.