Commit 71fe495b by Rocky Duan

add uniqueness check for feeds; does not send notifications to author; use autosave

parent 47c2e04a
...@@ -10,8 +10,8 @@ class Comment ...@@ -10,8 +10,8 @@ class Comment
field :course_id, type: String field :course_id, type: String
field :endorsed, type: Boolean, default: false field :endorsed, type: Boolean, default: false
belongs_to :author, class_name: "User", index: true belongs_to :author, class_name: "User", index: true, autosave: true
belongs_to :comment_thread, index: true belongs_to :comment_thread, index: true, autosave: true
attr_accessible :body, :course_id, :endorsed attr_accessible :body, :course_id, :endorsed
...@@ -63,8 +63,7 @@ class Comment ...@@ -63,8 +63,7 @@ class Comment
) )
feed.actor = author feed.actor = author
feed.target = self feed.target = self
feed.subscribers << get_comment_thread.watchers feed.subscribers << (get_comment_thread.watchers + author.followers).uniq_by(&:id).delete(author) # doesn't send notification to author
feed.subscribers << author.followers
feed.save! feed.save!
end end
......
...@@ -9,10 +9,10 @@ class CommentThread ...@@ -9,10 +9,10 @@ class CommentThread
field :body, type: String field :body, type: String
field :course_id, type: String, index: true field :course_id, type: String, index: true
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true, autosave: true
belongs_to :commentable, index: true belongs_to :commentable, index: true, autosave: true
has_many :comments, dependent: :destroy # Use destroy to envoke callback on the top-level comments TODO async 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 has_and_belongs_to_many :watchers, class_name: "User", inverse_of: :watched_comment_threads, autosave: true
attr_accessible :title, :body, :course_id attr_accessible :title, :body, :course_id
...@@ -45,8 +45,7 @@ class CommentThread ...@@ -45,8 +45,7 @@ class CommentThread
) )
feed.actor = author feed.actor = author
feed.target = self feed.target = self
feed.subscribers << commentable.watchers feed.subscribers << (commentable.watchers + author.followers).uniq_by(&:id).delete(author) # doesn't send notification to author
feed.subscribers << author.followers
feed.save! feed.save!
end end
......
...@@ -5,7 +5,7 @@ class Commentable ...@@ -5,7 +5,7 @@ class Commentable
field :commentable_id, type: String field :commentable_id, type: String
has_many :comment_threads, dependent: :destroy has_many :comment_threads, dependent: :destroy
has_and_belongs_to_many :watchers, class_name: "User", inverse_of: :watched_commentables has_and_belongs_to_many :watchers, class_name: "User", inverse_of: :watched_commentables, autosave: true
attr_accessible :commentable_type, :commentable_id attr_accessible :commentable_type, :commentable_id
......
...@@ -5,8 +5,8 @@ class Feed ...@@ -5,8 +5,8 @@ class Feed
field :feed_type, type: String field :feed_type, type: String
field :info, type: Hash field :info, type: Hash
belongs_to :actor, class_name: "User", inverse_of: :activities, index: true belongs_to :actor, class_name: "User", inverse_of: :activities, index: true, autosave: true
belongs_to :target, inverse_of: :activities, polymorphic: true belongs_to :target, inverse_of: :activities, polymorphic: true, autosave: true
attr_accessible :feed_type, :info attr_accessible :feed_type, :info
...@@ -14,7 +14,7 @@ class Feed ...@@ -14,7 +14,7 @@ class Feed
validates_presence_of :actor validates_presence_of :actor
validates_presence_of :target validates_presence_of :target
has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_feeds has_and_belongs_to_many :subscribers, class_name: "User", inverse_of: :subscribed_feeds, 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 feed_type info actor target])
......
...@@ -7,9 +7,9 @@ class User ...@@ -7,9 +7,9 @@ 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: "Feed", inverse_of: :actor
has_and_belongs_to_many :subscribed_feeds, class_name: "Feed", inverse_of: :subscribers has_and_belongs_to_many :subscribed_feeds, class_name: "Feed", inverse_of: :subscribers, autosave: true
has_and_belongs_to_many :followers, class_name: "User", inverse_of: :followings 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 has_and_belongs_to_many :followings, class_name: "User", inverse_of: :followers, autosave: true
def to_hash(params={}) def to_hash(params={})
as_document.slice(*%w[_id]) as_document.slice(*%w[_id])
...@@ -18,13 +18,11 @@ class User ...@@ -18,13 +18,11 @@ class User
def follow(user) def follow(user)
if id != user.id and not following.include? user if id != user.id and not following.include? user
following << user following << user
save!
end end
end end
def unfollow(user) def unfollow(user)
following.delete(user) following.delete(user)
save!
end end
def self.watching(class_plural_sym) def self.watching(class_plural_sym)
...@@ -39,13 +37,11 @@ class User ...@@ -39,13 +37,11 @@ class User
def watch_#{class_single}(watching_object) def watch_#{class_single}(watching_object)
if not watched_#{class_plural}.include? watching_object if not watched_#{class_plural}.include? watching_object
watched_#{class_plural} << watching_object watched_#{class_plural} << watching_object
save!
end end
end end
def unwatch_#{class_single}(watching_object) def unwatch_#{class_single}(watching_object)
watched_#{class_plural}.delete(watching_object) watched_#{class_plural}.delete(watching_object)
save!
end end
END END
end end
......
...@@ -71,6 +71,43 @@ def init_without_feeds ...@@ -71,6 +71,43 @@ def init_without_feeds
end end
end end
def init_with_feeds
Comment.delete_all
CommentThread.delete_all
Commentable.delete_all
User.delete_all
Feed.delete_all
user1 = User.create!(id: "1")
user2 = User.create!(id: "2")
user1.followers << user2
commentable = Commentable.new(commentable_type: "questions", commentable_id: "1")
commentable.watchers << [user1, user2]
commentable.save!
comment_thread = commentable.comment_threads.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1")
comment_thread.author = user1
comment_thread.watchers << user1
comment_thread.save!
comment = comment_thread.comments.new(body: "this problem is so easy", course_id: "1")
comment.author = user1
comment.save!
comment1 = comment.children.new(body: "not for me!", course_id: "1")
comment1.author = user1
comment1.save!
comment2 = comment1.children.new(body: "not for me neither!", course_id: "1")
comment2.author = user1
comment2.save!
comment_thread = commentable.comment_threads.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2")
comment_thread.author = user
comment_thread.save!
end
describe "app" do describe "app" do
describe "commentables" do describe "commentables" do
before(:each) { init_without_feeds } before(:each) { init_without_feeds }
...@@ -295,9 +332,9 @@ describe "app" do ...@@ -295,9 +332,9 @@ describe "app" do
end end
end end
describe "feeds" do describe "feeds" do
#before(:each) { init_with_feeds }
describe "GET /api/v1/users/:user_id/feeds" do describe "GET /api/v1/users/:user_id/feeds" do
it "get all subscribed feeds for the user" do it "get all subscribed feeds for the user" do
end end
end end
describe "POST /api/v1/users/:user_id/follow" do describe "POST /api/v1/users/:user_id/follow" do
......
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