Skip to content

Commit

Permalink
Add Title, Description, Url, User, Athlete and Date field options.
Browse files Browse the repository at this point in the history
migration:

Team.where(:activity_fields.nin => ['Default', 'All']).each do |team|
    puts "#{team.team_id} (#{team.activity_fields})"
    team.activity_fields.concat(['Title', 'Description', 'Url', 'User', 'Athlete', 'Date'])
    puts " => (#{team.activity_fields})"
    team.save!
end; nil
  • Loading branch information
dblock committed Jan 7, 2024
1 parent c6eac29 commit 67827e9
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 30 deletions.
12 changes: 10 additions & 2 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 2022-07-16 22:35:08 UTC using RuboCop version 1.31.2.
# on 2024-01-07 14:34:02 UTC using RuboCop version 1.31.2.
# 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 @@ -153,6 +153,14 @@ Style/OptionalBooleanParameter:
Exclude:
- 'slack-strava/models/team.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength.
# AllowedMethods: present?, blank?, presence, try, try!
Style/SafeNavigation:
Exclude:
- 'slack-strava/models/user_activity.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/SlicingWithRange:
Expand All @@ -169,7 +177,7 @@ Style/StringConcatenation:
- 'slack-strava/models/team.rb'
- 'tasks/db.rake'

# Offense count: 131
# Offense count: 139
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
# URISchemes: http, https
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Changelog

