Commit 96fb9e26 by Arjun Singh

Alter service to allow marking as read when a thread is fetched

parent 6f61a891
......@@ -3,7 +3,14 @@ get "#{APIPREFIX}/threads" do # retrieve threads by course
end
get "#{APIPREFIX}/threads/:thread_id" do |thread_id|
CommentThread.find(thread_id).to_hash(recursive: bool_recursive, user_id: params["user_id"]).to_json
thread = CommentThread.find(thread_id)
if params["user_id"] and bool_mark_as_read
user = User.only([:id, :read_states]).find_or_create_by(external_id: params["user_id"])
user.mark_as_read(thread)
end
thread.to_hash(recursive: bool_recursive, user_id: params["user_id"]).to_json
end
put "#{APIPREFIX}/threads/:thread_id" do |thread_id|
......
......@@ -43,25 +43,6 @@ get "#{APIPREFIX}/users/:user_id/active_threads" do |user_id|
end
put "#{APIPREFIX}/users/:user_id/read_states" do |user_id|
user = User.find_or_create_by(external_id: user_id)
read_state = user.read_states.find_or_create_by(course_id: params["course_id"])
# support updating single thread data or bulk update
if params["last_read_time"] and params["thread_id"]
read_state.last_read_times = read_state.last_read_times.with_indifferent_access.merge({
params["thread_id"] => params["last_read_time"]
})
elsif params["last_read_times"]
read_state.last_read_times = read_state.last_read_times.with_indifferent_access.merge(params["last_read_times"])
end
read_state.save
if read_state.errors.any?
error 400, read_state.errors.full_messages.to_json
else
read_state.to_hash.to_json
end
end
put "#{APIPREFIX}/users/:user_id" do |user_id|
user = User.find_or_create_by(external_id: user_id)
user.update_attributes(params.slice(*%w[username email default_sort_key]))
......
......@@ -51,6 +51,10 @@ helpers do
value_to_boolean params["recursive"]
end
def bool_mark_as_read
value_to_boolean params["mark_as_read"]
end
def bool_complete
value_to_boolean params["complete"]
end
......
......@@ -171,7 +171,7 @@ class CommentThread < Content
if params[:user_id]
user = User.find_or_create_by(external_id: params[:user_id])
read_state = user.read_states.where(course_id: self.course_id).first
last_read_time = Time.parse(read_state.last_read_times[self.id.to_s]) if read_state
last_read_time = read_state.last_read_times[self.id.to_s] if read_state
# comments created by the user are excluded in the count
# this is rather like a hack but it avoids the following situation:
# when you reply to a thread and while you are editing,
......
......@@ -102,6 +102,12 @@ class User
subscription
end
def mark_as_read(thread)
read_state = read_states.find_or_create_by(course_id: thread.course_id)
read_state.last_read_times[thread.id] = Time.now.utc
read_state.save
end
end
class ReadState
......
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