Commit 3e6177cf by Rocky Duan

changed naming

parent c5fa9f00
......@@ -115,26 +115,26 @@ end
post '/api/v1/users/:user_id/subscriptions' do |user_id|
user = User.find_or_create_by(external_id: user_id)
source = case params["subscribed_type"]
source = case params["source_type"]
when "user"
User.find_or_create_by(external_id: params["subscribed_id"])
User.find_or_create_by(external_id: params["source_id"])
when "thread"
CommentThread.find(params["subscribed_id"])
CommentThread.find(params["source_id"])
else
Commentable.find_or_create_by(commentable_type: params["subscribed_type"], commentable_id: params["subscribed_id"])
Commentable.find_or_create_by(commentable_type: params["source_type"], commentable_id: params["source_id"])
end
user.subscribe(source).to_hash.to_json
end
delete '/api/v1/users/:user_id/subscriptions' do |user_id|
user = User.find_or_create_by(external_id: user_id)
source = case params["subscribed_type"]
source = case params["source_type"]
when "user"
User.find_or_create_by(external_id: params["subscribed_id"])
User.find_or_create_by(external_id: params["source_id"])
when "thread"
CommentThread.find(params["subscribed_id"])
CommentThread.find(params["source_id"])
else
Commentable.find_or_create_by(commentable_type: params["subscribed_type"], commentable_id: params["subscribed_id"])
Commentable.find_or_create_by(commentable_type: params["source_type"], commentable_id: params["source_id"])
end
user.unsubscribe(source).to_hash.to_json
end
......
......@@ -340,16 +340,16 @@ describe "app" do
before(:each) { init_with_subscriptions }
describe "GET /api/v1/users/:user_id/notifications" do
it "get all notifications on the subscribed comment threads for the user" do
#user = User.find("1")
#get "/api/v1/users/#{user.external_id}/notifications"
#last_response.should be_ok
#notifications = parse last_response.body
#so_easy = Comment.all.select{|c| c.body == "this problem is so easy"}.first
#not_for_me_neither = Comment.all.select{|c| c.body == "not for me neither!"}.first
#notification_so_easy = notifications.select{|f| f["notification_type"] == "post_reply" and f["info"]["comment_id"] == so_easy.id.to_s}.first
#notification_so_easy.should_not be_nil
#notification_not_for_me_neither = notifications.select{|f| f["notification_type"] == "post_reply" and f["info"]["comment_id"] == not_for_me_neither.id.to_s}.first
#notification_not_for_me_neither.should_not be_nil
user = User.find("1")
get "/api/v1/users/#{user.external_id}/notifications"
last_response.should be_ok
notifications = parse last_response.body
so_easy = Comment.all.select{|c| c.body == "this problem is so easy"}.first
not_for_me_neither = Comment.all.select{|c| c.body == "not for me neither!"}.first
notification_so_easy = notifications.select{|f| f["notification_type"] == "post_reply" and f["info"]["comment_id"] == so_easy.id.to_s}.first
notification_so_easy.should_not be_nil
notification_not_for_me_neither = notifications.select{|f| f["notification_type"] == "post_reply" and f["info"]["comment_id"] == not_for_me_neither.id.to_s}.first
notification_not_for_me_neither.should_not be_nil
end
it "get all notifications on the subscribed commentable for the user" do
user = User.find("1")
......@@ -360,12 +360,13 @@ describe "app" do
problem_wrong = notifications.select{|f| f["notification_type"] == "post_topic"}.first
problem_wrong["info"]["thread_title"].should == "This problem is wrong"
end
# TODO spec for followed user
end
describe "POST /api/v1/users/:user_id/subscriptions" do
it "follow user" do
user1 = User.find("1")
user2 = User.find("2")
post "/api/v1/users/#{user1.external_id}/subscriptions", subscribed_type: "user", subscribed_id: user2.external_id
post "/api/v1/users/#{user1.external_id}/subscriptions", source_type: "user", source_id: user2.external_id
last_response.should be_ok
User.find("2").followers.length.should == 1
User.find("2").followers.should include user1
......@@ -373,20 +374,20 @@ describe "app" do
it "unfollow user" do
user1 = User.find("1")
user2 = User.find("2")
delete "/api/v1/users/#{user2.external_id}/subscriptions", subscribed_type: "user", subscribed_id: user1.external_id
delete "/api/v1/users/#{user2.external_id}/subscriptions", source_type: "user", source_id: user1.external_id
last_response.should be_ok
User.find("1").followers.length.should == 0
end
it "subscribe a commentable" do
user3 = User.find_or_create_by(external_id: "3")
post "/api/v1/users/#{user3.external_id}/subscriptions", subscribed_type: "questions", subscribed_id: "1"
post "/api/v1/users/#{user3.external_id}/subscriptions", source_type: "questions", source_id: "1"
last_response.should be_ok
Commentable.first.subscribers.length.should == 3
Commentable.first.subscribers.should include user3
end
it "unsubscribe a commentable" do
user2 = User.find_or_create_by(external_id: "2")
delete "/api/v1/users/#{user2.external_id}/subscriptions", subscribed_type: "questions", subscribed_id: "1"
delete "/api/v1/users/#{user2.external_id}/subscriptions", source_type: "questions", source_id: "1"
last_response.should be_ok
Commentable.first.subscribers.length.should == 1
Commentable.first.subscribers.should_not include user2
......@@ -394,7 +395,7 @@ describe "app" do
it "subscribe a comment thread" do
user1 = User.find_or_create_by(external_id: "1")
thread = CommentThread.where(body: "it is unsolvable").first
post "/api/v1/users/#{user1.external_id}/subscriptions", subscribed_type: "thread", subscribed_id: thread.id
post "/api/v1/users/#{user1.external_id}/subscriptions", source_type: "thread", source_id: thread.id
last_response.should be_ok
thread = CommentThread.where(body: "it is unsolvable").first
thread.subscribers.length.should == 2
......@@ -403,7 +404,7 @@ describe "app" do
it "unsubscribe a comment thread" do
user2 = User.find_or_create_by(external_id: "2")
thread = CommentThread.where(body: "it is unsolvable").first
delete "/api/v1/users/#{user2.external_id}/subscriptions", subscribed_type: "thread", subscribed_id: thread.id
delete "/api/v1/users/#{user2.external_id}/subscriptions", source_type: "thread", source_id: thread.id
last_response.should be_ok
thread = CommentThread.where(body: "it is unsolvable").first
thread.subscribers.length.should == 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment