Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

141/fix response system #147

Merged
merged 7 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 6 additions & 5 deletions app/models/mention.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ def self.retrieve_mentions
twitter = Twitter_API.new
@client = twitter.client
mentions = @client.mentions_timeline
@account_name = @client.user.screen_name
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this need to be an instance variable? It seems to only be used here and within the each block, so I think a normal variable without the @ would make more sense

mentions.each do |tweet|
Mention.check_unsubscribes(tweet)
Mention.generate_mention(tweet)
Mention.generate_mention(tweet, @account_name)
end
end

# Takes a tweet and generates a mention if it's a request for a lesson to be sent
def self.generate_mention(tweet)
def self.generate_mention(tweet, bot_name)
#Retrieves the first hashtag listed in the tweet.
lesson = Mention.identify_lesson(tweet)
if lesson && BlockList.can_send(tweet.user.id)
@handles = tweet.user_mentions.reject {|user| user.screen_name == "tweechable"}
@handles = tweet.user_mentions.reject {|user| user.screen_name == bot_name}
@handles.each do |handle|
# FIXME: need to check the timestamp is recent but we are not doing it right now. this is kind of hacky
if BlockList.can_receive_id(handle.id) && !Mention.where(handler: handle.screen_name, hash_tag: lesson.hash_tag).any?
Expand Down Expand Up @@ -54,11 +55,11 @@ def self.reply_mentions
if @lesson
twitters = Twitter_API.new
@client = twitters.client
to_send = "#{mention.handler} #{@lesson.intro} #{@lesson.thread_link}"
to_send = "@#{mention.handler} #{@lesson.intro} #{@lesson.thread_link}"
puts "Tweeting: #{to_send}"
@t = @client.update(to_send)
if !@t
puts "Something went wrong with post this tweet: #{to_send}"
puts "Something went wrong with posting this tweet: #{to_send}"
end

#Right now this is assuming the reply went successfully
Expand Down
10 changes: 5 additions & 5 deletions test/models/mentions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ class MentionsTest < ActiveSupport::TestCase
test "If a tweet doesn't have a hashtag, no mention should be generated" do
tweet = generate_tweet("@target hey @tweechable please help test")
lesson = Mention.identify_lesson(tweet)
assert_equal(nil, lesson, "A lesson shouldn't have been identified")
assert_nil(lesson, "A lesson shouldn't have been identified")
end

test "If a tweet has a hashtag but it doesn't match any lessons, don't generate a mention" do
tweet = generate_tweet("@target hey @tweechable please help #hashtag #blashtag")
lesson = Mention.identify_lesson(tweet)
assert_equal(nil, lesson, "A lesson shouldn't have been identified")
assert_nil(lesson, "A lesson shouldn't have been identified")
end

test "If a user is on the blocklist as can't receive, mentions shouldn't be generated for them" do
no_receive = block_lists(:NoReceive)
tweet = generate_tweet("@NoReceive hi @tweechable please help test #test")
Mention.generate_mention(tweet)
Mention.generate_mention(tweet, "tweechable")
assert_equal(0, Mention.count, "No new mentions should have been created")
end

test "If a user is on the blocklist as can't send, mentions shouldn't be generated for them" do
no_send = block_lists(:NoSend)
tweet = generate_tweet_author("@target_one hey @tweechable please help #test", no_send.user_id, no_send.user_name)
Mention.generate_mention(tweet)
Mention.generate_mention(tweet, "tweechable")
assert_equal(0, Mention.count, "No new mentions should have been created")
end

Expand All @@ -58,7 +58,7 @@ class MentionsTest < ActiveSupport::TestCase

test "If a tweet doesn't have a target, don't generate a mention" do
tweet = generate_tweet("I think @tweechable is a good service.")
Mention.generate_mention(tweet)
Mention.generate_mention(tweet, "tweechable")
assert_equal(0, Mention.count, "A new tweet shouldn't have been created")
end

Expand Down