<commit_before>
"use strict"

var _ = require('lodash');
var debug = require('debug')('cp-zen-platform:handlers');

module.exports = function(server, role) {

  function doAct (request, reply, cmd, param, user) {
    var msg = _.extend({cmd: cmd, role: role}, {locality: server.methods.locality(request)});
    debug ('handlers.doAct', request.url.path);
    if (request.payload) {
      msg = _.extend(msg, request.payload);
    }
    if (request.query) {
      msg = _.extend(msg, request.query);
    }

    if (param) {
      var paramsMsg = {};
      var params = _.isArray(param) ? param : [param];
      _.each(params, function (p) {
        if (request.params[p]) paramsMsg[p] = request.params[p];
      });
      msg = _.extend(msg, paramsMsg);
    }

    if (user) {
      msg = _.extend(msg, user);
    } else if (request.user) {
      msg = _.extend(msg, request.user);
    }

    debug('handlers.doAct', msg);
    request.seneca.act(msg, function(err, resp) {
      if (err) return reply(err).code(500);

      var code = 200;
      // This is a legacy seneca-web response
      if (resp.http$) {
        if (resp.http$.status) code = resp.http$.status;
        if (resp.http$.redirect) return reply.redirect(resp.http$.redirect);
      }
      reply(resp).code(code);
    });<commit_msg>Defensive tweak when checking for $http<commit_after>
"use strict"

var _ = require('lodash');
var debug = require('debug')('cp-zen-platform:handlers');

module.exports = function(server, role) {

  function doAct (request, reply, cmd, param, user) {
    var msg = _.extend({cmd: cmd, role: role}, {locality: server.methods.locality(request)});
    debug ('handlers.doAct', request.url.path);
    if (request.payload) {
      msg = _.extend(msg, request.payload);
    }
    if (request.query) {
      msg = _.extend(msg, request.query);
    }

    if (param) {
      var paramsMsg = {};
      var params = _.isArray(param) ? param : [param];
      _.each(params, function (p) {
        if (request.params[p]) paramsMsg[p] = request.params[p];
      });
      msg = _.extend(msg, paramsMsg);
    }

    if (user) {
      msg = _.extend(msg, user);
    } else if (request.user) {
      msg = _.extend(msg, request.user);
    }

    debug('handlers.doAct', msg);
    request.seneca.act(msg, function(err, resp) {
      if (err) return reply(err).code(500);

      var code = 200;
      // This is a legacy seneca-web response
      if (resp && resp.http$) {
        if (resp.http$.status) code = resp.http$.status;
        if (resp.http$.redirect) return reply.redirect(resp.http$.redirect);
      }
      reply(resp).code(code);
    });
{'fraction_non_alphanumeric': 0.1674259681093394, 'fraction_numerical': 0.004555808656036446, 'mean_word_length': 2.66991643454039, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6669212', 'n_tokens_mistral': 877, 'n_tokens_neox': 855, 'n_words': 202}