@@ -3,7 +3,6 @@ defmodule CodeCorps.Messages.Emails do
33 Handles email notifications used within the Messages context
44 """
55 alias CodeCorps . {
6- Conversation ,
76 ConversationPart ,
87 Emails ,
98 Mailer ,
@@ -14,23 +13,23 @@ defmodule CodeCorps.Messages.Emails do
1413 @ message_preloads [ :project , [ conversations: :user ] ]
1514
1615 @ doc ~S"""
17- Notifies all relevant users of a new `CodeCorps.Message` being created .
16+ Notifies all the recipients of a new `CodeCorps.Message`.
1817
19- We match against admin initiated messages only, because it's unclear who
20- message.author is for user-initiated messages, or who the email recipient
21- would be, assuming the message.author is the user who initiated the message.
18+ Target recipients are found in the `user` relationship of each
19+ `CodeCorps.Conversation`.
2220 """
23- @ spec notify_of_new_message ( Message . t ) :: :ok
24- def notify_of_new_message ( % Message { initiated_by: "admin" } = message ) do
21+ @ spec notify_message_targets ( Message . t ) :: :ok
22+ def notify_message_targets ( % Message { initiated_by: "admin" } = message ) do
2523 message = message |> Repo . preload ( @ message_preloads )
2624
2725 message
2826 |> Map . get ( :conversations )
29- |> Enum . map ( & Emails.MessageForUserEmail . create ( message , & 1 ) )
27+ |> Enum . map ( & Emails.MessageInitiatedByProjectEmail . create ( message , & 1 ) )
3028 |> Enum . each ( & Mailer . deliver_now / 1 )
3129 end
3230
33- @ part_preloads [
31+ @ cpart_preloads [
32+ :author ,
3433 conversation: [
3534 [ conversation_parts: :author ] ,
3635 [ message: [ :author , [ project: :organization ] ] ] ,
@@ -39,55 +38,36 @@ defmodule CodeCorps.Messages.Emails do
3938 ]
4039
4140 @ doc ~S"""
42- Notifies via email all relevant users when a new `CodeCorps.ConvrsationPart`
43- has been added to a `CodeCorps.Conversation`.
41+ Notifies users via email when a `CodeCorps.ConversationPart` has been added
42+ to a `CodeCorps.Conversation`.
43+
44+ Sends to users participating in the conversation, excluding the author of the
45+ conversation part.
4446 """
45- @ spec notify_of_new_reply ( ConvrsationPart . t ) :: :ok
47+ @ spec notify_of_new_reply ( ConversationPart . t ) :: :ok
4648 def notify_of_new_reply ( % ConversationPart { } = part ) do
47- part = part |> Repo . preload ( @ part_preloads )
48-
49- part |> maybe_send_message_for_user_email ( )
50- part |> send_message_for_project_emails ( )
49+ part = part |> Repo . preload ( @ cpart_preloads )
50+ part |> send_reply_to_conversation_emails ( )
5151 end
5252
5353 # we match against admin initiated only, because it's unclear who
5454 # message.author is for user-initiated messages, or who the email recipient
5555 # would be, assuming the message.author is the user who initiated the message
56- @ spec send_message_for_project_emails ( ConversationPart . t ) :: :ok
57- defp send_message_for_project_emails ( % ConversationPart {
58- conversation: % Conversation {
59- message: % Message { initiated_by: "admin" } = message
60- } = conversation
61- } = part ) do
62-
56+ @ spec send_reply_to_conversation_emails ( ConversationPart . t ) :: :ok
57+ defp send_reply_to_conversation_emails ( % ConversationPart { } = part ) do
6358 part
64- |> get_project_participants ( )
65- |> Enum . map ( & Emails.MessageForProjectEmail . create ( message , conversation , & 1 ) )
59+ |> get_conversation_participants ( )
60+ |> Enum . map ( & Emails.ReplyToConversationEmail . create ( part , & 1 ) )
6661 |> Enum . each ( & Mailer . deliver_now / 1 )
6762 end
6863
69- # we match against admin initiated only, because it's unclear who
70- # message.author is for user-initiated messages, or who the email recipient
71- # would be, assuming the message.author is the user who initiated the message
72- @ spec maybe_send_message_for_user_email ( ConversationPart . t ) :: Bambo.Email . t | nil
73- defp maybe_send_message_for_user_email ( % ConversationPart {
74- author_id: replier_id ,
75- conversation: % Conversation {
76- user_id: conversation_target_id ,
77- message: % Message { initiated_by: "admin" } = message
78- } = conversation
79- } ) when replier_id != conversation_target_id do
80-
81- message
82- |> Emails.MessageForUserEmail . create ( conversation )
83- |> Mailer . deliver_now
84- end
85- defp maybe_send_message_for_user_email ( % ConversationPart { } ) , do: nil
86-
87- @ spec get_project_participants ( ConversationPart . t ) :: list ( User . t )
88- defp get_project_participants ( % ConversationPart { author_id: author_id } = part ) do
89- part . conversation . conversation_parts |> Enum . map ( & Map . get ( & 1 , :author ) )
64+ @ spec get_conversation_participants ( ConversationPart . t ) :: list ( User . t )
65+ defp get_conversation_participants ( % ConversationPart { author_id: author_id } = part ) do
66+ part . conversation . conversation_parts
67+ |> Enum . map ( & Map . get ( & 1 , :author ) )
68+ |> Enum . concat ( [ part . conversation . user ] )
9069 |> Enum . concat ( [ part . conversation . message . author ] )
9170 |> Enum . reject ( fn u -> u . id == author_id end )
71+ |> Enum . uniq ( )
9272 end
9373end
0 commit comments