Commit 81b54db0 by Rocky Duan

major change in urls and naming

parent bc407596
...@@ -27,7 +27,7 @@ namespace :test do ...@@ -27,7 +27,7 @@ namespace :test do
CommentThread.delete_all CommentThread.delete_all
Commentable.delete_all Commentable.delete_all
User.delete_all User.delete_all
Feed.delete_all Notification.delete_all
commentable = Commentable.create!(commentable_type: "questions", commentable_id: "1") commentable = Commentable.create!(commentable_type: "questions", commentable_id: "1")
...@@ -67,7 +67,7 @@ namespace :db do ...@@ -67,7 +67,7 @@ namespace :db do
CommentThread.create_indexes CommentThread.create_indexes
User.create_indexes User.create_indexes
Commentable.create_indexes Commentable.create_indexes
Feed.create_indexes Notification.create_indexes
Delayed::Backend::Mongoid::Job.create_indexes Delayed::Backend::Mongoid::Job.create_indexes
puts "finished" puts "finished"
end end
......
...@@ -19,26 +19,17 @@ Mongoid.logger.level = Logger::INFO ...@@ -19,26 +19,17 @@ Mongoid.logger.level = Logger::INFO
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file} Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
# DELETE /api/v1/commentables/:commentable_type/:commentable_id delete '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, commentable_id|
# delete the commentable object and all of its associated comment threads and comments
delete '/api/v1/:commentable_type/:commentable_id/comments' do |commentable_type, commentable_id|
commentable = Commentable.find_or_initialize_by(commentable_type: commentable_type, commentable_id: commentable_id) commentable = Commentable.find_or_initialize_by(commentable_type: commentable_type, commentable_id: commentable_id)
commentable.destroy commentable.destroy
commentable.to_hash.to_json commentable.to_hash.to_json
end end
# GET /api/v1/commentables/:commentable_type/:commentable_id/threads
# get all comment threads associated with a commentable object
# additional parameters accepted: recursive
get '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, commentable_id| get '/api/v1/:commentable_type/:commentable_id/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)
commentable.comment_threads.map{|t| t.to_hash(recursive: params["recursive"])}.to_json commentable.comment_threads.map{|t| t.to_hash(recursive: params["recursive"])}.to_json
end end
# POST /api/v1/commentables/:commentable_type/:commentable_id/threads
# create a new comment thread for the commentable object
post '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, commentable_id| post '/api/v1/:commentable_type/:commentable_id/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)
thread = commentable.comment_threads.new(params.slice(*%w[title body course_id])) thread = commentable.comment_threads.new(params.slice(*%w[title body course_id]))
...@@ -47,26 +38,17 @@ post '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, c ...@@ -47,26 +38,17 @@ post '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, c
thread.to_hash.to_json thread.to_hash.to_json
end end
# GET /api/v1/threads/:thread_id
# get information of a single comment thread
# additional parameters accepted: recursive
get '/api/v1/threads/:thread_id' do |thread_id| get '/api/v1/threads/:thread_id' do |thread_id|
thread = CommentThread.find(thread_id) thread = CommentThread.find(thread_id)
thread.to_hash(recursive: params["recursive"]).to_json thread.to_hash(recursive: params["recursive"]).to_json
end end
# PUT /api/v1/threads/:thread_id
# update information of comment thread
put '/api/v1/threads/:thread_id' do |thread_id| put '/api/v1/threads/:thread_id' do |thread_id|
thread = CommentThread.find(thread_id) thread = CommentThread.find(thread_id)
thread.update_attributes!(params.slice(*%w[title body])) thread.update_attributes!(params.slice(*%w[title body]))
thread.to_hash.to_json thread.to_hash.to_json
end end
# POST /api/v1/threads/:thread_id/comments
# create a comment to the comment thread
post '/api/v1/threads/:thread_id/comments' do |thread_id| post '/api/v1/threads/:thread_id/comments' do |thread_id|
thread = CommentThread.find(thread_id) thread = CommentThread.find(thread_id)
comment = thread.comments.new(params.slice(*%w[body course_id])) comment = thread.comments.new(params.slice(*%w[body course_id]))
...@@ -75,36 +57,23 @@ post '/api/v1/threads/:thread_id/comments' do |thread_id| ...@@ -75,36 +57,23 @@ post '/api/v1/threads/:thread_id/comments' do |thread_id|
comment.to_hash.to_json comment.to_hash.to_json
end end
# DELETE /api/v1/threads/:thread_id
# delete the comment thread and its comments
delete '/api/v1/threads/:thread_id' do |thread_id| delete '/api/v1/threads/:thread_id' do |thread_id|
thread = CommentThread.find(thread_id) thread = CommentThread.find(thread_id)
thread.destroy thread.destroy
thread.to_hash.to_json thread.to_hash.to_json
end end
# GET /api/v1/comments/:comment_id
# retrieve information of a single comment
# additional parameters accepted: recursive
get '/api/v1/comments/:comment_id' do |comment_id| get '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
comment.to_hash(recursive: params["recursive"]).to_json comment.to_hash(recursive: params["recursive"]).to_json
end end
# PUT /api/v1/comments/:comment_id
# update information of the comment
put '/api/v1/comments/:comment_id' do |comment_id| put '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
comment.update_attributes!(params.slice(*%w[body endorsed])) comment.update_attributes!(params.slice(*%w[body endorsed]))
comment.to_hash.to_json comment.to_hash.to_json
end end
# POST /api/v1/comments/:comment_id
# create a sub comment to the comment
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]))
...@@ -113,123 +82,61 @@ post '/api/v1/comments/:comment_id' do |comment_id| ...@@ -113,123 +82,61 @@ post '/api/v1/comments/:comment_id' do |comment_id|
sub_comment.to_hash.to_json sub_comment.to_hash.to_json
end end
# DELETE /api/v1/comments/:comment_id
# delete the comment and its sub comments
delete '/api/v1/comments/:comment_id' do |comment_id| delete '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id) comment = Comment.find(comment_id)
comment.destroy comment.destroy
comment.to_hash.to_json comment.to_hash.to_json
end end
# PUT /api/v1/votes/comments/:comment_id/users/:user_id put '/api/v1/comments/:comment_id/votes' do |comment_id|
# create or update the vote on the comment
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) handle_vote_for comment
user.vote(comment, params["value"].intern)
Comment.find(comment_id).to_hash.to_json
end end
# DELETE /api/v1/votes/comments/:comment_id/users/:user_id delete '/api/v1/comments/:comment_id/votes' do |comment_id|
# unvote on the comment
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) handle_unvote_for comment
user.unvote(comment)
Comment.find(comment_id).to_hash.to_json
end end
# PUT /api/v1/votes/threads/:thread_id/users/:user_id put '/api/v1/threads/:thread_id/votes' do |thread_id|
# create or update the vote on the comment thread
put '/api/v1/votes/threads/:thread_id/users/:user_id' do |thread_id, user_id|
thread = CommentThread.find(thread_id) thread = CommentThread.find(thread_id)
user = User.find_or_create_by(external_id: user_id) handle_vote_for thread
user.vote(thread, params["value"].intern)
CommentThread.find(thread_id).to_hash.to_json
end end
# DELETE /api/v1/votes/threads/:thread_id/users/:user_id delete '/api/v1/threads/:thread_id/votes' do |thread_id|
# unvote on the comment thread
delete '/api/v1/votes/threads/:thread_id/users/:user_id' do |thread_id, user_id|
thread = CommentThread.find(thread_id) thread = CommentThread.find(thread_id)
user = User.find_or_create_by(external_id: user_id) handle_unvote_for thread
user.unvote(thread)
CommentThread.find(thread_id).to_hash.to_json
end
# GET /api/v1/users/:user_id/feeds
# get all subscribed feeds for the user
get '/api/v1/users/:user_id/feeds' do |user_id|
user = User.find_or_create_by(external_id: user_id)
user.subscribed_feeds.map(&:to_hash).to_json
end end
# POST /api/v1/users/:user_id/follow get '/api/v1/users/:user_id/notifications' do |user_id|
# follow user
post '/api/v1/users/:user_id/follow' do |user_id|
user = User.find_or_create_by(external_id: user_id)
followed_user = User.find_or_create_by(external_id: params["follow_user_id"])
user.follow(followed_user)
user.to_hash.to_json
end
# POST /api/v1/users/:user_id/unfollow
# unfollow user
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(external_id: user_id)
followed_user = User.find_or_create_by(external_id: params["follow_user_id"]) user.notifications.map(&:to_hash).to_json
user.unfollow(followed_user)
user.to_hash.to_json
end end
# POST /api/v1/users/:user_id/watch/commentable post '/api/v1/users/:user_id/subscriptions' do |user_id|
# watch a commentable
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(external_id: user_id)
commentable = Commentable.find_or_create_by(commentable_type: params[:commentable_type], case params["subscribed_type"]
commentable_id: params[:commentable_id]) when "user"
user.watch_commentable(commentable) user.follow(User.find_or_create_by(external_id: params["subscribed_id"]))
user.to_hash.to_json when "thread"
end user.watch_comment_thread(CommentThread.find(params["subscribed_id"]))
else
# POST /api/v1/users/:user_id/unwatch/commentable user.watch_commentable(Commentable.find_or_create_by(commentable_type: params["subscribed_type"], commentable_id: params["subscribed_id"]))
# unwatch a commentable end
user.reload.to_hash.to_json
post '/api/v1/users/:user_id/unwatch/commentable' do |user_id|
user = User.find_or_create_by(external_id: user_id)
commentable = Commentable.find_or_create_by(commentable_type: params["commentable_type"],
commentable_id: params["commentable_id"])
user.unwatch_commentable(commentable)
user.to_hash.to_json
end
# POST /api/v1/users/:user_id/watch/thread
# watch a comment thread
post '/api/v1/users/:user_id/watch/thread' do |user_id|
user = User.find_or_create_by(external_id: user_id)
thread = CommentThread.find(params["thread_id"])
user.watch_comment_thread(thread)
user.to_hash.to_json
end end
# POST /api/v1/users/:user_id/unwatch/thread delete '/api/v1/users/:user_id/subscriptions' do |user_id|
# unwatch a comment thread
post '/api/v1/users/:user_id/unwatch/thread' do |user_id|
user = User.find_or_create_by(external_id: user_id) user = User.find_or_create_by(external_id: user_id)
thread = CommentThread.find(params["thread_id"]) case params["subscribed_type"]
user.unwatch_comment_thread(thread) when "user"
user.to_hash.to_json user.unfollow(User.find_or_create_by(external_id: params["subscribed_id"]))
when "thread"
user.unwatch_comment_thread(CommentThread.find(params["subscribed_id"]))
else
user.unwatch_commentable(Commentable.find_or_create_by(commentable_type: params["subscribed_type"], commentable_id: params["subscribed_id"]))
end
user.reload.to_hash.to_json
end end
if env.to_s == "development" if env.to_s == "development"
...@@ -238,7 +145,19 @@ if env.to_s == "development" ...@@ -238,7 +145,19 @@ if env.to_s == "development"
CommentThread.delete_all CommentThread.delete_all
Commentable.delete_all Commentable.delete_all
User.delete_all User.delete_all
Feed.delete_all Notification.delete_all
{}.to_json {}.to_json
end end
end end
def handle_vote_for(obj)
user = User.find_or_create_by(external_id: params["user_id"])
user.vote(obj, params["value"].to_sym)
obj.reload.to_hash.to_json
end
def handle_unvote_for(obj)
user = User.find_or_create_by(external_id: params["user_id"])
user.unvote(obj)
obj.reload.to_hash.to_json
end
...@@ -53,21 +53,21 @@ class Comment ...@@ -53,21 +53,21 @@ class Comment
end end
private private
def generate_feeds def generate_notifications
if get_comment_thread.watchers or (author.followers if author) if get_comment_thread.watchers or (author.followers if author)
feed = Feed.new( notification = Notification.new(
feed_type: "post_reply", notification_type: "post_reply",
info: { info: {
thread_id: get_comment_thread.id, thread_id: get_comment_thread.id,
thread_title: get_comment_thread.title, thread_title: get_comment_thread.title,
comment_id: id, comment_id: id,
}, },
) )
feed.actor = author notification.actor = author
feed.target = self notification.target = self
feed.subscribers << (get_comment_thread.watchers + author.followers).uniq_by(&:id) notification.receivers << (get_comment_thread.watchers + author.followers).uniq_by(&:id)
feed.subscribers.delete(author) if not CommentService.config["send_notifications_to_author"] notification.receivers.delete(author) if not CommentService.config["send_notifications_to_author"]
feed.save! notification.save!
end end
end end
...@@ -78,7 +78,7 @@ private ...@@ -78,7 +78,7 @@ private
end end
def handle_after_create def handle_after_create
generate_feeds generate_notifications
auto_watch_comment_thread auto_watch_comment_thread
end end
......
...@@ -34,10 +34,10 @@ class CommentThread ...@@ -34,10 +34,10 @@ class CommentThread
end end
private private
def generate_feeds def generate_notifications
if watchers or (author.followers if author) if watchers or (author.followers if author)
feed = Feed.new( notification = Notification.new(
feed_type: "post_topic", notification_type: "post_topic",
info: { info: {
commentable_id: commentable.commentable_id, commentable_id: commentable.commentable_id,
commentable_type: commentable.commentable_type, commentable_type: commentable.commentable_type,
...@@ -45,11 +45,11 @@ private ...@@ -45,11 +45,11 @@ private
thread_title: title, thread_title: title,
}, },
) )
feed.actor = author notification.actor = author
feed.target = self notification.target = self
feed.subscribers << (commentable.watchers + (author.followers if author).to_a).uniq_by(&:id) notification.receivers << (commentable.watchers + (author.followers if author).to_a).uniq_by(&:id)
feed.subscribers.delete(author) if not CommentService.config["send_notifications_to_author"] and author notification.receivers.delete(author) if not CommentService.config["send_notifications_to_author"] and author
feed.save! notification.save!
end end
end end
...@@ -60,7 +60,7 @@ private ...@@ -60,7 +60,7 @@ private
end end
def handle_after_create def handle_after_create
generate_feeds generate_notifications
auto_watch_comment_thread auto_watch_comment_thread
end end
......
class Feed class Notification
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
field :feed_type, type: String field :notification_type, type: String
field :info, type: Hash field :info, type: Hash
belongs_to :actor, class_name: "User", inverse_of: :activities, index: true, autosave: true belongs_to :actor, class_name: "User", inverse_of: :activities, index: true, autosave: true
belongs_to :target, inverse_of: :activities, polymorphic: true, autosave: true belongs_to :target, inverse_of: :activities, polymorphic: true, autosave: true
attr_accessible :feed_type, :info attr_accessible :notification_type, :info
validates_presence_of :feed_type validates_presence_of :notification_type
if not CommentService.config["allow_anonymity"] if not CommentService.config["allow_anonymity"]
validates_presence_of :actor validates_presence_of :actor
end end
validates_presence_of :target validates_presence_of :target
has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_feeds, autosave: true has_and_belongs_to_many :receivers, class_name: "User", inverse_of: :notifications, autosave: true
def to_hash(params={}) def to_hash(params={})
as_document.slice(*%w[_id feed_type info actor target]) as_document.slice(*%w[_id notification_type info actor target])
end end
end end
...@@ -6,8 +6,8 @@ class User ...@@ -6,8 +6,8 @@ class User
has_many :comments has_many :comments
has_many :comment_threads, inverse_of: :author has_many :comment_threads, inverse_of: :author
has_many :activities, class_name: "Feed", inverse_of: :actor has_many :activities, class_name: "Notification", inverse_of: :actor
has_and_belongs_to_many :subscribed_feeds, class_name: "Feed", inverse_of: :subscribers has_and_belongs_to_many :notifications, inverse_of: :receivers
has_and_belongs_to_many :followers, class_name: "User", inverse_of: :followings, autosave: true has_and_belongs_to_many :followers, class_name: "User", inverse_of: :followings, autosave: true
has_and_belongs_to_many :followings, class_name: "User", inverse_of: :followers#, autosave: true has_and_belongs_to_many :followings, class_name: "User", inverse_of: :followers#, autosave: 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