Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 additions & 2 deletions app/controllers/admin/sponsors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ def audit_contact_subscription(contact)
return unless contact.previous_changes.key?(:mailing_list_consent)

contact_audit_key = contact.mailing_list_consent ? 'subscribe' : 'unsubscribe'
Auditor::Audit.new(@sponsor, "sponsor.admin_contact_#{contact_audit_key}", current_user, contact)
.log_with_note(contact.email)

MemberActivityRecorder.record(
actor: current_user,
key: "sponsor.admin_contact_#{contact_audit_key}",
trackable: @sponsor,
recipient: contact,
contact_email: contact.email
)
end

def filter_search_params
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/contact_preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def subscription_preference(contact)
end

def audit_contact_subscription(contact)
Auditor::Audit.new(contact.sponsor, subscription_preference(contact), contact)
.log_with_note(contact.email)
MemberActivityRecorder.record(
actor: contact,
key: subscription_preference(contact),
trackable: contact.sponsor,
contact_email: contact.email
)
end
end
9 changes: 7 additions & 2 deletions app/services/member_activity_recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ class MemberActivityRecorder
workshop.created
meeting_invitation.created
meeting_invitation.updated
sponsor.admin_contact_subscribe
sponsor.admin_contact_unsubscribe
sponsor.contact_subscribe
sponsor.contact_unsubscribe
].freeze

# Start of systematic member-activity collection; earlier rows are incidental and
# incomplete. Drives the marker line on Admin::Members::ActivityStrip. Update if
# historical activity is ever backfilled.
TRACKING_STARTED_ON = Date.new(2026, 9, 8).freeze

def self.record(actor:, key:, trackable: nil, recipient: nil)
def self.record(actor:, key:, trackable: nil, recipient: nil, contact_email: nil)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is now 11 lines against Metrics/MethodLength Max 10, so the lint job fails. Pulling the hash into a local keeps the create! call on one line:

params = contact_email ? { note: contact_email } : {}
PublicActivity::Activity.create!(owner: actor, key:, trackable:, recipient:, parameters: params)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: the parameters hash is assigned locally so record stays within the method-length limit. RuboCop passes.

Rails.logger.warn("MemberActivityRecorder: unregistered key '#{key}'") unless KEYS.include?(key)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both migrated call sites pass keys that KEYS does not list: sponsor.admin_contact_subscribe, sponsor.admin_contact_unsubscribe, sponsor.contact_subscribe and sponsor.contact_unsubscribe. Every consent toggle writes unregistered key here. That warning is the one signal for a genuinely unregistered key.

Adding the four keys follows the comment on KEYS: "extend when adding sites".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: added all four sponsor contact keys to MemberActivityRecorder::KEYS, so consent toggles no longer emit unregistered-key warnings.

trackable ||= actor
PublicActivity::Activity.create!(owner: actor, key:, trackable:, recipient:)
params = contact_email ? { note: contact_email } : {}
PublicActivity::Activity.create!(owner: actor, key:, trackable:, recipient:, parameters: params)
rescue StandardError => e
Rails.logger.warn("MemberActivityRecorder failed (#{key}): #{e.class}: #{e.message}")
end
Expand Down
70 changes: 70 additions & 0 deletions spec/controllers/contact_preferences_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'rails_helper'

RSpec.describe ContactPreferencesController do
let(:contact) { Fabricate(:contact, mailing_list_consent: false) }

before do
allow(ContactMailingListService).to receive(:sync)
end

describe 'GET #show' do
it 'sets the contact for a valid token' do
get :show, params: { token: contact.token }

expect(response).to have_http_status(:success)
expect(controller.instance_variable_get(:@contact)).to eq(contact)
expect(flash[:notice]).to be_nil
end

it 'shows an invalid-session notice for an unknown token' do
get :show, params: { token: 'unknown-token' }

expect(response).to have_http_status(:success)
expect(flash[:notice]).to eq(I18n.t('messages.invalid_session'))
end
end

describe 'PATCH #update' do
it 'subscribes the contact and records the consent activity' do
patch :update, params: {
contact: { token: contact.token, mailing_list_consent: '1' }
}

expect(contact.reload.mailing_list_consent).to be(true)
expect(ContactMailingListService).to have_received(:sync).with(contact)
expect(response).to redirect_to(
contact_preferences_path(token: contact.token, notice: I18n.t('messages.contact_preferences.updated'))
)

activity = PublicActivity::Activity.find_by!(key: 'sponsor.contact_subscribe')
expect(activity.owner).to eq(contact)
expect(activity.trackable).to eq(contact.sponsor)
expect(activity.recipient).to be_nil
expect(activity.parameters).to include(note: contact.email)
end

it 'unsubscribes the contact and records the consent activity' do
contact.update!(mailing_list_consent: true)

patch :update, params: {
contact: { token: contact.token, mailing_list_consent: '0' }
}

expect(contact.reload.mailing_list_consent).to be(false)
expect(ContactMailingListService).to have_received(:sync).with(contact)

activity = PublicActivity::Activity.find_by!(key: 'sponsor.contact_unsubscribe')
expect(activity.owner).to eq(contact)
expect(activity.trackable).to eq(contact.sponsor)
expect(activity.parameters).to include(note: contact.email)
end

it 'returns not found when the submitted token is unknown' do
patch :update, params: {
contact: { token: 'unknown-token', mailing_list_consent: '1' }
}

expect(response).to have_http_status(:not_found)
end
end
end
20 changes: 16 additions & 4 deletions spec/features/admin/sponsor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@
context 'with activities' do
scenario 'when there are activities' do
contact = sponsor.contacts.first
audit = Auditor::Audit.new(sponsor, 'sponsor.admin_contact_subscribe', manager, contact)
audit.log_with_note(contact.email)

MemberActivityRecorder.record(
actor: manager,
key: 'sponsor.admin_contact_subscribe',
trackable: sponsor,
recipient: contact,
contact_email: contact.email
)

visit admin_sponsor_path(sponsor)

Expand All @@ -173,8 +179,14 @@

scenario 'when an activity is associated with a deleted contact' do
contact = sponsor.contacts.first
audit = Auditor::Audit.new(sponsor, 'sponsor.admin_contact_subscribe', manager, contact)
audit.log_with_note(contact.email)

MemberActivityRecorder.record(
actor: manager,
key: 'sponsor.admin_contact_subscribe',
trackable: sponsor,
recipient: contact,
contact_email: contact.email
)

contact.delete

Expand Down
14 changes: 14 additions & 0 deletions spec/services/member_activity_recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@
expect(activity.trackable).to eq(member)
end

it 'stores the contact email in activity parameters' do
email = 'organiser@example.com'

activity = described_class.record(actor: member, key: 'sponsor.contact_subscribe', contact_email: email)

expect(activity.parameters).to eq(note: email)
end

it 'stores empty parameters when no contact email is provided' do
activity = described_class.record(actor: member, key: 'member.login')

expect(activity.parameters).to eq({})
end

it 'never raises on persistence failure' do
allow(PublicActivity::Activity).to receive(:create!)
.and_raise(ActiveRecord::RecordInvalid)
Expand Down
Loading