Skip to content

Commit e5ae982

Browse files
authored
Merge branch 'feature/notification_preferences' into main
Signed-off-by: Robert Smith <[email protected]>
2 parents 0a2db14 + a901e83 commit e5ae982

File tree

12 files changed

+76
-30
lines changed

12 files changed

+76
-30
lines changed

app/controllers/better_together/messages_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def notify_participants(message)
3535
recipients = message.conversation.participants.where.not(id: message.sender_id)
3636

3737
# Pass the array of recipients to the notification
38-
BetterTogether::NewMessageNotifier.with(record: message).deliver_later(recipients)
38+
BetterTogether::NewMessageNotifier.with(record: message,
39+
conversation_id: message.conversation_id).deliver_later(recipients)
3940
end
4041

4142
def broadcast_to_recipients(message, recipients)

app/controllers/better_together/people_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def set_resource_instance
7575

7676
def person_params
7777
params.require(:person).permit(
78-
:name, :description, :profile_image, :slug, :locale,
78+
:name, :description, :profile_image, :slug, :locale, :notify_by_email,
7979
:profile_image, :cover_image, :remove_profile_image, :remove_cover_image,
8080
*resource_class.permitted_attributes
8181
)

app/models/better_together/person.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def self.primary_community_delegation_attrs
5858
time_zone String, default: ENV.fetch('APP_TIME_ZONE', 'Newfoundland')
5959
end
6060

61+
store_attributes :notification_preferences do
62+
notify_by_email Boolean, default: true
63+
end
64+
6165
validates :name,
6266
presence: true
6367

app/notifiers/better_together/new_message_notifier.rb

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,24 @@ def sender
3333
delegate :url, to: :event
3434

3535
def send_email_notification?
36-
recipient.email.present? && should_send_email?
36+
recipient.email.present? && recipient.notification_preferences['notify_by_email'] && should_send_email?
3737
end
3838

39-
# rubocop:todo Metrics/MethodLength
40-
def should_send_email? # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
41-
# Find the events related to the conversation
42-
related_event_ids = BetterTogether::NewMessageNotifier.where(params: { conversation_id: conversation.id })
43-
.pluck(:id)
39+
def should_send_email?
40+
# Check for unread notifications for the recipient for the record's conversation
41+
unread_notifications = recipient.notifications.where(
42+
event_id: BetterTogether::NewMessageNotifier.where(params: { conversation_id: conversation.id }).select(:id),
43+
read_at: nil
44+
).order(created_at: :desc)
4445

45-
# Check for unread notifications for the recipient related to these events
46-
unread_notifications = recipient.notifications
47-
.where(event_id: related_event_ids, read_at: nil)
48-
49-
if unread_notifications.empty? || (unread_notifications.last.created_at <= 1.day.ago)
50-
# No unread recent notifications, send the email
51-
true
46+
if unread_notifications.none?
47+
# If the recipient has read their notifications, do not send
48+
false
5249
else
53-
# Optional: Implement a time-based delay or other conditions
54-
last_email_sent_at = recipient.notifications
55-
.where(event_id: related_event_ids)
56-
.order(created_at: :desc)
57-
.pluck(:created_at)
58-
.first
59-
60-
return true if last_email_sent_at.blank? # Send if no previous email sent
61-
62-
# Send email only if more than 30 minutes have passed since the last one
63-
last_email_sent_at < 30.minutes.ago
50+
# Only send one email per unread notifications per conversation
51+
message.id == unread_notifications.last.event.record_id
6452
end
6553
end
66-
# rubocop:enable Metrics/MethodLength
6754
end
6855

6956
def identifier

app/views/better_together/conversation_mailer/new_message_notification.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<p><%= t('.view_conversation') %></p>
88

9-
<p><%= link_to conversation_url(@conversation), conversation_url(@conversation) %></p>
9+
<p><%= link_to t('.view_conversation_link'),
10+
conversation_url(@conversation, anchor: "message_#{@message.id}") %></p>
1011

1112
<p><%= t('.signature_html', platform: @platform.name) %></p>

app/views/better_together/people/_form.html.erb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,16 @@
118118
id="person-preferences"
119119
role="tabpanel"
120120
aria-labelledby="person-preferences-tab">
121-
<div class="mb-3">
121+
<div class="mb-3 border-bottom">
122122
<%= required_label(form, :locale, class: "form-label") %>
123123
<%= language_select_field(form:, selected_locale: person.locale) %>
124124
<small class="form-text text-muted"><%= t('helpers.hint.person.locale') %></small>
125125
</div>
126+
<div class="mb-3">
127+
<p><%= t('better_together.people.notification_preferences') %></p>
128+
<%= render partial: 'better_together/shared/fields/toggle_switch', locals: {form:, attr: :notify_by_email} %>
129+
<small class="form-text text-muted"><%= t('helpers.hint.person.notify_by_email') %></small>
130+
</div>
126131
</div>
127132

