Commit 1f5684a7 by Kevin Chugh

restore notifications api post-merge-rollback

parent d2bf94ef
......@@ -72,6 +72,7 @@ require './api/votes'
require './api/flags'
require './api/pins'
require './api/notifications_and_subscriptions'
require './api/notifications'
if RACK_ENV.to_s == "development"
get "#{APIPREFIX}/clean" do
......
......@@ -203,4 +203,66 @@ helpers do
end.compact
end
def notifications_by_date_range_and_user_ids start_date_time, end_date_time, user_ids
#given a date range and a user, find all of the notifiable content
#key by thread id, and return notification messages for each user
#first, find the subscriptions for the users
subscriptions = Subscription.where(:subscriber_id.in => user_ids)
#get the thhread ids
thread_ids = subscriptions.collect{|t| t.source_id}.uniq
#find all the comments
comments = Comment.by_date_range_and_thread_ids start_date_time, end_date_time, thread_ids
#and get the threads too, b/c we'll need them for the title
thread_map = Hash[CommentThread.where(:_id.in => thread_ids).all.map { |t| [t.id, t] }]
#now build a thread to users subscription map
subscriptions_map = {}
subscriptions.each do |s|
if not subscriptions_map.keys.include? s.source_id.to_s
subscriptions_map[s.source_id.to_s] = []
end
subscriptions_map[s.source_id] << s.subscriber_id
end
#notification map will be user => course => thread => [comment bodies]
notification_map = {}
comments.each do |c|
user_ids = subscriptions_map[c.comment_thread_id.to_s]
user_ids.each do |u|
if not notification_map.keys.include? u
notification_map[u] = {}
end
if not notification_map[u].keys.include? c.course_id
notification_map[u][c.course_id] = {}
end
if not notification_map[u][c.course_id].include? c.comment_thread_id.to_s
t = notification_map[u][c.course_id][c.comment_thread_id.to_s] = {}
t["content"] = []
t["title"] = thread_map[c.comment_thread_id].title
t["commentable_id"] = thread_map[c.comment_thread_id].commentable_id
else
t = notification_map[u][c.course_id][c.comment_thread_id.to_s]
end
content_obj = {}
content_obj["username"] = c.author_with_anonymity(:username, "(anonymous)")
content_obj["updated_at"] = c.updated_at
content_obj["body"] = c.body
t["content"] << content_obj
end
end
notification_map.to_json
end
end
......@@ -97,6 +97,14 @@ class Comment < Content
end
end
end
def self.by_date_range_and_thread_ids from_when, to_when, thread_ids
#return all content between from_when and to_when
self.where(:created_at.gte => (from_when)).where(:created_at.lte => (to_when)).
where(:comment_thread_id.in => thread_ids)
end
private
def set_thread_last_activity_at
......
......@@ -84,4 +84,15 @@ class Content
answer
end
def self.by_date_range from_when, to_when
#return all content between from_when and to_when
Content.where(:created_at.gte => (from_when)).where(:created_at.lte => (to_when))
end
def self.by_date_range_and_thread_ids from_when, to_when, thread_ids
#return all content between from_when and to_when
Content.where(:created_at.gte => (from_when)).where(:created_at.lte => (to_when)).
where(:comment_thread_id.in => thread_ids)
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