notify group members of group changes

Send a notification to all group members when group name, description, or privacy are changed.
This commit is contained in:
Hugh Rundle 2021-10-22 20:23:45 +11:00
parent c9deda8fdd
commit 6bc86f189f
4 changed files with 60 additions and 1 deletions

View file

@ -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",
)

View file

@ -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 %}

View file

@ -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 %}
<span class="icon icon-local"></span>
{% 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 <a href="{{ group_path }}">{{ group_name }}</a>
{% 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 <a href="{{ group_path }}">{{ group_name }}</a>
{% endblocktrans %}
{% else %}
{% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %}
has changed the description of <a href="{{ group_path }}">{{ group_name }}</a>
{% endblocktrans %}
{% endif %}
{% endblock %}

View file

@ -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)