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

dev: email notifications #3421

Merged
merged 27 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0230563
dev: create email notification preference model
pablohashescobar Jan 10, 2024
88f5d2b
dev: intiate models
pablohashescobar Jan 10, 2024
793d85e
dev: user notification preferences
pablohashescobar Jan 12, 2024
0bd3d16
dev: create notification logs for the user.
pablohashescobar Jan 12, 2024
ef7503b
dev: email notification stacking and sending logic
pablohashescobar Jan 16, 2024
76a6e9a
feat: email notification preference settings page.
prateekshourya29 Jan 16, 2024
211cde8
dev: delete subscribers
pablohashescobar Jan 18, 2024
f0806ba
dev: issue update ui implementation in email notification
1akhanBaheti Jan 18, 2024
ba51a85
chore: integrate email notification endpoint.
prateekshourya29 Jan 18, 2024
a967fd9
chore: remove toggle switch.
prateekshourya29 Jan 18, 2024
6bebb96
chore: added labels part
rameshkumarchandra Jan 19, 2024
b3de9e0
fix: refactored base design with tables
1akhanBaheti Jan 19, 2024
a471a03
dev: email notification templates
pablohashescobar Jan 22, 2024
ff698a2
dev: template updates
pablohashescobar Jan 22, 2024
6ed7bff
dev: update models
pablohashescobar Jan 22, 2024
7aeffac
dev: update template for labels and new migrations
pablohashescobar Jan 22, 2024
d269c31
fix: profile settings preference sidebar.
prateekshourya29 Jan 22, 2024
02531f6
dev: update preference endpoints
pablohashescobar Jan 22, 2024
73e8422
dev: update the schedule to 5 minutes
pablohashescobar Jan 22, 2024
5b77875
dev: update template with priority data
pablohashescobar Jan 22, 2024
e2f54db
dev: update templates
pablohashescobar Jan 22, 2024
73d9ff8
chore: enable `issue subscribe` button for all users.
prateekshourya29 Jan 23, 2024
1192341
chore: notification handling for external api
NarayanBavisetti Jan 23, 2024
f828712
Merge branch 'develop' into dev/email-notifications
pablohashescobar Jan 23, 2024
fe05c28
Merge branch 'develop' of github.com:makeplane/plane into dev/email-n…
pablohashescobar Jan 23, 2024
cd08b9e
Merge branch 'dev/email-notifications' of github.com:makeplane/plane …
pablohashescobar Jan 23, 2024
b82e20e
dev: update origin request
pablohashescobar Jan 23, 2024
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
2 changes: 1 addition & 1 deletion apiserver/plane/app/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@

from .analytic import AnalyticViewSerializer

from .notification import NotificationSerializer
from .notification import NotificationSerializer, UserNotificationPreferenceSerializer

from .exporter import ExporterHistorySerializer

Expand Down
9 changes: 8 additions & 1 deletion apiserver/plane/app/serializers/notification.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Module imports
from .base import BaseSerializer
from .user import UserLiteSerializer
from plane.db.models import Notification
from plane.db.models import Notification, UserNotificationPreference


class NotificationSerializer(BaseSerializer):
Expand All @@ -12,3 +12,10 @@ class NotificationSerializer(BaseSerializer):
class Meta:
model = Notification
fields = "__all__"


class UserNotificationPreferenceSerializer(BaseSerializer):

class Meta:
model = UserNotificationPreference
fields = "__all__"
6 changes: 6 additions & 0 deletions apiserver/plane/app/urls/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
NotificationViewSet,
UnreadNotificationEndpoint,
MarkAllReadNotificationViewSet,
UserNotificationPreferenceEndpoint,
)


Expand Down Expand Up @@ -63,4 +64,9 @@
),
name="mark-all-read-notifications",
),
path(
"users/me/notification-preferences/",
UserNotificationPreferenceEndpoint.as_view(),
name="user-notification-preferences",
),
]
1 change: 1 addition & 0 deletions apiserver/plane/app/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
NotificationViewSet,
UnreadNotificationEndpoint,
MarkAllReadNotificationViewSet,
UserNotificationPreferenceEndpoint,
)

from .exporter import ExportIssuesEndpoint
Expand Down
59 changes: 53 additions & 6 deletions apiserver/plane/app/views/notification.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Django imports
from django.db.models import Q
from django.db.models import Q, OuterRef, Exists
from django.utils import timezone

