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 subcoms #1256

Merged
merged 1 commit into from
Dec 9, 2024
Merged
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
49 changes: 44 additions & 5 deletions invenio_communities/notifications/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,6 @@ class SubComInvitationBuilderBase(SubCommunityBuilderBase):
EntityResolve("executing_user"),
]

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

slint marked this conversation as resolved.
Show resolved Hide resolved

class SubComInvitationCreate(SubComInvitationBuilderBase):
"""Notification builder for subcommunity request creation."""
Expand Down Expand Up @@ -292,3 +287,47 @@ class SubComInvitationExpire(SubComInvitationBuilderBase):
recipients = [
CommunityMembersRecipient("request.receiver", roles=["owner", "manager"]),
]


#
# Comments
#
class SubComCommentNotificationBuilderBase(SubCommunityBuilderBase):
"""Notification builder for comment request event creation."""

context = [
EntityResolve(key="request"),
EntityResolve(key="request.created_by"),
EntityResolve(key="request.receiver"),
EntityResolve(key="request_event"),
EntityResolve(key="request_event.created_by"),
]

@classmethod
def build(cls, request, request_event):
"""Build notification with request context."""
return Notification(
type=cls.type,
context={
"request": EntityResolverRegistry.reference_entity(request),
"request_event": EntityResolverRegistry.reference_entity(request_event),
},
)

recipient_filters = [
# do not send notification to user creating the comment
UserRecipientFilter(key="request_event.created_by"),
UserPreferencesRecipientFilter(),
]


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

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


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

type = f"comment-{SubComInvitationBuilderBase.type}.create"
9 changes: 9 additions & 0 deletions invenio_communities/subcommunities/services/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import invenio_communities.notifications.builders as notifications
from invenio_communities.proxies import current_communities

from ...notifications.builders import (
SubComInvCommentNotificationBuilder,
SubComReqCommentNotificationBuilder,
)


class AcceptSubcommunity(actions.AcceptAction):
"""Represents an accept action used to accept a subcommunity."""
Expand Down Expand Up @@ -64,6 +69,8 @@ class SubCommunityRequest(RequestType):
allowed_receiver_ref_types = ["community"]
allowed_topic_ref_types = ["community"]

comment_notification_builder = SubComReqCommentNotificationBuilder

available_actions = {
"delete": actions.DeleteAction,
"create": actions.CreateAndSubmitAction,
Expand Down Expand Up @@ -149,6 +156,8 @@ class SubCommunityInvitationRequest(RequestType):
allowed_receiver_ref_types = ["community"]
allowed_topic_ref_types = ["community"]

comment_notification_builder = SubComInvCommentNotificationBuilder

available_actions = {
"delete": actions.DeleteAction,
"cancel": actions.CancelAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

{% extends "invenio_communities/details/base.html" %}

{%- set title = _("New community") -%}
{%- set title = _("New subcommunity") -%}

{%- block javascript %}
{{ super() }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% 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 %}
{# It would be more natural to send to receiver, but for now we send to parent #}
{% set parent_community = invenio_request.created_by.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