Skip to content

Commit

Permalink
avoid multiple notifications for recurring events (#854)
Browse files Browse the repository at this point in the history
* avoid multiple notifications for recurring events

CPCN-695

* only avoid extra notifications for new events
  • Loading branch information
petrjasek authored Mar 25, 2024
1 parent 345a61c commit 2077f08
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
10 changes: 7 additions & 3 deletions newsroom/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ def push():
if item.get("type") == "event":
orig = app.data.find_one("agenda", req=None, _id=item["guid"])
_id = publish_event(item, orig)
notify_new_agenda_item.delay(_id, check_topics=True)
notify_new_agenda_item.delay(_id, check_topics=True, is_new=orig is None)
elif item.get("type") == "planning":
orig = app.data.find_one("agenda", req=None, _id=item["guid"]) or {}
item["planning_date"] = parse_date_str(item["planning_date"])
plan_id = publish_planning_item(item, orig)
event_id = publish_planning_into_event(item)
# Prefer parent Event when sending notificaitons
_id = event_id or plan_id
notify_new_agenda_item.delay(_id, check_topics=True)
notify_new_agenda_item.delay(_id, check_topics=True, is_new=orig is None)
elif item.get("type") == "text":
orig = superdesk.get_resource_service("items").find_one(req=None, _id=item["guid"])
item["_id"] = publish_item(item, orig)
Expand Down Expand Up @@ -763,10 +763,14 @@ def notify_new_wire_item(_id, check_topics=True):


@celery.task
def notify_new_agenda_item(_id, check_topics=True):
def notify_new_agenda_item(_id, check_topics=True, is_new=False):
with locked(_id, "agenda"):
agenda = app.data.find_one("agenda", req=None, _id=_id)
if agenda:
if agenda.get("recurrence_id") and agenda.get("recurrence_id") != _id and is_new:
logger.info("Ignoring recurring event %s", _id)
return

superdesk.get_resource_service("agenda").enhance_items([agenda])
notify_new_item(agenda, check_topics=check_topics)

Expand Down
2 changes: 1 addition & 1 deletion newsroom/topics/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def get_user_id_to_topic_for_subscribers(
user_topic_map: Dict[ObjectId, Dict[ObjectId, Topic]] = {}
for topic in get_topics_with_subscribers():
for subscriber in topic.get("subscribers") or []:
if notification_type is not None and subscriber["notification_type"] != notification_type:
if notification_type is not None and subscriber.get("notification_type") != notification_type:
continue
user_topic_map.setdefault(subscriber["user_id"], {})
user_topic_map[subscriber["user_id"]][topic["_id"]] = topic
Expand Down
3 changes: 3 additions & 0 deletions newsroom/web/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,12 @@
"newsroom:monitoring_schedule_alerts": {
"task": "newsroom.monitoring.email_alerts.monitoring_schedule_alerts",
"schedule": timedelta(seconds=60),
"options": {"expires": 59}, # if the task is not executed within 59 seconds, it will be discarded
},
"newsroom:monitoring_immediate_alerts": {
"task": "newsroom.monitoring.email_alerts.monitoring_immediate_alerts",
"schedule": timedelta(seconds=60),
"options": {"expires": 59}, # if the task is not executed within 59 seconds, it will be discarded
},
"newsroom:remove_expired_content_api": {
"task": "content_api.commands.item_expiry",
Expand All @@ -462,6 +464,7 @@
"newsroom:send_scheduled_notifications": {
"task": "newsroom.notifications.send_scheduled_notifications.send_scheduled_notifications",
"schedule": crontab(minute="*/5"),
"options": {"expires": 5 * 60 - 1},
},
}

Expand Down
40 changes: 40 additions & 0 deletions tests/core/test_realtime_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,43 @@ def test_realtime_notifications_agenda(app, mocker):

assert len(outbox) == 2
assert "http://localhost:5050/agenda?item=event_id_1" in outbox[0].body


def test_realtime_notifications_agenda_reccuring_event(app):
app.data.insert(
"agenda",
[
{
"_id": "event_id_1",
"type": "agenda",
"versioncreated": datetime.utcnow(),
"name": "cheese event",
"dates": {
"start": datetime.utcnow(),
"end": datetime.utcnow(),
},
"recurrence_id": "event_id_1",
},
{
"_id": "event_id_2",
"type": "agenda",
"versioncreated": datetime.utcnow(),
"name": "another event",
"dates": {
"start": datetime.utcnow(),
"end": datetime.utcnow(),
},
"recurrence_id": "event_id_1",
},
],
)

with mock.patch("newsroom.push.notify_new_item") as notify_new_item:
notify_new_agenda_item("event_id_1")
assert notify_new_item.call_count == 1

notify_new_agenda_item("event_id_2", is_new=True)
assert notify_new_item.call_count == 1

notify_new_agenda_item("event_id_2")
assert notify_new_item.call_count == 2

0 comments on commit 2077f08

Please sign in to comment.