Skip to content

Commit

Permalink
twitter oauth support. if twitter, reqs page displays oauth follow bu…
Browse files Browse the repository at this point in the history
…tton. will associate twitter id to person, have them follow system twitter and system will follow them back. caveat: protected system twitter will follow back but manually accepts follow request
  • Loading branch information
herestomwiththeweather committed Feb 18, 2009
1 parent 694ac2d commit 20cf72f
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 2 deletions.
90 changes: 90 additions & 0 deletions app/controllers/reqs_controller.rb
Expand Up @@ -4,6 +4,54 @@ class ReqsController < ApplicationController
before_filter :login_or_oauth_required, :only => [:show, :index]
before_filter :correct_person_and_no_accept_required, :only => [ :edit, :update ]
before_filter :correct_person_and_no_commitment_required, :only => [ :destroy ]
before_filter :twitter_oauth_setup, :only => [:twitter_oauth_client, :twitter_oauth_callback]

def twitter_oauth_client
@request_token = @consumer.get_request_token
session[:req_token] = @request_token.token
session[:req_token_secret] = @request_token.secret
redirect_to @request_token.authorize_url
return
end

def twitter_oauth_callback
require 'json'
@request_token = OAuth::RequestToken.new(@consumer,session[:req_token],session[:req_token_secret])
@access_token = @request_token.get_access_token
@resp = @consumer.request(:get, '/account/verify_credentials.json', @access_token, {:scheme => :query_string})
case @resp
when Net::HTTPSuccess
user_info = JSON.parse(@resp.body)
unless user_info['screen_name']
flash[:error] = "Authentication failed"
redirect_to :action => :index
return
end

changed = update_screen_name( user_info['screen_name'] )
if changed
system_twitter = global_prefs.twitter_name
response = @access_token.post("/friendships/create/#{system_twitter}.json","")
case response
when Net::HTTPSuccess
follow(user_info['screen_name'])
flash[:notice] = "You are following requests on Twitter!"
redirect_to :action => :index
else
flash[:error] = "Authentication failed"
redirect_to :action => :index
end
return
end

else
flash[:error] = "Authentication failed"
redirect_to :action => :index
return
end

end

# GET /reqs
# GET /reqs.xml
def index
Expand Down Expand Up @@ -108,6 +156,48 @@ def destroy

private

def update_screen_name(screen_name)
changed = false

if current_person.twitter_name.blank?
current_person.twitter_name = screen_name
current_person.save!
flash[:notice] = "Your Twitter name has been set to #{screen_name}"
changed = true
else
if current_person.twitter_name != screen_name
current_person.twitter_name = screen_name
current_person.save!
logger.info "#{current_person.name} changing twitter from #{current_person.twitter_name} to #{screen_name}"
flash[:notice] = "Your Twitter has been changed from #{current_person.twitter_name} to #{screen_name}"
changed = true
end
end

changed
end

def twitter_oauth_setup
require 'oauth'
require 'oauth/consumer'
@consumer_key = global_prefs.twitter_oauth_consumer_key
@consumer_secret = global_prefs.plaintext_twitter_oauth_consumer_secret
@consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret, { :site => "http://twitter.com" })
end

def follow(twitter_id)
twitter_name = global_prefs.twitter_name
twitter_password = global_prefs.plaintext_twitter_password
twitter_api = global_prefs.twitter_api

twit = Twitter::Base.new(twitter_name,twitter_password, :api_host => twitter_api )
begin
twit.create_friendship(twitter_id)
rescue Twitter::CantConnect => e
logger.info "ERROR Twitter::CantConnect for [#{twitter_id}] (" + e.to_s + ")"
end
end

def correct_person_and_no_accept_required
request = Req.find(params[:id])
redirect_to home_url unless request.person == current_person
Expand Down
17 changes: 15 additions & 2 deletions app/models/preference.rb
Expand Up @@ -18,19 +18,23 @@
#

