Commit 6bdeede0 by Rocky Duan

all necessary apis implemented; untested

parent d5148ade
......@@ -41,7 +41,14 @@ config = YAML.load_file("config/application.yml")
# DELETE /api/v1/votes/comment_threads/:comment_thread_id/users/:user_id
#
# GET /api/v1/users/:user_id/feeds
# POST /api/v1/users/:user_id/feeds/subscribe
# POST /api/v1/users/:user_id/follow
# POST /api/v1/users/:user_id/unfollow
# POST /api/v1/users/:user_id/watch/commentable
# POST /api/v1/users/:user_id/unwatch/commentable
# POST /api/v1/users/:user_id/watch/comment_thread
# POST /api/v1/users/:user_id/unwatch/comment_thread
#
#
#
# DELETE /api/v1/commentables/:commentable_type/:commentable_id
......@@ -182,6 +189,76 @@ delete '/api/v1/votes/comment_threads/:comment_thread_id/users/:user_id' do |com
user.unvote(comment_thread)
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.feeds.to_json
end
# POST /api/v1/users/:user_id/follow
# 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[: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)
followed_user = User.find_or_create_by(external_id: params[:user_id])
user.unfollow(followed_user)
user.to_hash.to_json
end
# POST /api/v1/users/:user_id/watch/commentable
# watch a commentable
post '/api/v1/users/:user_id/watch/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: parasm[:commentable_id])
user.watch_commentable(commentable)
user.to_hash.to_json
end
# POST /api/v1/users/:user_id/unwatch/commentable
# unwatch a commentable
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: parasm[:commentable_id])
user.unwatch_commentable(commentable)
user.to_hash.to_json
end
# POST /api/v1/users/:user_id/watch/comment_thread
# watch a comment thread
post '/api/v1/users/:user_id/watch/comment_thread' do |user_id|
user = User.find_or_create_by(external_id: user_id)
comment_thread = CommentThread.find(params[:comment_thread_id])
user.watch_comment_thread(comment_thread)
user.to_hash.to_json
end
# POST /api/v1/users/:user_id/unwatch/comment_thread
# unwatch a comment thread
post '/api/v1/users/:user_id/unwatch/comment_thread' do |user_id|
user = User.find_or_create_by(external_id: user_id)
comment_thread = CommentThread.find(params[:comment_thread_id])
user.unwatch_comment_thread(comment_thread)
user.to_hash.to_json
end
if env.to_s == "development"
get '/api/v1/clean' do
Comment.delete_all
......
......@@ -19,7 +19,7 @@ class Comment
validates_presence_of :course_id # do we really need this?
#validates_presence_of :author # allow anonymity?
before_destroy :delete_descendants
before_destroy :delete_descendants # TODO async
after_create :generate_feeds
def self.hash_tree(nodes)
......
......@@ -11,7 +11,7 @@ class CommentThread
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true
belongs_to :commentable, index: true
has_many :comments, dependent: :destroy # Use destroy to envoke callback on the top-level comments
has_many :comments, dependent: :destroy # Use destroy to envoke callback on the top-level comments TODO async
has_and_belongs_to_many :watchers, class_name: "User", inverse_of: :watched_comment_threads
attr_accessible :title, :body, :course_id
......
......@@ -19,13 +19,15 @@ class User
index :external_id, unique: true
def follow(user)
if self.id != user.id and not self.following.include? user
self.following << user
if id != user.id and not following.include? user
following << user
save!
end
end
def unfollow(user)
self.following.delete(user)
following.delete(user)
save!
end
def self.watching(class_plural_sym)
......@@ -38,13 +40,15 @@ class User
self.class_eval <<-END
def watch_#{class_single}(watching_object)
if not self.watched_#{class_plural}.include? watching_object
self.watched_#{class_plural} << watching_object
if not watched_#{class_plural}.include? watching_object
watched_#{class_plural} << watching_object
save!
end
end
def unwatch_#{class_single}(watching_object)
self.watched_#{class_plural}.delete(watching_object)
watched_#{class_plural}.delete(watching_object)
save!
end
END
end
......
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