# Third party imports
Expand All @@ -15,8 +15,9 @@
IssueSubscriber,
Issue,
WorkspaceMember,
UserNotificationPreference,
)
from plane.app.serializers import NotificationSerializer
from plane.app.serializers import NotificationSerializer, UserNotificationPreferenceSerializer


class NotificationViewSet(BaseViewSet, BasePaginator):
Expand Down Expand Up @@ -71,11 +72,29 @@ def list(self, request, slug):

# Subscribed issues
if type == "watching":
issue_ids = IssueSubscriber.objects.filter(
workspace__slug=slug, subscriber_id=request.user.id
).values_list("issue_id", flat=True)
issue_ids = (
IssueSubscriber.objects.filter(
workspace__slug=slug, subscriber_id=request.user.id
)
.annotate(
created=Exists(
Issue.objects.filter(
created_by=request.user, pk=OuterRef("issue_id")
)
)
)
.annotate(
assigned=Exists(
IssueAssignee.objects.filter(
pk=OuterRef("issue_id"), assignee=request.user
)
)
)
.filter(created=False, assigned=False)
.values_list("issue_id", flat=True)
)
notifications = notifications.filter(
entity_identifier__in=issue_ids
entity_identifier__in=issue_ids,
)

# Assigned Issues
Expand Down Expand Up @@ -295,3 +314,31 @@ def create(self, request, slug):
updated_notifications, ["read_at"], batch_size=100
)
return Response({"message": "Successful"}, status=status.HTTP_200_OK)


class UserNotificationPreferenceEndpoint(BaseAPIView):
model = UserNotificationPreference
serializer_class = UserNotificationPreferenceSerializer

# request the object
def get(self, request):
user_notification_preference = UserNotificationPreference.objects.get(
user=request.user
)
serializer = UserNotificationPreferenceSerializer(
user_notification_preference
)
return Response(serializer.data, status=status.HTTP_200_OK)

# update the object
def patch(self, request):
user_notification_preference = UserNotificationPreference.objects.get(
user=request.user
)
serializer = UserNotificationPreferenceSerializer(
user_notification_preference, data=request.data, partial=True
)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
209 changes: 209 additions & 0 deletions apiserver/plane/bgtasks/email_notification_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Third party imports
from celery import shared_task
import os

# Django imports
from django.utils import timezone
from django.core.mail import EmailMultiAlternatives, get_connection
from django.template.loader import render_to_string
from django.utils.html import strip_tags

# Module imports
from plane.db.models import EmailNotificationLog, User, Issue
from plane.license.utils.instance_value import get_email_configuration


@shared_task
def stack_email_notification():
# get all email notifications
email_notifications = (
EmailNotificationLog.objects.filter(processed_at__isnull=True)
.order_by("receiver")
.values()
)

# Create the below format for each of the issues
# {"issue_id" : { "actor_id1": [ { data }, { data } ], "actor_id2": [ { data }, { data } ] }}

# Convert to unique receivers list
receivers = list(
set(
[
str(notification.get("receiver_id"))
for notification in email_notifications
]
)
)
processed_notifications = []
# Loop through all the issues to create the emails
for receiver_id in receivers:
# Notifcation triggered for the receiver
receiver_notifications = [
notification
for notification in email_notifications
if str(notification.get("receiver_id")) == receiver_id
]
# create payload for all issues
payload = {}
email_notification_ids = []
for receiver_notification in receiver_notifications:
payload.setdefault(
receiver_notification.get("entity_identifier"), {}
).setdefault(
str(receiver_notification.get("triggered_by_id")), []
).append(
receiver_notification.get("data")
)
# append processed notifications
processed_notifications.append(receiver_notification.get("id"))
email_notification_ids.append(receiver_notification.get("id"))

# Create emails for all the issues
for issue_id, notification_data in payload.items():
send_email_notification.delay(
issue_id=issue_id,
notification_data=notification_data,
receiver_id=receiver_id,
email_notification_ids=email_notification_ids,
)

# Update the email notification log
EmailNotificationLog.objects.filter(pk__in=processed_notifications).update(
processed_at=timezone.now()
)


