Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 136 lines (117 sloc) 4.016 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
class Rstatus

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

    @timeline = true

    if params[:reply]
      u = Update.first(:id => params[:reply])
      @update_text = "@#{u.author.username} "
      @update_id = u.id
    elsif params[:share]
      u = Update.first(:id => params[:share])
      @update_text = "RS @#{u.author.username}: #{u.text}"
      @update_id = u.id
    else
      @update_text = ""
      @update_id = ""
    end

    if params[:status]
      @update_text = @update_text + params[:status]
    end

    if pjax_request?
      haml :"updates/_list", :locals => {:updates => @updates}, :layout => false
    else
      haml :"updates/index"
    end
  end

  get '/replies' do
    if logged_in?
      set_params_page
      @updates = current_user.at_replies(params).paginate( :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)
      set_pagination_buttons(@updates)
      if pjax_request?
        haml :"updates/_list", :locals => {:updates => @updates}, :layout => false
      else
        haml :"updates/index"
      end
    else
      haml :"static/home", :layout => false
    end
  end

  # Ahh, the classic 'world' view.
  get '/updates' do
    @updates = Update.paginate( :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)
    set_pagination_buttons(@updates)

    if pjax_request?
      haml :"updates/_list", :locals => {:updates => @updates}, :layout => false
    else
      haml :"updates/index"
    end
  end
  
  get "/search" do
    @updates = []
    if params[:q]
      @updates = Update.filter(params[:q], :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)
      set_pagination_buttons(@updates)
    end
    haml :"updates/search"
  end
  
  # get "/hashtags/:tag" do
  # @hashtag = params[:tag]
  # set_params_page
  # @updates = Update.hashtag_search(@hashtag, params).paginate( :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)
  # set_pagination_buttons(@updates)
  # @timeline = true
  # @update_text = params[:status]
  # haml :"updates/hashtags"
  # end

  # If you're POST-ing to /updates, it means you're making a new one. Woo-hoo!
  # This is what it's all built for.
  post '/updates' do
    # XXX: This should really be put into a model. Fat controller here!
    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)

    # add entry to user's feed
    current_user.feed.updates << u
    unless u.valid?
      flash[:notice] = u.errors.errors.values.join("\n")
    else
      current_user.feed.save
      current_user.save
      # tell hubs there is a new entry
      current_user.feed.ping_hubs(url(current_user.feed.url))

      flash[:notice] = "Update created."
    end

    if request.referrer
      redirect request.referrer
    else
      redirect "/"
    end
  end

  # Yay for REST-y CRUD! This just shows an update.
  get '/updates/:id' do
    @update = Update.first :id => params[:id]
    @referral = @update.referral
    haml :"updates/show", :layout => :'layout/update'
  end

  # Hopefully people don't delete a whole bunch of their updates, but if they
  # want to, this is where they come.
  delete '/updates/:id' do |id|
    update = Update.first :id => params[:id]

    # lolsecurity.
    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
Something went wrong with that request. Please try again.