Skip to content

Commit

Permalink
Merge remote branch 'wilkie/master'
Browse files Browse the repository at this point in the history
Conflicts:
	Gemfile.lock
	models.rb
	rstatus.rb
	views/_updates.haml
  • Loading branch information
steveklabnik committed Mar 23, 2011
2 parents ff8dbe8 + 0e60d28 commit b7d0b0c
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 72 deletions.
108 changes: 51 additions & 57 deletions rstatus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ class Rstatus < Sinatra::Base

# show user profile
get "/users/:slug" do
@author = Author.first :username => params[:slug]
user = User.first :username => params[:slug]
@author = user.author
haml :"users/show"
end

Expand All @@ -214,18 +215,58 @@ class Rstatus < Sinatra::Base
feed.update_entries(request.body.read, request.url, request.env['HTTP_X_HUB_SIGNATURE'])
end

post "/feeds" do
feed_url = params[:url]
# unsubscribe from a feed
delete '/subscriptions/:id' do
require_login! :return => "/subscriptions/#{params[:id]}"

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

@author = feed.author
redirect "/" and return 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 "/"
return
end

#unfollow them!
current_user.unfollow! feed

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

post "/subscriptions" do
require_login! :return => "/subscriptions"

feed_url = params[:url]
if feed_url[0..3] = "feed"
feed_url = "http" + feed_url[4..-1]
end

#make sure we're not following them already
if current_user.following? feed_url
# which means it exists
feed = Feed.first(:url => feed_url)

flash[:notice] = "You're already following #{feed.author.username}."
redirect "/users/#{feed.author.username}"
return
end

# follow them!

f = current_user.follow! feed_url
unless f
flash[:notice] = "The was a problem following #{params[:url]}."
redirect "/users/#{@user.username}"
else
redirect "/follow"
return
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)
Expand All @@ -234,6 +275,10 @@ class Rstatus < Sinatra::Base
name = f.author.username
flash[:notice] = "Now following #{name}."
redirect "/"
else
# local feed... redirect to that user's profile
flash[:notice] = "Now following #{f.author.username}."
redirect "/users/#{f.author.username}"
end
end

Expand Down Expand Up @@ -298,63 +343,12 @@ class Rstatus < Sinatra::Base
end
end

# an alias for the above route
# an alias for the route of the feed
get "/users/:name/feed" do
feed = User.first(:username => params[:name]).feed
redirect "/feeds/#{feed.id}.atom"
end

# users can follow each other, and this route takes care of it!
get '/users/:name/follow' do
require_login! :return => "/users/#{params[:name]}/follow"

@author = Author.first(:username => params[:name])
redirect "/users/#{@author.username}" and return if @author.user == current_user

#make sure we're not following them already
if current_user.following? @author.feed.url
flash[:notice] = "You're already following #{params[:name]}."
redirect "/users/#{@author.username}"
return
end

# then follow them!
unless current_user.follow! @author.feed.url
flash[:notice] = "The was a problem following #{params[:name]}."
redirect "/users/#{@author.username}"
else
flash[:notice] = "Now following #{params[:name]}."
redirect "/users/#{@author.username}"
end
end

#this lets you unfollow a user
get '/users/:name/unfollow' do
require_login! :return => "/users/#{params[:name]}/unfollow"

@author = Author.first(:username => params[:name])
redirect "/users/#{@author.username}" and return if @author.user == current_user

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

#unfollow them!
current_user.unfollow! @author.feed

flash[:notice] = "No longer following #{params[:name]}."
redirect "/users/#{@author.username}"
end

# this lets us see followers.
get '/users/:name/followers' do
@users = User.first(:username => params[:name]).followers
haml :"users/list", :locals => {:title => "Followers"}
end

# This lets us see who is following.
get '/users/:name/following' do
@users = User.first(:username => params[:name]).following
Expand Down
8 changes: 4 additions & 4 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
end

Factory.define :update do |u|
u.text Factory.next(:update_text)
u.text { Factory.next(:update_text) }
end

Factory.sequence :usernames do |i|
Expand All @@ -18,7 +18,7 @@
end

Factory.define :user do |u|
u.username Factory.next(:usernames)
u.username { Factory.next(:usernames) }
u.author {|a| Factory(:author, :username => a.username) }
end

Expand All @@ -27,14 +27,14 @@
end

Factory.define :authorization do |a|
a.uid Factory.next(:integer)
a.uid { Factory.next(:integer) }
a.provider "twitter"
a.association :user
end

Factory.define :author do |a|
a.username "user"
a.email Factory.next(:emails)
a.email { Factory.next(:emails) }
a.website "http://example.com"
a.name "Something"
a.bio "Hi, I do stuff."
Expand Down
37 changes: 36 additions & 1 deletion test/rstatus_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ def test_visit_feeds

