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| ...@@ -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"]) read_state = user.read_states.find_or_create_by(course_id: params["course_id"])
# support updating single thread data or bulk update # support updating single thread data or bulk update
if params["last_read_time"] and params["thread_id"] 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"] params["thread_id"] => params["last_read_time"]
}) })
elsif params["read_states"] elsif params["last_read_times"]
read_state.last_read_time = read_state.last_read_time.with_indifferent_access.merge(params["read_states"]) read_state.last_read_times = read_state.last_read_times.with_indifferent_access.merge(params["last_read_times"])
end end
read_state.save read_state.save
if read_state.errors.any? if read_state.errors.any?
......
...@@ -16,10 +16,6 @@ helpers do ...@@ -16,10 +16,6 @@ helpers do
@comment ||= Comment.find(params[:comment_id]) @comment ||= Comment.find(params[:comment_id])
end end
def profile
@profile ||= user.profiles.find_or_create_by(course_id: params["course_id"])
end
def source def source
@source ||= case params["source_type"] @source ||= case params["source_type"]
when "user" when "user"
......
...@@ -165,10 +165,13 @@ class CommentThread < Content ...@@ -165,10 +165,13 @@ class CommentThread < Content
if params[:recursive] if params[:recursive]
doc = doc.merge("children" => root_comments.map{|c| c.to_hash(recursive: true)}) doc = doc.merge("children" => root_comments.map{|c| c.to_hash(recursive: true)})
end end
comments_count = comments.count
if params[:user_id] if params[:user_id]
user = User.find_or_create_by(external_id: 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 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 # comments created by the user are excluded in the count
# this is rather like a hack but it avoids the following situation: # this is rather like a hack but it avoids the following situation:
# when you reply to a thread and while you are editing, # when you reply to a thread and while you are editing,
...@@ -187,14 +190,18 @@ class CommentThread < Content ...@@ -187,14 +190,18 @@ class CommentThread < Content
unread_count = self.comments.where(:author_id => {:$ne => params[:user_id]}).count unread_count = self.comments.where(:author_id => {:$ne => params[:user_id]}).count
viewed = false viewed = false
end end
doc = doc.merge("unread_comments_count" => unread_count)
.merge("viewed" => viewed)
else else
doc = doc.merge("unread_comments_count" => comments.count) # If there's no user, say it's not viewed and all comments are unread
.merge("viewed" => false) unread_count = comments_count
viewed = false
end 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 doc
end end
def self.tag_name_valid?(tag) def self.tag_name_valid?(tag)
......
...@@ -107,7 +107,7 @@ end ...@@ -107,7 +107,7 @@ end
class ReadState class ReadState
include Mongoid::Document include Mongoid::Document
field :course_id, type: String field :course_id, type: String
field :last_read_time, type: Hash, default: {} field :last_read_times, type: Hash, default: {}
embedded_in :user embedded_in :user
validates :course_id, uniqueness: true, presence: true 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