def _send_add_comment_notifications()

in pontoon/base/views.py [0:0]


def _send_add_comment_notifications(user, comment, entity, locale, translation):
    # On translation comment, notify:
    #   - project-locale translators or locale translators
    #   - locale managers
    #   - authors of other translation comments in the thread
    #   - translation author
    #   - translation reviewers
    if translation:
        recipients = set(translation.comments.values_list("author__pk", flat=True))

        if translation.user:
            recipients.add(translation.user.pk)
        if translation.approved_user:
            recipients.add(translation.approved_user.pk)
        if translation.unapproved_user:
            recipients.add(translation.unapproved_user.pk)
        if translation.rejected_user:
            recipients.add(translation.rejected_user.pk)
        if translation.unrejected_user:
            recipients.add(translation.unrejected_user.pk)

    # On team comment, notify:
    #   - project-locale translators or locale translators
    #   - locale managers
    #   - authors of other team comments in the thread
    #   - authors of translation comments
    #   - translation authors
    #   - translation reviewers
    else:
        recipients = set()
        translations = Translation.objects.filter(entity=entity, locale=locale)

        recipients = recipients.union(
            Comment.objects.filter(entity=entity, locale=locale).values_list(
                "author__pk", flat=True
            )
        )

        recipients = recipients.union(
            Comment.objects.filter(translation__in=translations).values_list(
                "author__pk", flat=True
            )
        )

        recipients = recipients.union(translations.values_list("user__pk", flat=True))
        recipients = recipients.union(
            translations.values_list("approved_user__pk", flat=True)
        )
        recipients = recipients.union(
            translations.values_list("unapproved_user__pk", flat=True)
        )
        recipients = recipients.union(
            translations.values_list("rejected_user__pk", flat=True)
        )
        recipients = recipients.union(
            translations.values_list("unrejected_user__pk", flat=True)
        )

    # In both cases, notify locale managers and translators
    project_locale = ProjectLocale.objects.get(
        project=entity.resource.project,
        locale=locale,
    )
    translators = []
    # Some projects (e.g. system projects) don't have translators group
    if project_locale.translators_group:
        # Only notify translators of the project if defined
        translators = project_locale.translators_group.user_set.values_list(
            "pk", flat=True
        )
    if not translators:
        translators = locale.translators_group.user_set.values_list("pk", flat=True)

    recipients = recipients.union(translators)
    recipients = recipients.union(
        locale.managers_group.user_set.values_list("pk", flat=True)
    )

    # Notify users, mentioned in a comment
    usernames = re.findall(r"<a href=\"\/contributors/([\w.@+-]+)/\">.+</a>", comment)
    recipients = recipients.union(
        User.objects.filter(username__in=usernames).values_list("pk", flat=True)
    )

    for recipient in User.objects.filter(
        pk__in=recipients,
        profile__comment_notifications=True,
    ).exclude(pk=user.pk):
        notify.send(
            user,
            recipient=recipient,
            verb="has added a comment in",
            action_object=locale,
            target=entity,
            description=comment,
            category="comment",
        )