Skip to content

Commit

Permalink
Migrates to using an enum to indicate what type of status you want, e…
Browse files Browse the repository at this point in the history
….g. emoji or not. To allow extra status update types
  • Loading branch information
orta committed Jan 16, 2019
1 parent e007489 commit 6cb1dda
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ruby.useLanguageServer": true,
"ruby.intellisense": "rubyLocate",
"ruby.codeCompletion": "rcodetools"
}
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'http://rubygems.org'

ruby '2.5.3'
ruby '~> 2.5'

gem 'emoji_data'
gem 'faker'
Expand All @@ -10,6 +10,7 @@ gem 'hashie'
gem 'httparty'
gem 'mongoid'
gem 'mongoid-scroll'
gem "mongoid-enum"
gem 'newrelic_rpm'
gem 'nokogiri'
gem 'polylines'
Expand Down
25 changes: 16 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (5.2.1)
activesupport (= 5.2.1)
activesupport (5.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
activemodel (4.2.11)
activesupport (= 4.2.11)
builder (~> 3.1)
activesupport (4.2.11)
i18n (~> 0.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
Expand Down Expand Up @@ -37,7 +38,7 @@ GEM
ffi (~> 1.0, >= 1.0.11)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.0.5)
concurrent-ruby (1.1.4)
crack (0.4.3)
safe_yaml (~> 1.0.0)
dante (0.2.0)
Expand Down Expand Up @@ -104,7 +105,7 @@ GEM
faraday_middleware
net-http-digest_auth
uri_template
i18n (1.1.0)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
jaro_winkler (1.5.2)
Expand All @@ -123,12 +124,16 @@ GEM
minitest (5.11.3)
mongo (2.6.2)
bson (>= 4.3.0, < 5.0.0)
mongoid (7.0.2)
activemodel (>= 5.1, < 6.0.0)
mongoid (5.4.0)
activemodel (~> 4.0)
mongo (>= 2.5.1, < 3.0.0)
origin (~> 2.3)
tzinfo (>= 0.3.37)
mongoid-compatibility (0.5.1)
activesupport
mongoid (>= 2.0)
mongoid-enum (0.4.0)
mongoid (~> 5.0)
mongoid-scroll (0.3.6)
i18n
mongoid (>= 3.0)
Expand All @@ -150,6 +155,7 @@ GEM
nokogiri (1.10.0)
mini_portile2 (~> 2.4.0)
oj (3.6.11)
origin (2.3.1)
parallel (1.12.1)
parser (2.5.3.0)
ast (~> 2.4.0)
Expand Down Expand Up @@ -287,6 +293,7 @@ DEPENDENCIES
httparty
hyperclient
mongoid
mongoid-enum
mongoid-scroll
mongoid-shell
newrelic_rpm
Expand Down
4 changes: 2 additions & 2 deletions slack-moji/api/endpoints/slack_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class SlackEndpoint < Grape::API

case callback_id
when 'emoji-count'
emoji_count = payload['actions'].first['value'].to_i
user.update_attributes!(emoji_count: emoji_count, emoji: emoji_count > 0)
new_status = payload['actions'].first['value']
user.update_attributes!(status: new_status)
user.emoji!
user.to_slack_emoji_question("Got it, #{user.emoji_text.downcase}.")
else
Expand Down
41 changes: 23 additions & 18 deletions slack-moji/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Enum

field :user_id, type: String
field :user_name, type: String
field :access_token, type: String

field :emoji, type: Boolean, default: false
field :emoji_count, type: Integer, default: 0
field :is_bot, type: Boolean, default: false
enum :status, %i[unsubscribed emoji] # , :github]

belongs_to :team, index: true
validates_presence_of :team

index({ user_id: 1, team_id: 1 }, unique: true)
index(user_name: 1, team_id: 1)

scope :with_emoji, -> { where(emoji: true, :access_token.ne => nil) }
scope :with_emoji, -> { where(status_type: 'emoji', :access_token.ne => nil) }

def slack_mention
"<@#{user_id}>"
Expand Down Expand Up @@ -67,14 +67,19 @@ def to_s
"user_id=#{user_id}, user_name=#{user_name}"
end

def emoji_count?
!emoji_count.nil? || emoji_count == 0
def not_updating_status?
status == :unsubscribed
end

def using_emoji_status?
status == :emoji
end

def emoji_text
case emoji_count
when 0 then 'No Emoji'
else "#{emoji_count} Emoji"
if using_emoji_status?
'No Emoji'
else
'Emoji'
end
end

Expand All @@ -100,7 +105,7 @@ def to_slack_auth_request
}
end

