Commit 5f4bd140 by Rocky Duan

adjust naming to notification / subscription

parent 81b54db0
......@@ -119,9 +119,9 @@ post '/api/v1/users/:user_id/subscriptions' do |user_id|
when "user"
user.follow(User.find_or_create_by(external_id: params["subscribed_id"]))
when "thread"
user.watch_comment_thread(CommentThread.find(params["subscribed_id"]))
user.subscribe_comment_thread(CommentThread.find(params["subscribed_id"]))
else
user.watch_commentable(Commentable.find_or_create_by(commentable_type: params["subscribed_type"], commentable_id: params["subscribed_id"]))
user.subscribe_commentable(Commentable.find_or_create_by(commentable_type: params["subscribed_type"], commentable_id: params["subscribed_id"]))
end
user.reload.to_hash.to_json
end
......@@ -132,9 +132,9 @@ delete '/api/v1/users/:user_id/subscriptions' do |user_id|
when "user"
user.unfollow(User.find_or_create_by(external_id: params["subscribed_id"]))
when "thread"
user.unwatch_comment_thread(CommentThread.find(params["subscribed_id"]))
user.unsubscribe_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"]))
user.unsubscribe_commentable(Commentable.find_or_create_by(commentable_type: params["subscribed_type"], commentable_id: params["subscribed_id"]))
end
user.reload.to_hash.to_json
end
......
level_limit: 3
send_notifications_to_author: false
auto_watch_comment_threads: true
auto_subscribe_comment_threads: true
allow_anonymity: false
......@@ -54,7 +54,7 @@ class Comment
private
def generate_notifications
if get_comment_thread.watchers or (author.followers if author)
if get_comment_thread.subscribers or (author.followers if author)
notification = Notification.new(
notification_type: "post_reply",
info: {
......@@ -65,21 +65,21 @@ private
)
notification.actor = author
notification.target = self
notification.receivers << (get_comment_thread.watchers + author.followers).uniq_by(&:id)
notification.receivers << (get_comment_thread.subscribers + author.followers).uniq_by(&:id)
notification.receivers.delete(author) if not CommentService.config["send_notifications_to_author"]
notification.save!
end
end
def auto_watch_comment_thread
if CommentService.config["auto_watch_comment_threads"] and author
author.watch_comment_thread(get_comment_thread)
def auto_subscribe_comment_thread
if CommentService.config["auto_subscribe_comment_threads"] and author
author.subscribe_comment_thread(get_comment_thread)
end
end
def handle_after_create
generate_notifications
auto_watch_comment_thread
auto_subscribe_comment_thread
end
handle_asynchronously :handle_after_create
......
......@@ -12,7 +12,7 @@ class CommentThread
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true, autosave: true
belongs_to :commentable, index: true, autosave: true
has_many :comments, dependent: :destroy, autosave: true# 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, autosave: true
has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_comment_threads, autosave: true
attr_accessible :title, :body, :course_id
......@@ -35,7 +35,7 @@ class CommentThread
private
def generate_notifications
if watchers or (author.followers if author)
if subscribers or (author.followers if author)
notification = Notification.new(
notification_type: "post_topic",
info: {
......@@ -47,21 +47,21 @@ private
)
notification.actor = author
notification.target = self
notification.receivers << (commentable.watchers + (author.followers if author).to_a).uniq_by(&:id)
notification.receivers << (commentable.subscribers + (author.followers if author).to_a).uniq_by(&:id)
notification.receivers.delete(author) if not CommentService.config["send_notifications_to_author"] and author
notification.save!
end
end
def auto_watch_comment_thread
if CommentService.config["auto_watch_comment_threads"] and author
author.watch_comment_thread(self)
def auto_subscribe_comment_thread
if CommentService.config["auto_subscribe_comment_threads"] and author
author.subscribe_comment_thread(self)
end
end
def handle_after_create
generate_notifications
auto_watch_comment_thread
auto_subscribe_comment_thread
end
handle_asynchronously :handle_after_create
......
......@@ -5,7 +5,7 @@ class Commentable
field :commentable_id, type: String
has_many :comment_threads, dependent: :destroy
has_and_belongs_to_many :watchers, class_name: "User", inverse_of: :watched_commentables, autosave: true
has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_commentables, autosave: true
attr_accessible :commentable_type, :commentable_id
......
......@@ -9,7 +9,7 @@ class User
has_many :activities, class_name: "Notification", inverse_of: :actor
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 :followings, class_name: "User", inverse_of: :followers#, autosave: true
has_and_belongs_to_many :followings, class_name: "User", inverse_of: :followers
validates_presence_of :external_id
validates_uniqueness_of :external_id
......@@ -28,30 +28,28 @@ class User
followings.delete(user)
end
def self.watching(class_plural_sym)
def self.subscribing(class_plural_sym)
class_plural = class_plural_sym.to_s
class_single = class_plural.singularize
class_name = class_single.camelize
watched_symbol = "watched_#{class_plural}".intern
subscribed_symbol = "subscribed_#{class_plural}".intern
has_and_belongs_to_many watched_symbol, class_name: class_name, inverse_of: :watchers#, autosave: true
has_and_belongs_to_many subscribed_symbol, class_name: class_name, inverse_of: :subscribers
self.class_eval <<-END
def watch_#{class_single}(watching_object)
if not watched_#{class_plural}.include? watching_object
watched_#{class_plural} << watching_object
#save!
def subscribe_#{class_single}(subscribing_object)
if not subscribed_#{class_plural}.include? subscribing_object
subscribed_#{class_plural} << subscribing_object
end
end
def unwatch_#{class_single}(watching_object)
watched_#{class_plural}.delete(watching_object)
#save!
def unsubscribe_#{class_single}(subscribing_object)
subscribed_#{class_plural}.delete(subscribing_object)
end
END
end
watching :comment_threads
watching :commentables
subscribing :comment_threads
subscribing :commentables
end
......@@ -85,12 +85,12 @@ def init_with_subscriptions
user2.followings << user1
commentable = Commentable.new(commentable_type: "questions", commentable_id: "1")
commentable.watchers << [user1, user2]
commentable.subscribers << [user1, user2]
commentable.save!
thread = commentable.comment_threads.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1")
thread.author = user1
thread.watchers << user2
thread.subscribers << user2
thread.save!
comment = thread.comments.new(body: "this problem is so easy", course_id: "1")
......@@ -375,15 +375,15 @@ describe "app" do
user3 = User.find_or_create_by(external_id: "3")
post "/api/v1/users/#{user3.external_id}/subscriptions", subscribed_type: "questions", subscribed_id: "1"
last_response.should be_ok
Commentable.first.watchers.length.should == 3
Commentable.first.watchers.should include user3
Commentable.first.subscribers.length.should == 3
Commentable.first.subscribers.should include user3
end
it "unsubscribe a commentable" do
user2 = User.find_or_create_by(external_id: "2")
delete "/api/v1/users/#{user2.external_id}/subscriptions", subscribed_type: "questions", subscribed_id: "1"
last_response.should be_ok
Commentable.first.watchers.length.should == 1
Commentable.first.watchers.should_not include user2
Commentable.first.subscribers.length.should == 1
Commentable.first.subscribers.should_not include user2
end
it "subscribe a comment thread" do
user1 = User.find_or_create_by(external_id: "1")
......@@ -391,8 +391,8 @@ describe "app" do
post "/api/v1/users/#{user1.external_id}/subscriptions", subscribed_type: "thread", subscribed_id: thread.id
last_response.should be_ok
thread = CommentThread.where(body: "it is unsolvable").first
thread.watchers.length.should == 2
thread.watchers.should include user1
thread.subscribers.length.should == 2
thread.subscribers.should include user1
end
it "unsubscribe a comment thread" do
user2 = User.find_or_create_by(external_id: "2")
......@@ -400,7 +400,7 @@ describe "app" do
delete "/api/v1/users/#{user2.external_id}/subscriptions", subscribed_type: "thread", subscribed_id: thread.id
last_response.should be_ok
thread = CommentThread.where(body: "it is unsolvable").first
thread.watchers.length.should == 0
thread.subscribers.length.should == 0
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