complete group notifications

- notify group members when a new member accepts an invitation
- notify all group members when a member leaves or is removed
- notify ex-member when they are removed
This commit is contained in:
Hugh Rundle 2021-10-02 15:48:55 +10:00
parent f82af6382f
commit 21e6ed7388
4 changed files with 45 additions and 14 deletions

View file

@ -133,21 +133,27 @@ class GroupMemberInvitation(models.Model):
with transaction.atomic(): with transaction.atomic():
BookwyrmGroupMember.from_request(self) BookwyrmGroupMember.from_request(self)
# let the other members know about it
model = apps.get_model("bookwyrm.Notification", require_ready=True) model = apps.get_model("bookwyrm.Notification", require_ready=True)
# tell the group owner
model.objects.create(
user=self.group.user,
related_user=self.user,
related_group=self.group,
notification_type="ACCEPT",
)
# let the other members know about it
for membership in self.group.memberships.all(): for membership in self.group.memberships.all():
member = membership.user member = membership.user
if member != self.user: if member != self.user and member != self.group.user:
model.objects.create( model.objects.create(
user=member, user=member,
related_user=self.user, related_user=self.user,
related_group=self.group, related_group=self.group,
notification_type="ACCEPT", notification_type="JOIN",
) )
def reject(self): def reject(self):
"""generate a Reject for this membership request""" """generate a Reject for this membership request"""
self.delete() self.delete()
# TODO: send notification

View file

@ -7,7 +7,7 @@ from . import Boost, Favorite, ImportJob, Report, Status, User
NotificationType = models.TextChoices( NotificationType = models.TextChoices(
"NotificationType", "NotificationType",
"FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT ADD REPORT INVITE ACCEPT", "FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT ADD REPORT INVITE ACCEPT JOIN LEAVE REMOVE",
) )

View file

@ -42,7 +42,7 @@
<span class="icon icon-comment"></span> <span class="icon icon-comment"></span>
{% elif notification.notification_type == 'REPLY' %} {% elif notification.notification_type == 'REPLY' %}
<span class="icon icon-comments"></span> <span class="icon icon-comments"></span>
{% elif notification.notification_type == 'FOLLOW' or notification.notification_type == 'FOLLOW_REQUEST' or notification.notification_type == 'INVITE' or notification.notification_type == 'ACCEPT' %} {% elif notification.notification_type == 'FOLLOW' or notification.notification_type == 'FOLLOW_REQUEST' or notification.notification_type == 'INVITE' or notification.notification_type == 'ACCEPT' or notification.notification_type == 'JOIN' or notification.notification_type == 'LEAVE' or notification.notification_type == 'REMOVE'%}
<span class="icon icon-local"></span> <span class="icon icon-local"></span>
{% elif notification.notification_type == 'BOOST' %} {% elif notification.notification_type == 'BOOST' %}
<span class="icon icon-boost"></span> <span class="icon icon-boost"></span>
@ -131,9 +131,25 @@
{% endif %} {% endif %}
{% elif notification.notification_type == 'ACCEPT' %} {% elif notification.notification_type == 'ACCEPT' %}
{% if notification.related_group %} {% if notification.related_group %}
{% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %} accepted an invitation to join your group "<a href="{{ group_path }}">{{ group_name }}</a>"{% endblocktrans %} {% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %} accepted your invitation to join group "<a href="{{ group_path }}">{{ group_name }}</a>"{% endblocktrans %}
{% endif %}
{% elif notification.notification_type == 'JOIN' %}
{% if notification.related_group %}
{% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %} has joined your group "<a href="{{ group_path }}">{{ group_name }}</a>"{% endblocktrans %}
{% endif %}
{% elif notification.notification_type == 'LEAVE' %}
{% if notification.related_group %}
{% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %} has left your group "<a href="{{ group_path }}">{{ group_name }}</a>"{% endblocktrans %}
{% endif %}
{% elif notification.notification_type == 'REMOVE' %}
{% if notification.related_group %}
{% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %} has been removed from your group "<a href="{{ group_path }}">{{ group_name }}</a>"{% endblocktrans %}
{% endif %} {% endif %}
{% endif %} {% endif %}
{% elif notification.notification_type == 'REMOVE' and notification.related_group %}
{% blocktrans with group_name=notification.related_group.name group_path=notification.related_group.local_path %}
You have been removed from the "<a href="{{ group_path }}">{{ group_name }}</a> group"
{% endblocktrans %}
{% elif notification.related_import %} {% elif notification.related_import %}
{% url 'import-status' notification.related_import.id as url %} {% url 'import-status' notification.related_import.id as url %}
{% blocktrans %}Your <a href="{{ url }}">import</a> completed.{% endblocktrans %} {% blocktrans %}Your <a href="{{ url }}">import</a> completed.{% endblocktrans %}

View file

@ -158,7 +158,7 @@ def invite_member(request):
@require_POST @require_POST
@login_required @login_required
def remove_member(request): def remove_member(request):
"""invite a member to the group""" """remove a member from the group"""
group = get_object_or_404(models.BookwyrmGroup, id=request.POST.get("group")) group = get_object_or_404(models.BookwyrmGroup, id=request.POST.get("group"))
if not group: if not group:
@ -192,17 +192,26 @@ def remove_member(request):
except IntegrityError: except IntegrityError:
pass pass
# let the other members know about it memberships = models.BookwyrmGroupMember.objects.filter(group=group)
model = apps.get_model("bookwyrm.Notification", require_ready=True) model = apps.get_model("bookwyrm.Notification", require_ready=True)
memberships = models.BookwyrmGroupMember.objects.get(group=group) notification_type = "LEAVE" if "self_removal" in request.POST and request.POST["self_removal"] else "REMOVE"
# let the other members know about it
for membership in memberships: for membership in memberships:
member = membership.user member = membership.user
if member != request.user: if member != request.user:
model.objects.create( model.objects.create(
user=member, user=member,
related_user=request.user, related_user=user,
related_group=request.group, related_group=group,
notification_type="REMOVE", notification_type=notification_type,
)
# let the user (now ex-member) know as well, if they were removed
if notification_type == "REMOVE":
model.objects.create(
user=user,
related_group=group,
notification_type=notification_type,
) )
return redirect(user.local_path) return redirect(user.local_path)