<commit_before>var utils    = require("radiodan-client").utils,
    logger   = utils.logger(__filename);

module.exports = routes;

function routes(app, radiodan, bbcServices, states, Settings) {
  var Avoider  = require("./avoider")(
          states, bbcServices
      ),
      settings = Settings.build(
        "avoider",
        { station: false, avoidType: "programme" }
      ),
      avoider;

    app.get("/", index);
    app.post("/", avoid);
    app.delete("/", cancel);

    app.get("/settings.json",  settingsIndex);
    app.post("/settings.json", settingsUpdate);

    return app;

  function index(req, res) {
    res.render(
      __dirname+"/views/index"
    );
  }

  function avoid(req, res) {
    if (avoider) {
      avoider.cancel();
    }
    avoider = Avoider.create("radio4");
    avoider.avoid();
    res.redirect("./");
  }

  function cancel(req, res) {
    logger.info('/avoid', avoider);
    if (avoider) {
      avoider.cancel();
    }
    res.redirect("./");
  }

  function settingsIndex(req, res) {
    settings.get().then(function(settings) {
      res.json(settings);
    });
  }

  function settingsUpdate(req, res) {
    var newSettings = req.body;

    settings.set(newSettings).then(function() {
      res.json(newSettings);
    }, function(err) { res.json(500, {error: err}) });<commit_msg>Add basic validation for avoider settings<commit_after>var utils    = require("radiodan-client").utils,
    logger   = utils.logger(__filename);

module.exports = routes;

function routes(app, radiodan, bbcServices, states, Settings) {
  var Avoider  = require("./avoider")(
          states, bbcServices
      ),
      settings = Settings.build(
        "avoider",
        { station: false, avoidType: "programme" }
      ),
      avoider;

    app.get("/", index);
    app.post("/", avoid);
    app.delete("/", cancel);

    app.get("/settings.json",  settingsIndex);
    app.post("/settings.json", settingsUpdate);

    return app;

  function index(req, res) {
    res.render(
      __dirname+"/views/index"
    );
  }

  function avoid(req, res) {
    if (avoider) {
      avoider.cancel();
    }
    avoider = Avoider.create("radio4");
    avoider.avoid();
    res.redirect("./");
  }

  function cancel(req, res) {
    logger.info('/avoid', avoider);
    if (avoider) {
      avoider.cancel();
    }
    res.redirect("./");
  }

  function settingsIndex(req, res) {
    settings.get().then(function(settings) {
      res.json(settings);
    });
  }

  function settingsUpdate(req, res) {
    var newSettings = req.body;

    console.log("newSettings", newSettings);
    if(!(newSettings.avoidType) || !(newSettings.serviceId)) {
      return res.json(500, {error: "Settings are malformed"});
    }

    settings.set(newSettings).then(function() {
      res.json(newSettings);
    }, function(err) { res.json(500, {error: err}) });
{'fraction_non_alphanumeric': 0.16771269177126918, 'fraction_numerical': 0.0038354253835425384, 'mean_word_length': 2.745430809399478, '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': '5791896', 'n_tokens_mistral': 947, 'n_tokens_neox': 912, 'n_words': 173}