2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2015-11-19 21:12:56 -05:00
|
|
|
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
|
|
|
|
|
import EventEmitter from 'events';
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-12-15 11:40:46 -05:00
|
|
|
import TeamStore from 'stores/team_store.jsx';
|
2017-01-15 15:40:43 +00:00
|
|
|
import UserStore from 'stores/user_store.jsx';
|
2016-12-15 11:40:46 -05:00
|
|
|
|
2015-11-02 16:04:14 -08:00
|
|
|
var Utils;
|
2016-11-01 12:58:04 -04:00
|
|
|
import {ActionTypes, Constants} from 'utils/constants.jsx';
|
2015-12-02 10:22:39 -05:00
|
|
|
const NotificationPrefs = Constants.NotificationPrefs;
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-11-19 21:12:56 -05:00
|
|
|
const CHANGE_EVENT = 'change';
|
2016-10-19 14:49:25 -04:00
|
|
|
const STATS_EVENT = 'stats';
|
2016-07-14 15:19:27 +03:00
|
|
|
const LAST_VIEVED_EVENT = 'last_viewed';
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
class ChannelStoreClass extends EventEmitter {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
2016-10-20 14:39:19 -04:00
|
|
|
this.setMaxListeners(600);
|
2017-01-03 12:37:47 -05:00
|
|
|
this.clear();
|
|
|
|
|
}
|
2015-09-02 10:42:26 -04:00
|
|
|
|
2017-01-03 12:37:47 -05:00
|
|
|
clear() {
|
2015-09-02 10:42:26 -04:00
|
|
|
this.currentId = null;
|
2015-11-18 17:29:06 -05:00
|
|
|
this.postMode = this.POST_MODE_CHANNEL;
|
|
|
|
|
this.channels = [];
|
2017-01-15 15:40:43 +00:00
|
|
|
this.members_in_channel = {};
|
2016-10-19 14:49:25 -04:00
|
|
|
this.myChannelMembers = {};
|
2015-11-18 17:29:06 -05:00
|
|
|
this.moreChannels = {};
|
2016-10-19 14:49:25 -04:00
|
|
|
this.stats = {};
|
2015-12-02 10:22:39 -05:00
|
|
|
this.unreadCounts = {};
|
2015-11-18 17:29:06 -05:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
get POST_MODE_CHANNEL() {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
get POST_MODE_FOCUS() {
|
|
|
|
|
return 2;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
emitChange() {
|
2015-08-11 11:55:23 -04:00
|
|
|
this.emit(CHANGE_EVENT);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
addChangeListener(callback) {
|
2015-08-11 11:55:23 -04:00
|
|
|
this.on(CHANGE_EVENT, callback);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
removeChangeListener(callback) {
|
2015-08-11 11:55:23 -04:00
|
|
|
this.removeListener(CHANGE_EVENT, callback);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
emitStatsChange() {
|
|
|
|
|
this.emit(STATS_EVENT);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
addStatsChangeListener(callback) {
|
|
|
|
|
this.on(STATS_EVENT, callback);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
removeStatsChangeListener(callback) {
|
|
|
|
|
this.removeListener(STATS_EVENT, callback);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-11-21 12:17:58 -05:00
|
|
|
emitLastViewed() {
|
|
|
|
|
this.emit(LAST_VIEVED_EVENT);
|
2016-07-14 15:19:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addLastViewedListener(callback) {
|
|
|
|
|
this.on(LAST_VIEVED_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeLastViewedListener(callback) {
|
|
|
|
|
this.removeListener(LAST_VIEVED_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
findFirstBy(field, value) {
|
2016-04-04 11:09:59 -04:00
|
|
|
return this.doFindFirst(field, value, this.getChannels());
|
|
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-04-04 11:09:59 -04:00
|
|
|
findFirstMoreBy(field, value) {
|
|
|
|
|
return this.doFindFirst(field, value, this.getMoreChannels());
|
|
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-04-04 11:09:59 -04:00
|
|
|
doFindFirst(field, value, channels) {
|
2015-08-11 11:55:23 -04:00
|
|
|
for (var i = 0; i < channels.length; i++) {
|
|
|
|
|
if (channels[i][field] === value) {
|
|
|
|
|
return channels[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-11 11:55:23 -04:00
|
|
|
return null;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
get(id) {
|
2015-08-11 11:55:23 -04:00
|
|
|
return this.findFirstBy('id', id);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
getMyMember(id) {
|
|
|
|
|
return this.getMyMembers()[id];
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
getByName(name) {
|
2015-08-11 11:55:23 -04:00
|
|
|
return this.findFirstBy('name', name);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-06-02 15:45:38 -07:00
|
|
|
getByDisplayName(displayName) {
|
|
|
|
|
return this.findFirstBy('display_name', displayName);
|
|
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-04-04 11:09:59 -04:00
|
|
|
getMoreByName(name) {
|
|
|
|
|
return this.findFirstMoreBy('name', name);
|
|
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
getAll() {
|
2016-01-26 12:03:52 -05:00
|
|
|
return this.getChannels();
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
getMoreAll() {
|
2016-01-26 12:03:52 -05:00
|
|
|
return this.getMoreChannels();
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
setCurrentId(id) {
|
2015-08-13 08:00:10 -04:00
|
|
|
this.currentId = id;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
resetCounts(id) {
|
2016-10-19 14:49:25 -04:00
|
|
|
const cm = this.myChannelMembers;
|
2016-10-28 09:36:41 -04:00
|
|
|
for (const cmid in cm) {
|
2015-08-11 11:55:23 -04:00
|
|
|
if (cm[cmid].channel_id === id) {
|
2016-10-28 09:36:41 -04:00
|
|
|
const channel = this.get(id);
|
|
|
|
|
if (channel) {
|
|
|
|
|
cm[cmid].msg_count = channel.total_msg_count;
|
2015-08-11 11:55:23 -04:00
|
|
|
cm[cmid].mention_count = 0;
|
2016-10-27 12:24:30 -03:00
|
|
|
this.setUnreadCountByChannel(id);
|
2015-08-11 11:55:23 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
getCurrentId() {
|
2015-08-13 08:00:10 -04:00
|
|
|
return this.currentId;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
getCurrent() {
|
2015-08-11 11:55:23 -04:00
|
|
|
var currentId = this.getCurrentId();
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-11 11:55:23 -04:00
|
|
|
if (currentId) {
|
|
|
|
|
return this.get(currentId);
|
|
|
|
|
}
|
2015-09-02 10:42:26 -04:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
getCurrentMember() {
|
|
|
|
|
var currentId = this.getCurrentId();
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-11 11:55:23 -04:00
|
|
|
if (currentId) {
|
2016-10-19 14:49:25 -04:00
|
|
|
return this.getMyMembers()[currentId];
|
2015-08-11 11:55:23 -04:00
|
|
|
}
|
2015-09-02 10:42:26 -04:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
getCurrentStats() {
|
|
|
|
|
return this.getStats(this.getCurrentId());
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
getStats(channelId) {
|
|
|
|
|
let stats;
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-11 11:55:23 -04:00
|
|
|
if (channelId) {
|
2016-10-19 14:49:25 -04:00
|
|
|
stats = this.stats[channelId];
|
2015-08-11 11:55:23 -04:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
if (stats) {
|
2015-12-03 12:57:34 -05:00
|
|
|
// create a defensive copy
|
2016-10-19 14:49:25 -04:00
|
|
|
stats = Object.assign({}, stats);
|
2015-12-03 12:57:34 -05:00
|
|
|
} else {
|
2016-10-19 14:49:25 -04:00
|
|
|
stats = {member_count: 0};
|
2015-08-11 11:55:23 -04:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
return stats;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
storeChannel(channel) {
|
2016-01-26 12:03:52 -05:00
|
|
|
var channels = this.getChannels();
|
2015-08-11 11:55:23 -04:00
|
|
|
var found;
|
2015-07-27 11:30:03 -04:00
|
|
|
|
2015-08-11 11:55:23 -04:00
|
|
|
for (var i = 0; i < channels.length; i++) {
|
|
|
|
|
if (channels[i].id === channel.id) {
|
|
|
|
|
channels[i] = channel;
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-27 11:30:03 -04:00
|
|
|
|
2015-08-11 11:55:23 -04:00
|
|
|
if (!found) {
|
|
|
|
|
channels.push(channel);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 16:04:14 -08:00
|
|
|
if (!Utils) {
|
2016-03-14 08:50:46 -04:00
|
|
|
Utils = require('utils/utils.jsx'); //eslint-disable-line global-require
|
2015-11-02 16:04:14 -08:00
|
|
|
}
|
2015-07-08 09:55:24 -04:00
|
|
|
|
2015-11-02 16:04:14 -08:00
|
|
|
channels.sort(Utils.sortByDisplayName);
|
2016-01-26 12:03:52 -05:00
|
|
|
this.storeChannels(channels);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-01-26 12:03:52 -05:00
|
|
|
storeChannels(channels) {
|
2015-11-18 17:29:06 -05:00
|
|
|
this.channels = channels;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-01-26 12:03:52 -05:00
|
|
|
getChannels() {
|
2015-11-18 17:29:06 -05:00
|
|
|
return this.channels;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-12-19 10:05:46 -03:00
|
|
|
getChannelById(id) {
|
|
|
|
|
return this.channels.filter((c) => c.id === id)[0];
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
storeMyChannelMember(channelMember) {
|
|
|
|
|
const members = Object.assign({}, this.getMyMembers());
|
2015-08-11 11:55:23 -04:00
|
|
|
members[channelMember.channel_id] = channelMember;
|
2016-10-19 14:49:25 -04:00
|
|
|
this.storeMyChannelMembers(members);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
storeMyChannelMembers(channelMembers) {
|
|
|
|
|
this.myChannelMembers = channelMembers;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-27 12:24:30 -03:00
|
|
|
storeMyChannelMembersList(channelMembers) {
|
|
|
|
|
channelMembers.forEach((m) => {
|
|
|
|
|
this.myChannelMembers[m.channel_id] = m;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
getMyMembers() {
|
|
|
|
|
return this.myChannelMembers;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2017-01-15 15:40:43 +00:00
|
|
|
saveMembersInChannel(channelId = this.getCurrentId(), members) {
|
|
|
|
|
const oldMembers = this.members_in_channel[channelId] || {};
|
|
|
|
|
this.members_in_channel[channelId] = Object.assign({}, oldMembers, members);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeMemberInChannel(channelId = this.getCurrentId(), userId) {
|
|
|
|
|
if (this.members_in_channel[channelId]) {
|
|
|
|
|
Reflect.deleteProperty(this.members_in_channel[channelId], userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMembersInChannel(channelId = this.getCurrentId()) {
|
|
|
|
|
return Object.assign({}, this.members_in_channel[channelId]) || {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasActiveMemberInChannel(channelId = this.getCurrentId(), userId) {
|
|
|
|
|
if (this.members_in_channel[channelId] && this.members_in_channel[channelId][userId]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-15 11:40:46 -05:00
|
|
|
storeMoreChannels(channels, teamId = TeamStore.getCurrentId()) {
|
2016-11-24 09:35:09 -05:00
|
|
|
const newChannels = {};
|
|
|
|
|
for (let i = 0; i < channels.length; i++) {
|
|
|
|
|
newChannels[channels[i].id] = channels[i];
|
|
|
|
|
}
|
2016-12-15 11:40:46 -05:00
|
|
|
this.moreChannels[teamId] = Object.assign({}, this.moreChannels[teamId], newChannels);
|
2016-11-24 09:35:09 -05:00
|
|
|
}
|
|
|
|
|
|
2016-12-15 11:40:46 -05:00
|
|
|
removeMoreChannel(channelId, teamId = TeamStore.getCurrentId()) {
|
|
|
|
|
Reflect.deleteProperty(this.moreChannels[teamId], channelId);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-12-15 11:40:46 -05:00
|
|
|
getMoreChannels(teamId = TeamStore.getCurrentId()) {
|
|
|
|
|
return Object.assign({}, this.moreChannels[teamId]);
|
2016-11-24 09:35:09 -05:00
|
|
|
}
|
|
|
|
|
|
2016-12-15 11:40:46 -05:00
|
|
|
getMoreChannelsList(teamId = TeamStore.getCurrentId()) {
|
|
|
|
|
const teamChannels = this.moreChannels[teamId] || {};
|
2017-01-18 20:46:21 +00:00
|
|
|
|
|
|
|
|
if (!Utils) {
|
|
|
|
|
Utils = require('utils/utils.jsx'); //eslint-disable-line global-require
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.keys(teamChannels).map((cid) => teamChannels[cid]).sort(Utils.sortByDisplayName);
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
storeStats(stats) {
|
|
|
|
|
this.stats = stats;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
isDefault(channel) {
|
2015-08-11 11:55:23 -04:00
|
|
|
return channel.name === Constants.DEFAULT_CHANNEL;
|
|
|
|
|
}
|
2015-11-18 17:29:06 -05:00
|
|
|
|
2016-01-26 12:03:52 -05:00
|
|
|
setPostMode(mode) {
|
2015-11-18 17:29:06 -05:00
|
|
|
this.postMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPostMode() {
|
|
|
|
|
return this.postMode;
|
|
|
|
|
}
|
2015-12-02 10:22:39 -05:00
|
|
|
|
2016-10-27 12:24:30 -03:00
|
|
|
setUnreadCountsByMembers(members) {
|
|
|
|
|
members.forEach((m) => {
|
|
|
|
|
this.setUnreadCountByChannel(m.channel_id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 14:55:05 -05:00
|
|
|
setUnreadCountsByCurrentMembers() {
|
|
|
|
|
Object.keys(this.myChannelMembers).forEach((key) => {
|
|
|
|
|
this.setUnreadCountByChannel(this.myChannelMembers[key].channel_id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 09:36:41 -04:00
|
|
|
setUnreadCountsByChannels(channels) {
|
|
|
|
|
channels.forEach((c) => {
|
|
|
|
|
this.setUnreadCountByChannel(c.id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 12:24:30 -03:00
|
|
|
setUnreadCountByChannel(id) {
|
2015-12-02 10:22:39 -05:00
|
|
|
const ch = this.get(id);
|
2016-10-19 14:49:25 -04:00
|
|
|
const chMember = this.getMyMember(id);
|
2015-12-02 10:22:39 -05:00
|
|
|
|
2016-10-28 12:41:28 -04:00
|
|
|
if (ch == null || chMember == null) {
|
2016-10-28 09:36:41 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-14 09:17:25 -04:00
|
|
|
const chMentionCount = chMember.mention_count;
|
2016-08-18 18:45:35 -04:00
|
|
|
let chUnreadCount = ch.total_msg_count - chMember.msg_count;
|
2015-12-02 10:22:39 -05:00
|
|
|
|
2016-10-14 09:17:25 -04:00
|
|
|
if (chMember.notify_props && chMember.notify_props.mark_unread === NotificationPrefs.MENTION) {
|
2015-12-02 10:22:39 -05:00
|
|
|
chUnreadCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.unreadCounts[id] = {msgs: chUnreadCount, mentions: chMentionCount};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUnreadCount(id) {
|
|
|
|
|
return this.unreadCounts[id] || {msgs: 0, mentions: 0};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUnreadCounts() {
|
|
|
|
|
return this.unreadCounts;
|
|
|
|
|
}
|
2016-04-27 16:02:58 -04:00
|
|
|
|
2016-09-19 13:21:22 +01:00
|
|
|
getChannelNamesMap() {
|
|
|
|
|
var channelNamesMap = {};
|
|
|
|
|
|
|
|
|
|
var channels = this.getChannels();
|
|
|
|
|
for (var key in channels) {
|
|
|
|
|
if (channels.hasOwnProperty(key)) {
|
|
|
|
|
var channel = channels[key];
|
|
|
|
|
channelNamesMap[channel.name] = channel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var moreChannels = this.getMoreChannels();
|
|
|
|
|
for (var moreKey in moreChannels) {
|
|
|
|
|
if (moreChannels.hasOwnProperty(moreKey)) {
|
|
|
|
|
var moreChannel = moreChannels[moreKey];
|
|
|
|
|
channelNamesMap[moreChannel.name] = moreChannel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return channelNamesMap;
|
|
|
|
|
}
|
2017-01-15 15:40:43 +00:00
|
|
|
|
|
|
|
|
isChannelAdminForCurrentChannel() {
|
|
|
|
|
return this.isChannelAdmin(UserStore.getCurrentId(), this.getCurrentId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isChannelAdmin(userId, channelId) {
|
|
|
|
|
if (!Utils) {
|
|
|
|
|
Utils = require('utils/utils.jsx'); //eslint-disable-line global-require
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const channelMembers = this.getMembersInChannel(channelId);
|
|
|
|
|
const channelMember = channelMembers[userId];
|
|
|
|
|
|
|
|
|
|
if (channelMember) {
|
|
|
|
|
return Utils.isChannelAdmin(channelMember.roles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ChannelStore = new ChannelStoreClass();
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
ChannelStore.dispatchToken = AppDispatcher.register((payload) => {
|
2015-08-11 11:55:23 -04:00
|
|
|
var action = payload.action;
|
|
|
|
|
var currentId;
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
switch (action.type) {
|
|
|
|
|
case ActionTypes.CLICK_CHANNEL:
|
|
|
|
|
ChannelStore.setCurrentId(action.id);
|
2016-01-26 12:03:52 -05:00
|
|
|
ChannelStore.setPostMode(ChannelStore.POST_MODE_CHANNEL);
|
2015-09-02 10:42:26 -04:00
|
|
|
ChannelStore.emitChange();
|
|
|
|
|
break;
|
|
|
|
|
|
2016-02-08 10:33:59 -05:00
|
|
|
case ActionTypes.RECEIVED_FOCUSED_POST: {
|
2015-11-18 17:29:06 -05:00
|
|
|
const post = action.post_list.posts[action.postId];
|
|
|
|
|
ChannelStore.setCurrentId(post.channel_id);
|
2016-01-26 12:03:52 -05:00
|
|
|
ChannelStore.setPostMode(ChannelStore.POST_MODE_FOCUS);
|
2015-11-18 17:29:06 -05:00
|
|
|
ChannelStore.emitChange();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 10:33:59 -05:00
|
|
|
case ActionTypes.RECEIVED_CHANNELS:
|
2016-01-26 12:03:52 -05:00
|
|
|
ChannelStore.storeChannels(action.channels);
|
2016-10-28 09:36:41 -04:00
|
|
|
ChannelStore.setUnreadCountsByChannels(action.channels);
|
2015-09-02 10:42:26 -04:00
|
|
|
ChannelStore.emitChange();
|
|
|
|
|
break;
|
|
|
|
|
|
2016-02-08 10:33:59 -05:00
|
|
|
case ActionTypes.RECEIVED_CHANNEL:
|
2016-10-19 14:49:25 -04:00
|
|
|
ChannelStore.storeChannel(action.channel);
|
2015-12-09 16:35:49 -05:00
|
|
|
if (action.member) {
|
2016-10-19 14:49:25 -04:00
|
|
|
ChannelStore.storeMyChannelMember(action.member);
|
2015-12-09 16:35:49 -05:00
|
|
|
}
|
2015-09-02 10:42:26 -04:00
|
|
|
currentId = ChannelStore.getCurrentId();
|
2016-02-12 14:23:12 -05:00
|
|
|
if (currentId && window.isActive) {
|
2015-09-02 10:42:26 -04:00
|
|
|
ChannelStore.resetCounts(currentId);
|
|
|
|
|
}
|
2016-10-27 12:24:30 -03:00
|
|
|
ChannelStore.setUnreadCountByChannel(action.channel.id);
|
2015-09-02 10:42:26 -04:00
|
|
|
ChannelStore.emitChange();
|
|
|
|
|
break;
|
2015-07-27 11:30:03 -04:00
|
|
|
|
2016-10-27 12:24:30 -03:00
|
|
|
case ActionTypes.RECEIVED_MY_CHANNEL_MEMBERS:
|
|
|
|
|
ChannelStore.storeMyChannelMembersList(action.members);
|
|
|
|
|
currentId = ChannelStore.getCurrentId();
|
|
|
|
|
if (currentId && window.isActive) {
|
|
|
|
|
ChannelStore.resetCounts(currentId);
|
|
|
|
|
}
|
|
|
|
|
ChannelStore.setUnreadCountsByMembers(action.members);
|
2016-11-01 08:34:52 -04:00
|
|
|
ChannelStore.emitChange();
|
2016-11-21 12:17:58 -05:00
|
|
|
ChannelStore.emitLastViewed();
|
2016-10-27 12:24:30 -03:00
|
|
|
break;
|
2016-11-11 14:55:05 -05:00
|
|
|
case ActionTypes.RECEIVED_CHANNEL_MEMBER:
|
|
|
|
|
ChannelStore.storeMyChannelMember(action.member);
|
|
|
|
|
currentId = ChannelStore.getCurrentId();
|
|
|
|
|
if (currentId && window.isActive) {
|
|
|
|
|
ChannelStore.resetCounts(currentId);
|
|
|
|
|
}
|
|
|
|
|
ChannelStore.setUnreadCountsByCurrentMembers();
|
|
|
|
|
ChannelStore.emitChange();
|
2016-11-21 12:17:58 -05:00
|
|
|
ChannelStore.emitLastViewed();
|
2016-11-11 14:55:05 -05:00
|
|
|
break;
|
2016-02-08 10:33:59 -05:00
|
|
|
case ActionTypes.RECEIVED_MORE_CHANNELS:
|
2016-01-26 12:03:52 -05:00
|
|
|
ChannelStore.storeMoreChannels(action.channels);
|
2016-11-01 12:58:04 -04:00
|
|
|
ChannelStore.emitChange();
|
2015-09-02 10:42:26 -04:00
|
|
|
break;
|
2017-01-15 15:40:43 +00:00
|
|
|
case ActionTypes.RECEIVED_MEMBERS_IN_CHANNEL:
|
|
|
|
|
ChannelStore.saveMembersInChannel(action.channel_id, action.channel_members);
|
|
|
|
|
ChannelStore.emitChange();
|
|
|
|
|
break;
|
2016-10-19 14:49:25 -04:00
|
|
|
case ActionTypes.RECEIVED_CHANNEL_STATS:
|
|
|
|
|
var stats = Object.assign({}, ChannelStore.getStats());
|
|
|
|
|
stats[action.stats.channel_id] = action.stats;
|
|
|
|
|
ChannelStore.storeStats(stats);
|
|
|
|
|
ChannelStore.emitStatsChange();
|
2015-09-02 10:42:26 -04:00
|
|
|
break;
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-09-02 10:42:26 -04:00
|
|
|
default:
|
|
|
|
|
break;
|
2015-08-11 11:55:23 -04:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
});
|
|
|
|
|
|
2016-11-11 14:55:05 -05:00
|
|
|
export default ChannelStore;
|