Commit b9a7db7e by Arjun Singh

Refactor / rename a couple tiny things

parent 50a06762
......@@ -48,11 +48,11 @@ put "#{APIPREFIX}/users/:user_id/read_states" do |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_time = read_state.last_read_time.with_indifferent_access.merge({
read_state.last_read_times = read_state.last_read_times.with_indifferent_access.merge({
params["thread_id"] => params["last_read_time"]
})
elsif params["read_states"]
read_state.last_read_time = read_state.last_read_time.with_indifferent_access.merge(params["read_states"])
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?
......
......@@ -16,10 +16,6 @@ helpers do
@comment ||= Comment.find(params[:comment_id])
end
def profile
@profile ||= user.profiles.find_or_create_by(course_id: params["course_id"])
end
def source
@source ||= case params["source_type"]
when "user"
......
......@@ -165,10 +165,13 @@ class CommentThread < Content
if params[:recursive]
doc = doc.merge("children" => root_comments.map{|c| c.to_hash(recursive: true)})
end
comments_count = comments.count
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_time[self.id.to_s]) if read_state
last_read_time = Time.parse(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,
......@@ -187,14 +190,18 @@ class CommentThread < Content
unread_count = self.comments.where(:author_id => {:$ne => params[:user_id]}).count
viewed = false
end
doc = doc.merge("unread_comments_count" => unread_count)
.merge("viewed" => viewed)
else
doc = doc.merge("unread_comments_count" => comments.count)
.merge("viewed" => false)
# If there's no user, say it's not viewed and all comments are unread
unread_count = comments_count
viewed = false
end
doc = doc.merge("comments_count" => comments.count)
doc = doc.merge("unread_comments_count" => unread_count)
.merge("viewed" => viewed)
.merge("comments_count" => comments_count)
doc
end
def self.tag_name_valid?(tag)
......
......@@ -107,7 +107,7 @@ end
class ReadState
include Mongoid::Document
field :course_id, type: String
field :last_read_time, type: Hash, default: {}
field :last_read_times, type: Hash, default: {}
embedded_in :user
validates :course_id, uniqueness: true, presence: true
......
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