notifications_by_date_range_and_user_ids

in lib/helpers.rb [290:361]


  def notifications_by_date_range_and_user_ids(start_date_time, end_date_time, user_ids)
    
    

    
    subscriptions = Subscription.where(:subscriber_id.in => user_ids)

    
    thread_ids = subscriptions.collect{|t| t.source_id}.uniq

    
    comments = Comment.by_date_range_and_thread_ids start_date_time, end_date_time, thread_ids

    
    thread_map = Hash[CommentThread.where(:_id.in => thread_ids).all.map { |t| [t.id, t] }]

    
    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 = {}

    comments.each do |c|
      current_thread = thread_map[c.comment_thread_id]

      
      if current_thread.abuse_flaggers.to_a.empty? and
        current_thread.historical_abuse_flaggers.to_a.empty? and
        c.abuse_flaggers.to_a.empty? and
        c.historical_abuse_flaggers.to_a.empty?

          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"] = current_thread.title
              t["commentable_id"] = current_thread.commentable_id
              unless current_thread.group_id.nil?
                t["group_id"] = current_thread.group_id
              end
            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, t(:anonymous))
            content_obj["updated_at"] = c.updated_at
            content_obj["body"] = c.body
            t["content"] << content_obj
          end
      end
    end

    notification_map.to_json
  end