2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2017-04-28 13:16:03 -04:00
|
|
|
import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
|
2015-11-19 21:12:56 -05:00
|
|
|
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
|
|
|
|
2017-02-23 02:33:13 +00:00
|
|
|
var ChannelUtils;
|
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';
|
2017-03-09 14:41:23 -05:00
|
|
|
import {isSystemMessage, isFromWebhook} from 'utils/post_utils.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
|
|
|
|
2017-04-28 13:16:03 -04:00
|
|
|
import store from 'stores/redux_store.jsx';
|
|
|
|
|
import * as Selectors from 'mattermost-redux/selectors/entities/channels';
|
|
|
|
|
import {ChannelTypes, UserTypes} from 'mattermost-redux/action_types';
|
2017-05-10 07:41:12 -04:00
|
|
|
import {batchActions} from 'redux-batched-actions';
|
2017-04-28 13:16:03 -04: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();
|
2017-04-28 13:16:03 -04:00
|
|
|
|
|
|
|
|
this.entities = store.getState().entities.channels;
|
|
|
|
|
|
|
|
|
|
store.subscribe(() => {
|
|
|
|
|
const newEntities = store.getState().entities.channels;
|
2017-05-01 10:49:34 -04:00
|
|
|
let doEmit = false;
|
2017-04-28 13:16:03 -04:00
|
|
|
|
2017-05-01 10:49:34 -04:00
|
|
|
if (newEntities.currentChannelId !== this.entities.currentChannelId) {
|
|
|
|
|
doEmit = true;
|
2017-04-28 13:16:03 -04:00
|
|
|
}
|
|
|
|
|
if (newEntities.channels !== this.entities.channels) {
|
2017-05-01 10:49:34 -04:00
|
|
|
this.setUnreadCountsByChannels(Object.values(newEntities.channels));
|
|
|
|
|
doEmit = true;
|
2017-04-28 13:16:03 -04:00
|
|
|
}
|
|
|
|
|
if (newEntities.myMembers !== this.entities.myMembers) {
|
|
|
|
|
this.setUnreadCountsByMembers(Object.values(newEntities.myMembers));
|
2017-05-01 10:49:34 -04:00
|
|
|
this.emitLastViewed();
|
|
|
|
|
doEmit = true;
|
2017-04-28 13:16:03 -04:00
|
|
|
}
|
|
|
|
|
if (newEntities.membersInChannel !== this.entities.membersInChannel) {
|
2017-05-01 10:49:34 -04:00
|
|
|
doEmit = true;
|
2017-04-28 13:16:03 -04:00
|
|
|
}
|
|
|
|
|
if (newEntities.stats !== this.entities.stats) {
|
|
|
|
|
this.emitStatsChange();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 10:49:34 -04:00
|
|
|
if (doEmit) {
|
|
|
|
|
this.emitChange();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-28 13:16:03 -04:00
|
|
|
this.entities = newEntities;
|
|
|
|
|
});
|
2017-01-03 12:37:47 -05:00
|
|
|
}
|
2015-09-02 10:42:26 -04:00
|
|
|
|
2017-01-03 12:37:47 -05:00
|
|
|
clear() {
|
2015-11-18 17:29:06 -05:00
|
|
|
this.postMode = this.POST_MODE_CHANNEL;
|
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) {
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.SELECT_CHANNEL,
|
2017-06-18 14:42:32 -04:00
|
|
|
data: id,
|
|
|
|
|
member: this.getMyMember(id)
|
2017-04-28 13:16:03 -04:00
|
|
|
});
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2017-05-01 10:49:34 -04:00
|
|
|
resetCounts(ids) {
|
|
|
|
|
const membersToStore = [];
|
|
|
|
|
ids.forEach((id) => {
|
|
|
|
|
const member = this.getMyMember(id);
|
|
|
|
|
const channel = this.get(id);
|
|
|
|
|
if (member && channel) {
|
|
|
|
|
const memberToStore = {...member};
|
|
|
|
|
memberToStore.msg_count = channel.total_msg_count;
|
|
|
|
|
memberToStore.mention_count = 0;
|
|
|
|
|
membersToStore.push(memberToStore);
|
|
|
|
|
this.setUnreadCountByChannel(id);
|
2015-08-11 11:55:23 -04:00
|
|
|
}
|
2017-05-01 10:49:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.storeMyChannelMembersList(membersToStore);
|
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() {
|
2017-04-28 13:16:03 -04:00
|
|
|
return Selectors.getCurrentChannelId(store.getState());
|
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) {
|
2017-04-28 13:16:03 -04:00
|
|
|
stats = Selectors.getAllChannelStats(store.getState())[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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 02:33:13 +00:00
|
|
|
if (!ChannelUtils) {
|
|
|
|
|
ChannelUtils = require('utils/channel_utils.jsx'); //eslint-disable-line global-require
|
2015-11-02 16:04:14 -08:00
|
|
|
}
|
2015-07-08 09:55:24 -04:00
|
|
|
|
2017-02-23 02:33:13 +00:00
|
|
|
channels = channels.sort(ChannelUtils.sortChannelsByDisplayName);
|
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) {
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.RECEIVED_CHANNELS,
|
|
|
|
|
data: channels,
|
|
|
|
|
teamId: channels[0].team_id
|
|
|
|
|
});
|
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() {
|
2017-04-28 13:16:03 -04:00
|
|
|
return Selectors.getMyChannels(store.getState());
|
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) {
|
2017-05-02 09:28:44 -04:00
|
|
|
return this.get(id);
|
2016-12-19 10:05:46 -03:00
|
|
|
}
|
|
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
storeMyChannelMember(channelMember) {
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
|
|
|
|
|
data: channelMember
|
|
|
|
|
});
|
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) {
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBERS,
|
|
|
|
|
data: Object.values(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) {
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBERS,
|
|
|
|
|
data: channelMembers
|
2016-10-27 12:24:30 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 14:49:25 -04:00
|
|
|
getMyMembers() {
|
2017-04-28 13:16:03 -04:00
|
|
|
return Selectors.getMyChannelMemberships(store.getState());
|
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) {
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.RECEIVED_CHANNEL_MEMBERS,
|
|
|
|
|
data: Object.values(members)
|
|
|
|
|
});
|
2017-01-15 15:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeMemberInChannel(channelId = this.getCurrentId(), userId) {
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: UserTypes.RECEIVED_PROFILE_NOT_IN_CHANNEL,
|
|
|
|
|
data: {id: channelId, user_id: userId}
|
|
|
|
|
});
|
2017-01-15 15:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMembersInChannel(channelId = this.getCurrentId()) {
|
2017-04-28 13:16:03 -04:00
|
|
|
return Selectors.getChannelMembersInChannels(store.getState())[channelId] || {};
|
2017-01-15 15:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasActiveMemberInChannel(channelId = this.getCurrentId(), userId) {
|
2017-04-28 13:16:03 -04:00
|
|
|
const members = this.getMembersInChannel(channelId);
|
|
|
|
|
if (members && members[userId]) {
|
2017-01-15 15:40:43 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-15 11:40:46 -05:00
|
|
|
storeMoreChannels(channels, teamId = TeamStore.getCurrentId()) {
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.RECEIVED_CHANNELS,
|
|
|
|
|
data: channels,
|
|
|
|
|
teamId
|
|
|
|
|
});
|
2016-11-24 09:35:09 -05:00
|
|
|
}
|
|
|
|
|
|
2017-04-28 13:16:03 -04:00
|
|
|
getMoreChannels() {
|
|
|
|
|
const channels = Selectors.getOtherChannels(store.getState());
|
|
|
|
|
const channelMap = {};
|
|
|
|
|
channels.forEach((c) => {
|
|
|
|
|
channelMap[c.id] = c;
|
|
|
|
|
});
|
|
|
|
|
return channelMap;
|
2015-09-02 10:42:26 -04:00
|
|
|
}
|
2016-07-06 08:23:24 -04:00
|
|
|
|
2017-04-28 13:16:03 -04:00
|
|
|
getMoreChannelsList() {
|
|
|
|
|
return Selectors.getOtherChannels(store.getState());
|
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() {
|
2017-04-28 13:16:03 -04:00
|
|
|
Object.keys(this.getMyMembers()).forEach((key) => {
|
|
|
|
|
this.setUnreadCountByChannel(this.getMyMember(key).channel_id);
|
2016-11-11 14:55:05 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
2017-02-24 01:15:10 +00:00
|
|
|
if (!Utils) {
|
|
|
|
|
Utils = require('utils/utils.jsx'); //eslint-disable-line global-require
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const member = this.getMyMember(this.getCurrentId());
|
|
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Utils.isChannelAdmin(member.roles);
|
2017-01-15 15:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2017-02-13 09:27:28 -05:00
|
|
|
|
2017-02-14 15:55:35 -05:00
|
|
|
incrementMessages(id, markRead = false) {
|
2017-02-13 09:27:28 -05:00
|
|
|
if (!this.unreadCounts[id]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 13:50:08 -05:00
|
|
|
const member = this.getMyMember(id);
|
|
|
|
|
if (member && member.notify_props && member.notify_props.mark_unread === NotificationPrefs.MENTION) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-28 13:16:03 -04:00
|
|
|
const channel = {...this.get(id)};
|
|
|
|
|
channel.total_msg_count++;
|
2017-02-14 15:55:35 -05:00
|
|
|
|
2017-05-10 07:41:12 -04:00
|
|
|
const actions = [];
|
2017-02-14 15:55:35 -05:00
|
|
|
if (markRead) {
|
2017-05-10 07:41:12 -04:00
|
|
|
actions.push({
|
|
|
|
|
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
|
|
|
|
|
data: {...member, msg_count: channel.total_msg_count}
|
|
|
|
|
});
|
2017-02-14 15:55:35 -05:00
|
|
|
}
|
2017-05-10 07:41:12 -04:00
|
|
|
|
|
|
|
|
actions.push(
|
|
|
|
|
{
|
|
|
|
|
type: ChannelTypes.RECEIVED_CHANNEL,
|
|
|
|
|
data: channel
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
store.dispatch(batchActions(actions));
|
2017-02-13 09:27:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
incrementMentionsIfNeeded(id, msgProps) {
|
|
|
|
|
let mentions = [];
|
|
|
|
|
if (msgProps && msgProps.mentions) {
|
|
|
|
|
mentions = JSON.parse(msgProps.mentions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.unreadCounts[id]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mentions.indexOf(UserStore.getCurrentId()) !== -1) {
|
|
|
|
|
this.unreadCounts[id].mentions++;
|
2017-04-28 13:16:03 -04:00
|
|
|
const member = {...this.getMyMember(id)};
|
|
|
|
|
member.mention_count++;
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
|
|
|
|
|
data: member
|
|
|
|
|
});
|
2017-02-13 09:27:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
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;
|
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
|
|
|
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);
|
2015-09-02 10:42:26 -04:00
|
|
|
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
|
|
|
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);
|
|
|
|
|
break;
|
2016-11-11 14:55:05 -05:00
|
|
|
case ActionTypes.RECEIVED_CHANNEL_MEMBER:
|
|
|
|
|
ChannelStore.storeMyChannelMember(action.member);
|
|
|
|
|
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);
|
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);
|
|
|
|
|
break;
|
2016-10-19 14:49:25 -04:00
|
|
|
case ActionTypes.RECEIVED_CHANNEL_STATS:
|
2017-04-28 13:16:03 -04:00
|
|
|
store.dispatch({
|
|
|
|
|
type: ChannelTypes.RECEIVED_CHANNEL_STATS,
|
|
|
|
|
data: action.stats
|
|
|
|
|
});
|
2015-09-02 10:42:26 -04:00
|
|
|
break;
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2017-02-13 09:27:28 -05:00
|
|
|
case ActionTypes.RECEIVED_POST:
|
2017-02-22 09:16:17 -05:00
|
|
|
if (Constants.IGNORE_POST_TYPES.indexOf(action.post.type) !== -1) {
|
2017-02-13 15:19:41 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 14:41:23 -05:00
|
|
|
if (action.post.user_id === UserStore.getCurrentId() && !isSystemMessage(action.post) && !isFromWebhook(action.post)) {
|
2017-02-15 21:06:13 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 09:27:28 -05:00
|
|
|
var id = action.post.channel_id;
|
2017-02-13 13:50:08 -05:00
|
|
|
var teamId = action.websocketMessageProps ? action.websocketMessageProps.team_id : null;
|
2017-02-15 21:06:13 -05:00
|
|
|
var markRead = id === ChannelStore.getCurrentId() && window.isActive;
|
2017-02-13 09:27:28 -05:00
|
|
|
|
2017-02-15 21:06:13 -05:00
|
|
|
if (TeamStore.getCurrentId() === teamId || teamId === '') {
|
2017-05-19 12:19:04 -04:00
|
|
|
if (!markRead) {
|
|
|
|
|
ChannelStore.incrementMentionsIfNeeded(id, action.websocketMessageProps);
|
|
|
|
|
}
|
2017-02-15 21:06:13 -05:00
|
|
|
ChannelStore.incrementMessages(id, markRead);
|
2017-02-13 09:27:28 -05:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2017-02-14 11:38:07 -05:00
|
|
|
case ActionTypes.CREATE_POST:
|
2017-02-14 15:55:35 -05:00
|
|
|
ChannelStore.incrementMessages(action.post.channel_id, true);
|
2017-02-14 11:38:07 -05:00
|
|
|
break;
|
|
|
|
|
|
2017-02-15 21:06:13 -05:00
|
|
|
case ActionTypes.CREATE_COMMENT:
|
|
|
|
|
ChannelStore.incrementMessages(action.post.channel_id, true);
|
|
|
|
|
break;
|
|
|
|
|
|
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;
|