Skip to content

Commit

Permalink
Added facebook reposts and user profile authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
BRIMIL01 committed Mar 27, 2011
1 parent 071ed96 commit 5312b38
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 28 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem "rack-flash"
gem "time-ago-in-words"
gem "sinatra-content-for", :require => "sinatra/content_for"
gem "twitter"
gem "fb_graph"
gem "pony"
gem "bcrypt-ruby", :require => "bcrypt"
gem "rdiscount"
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ GEM
rack (< 2, >= 1.1.0)
faraday_middleware (0.3.2)
faraday (~> 0.5.4)
fb_graph (1.6.3)
activesupport (>= 2.3)
json (>= 1.4.3)
oauth2 (>= 0.1.0)
restclient_with_cert
ffi (1.0.7)
rake (>= 0.8.7)
haml (3.0.25)
hashie (1.0.0)
i18n (0.5.0)
jnunemaker-validatable (1.8.4)
activesupport (>= 2.3.4)
json (1.5.1)
json_pure (1.5.1)
mail (2.2.15)
activesupport (>= 2.3.6)
Expand Down Expand Up @@ -134,6 +140,8 @@ GEM
rest-client (>= 1.5.0)
rest-client (1.6.1)
mime-types (>= 1.16)
restclient_with_cert (0.0.3)
rest-client (>= 1.6)
rocco (0.6)
mustache
rdiscount
Expand Down Expand Up @@ -184,6 +192,7 @@ DEPENDENCIES
compass
database_cleaner
factory_girl
fb_graph
haml
i18n
mocha
Expand Down
41 changes: 27 additions & 14 deletions models/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Update
key :text, String, :default => ""
key :tags, Array, :default => []
key :language, String
key :tweeted, Boolean
key :twitter, Boolean
key :facebook, Boolean

# store in authorization
#attr_accessor :oauth_token, :oauth_secret
Expand Down Expand Up @@ -57,7 +58,7 @@ def mentioned? search
matches.nil? ? false : matches.length > 0
end

after_create :tweet
after_create :send_to_external_accounts

timestamps!

Expand All @@ -83,22 +84,34 @@ def get_language

protected

def tweet
def send_to_external_accounts
return if ENV['RACK_ENV'] == 'development'
if tweeted? && author.user && author.user.twitter?
begin
Twitter.configure do |config|
config.consumer_key = ENV["CONSUMER_KEY"]
config.consumer_secret = ENV["CONSUMER_SECRET"]
config.oauth_token = author.user.twitter.oauth_token
config.oauth_token_secret = author.user.twitter.oauth_secret
if author.user
if twitter? && author.user.twitter?
begin
Twitter.configure do |config|
config.consumer_key = ENV["CONSUMER_KEY"]
config.consumer_secret = ENV["CONSUMER_SECRET"]
config.oauth_token = author.user.twitter.oauth_token
config.oauth_token_secret = author.user.twitter.oauth_secret
end

Twitter.update(text)
rescue Exception => e
#I should be shot for doing this.
end
end

if facebook? && author.user.facebook?
begin
user = FbGraph::User.me(author.user.facebook.token)
user.feed!(:message => text)
rescue Exception => e
#I should be shot for doing this.
end

Twitter.update(text)
rescue Exception => e
#I should be shot for doing this.
end
end

end

end
22 changes: 19 additions & 3 deletions models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,32 @@ def url
end

def twitter?
a = Authorization.first(:provider => "twitter", :user_id => self.id)
has_authorization?(:twitter)
end

def twitter
get_authorization(:twitter)
end

def facebook?
has_authorization?(:facebook)
end

def facebook
get_authorization(:facebook)
end

def has_authorization?(auth)
a = Authorization.first(:provider => auth.to_s, :user_id => self.id)
if a.nil?
return false
else
return true
end
end

def twitter
Authorization.first(:provider => "twitter", :user_id => self.id)
def get_authorization(auth)
Authorization.first(:provider => auth.to_s, :user_id => self.id)
end

key :following_ids, Array
Expand Down
23 changes: 20 additions & 3 deletions rstatus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Rstatus < Sinatra::Base

use OmniAuth::Builder do
provider :twitter, ENV["CONSUMER_KEY"], ENV["CONSUMER_SECRET"]
provider :facebook, ENV["APP_ID"], ENV["APP_SECRET"]
provider :facebook, ENV["APP_ID"], ENV["APP_SECRET"], {:scope => 'publish_stream,offline_access,email'}
end

############################
Expand Down Expand Up @@ -241,6 +241,20 @@ class Rstatus < Sinatra::Base

if User.first :username => auth['user_info']['nickname'] or auth['user_info']['nickname'] =~ /profile[.]php[?]id=/
#we have a username conflict!

#let's store their oauth stuff so they don't have to re-login after
session[:oauth_token] = auth['credentials']['token']
session[:oauth_secret] = auth['credentials']['secret']

session[:uid] = auth['uid']
session[:provider] = auth['provider']
session[:name] = auth['user_info']['name']
session[:nickname] = auth['user_info']['nickname']
session[:website] = auth['user_info']['urls']['Website']
session[:description] = auth['user_info']['description']
session[:image] = auth['user_info']['image']
session[:email] = auth['user_info']['email']

flash[:notice] = "Sorry, someone has that name."
redirect '/users/new'
return
Expand Down Expand Up @@ -326,6 +340,7 @@ class Rstatus < Sinatra::Base
auth['user_info']['urls']['Website'] = session[:website]
auth['user_info']['description'] = session[:description]
auth['user_info']['image'] = session[:image]
auth['user_info']['email'] = session[:email]
auth['credentials'] = {}
auth['credentials']['token'] = session[:oauth_token]
auth['credentials']['secret'] = session[:oauth_secret]
Expand Down Expand Up @@ -577,11 +592,13 @@ class Rstatus < Sinatra::Base
end

