<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
var redis = require("redis");
var async = require("async");
var constants = require("../constants");

var VALID_SIMPLE_PUSH_TOPICS = ["calls", "rooms"];


function RedisStorage(options, settings) {
  this._settings = settings;
  this._client = redis.createClient(
    options.port,
    options.host,
    options.options
  );
  if (options.db) {
    this._client.select(options.db);
  }
}

RedisStorage.prototype = {
  addUserSimplePushURLs: function(userHmac, hawkHmacId, simplePushURLs, callback) {
    var self = this;
    // Remove any previous storage spurl.{userHmac} LIST
    // XXX - Remove this two month after 0.13 release
    self._client.del('spurl.' + userHmac, function(err) {
      if (err) {
        callback(err);
        return;
      }

      for (var topic in simplePushURLs) {
        if (VALID_SIMPLE_PUSH_TOPICS.indexOf(topic) === -1) {
          callback(new Error(topic + " should be one of " +
                             VALID_SIMPLE_PUSH_TOPICS.join(", ")));
          return;
        }
      }

      self._client.set('spurl.' + userHmac + '.' + hawkHmacId,
        JSON.stringify(simplePushURLs), function(err) {
          if (err) {
            callback(err);
            return;
          }
          callback(null);
        });
      });
  },

  getUserSimplePushURLs: function(userMac, callback) {
    var self = this;

    var result = {};
    for (var i = 0; i < VALID_SIMPLE_PUSH_TOPICS.length; i++) {
      result[VALID_SIMPLE_PUSH_TOPICS[i]] = [];
    }

    this._client.keys('spurl.' + userMac + '.*', function(err, spurl_keys) {
      if (err) {
        callback(err);
        return;
      }

      if (spurl_keys.length === 0) {
        callback(null, result);
        return;
      }

      self._client.mget(spurl_keys, function(err, simplePushURLsJSONList) {
        if (err) {
          callback(err);
          return;
        }
        var simplePushURLsList = simplePushURLsJSONList.map(function(json) {
          if (json) {
            try {
              return JSON.parse(json);
            } catch (e) {}
          }
          return null;
        }).filter(function (dict) { return dict !== null; });

        for (var i = 0; i < simplePushURLsList.length; i++) {
          var item = simplePushURLsList[i];

          for (var j = 0; j < VALID_SIMPLE_PUSH_TOPICS.length; j++) {
            var topic = VALID_SIMPLE_PUSH_TOPICS[j];
            var sp_topic = item[topic];
            if (sp_topic !== undefined) {
              if (result[topic].indexOf(sp_topic) === -1)
                result[topic].push(sp_topic);
            }
          }<commit_msg>@tarekziade review.<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
var redis = require("redis");
var async = require("async");
var constants = require("../constants");

var VALID_SIMPLE_PUSH_TOPICS = ["calls", "rooms"];


function RedisStorage(options, settings) {
  this._settings = settings;
  this._client = redis.createClient(
    options.port,
    options.host,
    options.options
  );
  if (options.db) {
    this._client.select(options.db);
  }
}

RedisStorage.prototype = {
  addUserSimplePushURLs: function(userHmac, hawkHmacId, simplePushURLs, callback) {
    var self = this;
    // Remove any previous storage spurl.{userHmac} LIST
    // XXX - Remove this two month after 0.13 release
    self._client.del('spurl.' + userHmac, function(err) {
      if (err) {
        callback(err);
        return;
      }

      for (var topic in simplePushURLs) {
        if (VALID_SIMPLE_PUSH_TOPICS.indexOf(topic) === -1) {
          callback(new Error(topic + " should be one of " +
                             VALID_SIMPLE_PUSH_TOPICS.join(", ")));
          return;
        }
      }

      self._client.set('spurl.' + userHmac + '.' + hawkHmacId,
        JSON.stringify(simplePushURLs), function(err) {
          if (err) {
            callback(err);
            return;
          }
          callback(null);
        });
      });
  },

  getUserSimplePushURLs: function(userMac, callback) {
    var self = this;

    var result = {};
    for (var i = 0; i < VALID_SIMPLE_PUSH_TOPICS.length; i++) {
      result[VALID_SIMPLE_PUSH_TOPICS[i]] = [];
    }

    this._client.keys('spurl.' + userMac + '.*', function(err, spurl_keys) {
      if (err) {
        callback(err);
        return;
      }

      if (spurl_keys.length === 0) {
        callback(null, result);
        return;
      }

      self._client.mget(spurl_keys, function(err, simplePushURLsJSONList) {
        if (err) {
          callback(err);
          return;
        }
        var simplePushURLsList = simplePushURLsJSONList.map(function(json) {
          if (json) {
            return JSON.parse(json);
          }
          return null;
        }).filter(function (dict) { return dict !== null; });

        for (var i = 0; i < simplePushURLsList.length; i++) {
          var item = simplePushURLsList[i];

          for (var j = 0; j < VALID_SIMPLE_PUSH_TOPICS.length; j++) {
            var topic = VALID_SIMPLE_PUSH_TOPICS[j];
            var sp_topic = item[topic];
            if (sp_topic !== undefined) {
              if (result[topic].indexOf(sp_topic) === -1)
                result[topic].push(sp_topic);
            }
          }
{'fraction_non_alphanumeric': 0.13090394480806652, 'fraction_numerical': 0.004599327790553688, 'mean_word_length': 2.0913067249863313, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5659964', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1778, 'n_words': 404}