var express = require('express');
var router = express.Router();
var newsFeedFilter = ("../engine/newsfeed_filter");
var models = require("../../models");
var auth = require('../authorization');
var log = require('../utils/logger');
var toJson = require('../utils/to_json');
var _ = require('lodash');

var getCuratedNewsItems = require('../engine/news_feeds/generate_dynamically').getCuratedNewsItems;

router.get('/domains/:id', auth.can('view domain'), auth.isLoggedIn, function(req, res) {

  var options = {
    user_id: req.user.id,
    domain_id: req.params.id
  };

  if (req.query.afterDate) {
    options = _.merge(options, {
      afterDate: new Date(req.query.afterDate)
    })
  }

  if (req.query.beforeDate) {
    options = _.merge(options, {
      beforeDate: new Date(req.query.beforeDate)
    })
  }

  getCuratedNewsItems(options, function (error, activities, oldestProcessedActivityAt) {
    if (error) {
      log.error("News Feed Error Domain", { domainId: req.params.id, userId: req.user.id, errorStatus:  500 });
      res.sendStatus(500);
    } else {
      res.send({
        activities: activities,
        oldestProcessedActivityAt: oldestProcessedActivityAt
      });
    }
  });
});

router.get('/groups/:id', auth.can('view group'), auth.isLoggedIn, function(req, res) {

  var options = {
    user_id: req.user.id,
    group_id: req.params.id
  };

  if (req.query.afterDate) {
    options = _.merge(options, {
      afterDate: new Date(req.query.afterDate)
    })
  }

  if (req.query.beforeDate) {
    options = _.merge(options, {
      beforeDate: new Date(req.query.beforeDate)
    })
  }

  getCuratedNewsItems(options, function (error, activities, oldestProcessedActivityAt) {
    if (error) {
      log.error("News Feed Error Domain", { groupId: req.params.id, userId: req.user.id, errorStatus:  500 });
      res.sendStatus(500);
    } else {
      res.send({
        activities: activities,
        oldestProcessedActivityAt: oldestProcessedActivityAt
      });
    }
  });
});

router.get('/communities/:id', auth.can('view community'), auth.isLoggedIn, function(req, res) {

  var options = {
    user_id: req.user.id,
    community_id: req.params.id
  };

  if (req.query.afterDate) {
    options = _.merge(options, {
      afterDate: new Date(req.query.afterDate)
    })
  }

  if (req.query.beforeDate) {
    options = _.merge(options, {
      beforeDate: new Date(req.query.beforeDate)
    })
  }

  getCuratedNewsItems(options, function (error, activities, oldestProcessedActivityAt) {
    if (error) {
      log.error("News Feed Error Domain", { communityId: req.params.id, userId: req.user.id, errorStatus:  500 });
      res.sendStatus(500);
    } else {
      res.send({
        activities: activities,
        oldestProcessedActivityAt: oldestProcessedActivityAt
      });
    }
  });
});

module.exports = router;
@@ -401,76 +401,26 @@
 s;%0A%0A
-router.get('/domains/:id', auth.can('view domain'), auth.isLoggedIn,
+var setupOptions =
  fun
@@ -420,38 +420,33 @@
 s = function
+ 
 (req
-, res
 ) %7B%0A
-%0A
   var option
@@ -479,38 +479,8 @@
 r.id
-,%0A    domain_id: req.params.id
 %0A  %7D
@@ -719,32 +719,248 @@
 te)%0A    %7D)%0A  %7D%0A%0A
+  return options;%0A%7D;%0A%0Arouter.get('/domains/:id', auth.can('view domain'), auth.isLoggedIn, function(req, res) %7B%0A%0A  var options = setupOptions(req);%0A%0A  options = _.merge(options, %7B%0A    domain_id: req.params.id%0A  %7D);%0A%0A
   getCuratedNews
@@ -1092,16 +1092,28 @@
 main%22, %7B
+ err: error,
  domainI
@@ -1459,313 +1459,92 @@
 s = 
-%7B%0A    user_id: req.user.id,%0A    group_id: req.params.id%0A  %7D;%0A%0A  if (req.query.afterDate) %7B%0A    options = _.merge(options, %7B%0A      afterDate: new Date(req.query.afterDate)%0A    %7D)%0A  %7D%0A%0A  if (req.query.beforeDate) %7B%0A    options = _.merge(options, %7B%0A      beforeDate: new Date(req.query.beforeDate)%0A    %7D)%0A  %7D
+setupOptions(req);%0A%0A  options = _.merge(options, %7B%0A    group_id: req.params.id%0A  %7D);
 %0A%0A  
@@ -1690,16 +1690,28 @@
 main%22, %7B
+ err: error,
  groupId
@@ -2065,317 +2065,96 @@
 s = 
-%7B%0A    user_id: req.user.id,%0A    community_id: req.params.id%0A  %7D;%0A%0A  if (req.query.afterDate) %7B%0A    options = _.merge(options, %7B%0A      afterDate: new Date(req.query.afterDate)%0A    %7D)%0A  %7D%0A%0A  if (req.query.beforeDate) %7B%0A    options = _.merge(options, %7B%0A      beforeDate: new Date(req.query.beforeDate)%0A    %7D)%0A  %7D
+setupOptions(req);%0A%0A  options = _.merge(options, %7B%0A    community_id: req.params.id%0A  %7D);
 %0A%0A  
@@ -2300,16 +2300,28 @@
 main%22, %7B
+ err: error,
  communi

Fix for news feeds