diff --git a/invenio_communities/members/services/request.py b/invenio_communities/members/services/request.py index 7d193c418..d607be264 100644 --- a/invenio_communities/members/services/request.py +++ b/invenio_communities/members/services/request.py @@ -17,8 +17,10 @@ from invenio_communities.notifications.builders import ( CommunityInvitationAcceptNotificationBuilder, CommunityInvitationCancelNotificationBuilder, + CommunityInvitationCommentNotificationBuilder, CommunityInvitationDeclineNotificationBuilder, CommunityInvitationExpireNotificationBuilder, + CommunityRequestCommentNotificationBuilder, ) from ...proxies import current_communities @@ -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"] @@ -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"] diff --git a/invenio_communities/notifications/builders.py b/invenio_communities/notifications/builders.py index 0beb82a8b..7a505c068 100644 --- a/invenio_communities/notifications/builders.py +++ b/invenio_communities/notifications/builders.py @@ -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 @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -292,7 +295,7 @@ class SubComInvitationExpire(SubComInvitationBuilderBase): # # Comments # -class SubComCommentNotificationBuilderBase(SubCommunityBuilderBase): +class CommunityCommentNotificationBuilderBase(SubCommunityBuilderBase): """Notification builder for comment request event creation.""" context = [ @@ -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" diff --git a/invenio_communities/templates/semantic-ui/invenio_notifications/comment-community-invitation.create.jinja b/invenio_communities/templates/semantic-ui/invenio_notifications/comment-community-invitation.create.jinja new file mode 100644 index 000000000..756968fb0 --- /dev/null +++ b/invenio_communities/templates/semantic-ui/invenio_notifications/comment-community-invitation.create.jinja @@ -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 -%} + + + + + + + + + + + + + + + + +
{{ _("'@{user_name}' commented on '{request_title}':").format(user_name=event_creator_name, request_title=request_title) }}
{{ request_event_content }}
{{ _("Check out the request")}}
_
{{ _("This is an auto-generated message. To manage notifications, visit your")}} {{ _("account settings")}}.
+{%- 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 %} diff --git a/invenio_communities/templates/semantic-ui/invenio_notifications/comment-community-request.create.jinja b/invenio_communities/templates/semantic-ui/invenio_notifications/comment-community-request.create.jinja new file mode 100644 index 000000000..756968fb0 --- /dev/null +++ b/invenio_communities/templates/semantic-ui/invenio_notifications/comment-community-request.create.jinja @@ -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 -%} + + + + + + + + + + + + + + + + +
{{ _("'@{user_name}' commented on '{request_title}':").format(user_name=event_creator_name, request_title=request_title) }}
{{ request_event_content }}
{{ _("Check out the request")}}
_
{{ _("This is an auto-generated message. To manage notifications, visit your")}} {{ _("account settings")}}.
+{%- 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 %}