Skip to content

Commit

Permalink
Merge branch 'master' of github.com:hotsh/rstat.us
Browse files Browse the repository at this point in the history
  • Loading branch information
LindseyB committed Mar 28, 2011
2 parents 2b87514 + 7f722f5 commit d52b0ca
Show file tree
Hide file tree
Showing 23 changed files with 680 additions and 685 deletions.
92 changes: 92 additions & 0 deletions config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class Rstatus
# The `PONY_VIA_OPTIONS` hash is used to configure `pony`. Basically, we only
# want to actually send mail if we're in the production environment. So we set
# the hash to just be `{}`, except when we want to send mail.
configure :test do
PONY_VIA_OPTIONS = {}
end

configure :development do
PONY_VIA_OPTIONS = {}
end

configure :production do
Compass.configuration do |config|
config.output_style = :compressed
end
end


# We're using [SendGrid](http://sendgrid.com/) to send our emails. It's really
# easy; the Heroku addon sets us up with environment variables with all of the
# configuration options that we need.
configure :production do
PONY_VIA_OPTIONS = {
:address => "smtp.sendgrid.net",
:port => "25",
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN']
}
end

# We need a secret for our sessions. This is set via an environment variable so
# that we don't have to give it away in the source code. Heroku makes it really
# easy to keep environment variables set up, so this ends up being pretty nice.
# This also has to be included before rack-flash, or it blows up.
use Rack::Session::Cookie, :secret => ENV['COOKIE_SECRET']

# We're using rack-timeout to ensure that our dynos don't get starved by renegade
# processes.
use Rack::Timeout
Rack::Timeout.timeout = 10

set :root, File.dirname(__FILE__)
set :haml, :escape_html => true

# This method enables the ability for our forms to use the _method hack for
# actual RESTful stuff.
set :method_override, true

# If you've used Rails' flash messages, you know how convenient they are.
# rack-flash lets us use them.
use Rack::Flash

configure do
if ENV['MONGOHQ_URL']
MongoMapper.config = {ENV['RACK_ENV'] => {'uri' => ENV['MONGOHQ_URL']}}
MongoMapper.database = ENV['MONGOHQ_DATABASE']
MongoMapper.connect("production")
else
MongoMapper.connection = Mongo::Connection.new('localhost')
MongoMapper.database = "rstatus-#{settings.environment}"
end

# configure compass
Compass.configuration do |config|
config.project_path = File.dirname(__FILE__)
config.sass_options = {:cache_location => "./tmp/sass-cache"}
end
MongoMapperExt.init

# now that we've connected to the db, let's load our models.
require_relative 'models/all'
end

helpers Sinatra::UserHelper
helpers Sinatra::ContentFor

helpers do
[:development, :production, :test].each do |environment|
define_method "#{environment.to_s}?" do
return settings.environment == environment
end
end
end

use OmniAuth::Builder do
provider :twitter, ENV["CONSUMER_KEY"], ENV["CONSUMER_SECRET"]
provider :facebook, ENV["APP_ID"], ENV["APP_SECRET"], {:scope => 'publish_stream,offline_access,email'}
end
end
56 changes: 56 additions & 0 deletions controllers/auth_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Rstatus
get '/auth/:provider/callback' do
auth = request.env['omniauth.auth']
unless @auth = Authorization.find_from_hash(auth)
if logged_in?
Authorization.create_from_hash(auth, uri("/"), current_user)
redirect "/users/#{current_user.username}/edit"
else
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']
#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']

## We can probably get rid of this since the user confirmation will check for duplicate usernames [brimil01]
if User.first :username => auth['user_info']['nickname'] or auth['user_info']['nickname'] =~ /profile[.]php[?]id=/
#we have a username conflict!
flash[:notice] = "Sorry, someone has that name."
redirect '/users/new'
return
else
# Redirect to confirm page to verify username and provide email
redirect '/users/confirm'
return
end
end
end

## Lets store the tokens if they don't alreay exist
if @auth.oauth_token.nil?
@auth.oauth_token = auth['credentials']['token']
@auth.oauth_secret = auth['credentials']['secret']
@auth.nickname = auth['user_info']['nickname']
@auth.save
end

session[:user_id] = @auth.user.id

flash[:notice] = "You're now logged in."
redirect '/'
end

get '/auth/failure' do
if params[:message] == "invalid_credentials"
haml :"signup/invalid_credentials"
else
raise Sinatra::NotFound
end
end
end
40 changes: 40 additions & 0 deletions controllers/feeds_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Rstatus

# publisher will feed the atom to a hub
# subscribers will verify a subscription
get "/feeds/:id.atom" do
content_type "application/atom+xml"

feed = Feed.first :id => params[:id]

if params['hub.challenge']
#sub = OSub::Subscription.new(request.url, feed.url, nil, feed.verify_token)

# perform the hub's challenge
#respond = sub.perform_challenge(params['hub.challenge'])