128133
<!-- Device Permissions Tab -->

config/locales/en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ en:
296296
member_communities: Member communities
297297
member_platforms: Member platforms
298298
name: Name
299+
notify_by_email: Receive email notifications
299300
notification_mentions: Notification mentions
300301
notifications: Notifications
301302
person_community_memberships: Person community memberships
@@ -638,6 +639,7 @@ en:
638639
subject_with_title: '[%{conversation}] conversation has an unread message'
639640
view_conversation: 'You can view and reply to this message by clicking the
640641
link below:'
642+
view_conversation_link: 'Go to conversation'
641643
conversations:
642644
communicator:
643645
active_conversations: Active Conversations
@@ -818,6 +820,7 @@ en:
818820
new_person: New person
819821
submit:
820822
save: Save
823+
notification_preferences: Message notification preferences
821824
tabs:
822825
contact_details: Contact Details
823826
details: Details
@@ -1299,6 +1302,7 @@ en:
12991302
description: Provide a brief description or biography.
13001303
locale: Select the preferred language for the person.
13011304
name: Enter the full name of the person.
1305+
notify_by_email: Send an email when a conversation has an unread message.
13021306
profile_image: Upload a profile image for the person.
13031307
slug: A URL-friendly identifier, typically auto-generated.
13041308
label:

config/locales/es.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ es:
296296
member_communities: Comunidades miembros
297297
member_platforms: Plataformas miembros
298298
name: Nombre
299+
notify_by_email: Recibir notificaciones por correo electrónico
299300
notification_mentions: Menciones de notificación
300301
notifications: Notificaciones
301302
person_community_memberships: Membresías de la comunidad de personas
@@ -641,6 +642,7 @@ es:
641642
subject_with_title: '[%{conversation}] tiene un mensaje sin leer'
642643
view_conversation: 'Puedes ver y responder a este mensaje haciendo clic en
643644
el enlace a continuación:'
645+
view_conversation_link: 'Ir a la conversación'
644646
conversations:
645647
communicator:
646648
active_conversations: Active Conversations
@@ -819,6 +821,7 @@ es:
819821
permissions_group_label: Permissions group label
820822
index:
821823
new_person: Nueva persona
824+
notification_preferences: Preferencias de notificación de mensajes
822825
submit:
823826
save: Guardar
824827
tabs:
@@ -1287,6 +1290,7 @@ es:
12871290
description: Proporcione una breve descripción o biografía.
12881291
locale: Selecciona el idioma preferido para la persona.
12891292
name: Ingrese el nombre completo de la persona.
1293+
notify_by_email: Envía un correo electrónico cuando una conversación tiene un mensage no leído.
12901294
profile_image: Sube una imagen de perfil para la persona.
12911295
slug: Un identificador compatible con URL, generalmente generado automáticamente.
12921296
label:

config/locales/fr.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ fr:
296296
member_communities: Communautés membres
297297
member_platforms: Plateformes membres
298298
name: Nom
299+
notify_by_email: Recevoir des notifications par e-mail
299300
notification_mentions: Mentions de notification
300301
notifications: Notifications
301302
person_community_memberships: Adhésions communautaires de personnes
@@ -643,6 +644,7 @@ fr:
643644
subject_with_title: '[%{conversation}] contient un message non lu'
644645
view_conversation: 'Vous pouvez voir et répondre à ce message en cliquant
645646
sur le lien ci-dessous :'
647+
view_conversation_link: 'Aller à la conversation'
646648
conversations:
647649
communicator:
648650
active_conversations: Active Conversations
@@ -822,6 +824,7 @@ fr:
822824
permissions_group_label: Permissions group label
823825
index:
824826
new_person: Nouvelle personne
827+
notification_preferences: Préférences de notification des messages
825828
submit:
826829
save: Enregistrer
827830
tabs:
@@ -1313,6 +1316,7 @@ fr:
13131316
description: Fournissez une brève description ou biographie.
13141317
locale: Sélectionnez la langue préférée pour la personne.
13151318
name: Entrez le nom complet de la personne.
1319+
notify_by_email: Envoyer un e-mail lorsqu'une conversation contient un message non lu.
13161320
profile_image: Téléchargez une image de profil pour la personne.
13171321
slug: Un identifiant compatible avec les URL, généralement généré automatiquement.
13181322
label:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
# adds a notifications preferences column to track and store message notification preferences
4+
class AddNotificationPreferencesToBetterTogetherPeople < ActiveRecord::Migration[7.1]
5+
def change
6+
add_column :better_together_people, :notification_preferences, :jsonb, null: false, default: {}
7+
end
8+
end

0 commit comments

Comments
 (0)