* 2024/01/07: Add Title, Description, Url, User, Athlete and Date field options - [@dblock](https://github.com/dblock).
* 2023/01/21: Upgrade to Ruby 2.7.7 - [@dblock](https://github.com/dblock).
* 2022/07/16: Fix [#112](https://github.com/dblock/slack-strava/issues/112), handle archived channels - [@dblock](https://github.com/dblock).
* 2022/07/16: [#137](https://github.com/dblock/slack-strava/issues/137), add `set units imperial` and `set units metric` - [@dblock](https://github.com/dblock).
Expand Down
17 changes: 16 additions & 1 deletion slack-strava/models/activity_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ class ActivityFields
define :CALORIES, 'Calories'
define :WEATHER, 'Weather'

DEFAULT_VALUES = ['Type', 'Distance', 'Time', 'Moving Time', 'Elapsed Time', 'Pace', 'Speed', 'Elevation', 'Weather'].freeze
define :TITLE, 'Title'
define :DESCRIPTION, 'Description'
define :URL, 'Url'
define :USER, 'User'
define :ATHLETE, 'Athlete'
define :DATE, 'Date'

HEADER_VALUES = %w[
Title Description Url User Athlete Date
].freeze

DEFAULT_VALUES = [
'Title', 'Description', 'Url', 'User', 'Athlete', 'Date',
'Type', 'Distance', 'Time', 'Moving Time', 'Elapsed Time', 'Pace', 'Speed', 'Elevation', 'Weather'
].freeze

def self.parse_s(values)
return unless values
Expand All @@ -29,6 +43,7 @@ def self.parse_s(values)
values.scan(/[\w\s']+/).map do |v|
v = v.strip
title = v.titleize
title = ActivityFields::PR_COUNT if title == 'Pr Count' # HACK: titleize
if value?(title)
fields << title
else
Expand Down
15 changes: 15 additions & 0 deletions slack-strava/models/activity_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ def time_in_hours_s(time)
].compact.join
end

def display_field?(name)
activity_fields = team.activity_fields

case activity_fields
when ['All']
true
when ['Default']
ActivityFields::DEFAULT_VALUES.include?(name)
when ['None']
ActivityFields::HEADER_VALUES.include?(name)
else
activity_fields.include?(name)
end
end

def slack_fields
activity_fields = team.activity_fields

Expand Down
44 changes: 37 additions & 7 deletions slack-strava/models/user_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,50 @@ def self.create_from_strava!(user, response)

def to_slack_attachment
result = {}
fallback_fields = [distance_s, moving_time_in_hours_s, pace_s].compact.join(' ')
result[:fallback] = ["#{name} via #{user.slack_mention}", fallback_fields].compact.join(', ')
result[:title] = name
result[:title_link] = strava_url
result[:text] = ["<@#{user.user_name}> on #{start_date_local_s}", description].compact.join("\n\n")

if display_field?(ActivityFields::TITLE) && display_field?(ActivityFields::URL)
result[:title] = name || strava_id
result[:title_link] = strava_url
elsif display_field?(ActivityFields::TITLE)
result[:title] = name || strava_id
elsif display_field?(ActivityFields::URL)
result[:title] = strava_id
result[:title_link] = strava_url
end

result_fallback = [
display_field?(ActivityFields::TITLE) ? name : nil,
display_field?(ActivityFields::USER) ? "via #{user.slack_mention}" : nil,
display_field?(ActivityFields::DISTANCE) ? distance_s : nil,
display_field?(ActivityFields::MOVING_TIME) ? moving_time_in_hours_s : nil,
display_field?(ActivityFields::PACE) ? pace_s : nil
].compact.join(' ')

result[:fallback] = result_fallback.blank? ? strava_id : result_fallback

result_text = [
if display_field?(ActivityFields::USER) || display_field?(ActivityFields::DATE)
[
display_field?(ActivityFields::USER) ? "<@#{user.user_name}>" : nil,
display_field?(ActivityFields::DATE) ? start_date_local_s : nil
].compact.join(' on ')
end,
display_field?(ActivityFields::DESCRIPTION) ? description : nil
].compact.join("\n\n")

result[:text] = result_text unless result_text.blank?

if map
if team.maps == 'full'
result[:image_url] = map.proxy_image_url
elsif team.maps == 'thumb'
result[:thumb_url] = map.proxy_image_url
end
end
result[:fields] = slack_fields
result.merge!(user.athlete.to_slack) if user.athlete

result_fields = slack_fields
result[:fields] = result_fields if result_fields && result_fields.any?
result.merge!(user.athlete.to_slack) if user.athlete && display_field?(ActivityFields::ATHLETE)
result
end

Expand Down
1 change: 1 addition & 0 deletions spec/fabricators/user_activity_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
user { Fabricate.build(:user) }
type 'Run'
name { Faker::Internet.user_name }
description 'Great run!'
start_date { DateTime.parse('2018-02-20T18:02:13Z') }
start_date_local { DateTime.parse('2018-02-20T10:02:13Z') }
distance 22_539.6
Expand Down
4 changes: 2 additions & 2 deletions spec/models/activity_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
expect(ActivityFields.parse_s('elapsed time, Elapsed Time, Time')).to eq([ActivityFields::ELAPSED_TIME, ActivityFields::TIME])
end
it 'raise an error on an invalid value' do
expect { ActivityFields.parse_s('invalid, elapsed time') }.to raise_error SlackStrava::Error, 'Invalid field: invalid, possible values are Default, All, None, Type, Distance, Time, Moving Time, Elapsed Time, Pace, Speed, Elevation, Max Speed, Heart Rate, Max Heart Rate, PR Count, Calories and Weather.'
expect { ActivityFields.parse_s('invalid, elapsed time') }.to raise_error SlackStrava::Error, 'Invalid field: invalid, possible values are Default, All, None, Type, Distance, Time, Moving Time, Elapsed Time, Pace, Speed, Elevation, Max Speed, Heart Rate, Max Heart Rate, PR Count, Calories, Weather, Title, Description, Url, User, Athlete and Date.'
end
it 'raise an error on invalid fields' do
expect { ActivityFields.parse_s('invalid, elapsed time, whatever') }.to raise_error SlackStrava::Error, 'Invalid fields: invalid and whatever, possible values are Default, All, None, Type, Distance, Time, Moving Time, Elapsed Time, Pace, Speed, Elevation, Max Speed, Heart Rate, Max Heart Rate, PR Count, Calories and Weather.'
expect { ActivityFields.parse_s('invalid, elapsed time, whatever') }.to raise_error SlackStrava::Error, 'Invalid fields: invalid and whatever, possible values are Default, All, None, Type, Distance, Time, Moving Time, Elapsed Time, Pace, Speed, Elevation, Max Speed, Heart Rate, Max Heart Rate, PR Count, Calories, Weather, Title, Description, Url, User, Athlete and Date.'
end
end
end
Loading

0 comments on commit 67827e9

Please sign in to comment.