Skip to content

Commit

Permalink
Merge pull request #184 from edx/jia/MA-2419
Browse files Browse the repository at this point in the history
MA-2419:threadPUT - make 'read' mutually exclusive to other fields
  • Loading branch information
wajeeha-khalid committed Jun 2, 2016
2 parents 26adedc + e70e963 commit 8cb5b01
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 37 deletions.
8 changes: 1 addition & 7 deletions api/comment_threads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,10 @@
filter_blocked_content params["body"]
thread.update_attributes(params.slice(*%w[title body pinned closed commentable_id group_id thread_type]))

# user_id is the owner for a thread, requested_user_id is the user requesting to update said thread
if params["requested_user_id"] and value_to_boolean(params["read"])
user = User.only([:id, :username, :read_states]).find_by(external_id: params["requested_user_id"])
user.mark_as_read(thread) if user
end

if thread.errors.any?
error 400, thread.errors.full_messages.to_json
else
presenter = ThreadPresenter.factory(thread, user || nil)
presenter = ThreadPresenter.factory(thread, nil)
presenter.to_hash.to_json
end
end
Expand Down
5 changes: 5 additions & 0 deletions api/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@
user.to_hash.to_json
end
end

post "#{APIPREFIX}/users/:user_id/read" do |user_id|
user.mark_as_read(source)
user.reload.to_hash.to_json
end
31 changes: 1 addition & 30 deletions spec/api/comment_thread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def thread_result(id, params)

before(:each) { init_without_subscriptions }

it "update information of comment thread and don't mark thread as read" do
it "updates information of comment thread" do
thread = CommentThread.first
comment = thread.comments.first
comment.endorsed = true
Expand All @@ -632,35 +632,6 @@ def thread_result(id, params)
comment.endorsement.should == nil
check_unread_thread_result_json(changed_thread, parse(last_response.body))
end
it "update information of comment thread and mark thread as read for owner user" do
thread = CommentThread.first
put "/api/v1/threads/#{thread.id}", body: "new body", title: "new title", commentable_id: "new_commentable_id", thread_type: "question", read: true, requested_user_id: thread.author.id
last_response.should be_ok
changed_thread = CommentThread.find(thread.id)
changed_thread.body.should == "new body"
changed_thread.title.should == "new title"
changed_thread.commentable_id.should == "new_commentable_id"
changed_thread.thread_type.should == "question"
user = User.find_by(external_id: thread.author.id)
json_response = parse(last_response.body)
check_thread_result_json(user, changed_thread, json_response)
json_response["read"].should == true
end
it "update information of comment thread and mark thread as read for non-owner user" do
thread = CommentThread.first
user = create_test_user(42)
put "/api/v1/threads/#{thread.id}", body: "new body", title: "new title", commentable_id: "new_commentable_id", thread_type: "question", read: true, requested_user_id: user.id
last_response.should be_ok
changed_thread = CommentThread.find(thread.id)
changed_thread.body.should == "new body"
changed_thread.title.should == "new title"
changed_thread.commentable_id.should == "new_commentable_id"
changed_thread.thread_type.should == "question"
user = User.find_by(external_id: user.id)
json_response = parse(last_response.body)
check_thread_result_json(user, changed_thread, json_response)
json_response["read"].should == true
end
it "returns 400 when the thread does not exist" do
put "/api/v1/threads/does_not_exist", body: "new body", title: "new title"
last_response.status.should == 400
Expand Down
17 changes: 17 additions & 0 deletions spec/api/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,22 @@ def test_unicode_data(text)

include_examples "unicode data"
end

describe "POST /api/v1/users/:user_id/read" do

before(:each) { setup_10_threads }

it "marks a thread as read for the user" do
thread = @threads["t0"]
user = create_test_user(42)
post "/api/v1/users/#{user.external_id}/read", source_type: "thread", source_id: thread.id
last_response.should be_ok
user.reload
read_states = user.read_states.where(course_id: thread.course_id).to_a
read_date = read_states.first.last_read_times[thread.id.to_s]
read_date.should >= thread.updated_at
end
end

end
end

0 comments on commit 8cb5b01

Please sign in to comment.