post '/updates' do
do_tweet = !params[:tweeted].nil? || params[:tweeted] == "1"
do_tweet = !params[:tweet].nil? || params[:tweet] == "1"
do_facebook = !params[:facebook].nil? || params[:facebook] == "1"
u = Update.new(:text => params[:text],
:referral_id => params[:referral_id],
:author => current_user.author,
:tweeted => do_tweet)
:twitter => do_tweet,
:facebook => do_facebook)

# and entry to user's feed
current_user.feed.updates << u
Expand Down
31 changes: 26 additions & 5 deletions test/rstatus_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def test_twitter_send_checkbox_present
a = Factory(:authorization, :user => u)
log_in(u, a.uid)

assert_match page.body, /Tweet Me/
assert_match page.body, /Twitter/
end

def test_twitter_send
Expand All @@ -305,7 +305,7 @@ def test_twitter_send
log_in(u, a.uid)

fill_in "text", :with => update_text
check("tweeted")
check("tweet")
click_button "Share"

assert_match /Update created/, page.body
Expand All @@ -319,8 +319,7 @@ def test_twitter_no_send
log_in(u, a.uid)

fill_in "text", :with => update_text
uncheck("tweeted")

uncheck("tweet")
click_button "Share"

assert_match /Update created/, page.body
Expand All @@ -338,7 +337,7 @@ def test_no_twitter_no_send
assert_match /Update created/, page.body
end

def add_twitter_to_email_account
def add_twitter_to_account
u = Factory(:user)
OmniAuth.config.add_mock(:twitter, {
:uid => uid,
Expand All @@ -358,6 +357,28 @@ def add_twitter_to_email_account
assert_equal "1234", u.twitter.oauth_token
assert_equal "4567", u.twitter.oauth_secret
end

def add_facebook_to_account
u = Factory(:user)
OmniAuth.config.add_mock(:facebook, {
:uid => uid,
:user_info => {
:name => "Joe Public",
:email => "[email protected]",
:nickname => u.username,
:urls => { :Website => "http://rstat.us" },
:description => "A description",
:image => "/images/something.png"
},
:credentials => {:token => "1234", :secret => "4567"}
})
log_in_no_twitter(u)
visit "/users/#{u.username}/edit"
click_button "Add Facebook Account"

assert_equal "1234", u.twitter.oauth_token
assert_equal "4567", u.twitter.oauth_secret
end

def test_facebook_username
new_user = Factory.build(:user, :username => 'profile.php?id=12345')
Expand Down
18 changes: 18 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ def log_in_fb(u, uid = 12345)
end


def log_in_facebook(u, uid = 12345)
Author.any_instance.stubs(:valid_gravatar?).returns(:false)
OmniAuth.config.add_mock(:facebook, {
:uid => uid,
:user_info => {
:name => "Joe Public",
:email => "[email protected]",
:nickname => u.username,
:urls => { :Website => "http://rstat.us" },
:description => "A description",
:image => "/images/something.png"
},
:credentials => {:token => "1234", :secret => "4567"}
})

visit '/auth/twitter'
end

def log_in_no_twitter(user)
User.stubs(:authenticate).returns(user).once
visit "/login"
Expand Down
6 changes: 3 additions & 3 deletions test/update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def test_hashtags

def test_tweeted_flag_default
u = Update.new(:text => "This is a message with a #hashtag.")
assert_equal false, u.tweeted?
assert_equal false, u.twitter?
end

def test_tweeted_flag
u = Update.new(:text => "This is a message with a #hashtag.", :tweeted => true)
assert_equal true, u.tweeted?
u = Update.new(:text => "This is a message with a #hashtag.", :twitter => true)
assert_equal true, u.twitter?
end


Expand Down
12 changes: 12 additions & 0 deletions test/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,17 @@ def test_user_returns_twitter
a = Factory.create(:authorization, :user => u)
assert_equal a, u.twitter
end

def test_user_has_facebook
u = Factory.create(:user)
a = Factory.create(:authorization, :user => u, :provider => "facebook")
assert_equal true, u.facebook?
end

def test_user_returns_facebook
u = Factory.create(:user)
a = Factory.create(:authorization, :user => u, :provider => "facebook")
assert_equal a, u.facebook
end

end
9 changes: 9 additions & 0 deletions views/_update_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
%input#update-referral{:type => 'hidden', :name => 'referral_id', :value => @update_id}
- if current_user.twitter?
%span
<<<<<<< HEAD
%input{:type => "checkbox", :name => "tweeted", :id => "tweeted", :checked => "checked"}
%label{:for => "tweeted"} Tweet Me

=======
%input{:type => "checkbox", :name => "tweet", :id => "tweet", :checked => "checked"}
%label{:for => "tweet"} Twitter
- if current_user.facebook?
%span
%input{:type => "checkbox", :name => "facebook", :id => "facebook", :checked => "checked"}
%label{:for => "facebook"} Facebook
>>>>>>> Added facebook reposts and user profile authorization
%input#update-button.button{:type => "submit", :value => "Share"}
#update-count{:title => 'Characters remaining'}
6 changes: 6 additions & 0 deletions views/users/edit.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@
- else
%form#profile-update{:action => "/auth/twitter", :method => "GET", :name => "profile_update_form"}
%input#update-button.button{:type => "submit", :value => "Add Twitter Account"}
%br
- if @user.facebook?
%span= @user.facebook.nickname
- else
%form#profile-update{:action => "/auth/facebook", :method => "GET", :name => "profile_update_form"}
%input#update-button.button{:type => "submit", :value => "Add Facebook Account"}


0 comments on commit 5312b38

Please sign in to comment.