def create_payload(notification_data):
# return format {"actor_id": { "key": { "old_value": [], "new_value": [] } }}
data = {}
for actor_id, changes in notification_data.items():
for change in changes:
issue_activity = change.get("issue_activity")
if issue_activity: # Ensure issue_activity is not None
field = issue_activity.get("field")
old_value = issue_activity.get("old_value")
new_value = issue_activity.get("new_value")

# Append old_value if it's not empty and not already in the list
if old_value:
data.setdefault(actor_id, {}).setdefault(
field, {}
).setdefault("old_value", []).append(
old_value
) if old_value not in data.setdefault(
actor_id, {}
).setdefault(
field, {}
).get(
"old_value", []
) else None

# Append new_value if it's not empty and not already in the list
if new_value:
data.setdefault(actor_id, {}).setdefault(
field, {}
).setdefault("new_value", []).append(
new_value
) if new_value not in data.setdefault(
actor_id, {}
).setdefault(
field, {}
).get(
"new_value", []
) else None

return data


@shared_task
def send_email_notification(
issue_id, notification_data, receiver_id, email_notification_ids
):
base_api = "http://localhost:3000"
data = create_payload(notification_data=notification_data)

# Get email configurations
(
EMAIL_HOST,
EMAIL_HOST_USER,
EMAIL_HOST_PASSWORD,
EMAIL_PORT,
EMAIL_USE_TLS,
EMAIL_FROM,
) = get_email_configuration()

receiver = User.objects.get(pk=receiver_id)
issue = Issue.objects.get(pk=issue_id)
template_data = []
total_changes = 0
for actor_id, changes in data.items():
actor = User.objects.get(pk=actor_id)
total_changes = total_changes + len(changes)
template_data.append(
{
"actor_detail": {
"avatar": actor.avatar,
"first_name": actor.first_name,
"last_name": actor.last_name,
},
"changes": changes,
"issue_details": {
"name": issue.name,
"identifier": f"{issue.project.identifier}-{issue.sequence_id}",
},
}
)

summary = ""
if len(template_data) == 1:
summary = f"{template_data[0]['actor_detail']['first_name']} {template_data[0]['actor_detail']['last_name']} made {total_changes} changes to the issue"
else:
summary = f"{template_data[0]['actor_detail']['first_name']} {template_data[0]['actor_detail']['last_name']} and others made {total_changes} changes to the issue"

# Send the mail
subject = f"{issue.project.identifier}-{issue.sequence_id} {issue.name}"
context = {
"data": template_data,
"summary": summary,
"issue": {
"issue_identifier": f"{str(issue.project.identifier)}-{str(issue.sequence_id)}",
"name": issue.name,
},
"receiver": {
"email": receiver.email,
},
"issue_unsubscribe": f"{base_api}/{str(issue.project.workspace.slug)}/projects/{str(issue.project.id)}/issues/{str(issue.id)}",
"user_preference": f"{base_api}/profile/preferences/email"
}
html_content = render_to_string(
"emails/notifications/issue-updates.html", context
)
text_content = strip_tags(html_content)

try:
connection = get_connection(
host=EMAIL_HOST,
port=int(EMAIL_PORT),
username=EMAIL_HOST_USER,
password=EMAIL_HOST_PASSWORD,
use_tls=EMAIL_USE_TLS == "1",
)

msg = EmailMultiAlternatives(
subject=subject,
body=text_content,
from_email=EMAIL_FROM,
to=[receiver.email],
connection=connection,
)
msg.attach_alternative(html_content, "text/html")
msg.send()

EmailNotificationLog.objects.filter(
pk__in=email_notification_ids
).update(sent_at=timezone.now())
print("Email Sent")
return
except Exception as e:
print(e)
return
17 changes: 16 additions & 1 deletion apiserver/plane/bgtasks/importer_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Label,
User,
IssueProperty,
UserNotificationPreference,
)


Expand All @@ -50,10 +51,24 @@ def service_importer(service, importer_id):
for user in users
if user.get("import", False) == "invite"
],
batch_size=10,
batch_size=100,
ignore_conflicts=True,
)

_ = UserNotificationPreference.objects.bulk_create(
[UserNotificationPreference(user=user) for user in new_users],
batch_size=100,
)

_ = [
send_welcome_slack.delay(
str(user.id),
True,
f"{user.email} was imported to Plane from {service}",
)
for user in new_users
]

workspace_users = User.objects.filter(
email__in=[
user.get("email").strip().lower()
Expand Down
Loading
Loading