Commit 47c2e04a by Rocky Duan

do not use external_id for users; store id directly

parent aa5c0920
...@@ -26,7 +26,7 @@ namespace :test do ...@@ -26,7 +26,7 @@ namespace :test do
commentable = Commentable.create!(commentable_type: "questions", commentable_id: "1") commentable = Commentable.create!(commentable_type: "questions", commentable_id: "1")
user = User.create!(external_id: "1") user = User.create!(id: "1")
comment_thread = commentable.comment_threads.create!(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1") comment_thread = commentable.comment_threads.create!(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1")
comment_thread.author = user comment_thread.author = user
...@@ -78,7 +78,7 @@ namespace :db do ...@@ -78,7 +78,7 @@ namespace :db do
level_limit = YAML.load_file("config/application.yml")["level_limit"] level_limit = YAML.load_file("config/application.yml")["level_limit"]
user = User.create!(external_id: "1") user = User.create!(id: "1")
def generate_comments(commentable_type, commentable_id, level_limit, user) def generate_comments(commentable_type, commentable_id, level_limit, user)
commentable = Commentable.create!(commentable_type: commentable_type, commentable_id: commentable_id) commentable = Commentable.create!(commentable_type: commentable_type, commentable_id: commentable_id)
...@@ -115,7 +115,7 @@ namespace :db do ...@@ -115,7 +115,7 @@ namespace :db do
puts "voting" puts "voting"
users = [] users = []
(1..10).each do |id| (1..10).each do |id|
users << User.find_or_create_by(external_id: id.to_s) users << User.find_or_create_by(id: id.to_s)
end end
CommentThread.all.each do |c| CommentThread.all.each do |c|
......
...@@ -71,7 +71,7 @@ end ...@@ -71,7 +71,7 @@ end
post '/api/v1/commentables/:commentable_type/:commentable_id/comment_threads' do |commentable_type, commentable_id| post '/api/v1/commentables/:commentable_type/:commentable_id/comment_threads' do |commentable_type, commentable_id|
commentable = Commentable.find_or_create_by(commentable_type: commentable_type, commentable_id: commentable_id) commentable = Commentable.find_or_create_by(commentable_type: commentable_type, commentable_id: commentable_id)
comment_thread = commentable.comment_threads.new(params.slice(*%w[title body course_id])) comment_thread = commentable.comment_threads.new(params.slice(*%w[title body course_id]))
comment_thread.author = User.find_or_create_by(external_id: params["user_id"]) comment_thread.author = User.find_or_create_by(id: params["user_id"])
comment_thread.save! comment_thread.save!
comment_thread.to_hash.to_json comment_thread.to_hash.to_json
end end
...@@ -99,7 +99,7 @@ end ...@@ -99,7 +99,7 @@ end
post '/api/v1/comment_threads/:comment_thread_id/comments' do |comment_thread_id| post '/api/v1/comment_threads/:comment_thread_id/comments' do |comment_thread_id|
comment_thread = CommentThread.find(comment_thread_id) comment_thread = CommentThread.find(comment_thread_id)
comment = comment_thread.comments.new(params.slice(*%w[body course_id])) comment = comment_thread.comments.new(params.slice(*%w[body course_id]))
comment.author = User.find_or_create_by(external_id: params["user_id"]) comment.author = User.find_or_create_by(id: params["user_id"])
comment.save! comment.save!
comment.to_hash.to_json comment.to_hash.to_json
end end
...@@ -137,7 +137,7 @@ end ...@@ -137,7 +137,7 @@ end
post '/api/v1/comments/:comment_id' do |comment_id| post '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
sub_comment = comment.children.new(params.slice(*%w[body course_id])) sub_comment = comment.children.new(params.slice(*%w[body course_id]))
sub_comment.author = User.find_or_create_by(external_id: params["user_id"]) sub_comment.author = User.find_or_create_by(id: params["user_id"])
sub_comment.save! sub_comment.save!
sub_comment.to_hash.to_json sub_comment.to_hash.to_json
end end
...@@ -156,7 +156,7 @@ end ...@@ -156,7 +156,7 @@ end
put '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_id| put '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
user.vote(comment, params["value"].intern) user.vote(comment, params["value"].intern)
Comment.find(comment_id).to_hash.to_json Comment.find(comment_id).to_hash.to_json
end end
...@@ -166,7 +166,7 @@ end ...@@ -166,7 +166,7 @@ end
delete '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_id| delete '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
user.unvote(comment) user.unvote(comment)
Comment.find(comment_id).to_hash.to_json Comment.find(comment_id).to_hash.to_json
end end
...@@ -176,7 +176,7 @@ end ...@@ -176,7 +176,7 @@ end
put '/api/v1/votes/comment_threads/:comment_thread_id/users/:user_id' do |comment_thread_id, user_id| put '/api/v1/votes/comment_threads/:comment_thread_id/users/:user_id' do |comment_thread_id, user_id|
comment_thread = CommentThread.find(comment_thread_id) comment_thread = CommentThread.find(comment_thread_id)
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
user.vote(comment_thread, params["value"].intern) user.vote(comment_thread, params["value"].intern)
CommentThread.find(comment_thread_id).to_hash.to_json CommentThread.find(comment_thread_id).to_hash.to_json
end end
...@@ -186,7 +186,7 @@ end ...@@ -186,7 +186,7 @@ end
delete '/api/v1/votes/comment_threads/:comment_thread_id/users/:user_id' do |comment_thread_id, user_id| delete '/api/v1/votes/comment_threads/:comment_thread_id/users/:user_id' do |comment_thread_id, user_id|
comment_thread = CommentThread.find(comment_thread_id) comment_thread = CommentThread.find(comment_thread_id)
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
user.unvote(comment_thread) user.unvote(comment_thread)
CommentThread.find(comment_thread_id).to_hash.to_json CommentThread.find(comment_thread_id).to_hash.to_json
end end
...@@ -195,7 +195,7 @@ end ...@@ -195,7 +195,7 @@ end
# get all subscribed feeds for the user # get all subscribed feeds for the user
get '/api/v1/users/:user_id/feeds' do |user_id| get '/api/v1/users/:user_id/feeds' do |user_id|
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
user.feeds.map(&:to_hash).to_json user.feeds.map(&:to_hash).to_json
end end
...@@ -203,8 +203,8 @@ end ...@@ -203,8 +203,8 @@ end
# follow user # follow user
post '/api/v1/users/:user_id/follow' do |user_id| post '/api/v1/users/:user_id/follow' do |user_id|
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
followed_user = User.find_or_create_by(external_id: params[:user_id]) followed_user = User.find_or_create_by(id: params[:user_id])
user.follow(followed_user) user.follow(followed_user)
user.to_hash.to_json user.to_hash.to_json
end end
...@@ -213,8 +213,8 @@ end ...@@ -213,8 +213,8 @@ end
# unfollow user # unfollow user
post '/api/v1/users/:user_id/unfollow' do |user_id| post '/api/v1/users/:user_id/unfollow' do |user_id|
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
followed_user = User.find_or_create_by(external_id: params[:user_id]) followed_user = User.find_or_create_by(id: params[:user_id])
user.unfollow(followed_user) user.unfollow(followed_user)
user.to_hash.to_json user.to_hash.to_json
end end
...@@ -223,7 +223,7 @@ end ...@@ -223,7 +223,7 @@ end
# watch a commentable # watch a commentable
post '/api/v1/users/:user_id/watch/commentable' do |user_id| post '/api/v1/users/:user_id/watch/commentable' do |user_id|
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
commentable = Commentable.find_or_create_by(commentable_type: params[:commentable_type], commentable = Commentable.find_or_create_by(commentable_type: params[:commentable_type],
commentable_id: parasm[:commentable_id]) commentable_id: parasm[:commentable_id])
user.watch_commentable(commentable) user.watch_commentable(commentable)
...@@ -234,7 +234,7 @@ end ...@@ -234,7 +234,7 @@ end
# unwatch a commentable # unwatch a commentable
post '/api/v1/users/:user_id/unwatch/commentable' do |user_id| post '/api/v1/users/:user_id/unwatch/commentable' do |user_id|
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
commentable = Commentable.find_or_create_by(commentable_type: params[:commentable_type], commentable = Commentable.find_or_create_by(commentable_type: params[:commentable_type],
commentable_id: parasm[:commentable_id]) commentable_id: parasm[:commentable_id])
user.unwatch_commentable(commentable) user.unwatch_commentable(commentable)
...@@ -245,7 +245,7 @@ end ...@@ -245,7 +245,7 @@ end
# watch a comment thread # watch a comment thread
post '/api/v1/users/:user_id/watch/comment_thread' do |user_id| post '/api/v1/users/:user_id/watch/comment_thread' do |user_id|
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
comment_thread = CommentThread.find(params[:comment_thread_id]) comment_thread = CommentThread.find(params[:comment_thread_id])
user.watch_comment_thread(comment_thread) user.watch_comment_thread(comment_thread)
user.to_hash.to_json user.to_hash.to_json
...@@ -255,7 +255,7 @@ end ...@@ -255,7 +255,7 @@ end
# unwatch a comment thread # unwatch a comment thread
post '/api/v1/users/:user_id/unwatch/comment_thread' do |user_id| post '/api/v1/users/:user_id/unwatch/comment_thread' do |user_id|
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(id: user_id)
comment_thread = CommentThread.find(params[:comment_thread_id]) comment_thread = CommentThread.find(params[:comment_thread_id])
user.unwatch_comment_thread(comment_thread) user.unwatch_comment_thread(comment_thread)
user.to_hash.to_json user.to_hash.to_json
......
...@@ -47,7 +47,7 @@ class Comment ...@@ -47,7 +47,7 @@ class Comment
self.class.hash_tree(subtree(sort: sort_by_parent_and_time)).first self.class.hash_tree(subtree(sort: sort_by_parent_and_time)).first
else else
as_document.slice(*%w[body course_id endorsed _id]). as_document.slice(*%w[body course_id endorsed _id]).
merge("user_id" => author.external_id). merge("user_id" => author.id).
merge("votes" => votes.slice(*%w[count up_count down_count point])) merge("votes" => votes.slice(*%w[count up_count down_count point]))
end end
end end
......
...@@ -25,7 +25,7 @@ class CommentThread ...@@ -25,7 +25,7 @@ class CommentThread
def to_hash(params={}) def to_hash(params={})
doc = as_document.slice(*%w[title body course_id _id]). doc = as_document.slice(*%w[title body course_id _id]).
merge("user_id" => author.external_id). merge("user_id" => author.id).
merge("votes" => votes.slice(*%w[count up_count down_count point])) merge("votes" => votes.slice(*%w[count up_count down_count point]))
if params[:recursive] if params[:recursive]
doc = doc.merge("children" => comments.map{|c| c.to_hash(recursive: true)}) doc = doc.merge("children" => comments.map{|c| c.to_hash(recursive: true)})
......
...@@ -2,7 +2,7 @@ class User ...@@ -2,7 +2,7 @@ class User
include Mongoid::Document include Mongoid::Document
include Mongo::Voter include Mongo::Voter
field :external_id, type: String identity type: String
has_many :comments has_many :comments
has_many :comment_threads, inverse_of: :author has_many :comment_threads, inverse_of: :author
...@@ -11,15 +11,8 @@ class User ...@@ -11,15 +11,8 @@ class User
has_and_belongs_to_many :followers, class_name: "User", inverse_of: :followings has_and_belongs_to_many :followers, class_name: "User", inverse_of: :followings
has_and_belongs_to_many :followings, class_name: "User", inverse_of: :followers has_and_belongs_to_many :followings, class_name: "User", inverse_of: :followers
attr_accessible :external_id
validates_uniqueness_of :external_id
validates_presence_of :external_id
index :external_id, unique: true
def to_hash(params={}) def to_hash(params={})
as_document.slice(*%w[_id external_id]) as_document.slice(*%w[_id])
end end
def follow(user) def follow(user)
......
...@@ -18,7 +18,7 @@ def init_without_feeds ...@@ -18,7 +18,7 @@ def init_without_feeds
commentable = Commentable.new(commentable_type: "questions", commentable_id: "1") commentable = Commentable.new(commentable_type: "questions", commentable_id: "1")
commentable.save! commentable.save!
user = User.create!(external_id: "1") user = User.create!(id: "1")
comment_thread = commentable.comment_threads.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1") comment_thread = commentable.comment_threads.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1")
comment_thread.author = user comment_thread.author = user
...@@ -58,7 +58,7 @@ def init_without_feeds ...@@ -58,7 +58,7 @@ def init_without_feeds
comment1.author = user comment1.author = user
comment1.save! comment1.save!
users = (2..10).map{|id| User.find_or_create_by(external_id: id.to_s)} users = (2..10).map{|id| User.find_or_create_by(id: id.to_s)}
Comment.all.each do |c| Comment.all.each do |c|
user.vote(c, :up) # make the first user always vote up for convenience user.vote(c, :up) # make the first user always vote up for convenience
...@@ -163,13 +163,13 @@ describe "app" do ...@@ -163,13 +163,13 @@ describe "app" do
it "create a comment to the comment thread" do it "create a comment to the comment thread" do
comment_thread = CommentThread.first.to_hash(recursive: true) comment_thread = CommentThread.first.to_hash(recursive: true)
user = User.first user = User.first
post "/api/v1/comment_threads/#{comment_thread["_id"]}/comments", body: "new comment", course_id: "1", user_id: User.first.external_id post "/api/v1/comment_threads/#{comment_thread["_id"]}/comments", body: "new comment", course_id: "1", user_id: User.first.id
last_response.should be_ok last_response.should be_ok
changed_thread = CommentThread.find(comment_thread["_id"]).to_hash(recursive: true) changed_thread = CommentThread.find(comment_thread["_id"]).to_hash(recursive: true)
changed_thread["children"].length.should == comment_thread["children"].length + 1 changed_thread["children"].length.should == comment_thread["children"].length + 1
comment = changed_thread["children"].select{|c| c["body"] == "new comment"}.first comment = changed_thread["children"].select{|c| c["body"] == "new comment"}.first
comment.should_not be_nil comment.should_not be_nil
comment["user_id"].should == user.external_id comment["user_id"].should == user.id
end end
end end
describe "DELETE /api/v1/comment_threads/:comment_thread_id" do describe "DELETE /api/v1/comment_threads/:comment_thread_id" do
...@@ -223,13 +223,13 @@ describe "app" do ...@@ -223,13 +223,13 @@ describe "app" do
it "create a sub comment to the comment" do it "create a sub comment to the comment" do
comment = Comment.first.to_hash(recursive: true) comment = Comment.first.to_hash(recursive: true)
user = User.first user = User.first
post "/api/v1/comments/#{comment["_id"]}", body: "new comment", course_id: "1", user_id: User.first.external_id post "/api/v1/comments/#{comment["_id"]}", body: "new comment", course_id: "1", user_id: User.first.id
last_response.should be_ok last_response.should be_ok
changed_comment = Comment.find(comment["_id"]).to_hash(recursive: true) changed_comment = Comment.find(comment["_id"]).to_hash(recursive: true)
changed_comment["children"].length.should == comment["children"].length + 1 changed_comment["children"].length.should == comment["children"].length + 1
subcomment = changed_comment["children"].select{|c| c["body"] == "new comment"}.first subcomment = changed_comment["children"].select{|c| c["body"] == "new comment"}.first
subcomment.should_not be_nil subcomment.should_not be_nil
subcomment["user_id"].should == user.external_id subcomment["user_id"].should == user.id
end end
end end
describe "DELETE /api/v1/comments/:comment_id" do describe "DELETE /api/v1/comments/:comment_id" do
...@@ -251,7 +251,7 @@ describe "app" do ...@@ -251,7 +251,7 @@ describe "app" do
comment = Comment.first comment = Comment.first
prev_up_votes = comment.up_votes_count prev_up_votes = comment.up_votes_count
prev_down_votes = comment.down_votes_count prev_down_votes = comment.down_votes_count
put "/api/v1/votes/comments/#{comment.id}/users/#{user.external_id}", value: "down" put "/api/v1/votes/comments/#{comment.id}/users/#{user.id}", value: "down"
comment = Comment.find(comment.id) comment = Comment.find(comment.id)
comment.up_votes_count.should == prev_up_votes - 1 comment.up_votes_count.should == prev_up_votes - 1
comment.down_votes_count.should == prev_down_votes + 1 comment.down_votes_count.should == prev_down_votes + 1
...@@ -263,7 +263,7 @@ describe "app" do ...@@ -263,7 +263,7 @@ describe "app" do
comment = Comment.first comment = Comment.first
prev_up_votes = comment.up_votes_count prev_up_votes = comment.up_votes_count
prev_down_votes = comment.down_votes_count prev_down_votes = comment.down_votes_count
delete "/api/v1/votes/comments/#{comment.id}/users/#{user.external_id}" delete "/api/v1/votes/comments/#{comment.id}/users/#{user.id}"
comment = Comment.find(comment.id) comment = Comment.find(comment.id)
comment.up_votes_count.should == prev_up_votes - 1 comment.up_votes_count.should == prev_up_votes - 1
comment.down_votes_count.should == prev_down_votes comment.down_votes_count.should == prev_down_votes
...@@ -275,7 +275,7 @@ describe "app" do ...@@ -275,7 +275,7 @@ describe "app" do
comment_thread = CommentThread.first comment_thread = CommentThread.first
prev_up_votes = comment_thread.up_votes_count prev_up_votes = comment_thread.up_votes_count
prev_down_votes = comment_thread.down_votes_count prev_down_votes = comment_thread.down_votes_count
put "/api/v1/votes/comment_threads/#{comment_thread.id}/users/#{user.external_id}", value: "down" put "/api/v1/votes/comment_threads/#{comment_thread.id}/users/#{user.id}", value: "down"
comment_thread = CommentThread.find(comment_thread.id) comment_thread = CommentThread.find(comment_thread.id)
comment_thread.up_votes_count.should == prev_up_votes - 1 comment_thread.up_votes_count.should == prev_up_votes - 1
comment_thread.down_votes_count.should == prev_down_votes + 1 comment_thread.down_votes_count.should == prev_down_votes + 1
...@@ -287,7 +287,7 @@ describe "app" do ...@@ -287,7 +287,7 @@ describe "app" do
comment_thread = CommentThread.first comment_thread = CommentThread.first
prev_up_votes = comment_thread.up_votes_count prev_up_votes = comment_thread.up_votes_count
prev_down_votes = comment_thread.down_votes_count prev_down_votes = comment_thread.down_votes_count
delete "/api/v1/votes/comment_threads/#{comment_thread.id}/users/#{user.external_id}" delete "/api/v1/votes/comment_threads/#{comment_thread.id}/users/#{user.id}"
comment_thread = CommentThread.find(comment_thread.id) comment_thread = CommentThread.find(comment_thread.id)
comment_thread.up_votes_count.should == prev_up_votes - 1 comment_thread.up_votes_count.should == prev_up_votes - 1
comment_thread.down_votes_count.should == prev_down_votes comment_thread.down_votes_count.should == prev_down_votes
......
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