Skip to content

Commit

Permalink
Fix: do not unfurl links for users not connected to Strava.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Nov 24, 2024
1 parent 3d9e48b commit 9033c80
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 52 deletions.
13 changes: 5 additions & 8 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-11-24 16:52:43 UTC using RuboCop version 1.68.0.
# on 2024-11-24 17:13:54 UTC using RuboCop version 1.68.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -78,7 +78,7 @@ Naming/VariableNumber:
- 'spec/models/team_leaderboard_spec.rb'
- 'spec/models/user_spec.rb'

# Offense count: 138
# Offense count: 140
RSpec/AnyInstance:
Enabled: false

Expand Down Expand Up @@ -155,12 +155,9 @@ RSpec/InstanceVariable:
RSpec/LetSetup:
Enabled: false

# Offense count: 4
# Offense count: 1
RSpec/MessageChain:
Exclude:
- 'spec/api/endpoints/slack_endpoint_spec.rb'
- 'spec/api/endpoints/status_endpoint_spec.rb'
- 'spec/models/activity_spec.rb'
- 'spec/models/user_activity_spec.rb'

# Offense count: 95
Expand All @@ -169,7 +166,7 @@ RSpec/MessageChain:
RSpec/MessageSpies:
EnforcedStyle: receive

# Offense count: 203
# Offense count: 204
RSpec/MultipleExpectations:
Max: 11

Expand All @@ -187,7 +184,7 @@ RSpec/NamedSubject:
- 'spec/api/endpoints/teams_endpoint_spec.rb'
- 'spec/slack-strava/app_spec.rb'

# Offense count: 104
# Offense count: 106
# Configuration parameters: AllowedGroups.
RSpec/NestedGroups:
Max: 7
Expand Down
14 changes: 11 additions & 3 deletions slack-strava/api/endpoints/requests/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ def unfurl!
m = link.url.match(%r{strava\.com/activities/(?<strava_id>\d+)\b})
next unless m && m[:strava_id]

unless user.connected_to_strava?
logger.info "UNFURL: #{link.url}, #{user}, not connected to Strava"
next
end

activity = user.sync_strava_activity!(m[:strava_id])
next unless activity

logger.info "UNFURL: #{link.url}, #{activity}"
if activity.nil?
logger.info "UNFURL: #{link.url}, #{user}, missing activity"
next
end

unfurls = { link.url => { blocks: activity.to_slack_blocks } }
logger.debug(unfurls)
logger.info "UNFURL: #{link.url}, #{user}, #{activity}"
logger.debug unfurls

team.activated_user_slack_client.chat_unfurl(
channel: event.channel,
Expand Down
85 changes: 52 additions & 33 deletions spec/api/endpoints/slack_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,44 +458,63 @@
context 'with an activity' do
let(:activity) { Fabricate(:user_activity, user: user) }

before do
allow(HTTParty).to receive_message_chain(:get, :body).and_return('PNG')
let(:payload) do
{
token: token,
team_id: team.team_id,
api_app_id: 'A19GAJ72T',
event: {
type: 'link_shared',
user: user.user_id,
channel: 'C1',
message_ts: '1547842100.001400',
links: [{
url: activity.strava_url,
domain: 'strava.com'
}]
},
type: 'event_callback',
event_id: 'EvFGTNRKLG',
event_time: 1_547_842_101,
authed_users: ['U04KB5WQR']
}
end

it 'unfurls a strava URL' do
expect_any_instance_of(User).to receive(:sync_strava_activity!)
.with(activity.strava_id)
.and_return(activity)
context 'with a user connected to Strava' do
before do
user.update_attributes!(access_token: 'token', connected_to_strava_at: Time.now)
end

expect_any_instance_of(Slack::Web::Client).to receive(:chat_unfurl).with(
channel: 'C1',
ts: '1547842100.001400',
unfurls: {
activity.strava_url => { 'blocks' => activity.to_slack_blocks }
}.to_json
)
it 'unfurls a strava URL' do
expect_any_instance_of(User).to receive(:sync_strava_activity!)
.with(activity.strava_id)
.and_return(activity)

expect_any_instance_of(Slack::Web::Client).to receive(:chat_unfurl).with(
channel: 'C1',
ts: '1547842100.001400',
unfurls: {
activity.strava_url => { 'blocks' => activity.to_slack_blocks }
}.to_json
)

post '/api/slack/event',
token: token,
team_id: team.team_id,
api_app_id: 'A19GAJ72T',
event: {
type: 'link_shared',
user: user.user_id,
channel: 'C1',
message_ts: '1547842100.001400',
links: [{
url: activity.strava_url,
domain: 'strava.com'
}]
},
type: 'event_callback',
event_id: 'EvFGTNRKLG',
event_time: 1_547_842_101,
authed_users: ['U04KB5WQR']
expect(last_response.status).to eq 201
post '/api/slack/event', payload
expect(last_response.status).to eq 201
expect(activity.reload.bragged_at).not_to be_nil
end
end

context 'with a user that has not connected to Strava' do
before do
user.update_attributes!(access_token: nil, connected_to_strava_at: nil)
end

expect(activity.reload.bragged_at).not_to be_nil
it 'does not unfurl' do
expect_any_instance_of(User).not_to receive(:sync_strava_activity!)
expect_any_instance_of(Slack::Web::Client).not_to receive(:chat_unfurl)
post '/api/slack/event', payload
expect(last_response.status).to eq 201
end
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions spec/api/endpoints/status_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
let!(:user) { Fabricate(:user, team: team) }
let!(:connected_user) { Fabricate(:user, team: team, access_token: 'xyz') }

before do
allow(HTTParty).to receive_message_chain(:get, :body).and_return('PNG')
end

it 'returns a status with distance and users' do
status = client.status
expect(status.connected_users_count).to eq 1
Expand Down
4 changes: 0 additions & 4 deletions spec/models/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@
end

context 'access changes' do
before do
allow(HTTParty).to receive_message_chain(:get, :body).and_return('PNG')
end

describe 'privacy changes' do
context 'a private, bragged activity that was not posted to any channels' do
let!(:activity) { Fabricate(:user_activity, private: true, bragged_at: Time.now.utc) }
Expand Down

0 comments on commit 9033c80

Please sign in to comment.