def test_feed_render
feed = Factory(:feed)

updates = []
5.times do
updates << Factory(:update)
end

feed.updates = updates
feed.save

Expand All @@ -29,7 +31,6 @@ def test_feed_render
updates.each do |update|
assert_match page.body, /#{update.text}/
end

end

def test_user_feed_render
Expand Down Expand Up @@ -79,6 +80,40 @@ def test_subscribe_to_users_on_other_sites
assert "/", current_path
end

def test_user_follow_another_user
u = Factory(:user)
a = Factory(:authorization, :user => u)
u.finalize("http://example.com/")

u2 = Factory(:user)
u2.finalize("http://example.com")

log_in(u, a.uid)

visit "/users/#{u2.username}"

click_button "follow-#{u2.feed.id}"
assert_match "Now following #{u2.username}", page.body
end

def test_user_unfollow_another_user
u = Factory(:user)
a = Factory(:authorization, :user => u)
u.finalize("http://example.com/")

u2 = Factory(:user)
a2 = Factory(:authorization, :user => u2)
u2.finalize("http://example.com/")

log_in(u, a.uid)
u.follow! u2.feed.url

visit "/users/#{u2.username}/following"
click_button "unfollow-#{u2.feed.id}"

assert_match "No longer following #{u2.username}", page.body
end

def test_user_edit_own_profile_link
u = Factory(:user)
a = Factory(:authorization, :user => u)
Expand Down
2 changes: 1 addition & 1 deletion views/_updates.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
.text
!= update.to_html
.date
%a{:href => update.url}= update.created_at.ago_in_words
%a{:href => update_url}= update.created_at.ago_in_words
- if updates.empty?
.empty
There's no updates here yet
2 changes: 1 addition & 1 deletion views/external_subscription.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
%img{:src=>"images/identica.png"}

#follow-form
%form{:action => "/feeds", :method => "POST"}
%form{:action => "/subscriptions", :method => "POST"}
%input{:type => "text", :name => "url"}
%input{:type => "submit", :value => "Follow"}
19 changes: 15 additions & 4 deletions views/users/list.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
%ul.user-list
- @users.each_with_index do |feed, i|
%li.user{:class => i%2 == 0 ? "even" : "odd"}
-user_url = feed.local ? "/users/#{feed.author.username}" : feed.author.url
-unfollow_url = feed.local ? "/users/#{feed.author.username}/unfollow" : "/feeds/#{feed.id}/unsubscribe"
.avatar
%a{:href => "/users/#{feed.author.username}"}
%a{:href => user_url}
%img{:alt => "avatar", :src => feed.author.avatar_url}/

.name
%a{:href => "/users/#{feed.author.username}"}
%a{:href => user_url}
%b
= feed.author.name
%br
(#{feed.author.username})

- if current_user.following? feed.url
.follow.negative
%a.button{:href => "/users/#{feed.author.username}/unfollow"}
Unfollow
%form{:method => "post", :action => "/subscriptions/#{feed.id}"}
%input{:type => "hidden", :name => "_method", :value => "delete"}
%input.button.unfollow{:type => "submit", :value => "Unfollow", :id => "unfollow-#{feed.id}"}

:javascript
$(document).ready(function(){
$(".unfollow").click(function(){
var r = confirm("Are you sure you want to unsubscribe from this user?");
return r;
})
})
18 changes: 14 additions & 4 deletions views/users/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@
#follow
- if current_user and current_user.following? @author.feed.url
.negative
%a.button{:href => "/users/#{@author.username}/unfollow"}
Unfollow
%form{:method => "post", :action => "/subscriptions/#{@author.feed.id}"}
%input{:type => "hidden", :name => "_method", :value => "delete"}
%input.button.unfollow{:type => "submit", :value => "Unfollow", :id => "unfollow-#{@author.feed.id}"}
.follow-status You are currently following #{@author.username}
- elsif current_user
%a.button{:href => "/users/#{@author.username}/follow"}
Follow
%form{:method => "post", :action => "/subscriptions"}
%input{:type => "hidden", :name => "url", :value => "/feeds/#{@author.feed.id}"}
%input.button.follow{:type => "submit", :value => "Follow", :id => "follow-#{@author.feed.id}"}
.follow-status You are not following #{@author.username}

#updates.updates
!= haml :_updates, :locals => {:updates => @author.feed.updates.to_a.sort{|a, b| b.created_at <=> a.created_at }}

:javascript
$(document).ready(function(){
$(".unfollow").click(function(){
var r = confirm("Are you sure you want to unfollow this user?");
return r;
})
})

0 comments on commit b7d0b0c

Please sign in to comment.