From 6bc86f189fdc898fe8baeaf22f8bcdc0739636bf Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Fri, 22 Oct 2021 20:23:45 +1100 Subject: [PATCH] notify group members of group changes Send a notification to all group members when group name, description, or privacy are changed. --- bookwyrm/models/notification.py | 2 +- bookwyrm/templates/notifications/item.html | 6 ++++ .../templates/notifications/items/update.html | 28 +++++++++++++++++++ bookwyrm/views/group.py | 25 +++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/templates/notifications/items/update.html diff --git a/bookwyrm/models/notification.py b/bookwyrm/models/notification.py index b15b95c2c..2f1aae4f3 100644 --- a/bookwyrm/models/notification.py +++ b/bookwyrm/models/notification.py @@ -7,7 +7,7 @@ from . import Boost, Favorite, ImportJob, Report, Status, User # pylint: disable=line-too-long NotificationType = models.TextChoices( "NotificationType", - "FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT ADD REPORT INVITE ACCEPT JOIN LEAVE REMOVE", + "FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT ADD REPORT INVITE ACCEPT JOIN LEAVE REMOVE GROUP_PRIVACY GROUP_NAME GROUP_DESCRIPTION", ) diff --git a/bookwyrm/templates/notifications/item.html b/bookwyrm/templates/notifications/item.html index 3ed43a96a..e8e2dcb26 100644 --- a/bookwyrm/templates/notifications/item.html +++ b/bookwyrm/templates/notifications/item.html @@ -27,4 +27,10 @@ {% include 'notifications/items/leave.html' %} {% elif notification.notification_type == 'REMOVE' %} {% include 'notifications/items/remove.html' %} +{% elif notification.notification_type == 'GROUP_PRIVACY' %} + {% include 'notifications/items/update.html' %} +{% elif notification.notification_type == 'GROUP_NAME' %} + {% include 'notifications/items/update.html' %} +{% elif notification.notification_type == 'GROUP_DESCRIPTION' %} + {% include 'notifications/items/update.html' %} {% endif %} diff --git a/bookwyrm/templates/notifications/items/update.html b/bookwyrm/templates/notifications/items/update.html new file mode 100644 index 000000000..5c60e5a24 --- /dev/null +++ b/bookwyrm/templates/notifications/items/update.html @@ -0,0 +1,28 @@ +{% extends 'notifications/items/item_layout.html' %} + +{% load i18n %} +{% load utilities %} + +{% block primary_link %}{% spaceless %} + {{ notification.related_group.local_path }} +{% endspaceless %}{% endblock %} + +{% block icon %} + +{% endblock %} + +{% block description %} + {% if notification.notification_type == 'GROUP_PRIVACY' %} + {% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %} + has changed the privacy level for {{ group_name }} + {% endblocktrans %} + {% elif notification.notification_type == 'GROUP_NAME' %} + {% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %} + has changed the name of {{ group_name }} + {% endblocktrans %} + {% else %} + {% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %} + has changed the description of {{ group_name }} + {% endblocktrans %} + {% endif %} +{% endblock %} diff --git a/bookwyrm/views/group.py b/bookwyrm/views/group.py index 1c0a28486..9ee99bffa 100644 --- a/bookwyrm/views/group.py +++ b/bookwyrm/views/group.py @@ -47,6 +47,31 @@ class Group(View): if not form.is_valid(): return redirect("group", user_group.id) user_group = form.save() + + # let the other members know something about the group changed + memberships = models.GroupMember.objects.filter(group=user_group) + model = apps.get_model("bookwyrm.Notification", require_ready=True) + for field in form.changed_data: + notification_type = ( + "GROUP_PRIVACY" + if field == "privacy" + else "GROUP_NAME" + if field == "name" + else "GROUP_DESCRIPTION" + if field == "description" + else None + ) + if notification_type: + for membership in memberships: + member = membership.user + if member != request.user: + model.objects.create( + user=member, + related_user=request.user, + related_group=user_group, + notification_type=notification_type, + ) + return redirect("group", user_group.id)