summaryrefslogtreecommitdiffstats
path: root/web/react/stores/channel_store.jsx
blob: 3f259bc7db7b3aa356a4dffe6f84e2f7ab7629fb (plain)
1
2
3
4
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
47
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');

var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;


var CHANGE_EVENT = 'change';
var MORE_CHANGE_EVENT = 'change';
var EXTRA_INFO_EVENT = 'extra_info';

var ChannelStore = assign({}, EventEmitter.prototype, {
  emitChange: function() {
    this.emit(CHANGE_EVENT);
  },
  addChangeListener: function(callback) {
    this.on(CHANGE_EVENT, callback);
  },
  removeChangeListener: function(callback) {
    this.removeListener(CHANGE_EVENT, callback);
  },
  emitMoreChange: function() {
    this.emit(MORE_CHANGE_EVENT);
  },
  addMoreChangeListener: function(callback) {
    this.on(MORE_CHANGE_EVENT, callback);
  },
  removeMoreChangeListener: function(callback) {
    this.removeListener(MORE_CHANGE_EVENT, callback);
  },
  emitExtraInfoChange: function() {
    this.emit(EXTRA_INFO_EVENT);
  },
  addExtraInfoChangeListener: function(callback) {
    this.on(EXTRA_INFO_EVENT, callback);
  },
  removeExtraInfoChangeListener: function(callback) {
    this.removeListener(EXTRA_INFO_EVENT, callback);
  },
  get: function(id) {
    var current = null;
    var c = this._getChannels();

    c.some(function(channel) {
      if (channel.id == id) {
        current = channel;
        return true;
      }
      return false;
    });

    return current;
  },
  getMember: function(id) {
    var current = null;
    return this.getAllMembers()[id];
  },
  getByName: function(name) {
    var current = null;
    var c = this._getChannels();

    c.some(function(channel) {
      if (channel.name == name) {
        current = channel;
        return true;
      }

      return false;

    });

    return current;

  },
  getAll: function() {
    return this._getChannels();
  },
  getAllMembers: function() {
    return this._getChannelMembers();
  },
  getMoreAll: function() {
    return this._getMoreChannels();
  },
  setCurrentId: function(id) {
    if (id == null)
      sessionStorage.removeItem("current_channel_id");
    else
      sessionStorage.setItem("current_channel_id", id);
  },
  setLastVisitedName: function(name) {
    if (name == null)
      localStorage.removeItem("last_visited_name");
    else
      localStorage.setItem("last_visited_name", name);
  },
  getLastVisitedName: function() {
    return localStorage.getItem("last_visited_name");
  },
  resetCounts: function(id) {
      var cm = this._getChannelMembers();
      for (var cmid in cm) {
          if (cm[cmid].channel_id == id) {
              var c = this.get(id);
              if (c) {
                  cm[cmid].msg_count = this.get(id).total_msg_count;
                  cm[cmid].mention_count = 0;
              }
              break;
          }
      }
      this._storeChannelMembers(cm);
  },
  getCurrentId: function() {
    return sessionStorage.getItem("current_channel_id");
  },
  getCurrent: function() {
    var currentId = ChannelStore.getCurrentId();

    if (currentId != null)
      return this.get(currentId);
    else
      return null;
  },
  getCurrentMember: function() {
    var currentId = ChannelStore.getCurrentId();

    if (currentId != null)
      return this.getAllMembers()[currentId];
    else
      return null;
  },
  setChannelMember: function(member) {
    var members = this._getChannelMembers();
    members[member.channel_id] = member;
    this._storeChannelMembers(members);
    this.emitChange();
  },
  getCurrentExtraInfo: function() {
    var currentId = ChannelStore.getCurrentId();
    var extra = null;

    if (currentId != null)
      extra = this._getExtraInfos()[currentId];

    if (extra == null)
      extra = {members: []};

    return extra;
  },
  getExtraInfo: function(channel_id) {
    var extra = null;

    if (channel_id != null)
      extra = this._getExtraInfos()[channel_id];

    if (extra == null)
      extra = {members: []};

    return extra;
  },
  _storeChannels: function(channels) {
    sessionStorage.setItem("channels", JSON.stringify(channels));
  },
  _getChannels: function() {
    var channels = [];
    try {
        channels = JSON.parse(sessionStorage.channels);
    }
    catch (err) {
    }

    return channels;
  },
  _storeChannelMembers: function(channelMembers) {
    sessionStorage.setItem("channel_members", JSON.stringify(channelMembers));
  },
  _getChannelMembers: function() {
    var members = {};
    try {
        members = JSON.parse(sessionStorage.channel_members);
    }
    catch (err) {
    }

    return members;
  },
  _storeMoreChannels: function(channels) {
    sessionStorage.setItem("more_channels", JSON.stringify(channels));
  },
  _getMoreChannels: function() {
    var channels = [];
    try {
        channels = JSON.parse(sessionStorage.more_channels);
    }
    catch (err) {
    }

    return channels;
  },
  _storeExtraInfos: function(extraInfos) {
    sessionStorage.setItem("extra_infos", JSON.stringify(extraInfos));
  },
  _getExtraInfos: function() {
    var members = {};
    try {
        members = JSON.parse(sessionStorage.extra_infos);
    }
    catch (err) {
    }

    return members;
  }
});

ChannelStore.dispatchToken = AppDispatcher.register(function(payload) {
  var action = payload.action;

  switch(action.type) {

    case ActionTypes.CLICK_CHANNEL:
      ChannelStore.setCurrentId(action.id);
      ChannelStore.setLastVisitedName(action.name);
      ChannelStore.resetCounts(action.id);
      ChannelStore.emitChange();
      break;

    case ActionTypes.RECIEVED_CHANNELS:
      ChannelStore._storeChannels(action.channels);
      ChannelStore._storeChannelMembers(action.members);
      var currentId = ChannelStore.getCurrentId();
      if (currentId) ChannelStore.resetCounts(currentId);
      ChannelStore.emitChange();
      break;

    case ActionTypes.RECIEVED_MORE_CHANNELS:
      ChannelStore._storeMoreChannels(action.channels);
      ChannelStore.emitMoreChange();
      break;

    case ActionTypes.RECIEVED_CHANNEL_EXTRA_INFO:
      var extra_infos = ChannelStore._getExtraInfos();
      extra_infos[action.extra_info.id] = action.extra_info;
      ChannelStore._storeExtraInfos(extra_infos);
      ChannelStore.emitExtraInfoChange();
      break;

    default:
  }
});

module.exports = ChannelStore;