Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notifications: add comment notification for record and membership requests #1262

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions invenio_communities/members/services/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
from invenio_communities.notifications.builders import (
CommunityInvitationAcceptNotificationBuilder,
CommunityInvitationCancelNotificationBuilder,
CommunityInvitationCommentNotificationBuilder,
CommunityInvitationDeclineNotificationBuilder,
CommunityInvitationExpireNotificationBuilder,
CommunityRequestCommentNotificationBuilder,
)

from ...proxies import current_communities
Expand Down Expand Up @@ -117,6 +119,8 @@ class CommunityInvitation(RequestType):
"expire": ExpireAction,
}

comment_notification_builder = CommunityInvitationCommentNotificationBuilder

creator_can_be_none = False
topic_can_be_none = False
allowed_creator_ref_types = ["community"]
Expand Down Expand Up @@ -158,6 +162,8 @@ class MembershipRequestRequestType(RequestType):
"cancel": CancelMembershipRequestAction,
}

comment_notification_builder = CommunityRequestCommentNotificationBuilder

creator_can_be_none = False
topic_can_be_none = False
allowed_creator_ref_types = ["user"]
Expand Down
53 changes: 45 additions & 8 deletions invenio_communities/notifications/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from invenio_notifications.services.builders import NotificationBuilder
from invenio_notifications.services.generators import EntityResolve, UserEmailBackend
from invenio_requests.notifications.filters import UserRecipientFilter
from invenio_requests.notifications.generators import RequestParticipantsRecipient
from invenio_users_resources.notifications.filters import UserPreferencesRecipientFilter
from invenio_users_resources.notifications.generators import UserRecipient

Expand Down Expand Up @@ -40,13 +41,15 @@ class BaseNotificationBuilder(NotificationBuilder):
class CommunityInvitationNotificationBuilder(BaseNotificationBuilder):
"""Base notification builder for community invitation action."""

type = "community-invitation"


class CommunityInvitationSubmittedNotificationBuilder(
CommunityInvitationNotificationBuilder
):
"""Notification builder for community invitation submit action."""

type = "community-invitation.submit"
type = f"{CommunityInvitationNotificationBuilder.type}.submit"

@classmethod
def build(cls, request, role, message=None):
Expand All @@ -70,7 +73,7 @@ class CommunityInvitationAcceptNotificationBuilder(
):
"""Notification builder for community invitation accept action."""

type = "community-invitation.accept"
type = f"{CommunityInvitationNotificationBuilder.type}.accept"

@classmethod
def build(cls, request):
Expand All @@ -92,7 +95,7 @@ class CommunityInvitationCancelNotificationBuilder(
):
"""Notification builder for community invitation cancel action."""

type = "community-invitation.cancel"
type = f"{CommunityInvitationNotificationBuilder.type}.cancel"

@classmethod
def build(cls, request):
Expand All @@ -114,7 +117,7 @@ class CommunityInvitationDeclineNotificationBuilder(
):
"""Notification builder for community invitation decline action."""

type = "community-invitation.decline"
type = f"{CommunityInvitationNotificationBuilder.type}.decline"

@classmethod
def build(cls, request):
Expand All @@ -136,7 +139,7 @@ class CommunityInvitationExpireNotificationBuilder(
):
"""Notification builder for community invitation expire action."""

type = "community-invitation.expire"
type = f"{CommunityInvitationNotificationBuilder.type}.expire"

@classmethod
def build(cls, request):
Expand Down Expand Up @@ -292,7 +295,7 @@ class SubComInvitationExpire(SubComInvitationBuilderBase):
#
# Comments
#
class SubComCommentNotificationBuilderBase(SubCommunityBuilderBase):
class CommunityCommentNotificationBuilderBase(SubCommunityBuilderBase):
"""Notification builder for comment request event creation."""

context = [
Expand Down Expand Up @@ -321,13 +324,47 @@ def build(cls, request, request_event):
]


class SubComReqCommentNotificationBuilder(SubComCommentNotificationBuilderBase):
class CommunityRequestCommentNotificationBuilder(
CommunityCommentNotificationBuilderBase
):
"""Notification builder for community membership request comment creation.
The CommunityRequest notification builders are shared with CommunityInvitation,
but here we have different handlers for comments as the receiver/creator are
different in each case.
Requests go to a community and invitations go to a user. CommunityMembersRecipient
doesn't allow you to pass a user object, so we manually set which party is the community.
"""

type = f"comment-community-request.create"

recipients = [
RequestParticipantsRecipient(key="request"),
CommunityMembersRecipient("request.receiver", roles=["owner", "manager"]),
]


class CommunityInvitationCommentNotificationBuilder(
CommunityCommentNotificationBuilderBase
):
"""Notification builder for community membership invitation comment creation."""

type = f"comment-{CommunityInvitationNotificationBuilder.type}.create"

recipients = [
RequestParticipantsRecipient(key="request"),
CommunityMembersRecipient("request.created_by", roles=["owner", "manager"]),
]


class SubComReqCommentNotificationBuilder(CommunityCommentNotificationBuilderBase):
"""Notification builder for comment request event creation."""

type = f"comment-{SubCommunityBuilderBase.type}.create"


