Skip to content

Commit

Permalink
Bother free teams to upgrade.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Aug 5, 2017
1 parent 967f194 commit 261443a
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 10 deletions.
13 changes: 13 additions & 0 deletions slack-gamebot/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def prepare!

def after_start!
nudge_sleeping_teams!
bother_free_teams!
end

private
Expand Down Expand Up @@ -41,6 +42,18 @@ def nudge_sleeping_teams!
end
end

def bother_free_teams!
Team.active.each do |team|
next if team.premium?
next unless team.bother?
begin
team.bother! "Enjoying this free bot? Don't be cheap! #{team.upgrade_text}"
rescue StandardError => e
logger.warn "Error bothering team #{team}, #{e.message}."
end
end
end

def update_unbalanced_teams!
Team.where(unbalanced: nil).update_all(unbalanced: false)
end
Expand Down
19 changes: 14 additions & 5 deletions slack-gamebot/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def premium_text
end

def upgrade_text
"Upgrade your team to premium for $29.99 a year at #{SlackGamebot::Service.url}/upgrade?team_id=#{team_id}&game=#{game.name}."
"Upgrade your team to premium and enable paid features for $29.99 a year at #{SlackGamebot::Service.url}/upgrade?team_id=#{team_id}&game=#{game.name}."
end

def update_cc_text
Expand Down Expand Up @@ -59,21 +59,30 @@ def asleep?(dt = 2.weeks)
true
end

def nudge?(dt = 2.weeks)
def bother?(dt = 1.week)
time_limit = Time.now - dt
return false if created_at > time_limit
return false if nudge_at && nudge_at > time_limit
asleep?(dt)
true
end

def nudge?(dt = 2.weeks)
bother?(dt) && asleep?(dt)
end

def dead?(dt = 1.month)
asleep?(dt)
end

def nudge!
inform! "Challenge someone to a game of #{game.name} today!", 'nudge'
def bother!(message, gif = nil)
inform! message, gif
update_attributes!(nudge_at: Time.now)
end

def nudge!
bother! "Challenge someone to a game of #{game.name} today!", 'nudge'
end

def api_url
return unless api?
"#{SlackGamebot::Service.api_url}/teams/#{id}"
Expand Down
37 changes: 36 additions & 1 deletion spec/models/team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,21 @@
expect(Team.find(inactive_team_a_month_ago.id)).to be nil
end
end
context '#nudge? #dead? and #asleep?' do
context '#nudge? #dead? #bother? and #asleep?' do
context 'default' do
let(:team) { Fabricate(:team) }
it 'false' do
expect(team.asleep?).to be false
expect(team.nudge?).to be false
expect(team.dead?).to be false
expect(team.bother?).to be false
end
end
context 'team created three weeks ago' do
let(:team) { Fabricate(:team, created_at: 3.weeks.ago) }
it 'nudge=true dead=false' do
expect(team.asleep?).to be true
expect(team.bother?).to be true
expect(team.nudge?).to be true
expect(team.dead?).to be false
end
Expand All @@ -82,6 +84,7 @@
it 'false' do
expect(team.asleep?).to be false
expect(team.nudge?).to be false
expect(team.bother?).to be true
expect(team.dead?).to be false
end
context 'awaken three weeks ago' do
Expand All @@ -90,6 +93,15 @@
end
it 'nudge' do
expect(team.nudge?).to be false
expect(team.bother?).to be true
end
end
context 'bothered less than a week ago' do
before do
team.update_attributes!(nudge_at: 3.days.ago)
end
it 'bother' do
expect(team.bother?).to be false
end
end
end
Expand Down Expand Up @@ -199,6 +211,29 @@
end.to change(team, :nudge_at)
end
end
context '#bother!' do
let(:team) { Fabricate(:team) }
let(:client) { double(Slack::Web::Client) }
before do
allow(Slack::Web::Client).to receive(:new).with(token: team.token).and_return(client)
end
it 'sends a bother message to the active channel' do
expect(client).to receive(:channels_list).and_return(
'channels' => [
{ 'name' => 'general', 'is_member' => false, 'id' => 'general_id' },
{ 'name' => 'pong', 'is_member' => true, 'id' => 'pong_id' }
]
)
expect(client).to receive(:chat_postMessage).with(
text: 'Please upgrade!',
channel: 'pong_id',
as_user: true
)
expect do
team.bother! 'Please upgrade!'
end.to change(team, :nudge_at)
end
end
context 'gifs' do
let!(:team) { Fabricate(:team) }
context 'with a played challenge' do
Expand Down
9 changes: 9 additions & 0 deletions spec/slack-gamebot/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,14 @@
subject.send(:nudge_sleeping_teams!)
end
end
context '#bother_free_teams!' do
it 'bothers free teams' do
expect(active_team).to_not receive(:bother!)
expect(active_team_one_week_ago).to receive(:bother!).with("Enjoying this free bot? Don't be cheap! #{active_team_one_week_ago.upgrade_text}")
expect(active_team_four_weeks_ago).to receive(:bother!).with("Enjoying this free bot? Don't be cheap! #{active_team_four_weeks_ago.upgrade_text}")
expect(premium_team_a_month_ago).to_not receive(:bother!)
subject.send(:bother_free_teams!)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/slack-gamebot/commands/premium_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let!(:team) { Fabricate(:team) }
it 'is a premium feature' do
expect(message: "#{SlackRubyBot.config.user} premium", user: 'user').to respond_with_slack_message(
"This is a premium feature. Upgrade your team to premium for $29.99 a year at https://www.playplay.io/upgrade?team_id=#{team.team_id}&game=#{team.game.name}."
"This is a premium feature. Upgrade your team to premium and enable paid features for $29.99 a year at https://www.playplay.io/upgrade?team_id=#{team.team_id}&game=#{team.game.name}."
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/slack-gamebot/commands/reset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let!(:team) { Fabricate(:team) }
it 'is a premium feature' do
expect(message: "#{SlackRubyBot.config.user} reset", user: 'user').to respond_with_slack_message(
"This is a premium feature. Upgrade your team to premium for $29.99 a year at https://www.playplay.io/upgrade?team_id=#{team.team_id}&game=#{team.game.name}."
"This is a premium feature. Upgrade your team to premium and enable paid features for $29.99 a year at https://www.playplay.io/upgrade?team_id=#{team.team_id}&game=#{team.game.name}."
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/slack-gamebot/commands/seasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let!(:team) { Fabricate(:team) }
it 'is a premium feature' do
expect(message: "#{SlackRubyBot.config.user} seasons", user: 'user').to respond_with_slack_message(
"This is a premium feature. Upgrade your team to premium for $29.99 a year at https://www.playplay.io/upgrade?team_id=#{team.team_id}&game=#{team.game.name}."
"This is a premium feature. Upgrade your team to premium and enable paid features for $29.99 a year at https://www.playplay.io/upgrade?team_id=#{team.team_id}&game=#{team.game.name}."
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/slack-gamebot/commands/unregister_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let!(:team) { Fabricate(:team) }
it 'is a premium feature' do
expect(message: "#{SlackRubyBot.config.user} unregister", user: 'user').to respond_with_slack_message(
"This is a premium feature. Upgrade your team to premium for $29.99 a year at https://www.playplay.io/upgrade?team_id=#{team.team_id}&game=#{team.game.name}."
"This is a premium feature. Upgrade your team to premium and enable paid features for $29.99 a year at https://www.playplay.io/upgrade?team_id=#{team.team_id}&game=#{team.game.name}."
)
end
end
Expand Down

0 comments on commit 261443a

Please sign in to comment.