<commit_before>var _ = require('lodash')
var createCheckFreshnessAction = require('../../lib/freshness').createCheckFreshnessAction
var sortColumns = {
  'fulfilled-last': 'fulfilled_at',
  'top': 'post.num_votes',
  'recent': 'post.updated_at',
  'suggested': 'suggested',
  'start_time': ['post.start_time', 'asc']
}

var queryPosts = function (req, opts) {
  var params = _.merge(
    _.pick(req.allParams(), [
      'sort', 'limit', 'offset', 'type', 'start_time', 'end_time', 'filter'
    ]),
    _.pick(opts, 'sort')
  )

  // using Promise.props here allows us to pass queries as attributes,
  // e.g. when looking up communities in PostController.findForUser

  return Promise.props(_.merge(
    {
      sort: sortColumns[params.sort || 'recent'],
      forUser: req.session.userId,
      term: req.param('search')
    },
    _.pick(params, 'type', 'limit', 'offset', 'start_time', 'end_time', 'filter'),
    _.pick(opts, 'communities', 'project', 'users', 'visibility', 'tag')
  ))
  .then(args => {
    var result = Search.forPosts(args)
    return result
  })
}

var fetchAndPresentPosts = function (query, userId, relationsOpts) {
  return query.fetchAll({
    withRelated: PostPresenter.relations(userId, relationsOpts || {})
  })
  .then(posts => ({
    posts_total: (posts.first() ? Number(posts.first().get('total')) : 0),
    posts: posts.map(p => PostPresenter.present(p, userId, relationsOpts))
  }))
}

var queryForCommunity = function (req, res) {
  if (TokenAuth.isAuthenticated(res)) {
    if (!RequestValidation.requireTimeRange(req, res)) return
  }

  return queryPosts(req, {
    communities: [res.locals.community.id],
    visibility: (res.locals.membership.dummy ? Post.Visibility.PUBLIC_READABLE : null)
  })
}

var queryForUser = function (req, res) {
  return queryPosts(req, {
    users: [req.param('userId')],
    communities: Membership.activeCommunityIds(req.session.userId),
    visibility: (req.session.userId ? null : Post.Visibility.PUBLIC_READABLE)
  })
}

var queryForAllForUser = function (req, res) {
  return Membership.activeCommunityIds(req.session.userId)
  .then(function (communityIds) {
    return Search.forPosts({
      communities: communityIds,
      limit: req.param('limit') || 10,
      offset: req.param('offset'),
      sort: sortColumns[req.param('sort') || 'recent'],
      type: req.param('type') || 'all+welcome',
      forUser: req.session.userId,
      term: req.param('search')
    })
  })
}<commit_msg>fix queryForAllForUser<commit_after>var _ = require('lodash')
var createCheckFreshnessAction = require('../../lib/freshness').createCheckFreshnessAction
var sortColumns = {
  'fulfilled-last': 'fulfilled_at',
  'top': 'post.num_votes',
  'recent': 'post.updated_at',
  'suggested': 'suggested',
  'start_time': ['post.start_time', 'asc']
}

var queryPosts = function (req, opts) {
  var params = _.merge(
    _.pick(req.allParams(), [
      'sort', 'limit', 'offset', 'type', 'start_time', 'end_time', 'filter'
    ]),
    _.pick(opts, 'sort')
  )

  // using Promise.props here allows us to pass queries as attributes,
  // e.g. when looking up communities in PostController.findForUser

  return Promise.props(_.merge(
    {
      sort: sortColumns[params.sort || 'recent'],
      forUser: req.session.userId,
      term: req.param('search')
    },
    _.pick(params, 'type', 'limit', 'offset', 'start_time', 'end_time', 'filter'),
    _.pick(opts, 'communities', 'project', 'users', 'visibility', 'tag')
  ))
  .then(args => {
    var result = Search.forPosts(args)
    return result
  })
}

var fetchAndPresentPosts = function (query, userId, relationsOpts) {
  return query.fetchAll({
    withRelated: PostPresenter.relations(userId, relationsOpts || {})
  })
  .then(posts => ({
    posts_total: (posts.first() ? Number(posts.first().get('total')) : 0),
    posts: posts.map(p => PostPresenter.present(p, userId, relationsOpts))
  }))
}

var queryForCommunity = function (req, res) {
  if (TokenAuth.isAuthenticated(res)) {
    if (!RequestValidation.requireTimeRange(req, res)) return
  }

  return queryPosts(req, {
    communities: [res.locals.community.id],
    visibility: (res.locals.membership.dummy ? Post.Visibility.PUBLIC_READABLE : null)
  })
}

var queryForUser = function (req, res) {
  return queryPosts(req, {
    users: [req.param('userId')],
    communities: Membership.activeCommunityIds(req.session.userId),
    visibility: (req.session.userId ? null : Post.Visibility.PUBLIC_READABLE)
  })
}

const queryForAllForUser = function (req, res) {
  return queryPosts(req, {
    communities: Membership.activeCommunityIds(req.session.userId)
  })
}
{'fraction_non_alphanumeric': 0.16407934454506254, 'fraction_numerical': 0.0008624407072013799, 'mean_word_length': 4.020562770562771, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '798334', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1518, 'n_words': 310}