class SubComInvCommentNotificationBuilder(SubComCommentNotificationBuilderBase):
class SubComInvCommentNotificationBuilder(CommunityCommentNotificationBuilderBase):
"""Notification builder for comment request event creation."""

type = f"comment-{SubComInvitationBuilderBase.type}.create"
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{% set invenio_request = notification.context.request %}
{% set invenio_request_event = notification.context.request_event %}

{# created_by is either a resolved user or an email (for guests) #}
{% set event_creator_name = invenio_request_event.created_by.username or invenio_request_event.created_by %}
{% set request_id = invenio_request.id %}
{% set request_event_content = invenio_request_event.payload.content | safe %}
{% set request_title = invenio_request.title | safe %}
{% set parent_community = invenio_request.receiver.slug %}

{# TODO: use request.links.self_html when issue issue is resolved: https://github.com/inveniosoftware/invenio-rdm-records/issues/1327 #}
{% set request_link = "{ui}/communities/{parent_community}/requests/{request_id}".format(
ui=config.SITE_UI_URL, parent_community=parent_community, request_id=request_id
)
%}
{% set account_settings_link = "{ui}/account/settings/notifications".format(
ui=config.SITE_UI_URL
)
%}

{%- block subject -%}
{{ _("💬 New comment on '{request_title}'").format(request_title=request_title) }}
{%- endblock subject -%}

{%- block html_body -%}
<table style="font-family:'Lato',Helvetica,Arial,sans-serif;border-spacing:15px">
<tr>
<td>{{ _("'@{user_name}' commented on '{request_title}':").format(user_name=event_creator_name, request_title=request_title) }}</td>
</tr>
<tr>
<td><em>{{ request_event_content }}</em></td>
</tr>
<tr>
<td><a href="{{ request_link }}" class="button">{{ _("Check out the request")}}</a></td>
</tr>
<tr>
<td><strong>_</strong></td>
</tr>
<tr>
<td style="font-size:smaller">{{ _("This is an auto-generated message. To manage notifications, visit your")}} <a href="{{account_settings_link}}">{{ _("account settings")}}</a>.</td>
</tr>
</table>
{%- endblock html_body %}

{%- block plain_body -%}
{{ _("@{user_name} commented on '{request_title}'").format(user_name=event_creator_name, request_title=request_title) }}.

{{ request_event_content }}

{{ _("Check out the request: {request_link}").format(request_link=request_link) }}

{%- endblock plain_body %}

{# Markdown for Slack/Mattermost/chat #}
{%- block md_body -%}
{{ _("*@{user_name}* commented on *{request_title}*").format(user_name=event_creator_name, request_title=request_title) }}.

{{ request_event_content }}

[{{_("Check out the request")}}]({{request_link}})
{%- endblock md_body %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{% set invenio_request = notification.context.request %}
{% set invenio_request_event = notification.context.request_event %}

{# created_by is either a resolved user or an email (for guests) #}
{% set event_creator_name = invenio_request_event.created_by.username or invenio_request_event.created_by %}
{% set request_id = invenio_request.id %}
{% set request_event_content = invenio_request_event.payload.content | safe %}
{% set request_title = invenio_request.title | safe %}
{% set parent_community = invenio_request.receiver.slug %}

{# TODO: use request.links.self_html when issue issue is resolved: https://github.com/inveniosoftware/invenio-rdm-records/issues/1327 #}
{% set request_link = "{ui}/communities/{parent_community}/requests/{request_id}".format(
ui=config.SITE_UI_URL, parent_community=parent_community, request_id=request_id
)
%}
{% set account_settings_link = "{ui}/account/settings/notifications".format(
ui=config.SITE_UI_URL
)
%}

{%- block subject -%}
{{ _("💬 New comment on '{request_title}'").format(request_title=request_title) }}
{%- endblock subject -%}

{%- block html_body -%}
<table style="font-family:'Lato',Helvetica,Arial,sans-serif;border-spacing:15px">
<tr>
<td>{{ _("'@{user_name}' commented on '{request_title}':").format(user_name=event_creator_name, request_title=request_title) }}</td>
</tr>
<tr>
<td><em>{{ request_event_content }}</em></td>
</tr>
<tr>
<td><a href="{{ request_link }}" class="button">{{ _("Check out the request")}}</a></td>
</tr>
<tr>
<td><strong>_</strong></td>
</tr>
<tr>
<td style="font-size:smaller">{{ _("This is an auto-generated message. To manage notifications, visit your")}} <a href="{{account_settings_link}}">{{ _("account settings")}}</a>.</td>
</tr>
</table>
{%- endblock html_body %}

{%- block plain_body -%}
{{ _("@{user_name} commented on '{request_title}'").format(user_name=event_creator_name, request_title=request_title) }}.

{{ request_event_content }}

{{ _("Check out the request: {request_link}").format(request_link=request_link) }}

{%- endblock plain_body %}

{# Markdown for Slack/Mattermost/chat #}
{%- block md_body -%}
{{ _("*@{user_name}* commented on *{request_title}*").format(user_name=event_creator_name, request_title=request_title) }}.

{{ request_event_content }}

[{{_("Check out the request")}}]({{request_link}})
{%- endblock md_body %}
Loading