Skip to content

Commit

Permalink
feat(notification): notify each stackeholders of new replies
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 11, 2023
1 parent d92ff8a commit 66e4275
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
84 changes: 46 additions & 38 deletions lacommunaute/notification/tests/tests_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.conf import settings
from django.test import TestCase
from faker import Faker

from lacommunaute.forum_conversation.factories import PostFactory, TopicFactory
from lacommunaute.forum_conversation.models import Post
from lacommunaute.notification.enums import EmailSentTrackKind
from lacommunaute.notification.factories import BouncedEmailFactory, EmailSentTrackFactory
from lacommunaute.notification.models import EmailSentTrack
from lacommunaute.notification.utils import (
Expand Down Expand Up @@ -61,59 +62,66 @@ def test_first_reply_since_last_notification(self):

class CollectFollowingRepliesTestCase(TestCase):
@classmethod
def setUp(cls):
def setUpTestData(cls):
cls.topic = TopicFactory(with_post=True)

def test_no_reply_ever(self):
self.assertEqual(len(collect_following_replies()), 0)
def test_no_reply_to_be_notified(self):
self.assertEqual(len(list(collect_following_replies())), 0)

def test_one_answer(self):
PostFactory(topic=self.topic)
self.assertEqual(len(collect_following_replies()), 0)
def test_one_reply_to_be_notified(self):
# first reply
PostFactory(topic=self.topic, poster__email=self.topic.poster_email)
EmailSentTrackFactory(kind=EmailSentTrackKind.FOLLOWING_REPLIES)

def test_some_more_answers(self):
PostFactory.create_batch(2, topic=self.topic)
# following reply
PostFactory(topic=self.topic)
self.assertEqual(
collect_following_replies(),
list(collect_following_replies()),
[
(
f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{self.topic.get_absolute_url()}",
self.topic.get_absolute_url(with_fqdn=True),
self.topic.subject,
self.topic.poster_email,
"2 nouvelles réponses",
[self.topic.poster_email],
"1 nouvelle réponse",
)
],
)

def test_multiple_replies_to_be_notified(self):
PostFactory(topic=self.topic)
self.assertEqual(
collect_following_replies(),
[
(
f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{self.topic.get_absolute_url()}",
self.topic.subject,
self.topic.poster_email,
"3 nouvelles réponses",

for i in range(2, 10):
with self.subTest(i=i):
PostFactory(topic=self.topic)

self.assertEqual(
list(collect_following_replies()),
[
(
self.topic.get_absolute_url(with_fqdn=True),
self.topic.subject,
sorted(
list(
set(
Post.objects.filter(topic=self.topic)
.exclude(id=self.topic.last_post_id)
.values_list("poster__email", flat=True)
)
)
),
f"{i} nouvelles réponses",
)
],
)
],
)

def test_replies_after_previous_notification(self):
def test_following_replies_since_last_notification(self):
PostFactory.create_batch(2, topic=self.topic)
EmailSentTrackFactory(kind="following_replies")
self.assertEqual(len(collect_following_replies()), 0)

PostFactory(topic=self.topic)
self.assertEqual(
collect_following_replies(),
[
(
f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{self.topic.get_absolute_url()}",
self.topic.subject,
self.topic.poster_email,
"1 nouvelle réponse",
)
],
)
EmailSentTrackFactory(kind="other")
self.assertEqual(len(list(collect_following_replies())), 1)

EmailSentTrackFactory(kind=EmailSentTrackKind.FOLLOWING_REPLIES)
self.assertEqual(len(list(collect_following_replies())), 0)


class CollectNewUsersForOnBoardingTestCase(TestCase):
Expand Down
17 changes: 4 additions & 13 deletions lacommunaute/notification/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from django.db.models import Count, F, Q

from django.utils.timezone import now, timedelta

from lacommunaute.forum_conversation.models import Topic
Expand All @@ -25,25 +25,16 @@ def collect_first_replies():


def collect_following_replies():
# vincentporte : assumed disapproved post are counted
return [
(
f"{settings.COMMU_PROTOCOL}://{settings.COMMU_FQDN}{topic.get_absolute_url()}",
topic.get_absolute_url(with_fqdn=True),
topic.subject,
topic.poster_email,
topic.mails_to_notify(),
f"{topic.new_replies} nouvelle réponse"
if topic.new_replies == 1
else f"{topic.new_replies} nouvelles réponses",
)
for topic in Topic.objects.filter(
updated__gte=last_notification(kind=EmailSentTrackKind.FOLLOWING_REPLIES), posts_count__gte=3
).annotate(
new_replies=Count(
"posts",
filter=Q(posts__created__gte=last_notification(kind=EmailSentTrackKind.FOLLOWING_REPLIES))
& ~Q(posts__id=F("first_post_id")),
)
)
for topic in Topic.objects.with_following_replies(last_notification(kind=EmailSentTrackKind.FOLLOWING_REPLIES))
]


Expand Down

0 comments on commit 66e4275

Please sign in to comment.