Commit c6dd1a8a by Rocky Duan

recent activity search

parent 11ff55bb
...@@ -130,7 +130,8 @@ namespace :db do ...@@ -130,7 +130,8 @@ namespace :db do
comment_thread.course_id = COURSE_ID comment_thread.course_id = COURSE_ID
comment_thread.save! comment_thread.save!
threads << comment_thread threads << comment_thread
TOP_COMMENTS_PER_THREAD.times do users.sample(3).each {|user| user.subscribe(comment_thread)}
(1 + rand(TOP_COMMENTS_PER_THREAD)).times do
comment = comment_thread.comments.new(body: Faker::Lorem.paragraph(2)) comment = comment_thread.comments.new(body: Faker::Lorem.paragraph(2))
comment.author = users.sample comment.author = users.sample
comment.endorsed = [true, false].sample comment.endorsed = [true, false].sample
...@@ -140,7 +141,7 @@ namespace :db do ...@@ -140,7 +141,7 @@ namespace :db do
top_comments << comment top_comments << comment
inner_top_comments << comment inner_top_comments << comment
end end
ADDITIONAL_COMMENTS_PER_THREAD.times do (1 + rand(ADDITIONAL_COMMENTS_PER_THREAD)).times do
comment = inner_top_comments.sample comment = inner_top_comments.sample
sub_comment = comment.children.new(body: Faker::Lorem.paragraph(2)) sub_comment = comment.children.new(body: Faker::Lorem.paragraph(2))
sub_comment.author = users.sample sub_comment.author = users.sample
......
...@@ -88,6 +88,41 @@ get "#{api_prefix}/search/threads/more_like_this" do ...@@ -88,6 +88,41 @@ get "#{api_prefix}/search/threads/more_like_this" do
end.results.map(&:to_hash).to_json end.results.map(&:to_hash).to_json
end end
get "#{api_prefix}/search/threads/recent_active" do
return [].to_json if not params["course_id"]
follower_id = params["follower_id"]
from_time = {
"today" => Date.today.to_time,
"this_week" => Date.today.to_time - 1.weeks,
"this_month" => Date.today.to_time - 1.months,
}[params["from_time"] || "today"]
query_params = {}
query_params["course_id"] = params["course_id"] if params["course_id"]
query_params["commentable_id"] = params["commentable_id"] if params["commentable_id"]
comment_threads = if follower_id
User.find(follower_id).subscribed_threads.select do |thread|
thread.last_activity_at >= from_time and \
query_params.to_a.map {|query| thread[query.first] == query.last}.all?
end
else
CommentThread.all.where(query_params.merge(:last_activity_at => {:$gte => from_time}))
end
comment_threads.to_a.sort {|x, y| y.last_activity_at <=> x.last_activity_at}[0..4].to_json
end
#
#get "#{api_prefix}/search/tags/trending" do
# query_params = {}
# query_params["course_id"] = params["course_id"] if params["course_id"]
# query_params["commentable_id"] = params["commentable_id"] if params["commentable_id"]
# CommentThread.all.where(query_params).to_a.map(&:tags_array).flatten.g
#end
delete "#{api_prefix}/:commentable_id/threads" do |commentable_id| delete "#{api_prefix}/:commentable_id/threads" do |commentable_id|
commentable.comment_threads.destroy_all commentable.comment_threads.destroy_all
{}.to_json {}.to_json
......
...@@ -39,6 +39,7 @@ class CommentThread < Content ...@@ -39,6 +39,7 @@ class CommentThread < Content
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true#, autosave: true belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true#, autosave: true
has_many :comments, dependent: :destroy#, autosave: true# Use destroy to envoke callback on the top-level comments TODO async has_many :comments, dependent: :destroy#, autosave: true# Use destroy to envoke callback on the top-level comments TODO async
has_many :activities, autosave: true
attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :closed attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :closed
...@@ -54,6 +55,8 @@ class CommentThread < Content ...@@ -54,6 +55,8 @@ class CommentThread < Content
before_create :set_last_activity_at before_create :set_last_activity_at
before_update :set_last_activity_at before_update :set_last_activity_at
scope :active_since, ->(from_time) { where(:last_activity_at => {:$gte => from_time}) }
def self.new_dumb_thread(options={}) def self.new_dumb_thread(options={})
c = self.new c = self.new
c.title = options[:title] || "title" c.title = options[:title] || "title"
...@@ -94,6 +97,23 @@ class CommentThread < Content ...@@ -94,6 +97,23 @@ class CommentThread < Content
search search
end end
def activity_since(from_time=nil)
if from_time
activities.where(:created_at => {:$gte => from_time})
else
activities
end
end
def activity_today; activity_since(Date.today.to_time); end
def activity_this_week; activity_since(Date.today.to_time - 1.weeks); end
def activity_this_month; activity_since(Date.today.to_time - 1.months); end
def activity_overall; activity_since(nil); end
def root_comments def root_comments
Comment.roots.where(comment_thread_id: self.id) Comment.roots.where(comment_thread_id: self.id)
end end
...@@ -139,8 +159,6 @@ private ...@@ -139,8 +159,6 @@ private
RE_WORD = /#{RE_HEADCHAR}(((#{RE_CHAR})*(#{RE_ENDCHAR})+)?(#{RE_ENDONLYCHAR})*)?/ RE_WORD = /#{RE_HEADCHAR}(((#{RE_CHAR})*(#{RE_ENDCHAR})+)?(#{RE_ENDONLYCHAR})*)?/
RE_TAG = /^#{RE_WORD}( #{RE_WORD})*$/ RE_TAG = /^#{RE_WORD}( #{RE_WORD})*$/
def tag_names_valid def tag_names_valid
unless tags_array.all? {|tag| self.class.tag_name_valid? tag} unless tags_array.all? {|tag| self.class.tag_name_valid? tag}
errors.add :tag, "can consist of words, numbers, dashes and spaces only and cannot start with dash" errors.add :tag, "can consist of words, numbers, dashes and spaces only and cannot start with dash"
......
...@@ -5,8 +5,8 @@ class Notification ...@@ -5,8 +5,8 @@ class Notification
field :notification_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, index: true, autosave: true
attr_accessible :notification_type, :info attr_accessible :notification_type, :info
......
...@@ -74,7 +74,7 @@ private ...@@ -74,7 +74,7 @@ private
username = parts.first[1..-1] username = parts.first[1..-1]
user = User.where(username: username).first user = User.where(username: username).first
if user if user
list << { position: parts.last.to_i, username: parts.first[1..-1], user_id: user.id }#parts.last.to_i, parts.first[1..-1], user.id] list << { position: parts.last.to_i, username: parts.first[1..-1], user_id: user.id }
end end
end end
list list
......
...@@ -2,10 +2,19 @@ class PostReplyObserver < Mongoid::Observer ...@@ -2,10 +2,19 @@ class PostReplyObserver < Mongoid::Observer
observe :comment observe :comment
def after_create(comment) def after_create(comment)
self.class.delay.generate_notifications(comment) self.class.delay.generate_activity_and_notifications(comment)
end end
def self.generate_notifications(comment) def self.generate_activity_and_notifications(comment)
activity = Activity.new
activity.happend_at = comment.created_at
activity.anonymous = comment.anonymous
activity.actor = comment.author
activity.target = comment.comment_thread
activity.activity_type = "post_reply"
activity.save!
if comment.comment_thread.subscribers or (comment.author.followers if not comment.anonymous) if comment.comment_thread.subscribers or (comment.author.followers if not comment.anonymous)
notification = Notification.new( notification = Notification.new(
notification_type: "post_reply", notification_type: "post_reply",
......
...@@ -41,6 +41,22 @@ class User ...@@ -41,6 +41,22 @@ class User
subscriptions_as_subscriber.where(source_type: "User").map(&:source_id) subscriptions_as_subscriber.where(source_type: "User").map(&:source_id)
end end
def subscribed_threads
subscribed_thread_ids.map {|id| CommentThread.find(id)}
end
def subscribed_commentables
subscribed_commentable_ids.map {|id| Commentable.find(id)}
end
def subscribed_users
subscribed_user_ids.map {|id| User.find(id)}
end
def to_hash(params={}) def to_hash(params={})
hash = as_document.slice(*%w[_id username external_id]) hash = as_document.slice(*%w[_id username external_id])
if params[:complete] if params[:complete]
......
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