# verify that the random token is the same as when we
# subscribed with the hub initially and that the topic
# url matches what we expect
#verified = params['hub.topic'] == feed.url
#if verified and sub.verify_subscription(params['hub.verify_token'])
# if development?
# puts "Verified"
# end
# body respond[:body]
# status respond[:status]
#else
# if development?
# puts "Verification Failed"
# end
# if the verification fails, the specification forces us to
# return a 404 status
status 404
#end
else
# TODO: Abide by headers that supply cache information
body feed.atom(uri("/"))
end
end

end
46 changes: 46 additions & 0 deletions controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Rstatus

get "/login" do
if logged_in?
redirect '/'
else
haml :"login"
end
end

post "/login" do
u = User.first :username => params[:username]
if u.nil?
#signup
user = User.new params
if user.save
session[:user_id] = user.id
flash[:notice] = "Thanks for signing up!"
redirect "/"
else
puts "not saved"
flash[:notice] = "There was a problem... can you pick a different username?"
redirect "/login"
end
else
#login
if user = User.authenticate(params[:username], params[:password])
session[:user_id] = user.id
flash[:notice] = "Login successful."
redirect "/"
else
flash[:notice] = "The username or password you entered was incorrect"
redirect "/login"
end
end
end

get "/logout" do
if logged_in?
session[:user_id] = nil
flash[:notice] = "You've been logged out."
end
redirect '/'
end

end
13 changes: 13 additions & 0 deletions controllers/static_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Rstatus
get "/open_source" do
haml :opensource
end

get "/follow" do
haml :external_subscription
end

get "/contact" do
haml :contact
end
end
81 changes: 81 additions & 0 deletions controllers/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class Rstatus

# unsubscribe from a feed
delete '/subscriptions/:id' do
require_login! :return => request.referrer

feed = Feed.first :id => params[:id]

@author = feed.author
redirect request.referrer if @author.user == current_user

#make sure we're following them already
unless current_user.following? feed.url
flash[:notice] = "You're not following #{@author.username}."
redirect request.referrer
end

#unfollow them!
current_user.unfollow! feed

flash[:notice] = "No longer following #{@author.username}."
redirect request.referrer
end

post "/subscriptions" do
require_login! :return => request.referrer

feed_url = nil

# Allow for a variety of feed addresses
case params[:url]
when /^feed:\/\//
feed_url = "http" + params[:url][4..-1]
when /@/
# TODO: ensure caching of finger lookup.
acct = Redfinger.finger(params[:url])
feed_url = acct.links.find { |l| l['rel'] == 'http://schemas.google.com/g/2010#updates-from' }
else
feed_url = params[:url]
end

#make sure we're not following them already
if current_user.following? feed_url
# which means it exists
feed = Feed.first(:remote_url => feed_url)
if feed.nil? and feed_url[0] == "/"
feed_id = feed_url[/^\/feeds\/(.+)$/,1]
feed = Feed.first(:id => feed_id)
end

flash[:notice] = "You're already following #{feed.author.username}."

redirect request.referrer
end

# follow them!
f = current_user.follow! feed_url
unless f
flash[:notice] = "There was a problem following #{params[:url]}."
redirect request.referrer
end

if not f.local?

# remote feeds require some talking to a hub
hub_url = f.hubs.first

sub = OSub::Subscription.new(url("/feeds/#{f.id}.atom"), f.url, f.secret)
sub.subscribe(hub_url, f.verify_token)

name = f.author.username
flash[:notice] = "Now following #{name}."
redirect request.referrer
else
# local feed... redirect to that user's profile
flash[:notice] = "Now following #{f.author.username}."
redirect request.referrer
end
end

end
65 changes: 65 additions & 0 deletions controllers/updates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Rstatus

get '/updates' do
@updates = Update.paginate( :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)

if @updates.next_page
@next_page = "?#{Rack::Utils.build_query :page => @updates.next_page}"
end

if @updates.previous_page
@prev_page = "?#{Rack::Utils.build_query :page => @updates.previous_page}"
end

haml :world
end

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

# and entry to user's feed
current_user.feed.updates << u
current_user.feed.save
current_user.save

# tell hubs there is a new entry
current_user.feed.ping_hubs(url(current_user.feed.url))

if params[:text].length < 1
flash[:notice] = "Your status is too short!"
elsif params[:text].length > 140
flash[:notice] = "Your status is too long!"
else
flash[:notice] = "Update created."
end

redirect "/"
end

get '/updates/:id' do
@update = Update.first :id => params[:id]
@referral = @update.referral
haml :"updates/show", :layout => :'updates/layout'
end

delete '/updates/:id' do |id|
update = Update.first :id => params[:id]

if update.author == current_user.author
update.destroy

flash[:notice] = "Update Baleeted!"
redirect "/"
else
flash[:notice] = "I'm afraid I can't let you do that, " + current_user.name + "."
redirect back
end
end

end
Loading

0 comments on commit d52b0ca

Please sign in to comment.