class Preference < ActiveRecord::Base
attr_accessor :twitter_password
attr_accessor :twitter_password,
:twitter_oauth_consumer_secret
attr_accessible :app_name, :server_name, :domain, :smtp_server,
:exception_notification,
:email_notifications, :email_verifications, :analytics,
:about, :demo, :whitelist, :gmail, :registration_notification,
:practice, :steps, :questions, :memberships, :contact,
:twitter_name, :twitter_password, :twitter_api
:twitter_name, :twitter_password, :twitter_api,
:twitter_oauth_consumer_key, :twitter_oauth_consumer_secret

validates_presence_of :domain, :if => :using_email?
validates_presence_of :smtp_server, :if => :using_email?
validates_presence_of :twitter_api, :if => :using_twitter?

before_save :encrypt_twitter_password
before_save :encrypt_twitter_oauth_consumer_secret

before_validation :set_default_twitter_api_if_using_twitter

# Can we send mail with the present configuration?
Expand All @@ -42,6 +46,10 @@ def plaintext_twitter_password
decrypt(crypted_twitter_password)
end

def plaintext_twitter_oauth_consumer_secret
decrypt(crypted_twitter_oauth_consumer_secret)
end

private
def decrypt(password)
Crypto::Key.from_file("#{RAILS_ROOT}/rsa_key").decrypt(password)
Expand All @@ -61,6 +69,11 @@ def encrypt_twitter_password
self.crypted_twitter_password = encrypt(twitter_password)
end

def encrypt_twitter_oauth_consumer_secret
return if twitter_oauth_consumer_secret.blank?
self.crypted_twitter_oauth_consumer_secret = encrypt(twitter_oauth_consumer_secret)
end

def set_default_twitter_api_if_using_twitter
self.twitter_api = 'twitter.com' if self.twitter_api.blank?
end
Expand Down
10 changes: 10 additions & 0 deletions app/views/admin/preferences/edit.html.erb
Expand Up @@ -37,6 +37,16 @@
<%= f.password_field :twitter_password %>
</div>

<div class="form_row">
<label for="twitter_oauth_consumer_key">Twitter OAuth Consumer Key</label>
<%= f.text_field :twitter_oauth_consumer_key %>
</div>

<div class="form_row">
<label for="twitter_password">Twitter OAuth Consumer Secret</label>
<%= f.password_field :twitter_oauth_consumer_secret %>
</div>

<div class="form_row">
<label for="twitter_api">Twitter api</label>
<%= f.text_field :twitter_api %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/admin/preferences/show.html.erb
Expand Up @@ -25,6 +25,11 @@
<%=h @preferences.twitter_name %>
</p>

<p>
<b>Twitter oauth consumer key:</b>
<%=h @preferences.twitter_oauth_consumer_key %>
</p>

<p>
<b>Twitter api:</b>
<%=h @preferences.twitter_api %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/reqs/index.html.erb
@@ -1,5 +1,9 @@
<%- column_div :type => :primary do -%>
<% if global_prefs.twitter_name.blank? %>
<h2>Requests</h2>
<% else %>
<h2>Requests &nbsp;&nbsp;<%= link_to( image_tag("twitter.gif", :border => 0, :title => "follow me"), twitter_oauth_client_path ) %></h2>
<%- end -%>
<%- if @reqs.empty? -%>
<h3 class="blankslate">No requests (yet!)</h3>
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Expand Up @@ -3,6 +3,9 @@

map.resources :bids

map.twitter_oauth_client '/reqs/twitter_oauth_client', :controller => "reqs", :action => "twitter_oauth_client"
map.twitter_oauth_callback '/reqs/twitter_oauth_callback', :controller => "reqs", :action => "twitter_oauth_callback"

map.resources :reqs do |req|
req.resources :bids
end
Expand Down
@@ -0,0 +1,11 @@
class AddTwitterConsumerCredsPreferences < ActiveRecord::Migration
def self.up
add_column :preferences, :twitter_oauth_consumer_key, :string
add_column :preferences, :crypted_twitter_oauth_consumer_secret, :string
end

def self.down
remove_column :preferences, :twitter_oauth_consumer_key
remove_column :preferences, :crypted_twitter_oauth_consumer_secret
end
end
Binary file added public/images/twitter.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 20cf72f

Please sign in to comment.