def to_slack_emoji_question(text = 'How much emoji would you like?')
def to_slack_emoji_question(text = 'What type of updates would you like?')
{
text: text,
attachments: [
Expand All @@ -110,18 +115,18 @@ def to_slack_emoji_question(text = 'How much emoji would you like?')
callback_id: 'emoji-count',
actions: [
{
name: 'emoji-count',
name: 'status',
text: 'No Emoji',
type: 'button',
value: 0,
style: emoji_count == 0 ? 'primary' : 'default'
value: 'unsubscribed',
style: not_updating_status? ? 'primary' : 'default'
},
{
name: 'emoji-count',
name: 'status',
text: 'Yes Emoji',
type: 'button',
type: 'emoji',
value: 1,
style: emoji_count == 1 ? 'primary' : 'default'
style: using_emoji_status? ? 'primary' : 'default'
}
]
}
Expand All @@ -137,7 +142,7 @@ def authorize!(code)
redirect_uri: moji_authorize_uri
)

update_attributes!(access_token: rc['access_token'], emoji_count: 1, emoji: true)
update_attributes!(access_token: rc['access_token'], status: :emoji)

dm!(text: "May the moji be with you!\nTo configure try `/moji me`.")

Expand All @@ -149,14 +154,14 @@ def slack_client
end

def emoji!
if emoji_count && emoji_count > 0
if using_emoji_status?
emoji = EmojiData.all[rand(EmojiData.all.count)]
logger.info "Emoji :#{emoji.short_name}: #{self}."
slack_client.users_profile_set(profile: {
status_text: Faker::GreekPhilosophers.quote,
status_emoji: ":#{emoji.short_name}:"
}.to_json)
elsif emoji_count == 0
elsif not_updating_status?
logger.info "Removing emoji #{self}."
slack_client.users_profile_set(profile: {
status_text: nil,
Expand Down
4 changes: 2 additions & 2 deletions spec/api/endpoints/slack_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
callback_id: 'emoji-count'
}.to_json
expect(last_response.status).to eq 201
expect(user.reload.emoji_count).to eq 0
expect(user.reload.using_emoji_status?).to eq false
expect(last_response.body).to eq(
user.to_slack_emoji_question('Got it, no emoji.').to_json
)
Expand All @@ -96,7 +96,7 @@
callback_id: 'emoji-count'
}.to_json
expect(last_response.status).to eq 201
expect(user.reload.emoji_count).to eq 1
expect(user.reload.using_emoji_status?).to eq true
expect(last_response.body).to eq(
user.to_slack_emoji_question('Got it, 1 emoji.').to_json
)
Expand Down
11 changes: 6 additions & 5 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
end
context '#emoji!' do
let!(:user) { Fabricate(:user) }
context 'emoji_count is 1' do
context 'using_emoji_status? is true' do
before do
user.update_attributes!(emoji_count: 1)
user.update_attributes!(status: :emoji)
end
it 'sets user emoji' do
expect(user.slack_client).to receive(:users_profile_set) do |arg|
Expand All @@ -84,14 +84,15 @@
user.emoji!
end
end
context 'emoji_count is 0' do
context 'using_emoji_status? is false' do
before do
user.update_attributes!(emoji_count: 0)
user.update_attributes!(status: :unsubscribed)
end
it 'unsets user emoji' do
expect(user.slack_client).to receive(:users_profile_set).with(
profile: {
status_text: nil, status_emoji: nil
status_text: nil,
status_emoji: nil
}.to_json
)
user.emoji!
Expand Down

0 comments on commit 6cb1dda

Please sign in to comment.