Skip to content

Commit 7fcdec6

Browse files
committed
WIP: Add toggle switch to allow peopel to opt-in to receive messages from members
1 parent 45ef494 commit 7fcdec6

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

app/models/better_together/person.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def self.primary_community_delegation_attrs
6464
store_attributes :preferences do
6565
locale String, default: I18n.default_locale.to_s
6666
time_zone String, default: ENV.fetch('APP_TIME_ZONE', 'Newfoundland')
67+
receive_messages_from_members Boolean, default: false
6768
end
6869

6970
store_attributes :notification_preferences do

app/policies/better_together/conversation_policy.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ def show?
1515
user.present? && record.participants.include?(agent)
1616
end
1717

18-
class Scope < ApplicationPolicy::Scope
18+
class Scope < ApplicationPolicy::Scope
19+
end
20+
21+
# Returns the people that the agent is permitted to message
22+
def permitted_participants
23+
if permitted_to?('manage_platform')
24+
BetterTogether::Person.all
25+
else
26+
role = BetterTogether::Role.find_by(identifier: 'platform_manager')
27+
manager_ids = BetterTogether::PersonPlatformMembership.where(role_id: role.id).pluck(:member_id)
28+
BetterTogether::Person.where(id: manager_ids)
29+
.or(BetterTogether::Person.where("preferences @> ?", { receive_messages_from_members: true }.to_json))
30+
.distinct
1931
end
2032
end
33+
end
2134
end

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@
126126
<div class="mb-3">
127127
<p><%= t('better_together.people.notification_preferences') %></p>
128128
<%= 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>
129+
<small class="form-text text-muted"><%= t('helpers.hint.person.notify_by_email') %></small>
130+
</div>
131+
<div class="mb-3">
132+
<p><%= t('better_together.people.allow_messages_from_members') %></p>
133+
<%= render partial: 'better_together/shared/fields/toggle_switch', locals: {form:, attr: :receive_messages_from_members} %>
134+
<small class="form-text text-muted"><%= t('helpers.hint.person.allow_messages_from_members') %></small>
135+
</div>
131136
</div>
132137

133138
<!-- Device Permissions Tab -->

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
3737
<% end %>
3838
<% end %>
39-
<% if current_person && current_person != @person %>
39+
<% conversation = BetterTogether::Conversation.new %>
40+
<% if policy(conversation).create? && policy(conversation).permitted_participants.include?(@person) %>
4041
<%= link_to new_conversation_path(conversation: { participant_ids: [@person.id] }), class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => t('globals.message') do %>
4142
<i class="fas fa-envelope"></i> <%= t('globals.message') %>
4243
<% end %>

config/locales/en.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,13 @@ en:
837837
submit:
838838
save: Save
839839
notification_preferences: Message notification preferences
840-
tabs:
841-
contact_details: Contact Details
842-
details: Details
843-
device-permissions: Device-permissions
844-
images: Images
845-
preferences: Preferences
840+
tabs:
841+
contact_details: Contact Details
842+
details: Details
843+
device-permissions: Device-permissions
844+
images: Images
845+
preferences: Preferences
846+
allow_messages_from_members: "Allow messages from platform members"
846847
phone_numbers:
847848
add: Add Phone Number
848849
label: Label
@@ -1320,6 +1321,7 @@ en:
13201321
locale: Select the preferred language for the person.
13211322
name: Enter the full name of the person.
13221323
notify_by_email: Send an email when a conversation has an unread message.
1324+
allow_messages_from_members: Opt-in to receive messages from people other than platform managers.
13231325
profile_image: Upload a profile image for the person.
13241326
slug: A URL-friendly identifier, typically auto-generated.
13251327
label:

0 commit comments

Comments
 (0)