Notification emails #80

Merged
Oleg-Komarov merged 31 commits from notifications into main 2024-04-18 16:11:20 +02:00
4 changed files with 40 additions and 6 deletions
Showing only changes of commit 144712c3f7 - Show all commits

View File

@ -399,10 +399,12 @@ class DraftExtensionView(
add_preview_formset.save() add_preview_formset.save()
form.save() form.save()
# setup initial followers # setup initial followers
authors = extension_form.instance.authors.all()
moderators = Group.objects.get(name='moderators').user_set.all() moderators = Group.objects.get(name='moderators').user_set.all()
audience = list(moderators) + list(extension_form.instance.authors.all()) for recipient in authors:
for recipient in audience: follow(recipient, extension_form.instance, send_action=False, flag='author')
follow(recipient, extension_form.instance, send_action=False) for recipient in moderators:
follow(recipient, extension_form.instance, send_action=False, flag='moderator')
if 'submit_draft' in self.request.POST: if 'submit_draft' in self.request.POST:
action.send( action.send(
self.request.user, self.request.user,

View File

@ -80,9 +80,10 @@ class ExtensionsApprovalFormView(LoginRequiredMixin, FormView):
form.save() form.save()
self.approve_if_allowed(form) self.approve_if_allowed(form)
# automatically follow after ineraction # automatically follow after an interaction
# if a user had unfollowed this extension before, # if a user had unfollowed this extension before,
# we unfortunately are making them a follower again # we unfortunately are making them a follower again
# TODO? set a specific flag?
follow(self.request.user, form.instance.extension, send_action=False) follow(self.request.user, form.instance.extension, send_action=False)
activity_type2verb = { activity_type2verb = {

View File

@ -4,7 +4,7 @@ import logging
import time import time
from django.contrib.admin.utils import NestedObjects from django.contrib.admin.utils import NestedObjects
from django.contrib.auth.models import AbstractUser, Group from django.contrib.auth.models import AbstractUser
from django.db import models, DEFAULT_DB_ALIAS, transaction from django.db import models, DEFAULT_DB_ALIAS, transaction
from django.templatetags.static import static from django.templatetags.static import static

View File

@ -1,14 +1,17 @@
from typing import Dict from typing import Dict
import logging import logging
from actstream.actions import follow, unfollow
from actstream.models import Action, followers from actstream.models import Action, followers
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db.models.signals import post_save, pre_save from django.contrib.auth.models import Group
from django.db.models.signals import m2m_changed, post_save, pre_save
from django.dispatch import receiver from django.dispatch import receiver
from blender_id_oauth_client import signals as bid_signals from blender_id_oauth_client import signals as bid_signals
from users.blender_id import BIDSession from users.blender_id import BIDSession
from extensions.models import Extension
User = get_user_model() User = get_user_model()
bid = BIDSession() bid = BIDSession()
@ -61,3 +64,31 @@ def create_notification(
f'create notification for {recipient}: ', f'create notification for {recipient}: ',
f'{instance.actor} {instance.verb} {instance.target}', f'{instance.actor} {instance.verb} {instance.target}',
) )
@receiver(m2m_changed, sender=User.groups.through)
def update_moderator_follows(instance, action, model, reverse, pk_set, **kwargs):
"""Users becoming moderators should follow all extensions,
and users that stop being moderators should no longer follow all extensions.
The flag='moderator' is used to avoid deleting follow relations that were created in contexts
other than moderator's duties.
"""
moderators = Group.objects.get(name='moderators')
extensions = Extension.objects.all()
users = []
if model == Group and not reverse:
if moderators.pk not in pk_set:
return
users = [instance]
else:
if instance != moderators:
return
users = User.objects.filter(pk__in=pk_set)
for user in users:
if action == 'post_remove':
for extension in extensions:
unfollow(user, extension, send_action=False, flag='moderator')
elif action == 'post_add':
for extension in extensions:
follow(user, extension, send_action=False, flag='moderator')