redis.js 3.18 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
'use strict';

mntmn's avatar
mntmn committed
3
4
// this is a mock version of the Redis API,
// emulating Redis if it is not available locally
mntmn's avatar
mntmn committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var notRedis = {
  state: {},
  topics: {},
  
  publish: function(topic, msg, cb) {
    if (!this.topics[topic]) {
      this.topics[topic] = {
        subscribers: []
      };
    }
    var t=this.topics[topic];
    for (var i=0; i<t.subscribers.length; i++) {
      var s=t.subscribers[i];
      if (s.handler) {
        s.handler(topic, msg);
      }
    }
    if (cb) cb(null);
  },

  subscribe: function(topics, cb) {
    var handle = {
      handler: null,
      on: function(evt, cb) {
        if (evt == "message") {
          this.handler = cb;
        }
      }
    };
    
    for (var i=0; i<topics.length; i++) {
      var topic = topics[i];
      if (!this.topics[topic]) {
        this.topics[topic] = {
          subscribers: []
        };
      }
    
      var t=this.topics[topic];
      t.subscribers.push(handle);
    }

mntmn's avatar
mntmn committed
47
    cb(null, topics.length);
mntmn's avatar
mntmn committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    return handle;
  },

  get: function(key, cb) {
    cb(null, this.state[key]);
    return this.state[key];
  },

  set: function(key, val, cb) {
    this.state[key] = val;
    cb();
  },

  del: function(key, cb) {
    delete this.state[key];
    cb(null);
  },

  sadd: function(key, skey, cb) {
    if (!this.state[key]) this.state[key] = {};
    this.state[key][skey] = true;
    cb(null);
  },

  srem: function(key, skey, cb) {
    if (this.state[key]) {
      delete this.state[key][skey];
    }
    cb(null);
  },

  smembers: function(key, cb) {
    cb(null, Object.keys(this.state[key]));
  },

  incr: function(key, cb) {
    if (!this.state[key]) this.state[key] = 0;
    this.state[key]++;
mntmn's avatar
mntmn committed
86
    cb(null, this.state[key]);
mntmn's avatar
mntmn committed
87
88
89
90
91
  },

  expire: function() {
  },
}
mntmn's avatar
mntmn committed
92
93

module.exports = {
mntmn's avatar
mntmn committed
94
95
  connectRedis: function() {
    this.connection = notRedis;
mntmn's avatar
mntmn committed
96
  },
mntmn's avatar
mntmn committed
97
98
99
100
101
  getConnection: function() {
    this.connectRedis();
    return this.connection;
  },
  sendMessage: function(action, model, attributes, channelId) {
mntmn's avatar
mntmn committed
102
103
104
105
106
107
108
109
    const data = JSON.stringify({
      channel_id: channelId,
      action: action,
      model: model,
      object: attributes
    });
    this.connection.publish('updates', data);
  },
mntmn's avatar
mntmn committed
110
  logIp: function(ip, cb) {
mntmn's avatar
mntmn committed
111
112
113
114
    this.connection.incr("ip_"+ ip, (err, socketCounter) => {
      cb();
    });
  },
mntmn's avatar
mntmn committed
115
  rateLimit: function(namespace, ip, cb) {
mntmn's avatar
mntmn committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    const key = "limit_"+ namespace + "_"+ ip;
    const redis = this.connection;

    redis.get(key, (err, count)=> {
      if (count) {
        if(count < 150) {
          redis.incr(key, (err, newCount) => {
            if (newCount==150) {
              // limit
            }
            cb(true);
          });
        } else {
          cb(false);
        }
      } else {
        redis.set(key, 1, (err, count) => {
          redis.expire(key, 1800, (err, expResult) => {
            cb(true);
          });
        });
      }
    });
  }, 
mntmn's avatar
mntmn committed
140
  isOnlineInSpace: function(user, space, cb) {
mntmn's avatar
mntmn committed
141
142
143
144
145
146
147
148
149
150
151
    this.connection.smembers("space_" + space._id.toString(), function(err, list) {
      if (err) cb(err);
      else {
        var users = list.filter(function(item) {
          return user._id.toString() === item;
        });
        cb(null, (users.length > 0));
      }
    });
  }
};
mntmn's avatar
mntmn committed
152
153
154

return module.exports;