// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var AsyncClient = require('../utils/async_client.jsx');
var SocketStore = require('../stores/socket_store.jsx');
var UserStore = require('../stores/user_store.jsx');
var TeamStore = require('../stores/team_store.jsx');
var BrowserStore = require('../stores/browser_store.jsx')
var utils = require('../utils/utils.jsx');
var SidebarHeader = require('./sidebar_header.jsx');
var SearchBox = require('./search_bar.jsx');
var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;
function getStateFromStores() {
var members = ChannelStore.getAllMembers();
var team_member_map = UserStore.getActiveOnlyProfiles();
var current_id = ChannelStore.getCurrentId();
var teammates = [];
for (var id in team_member_map) {
if (id === UserStore.getCurrentId()) continue;
teammates.push(team_member_map[id]);
}
// Create lists of all read and unread direct channels
var showDirectChannels = [];
var readDirectChannels = [];
for (var i = 0; i < teammates.length; i++) {
var teammate = teammates[i];
if (teammate.id == UserStore.getCurrentId()) {
continue;
}
var channelName = "";
if (teammate.id > UserStore.getCurrentId()) {
channelName = UserStore.getCurrentId() + '__' + teammate.id;
} else {
channelName = teammate.id + '__' + UserStore.getCurrentId();
}
var channel = ChannelStore.getByName(channelName);
if (channel != null) {
channel.display_name = utils.getDisplayName(teammate);
channel.teammate_username = teammate.username;
channel.status = UserStore.getStatus(teammate.id);
var channelMember = members[channel.id];
var msg_count = channel.total_msg_count - channelMember.msg_count;
if (msg_count > 0) {
channel.unread = msg_count;
showDirectChannels.push(channel);
} else if (current_id === channel.id) {
showDirectChannels.push(channel);
} else {
readDirectChannels.push(channel);
}
} else {
var tempChannel = {};
tempChannel.fake = true;
tempChannel.name = channelName;
tempChannel.display_name = utils.getDisplayName(teammate);
tempChannel.status = UserStore.getStatus(teammate.id);
tempChannel.last_post_at = 0;
readDirectChannels.push(tempChannel);
}
}
// If we don't have MAX_DMS unread channels, sort the read list by last_post_at
if (showDirectChannels.length < Constants.MAX_DMS) {
readDirectChannels.sort(function(a,b) {
// sort by last_post_at first
if (a.last_post_at > b.last_post_at) return -1;
if (a.last_post_at < b.last_post_at) return 1;
// if last_post_at is equal, sort by name
if (a.display_name < b.display_name) return -1;
if (a.display_name > b.display_name) return 1;
return 0;
});
var index = 0;
while (showDirectChannels.length < Constants.MAX_DMS && index < readDirectChannels.length) {
showDirectChannels.push(readDirectChannels[index]);
index++;
}
readDirectChannels = readDirectChannels.slice(index);
showDirectChannels.sort(function(a,b) {
if (a.display_name < b.display_name) return -1;
if (a.display_name > b.display_name) return 1;
return 0;
});
}
return {
active_id: current_id,
channels: ChannelStore.getAll(),
members: members,
showDirectChannels: showDirectChannels,
hideDirectChannels: readDirectChannels
};
}
module.exports = React.createClass({
componentDidMount: function() {
ChannelStore.addChangeListener(this._onChange);
UserStore.addChangeListener(this._onChange);
UserStore.addStatusesChangeListener(this._onChange);
SocketStore.addChangeListener(this._onSocketChange);
$(".nav-pills__container").perfectScrollbar();
this.updateTitle();
},
componentDidUpdate: function() {
this.updateTitle();
},
componentWillUnmount: function() {
ChannelStore.removeChangeListener(this._onChange);
UserStore.removeChangeListener(this._onChange);
UserStore.removeStatusesChangeListener(this._onChange);
SocketStore.removeChangeListener(this._onSocketChange);
},
_onChange: function() {
var newState = getStateFromStores();
if (!utils.areStatesEqual(newState, this.state)) {
this.setState(newState);
}
},
_onSocketChange: function(msg) {
if (msg.action == "posted") {
if (ChannelStore.getCurrentId() === msg.channel_id) {
AsyncClient.getChannels(true, window.isActive);
} else {
AsyncClient.getChannels(true);
}
if (UserStore.getCurrentId() != msg.user_id) {
var mentions = msg.props.mentions ? JSON.parse(msg.props.mentions) : [];
var channel = ChannelStore.get(msg.channel_id);
var user = UserStore.getCurrentUser();
if (user.notify_props && ((user.notify_props.desktop === "mention" && mentions.indexOf(user.id) === -1 && channel.type !== 'D') || user.notify_props.desktop === "none")) {
return;
}
var member = ChannelStore.getMember(msg.channel_id);
if ((member.notify_level === "mention" && mentions.indexOf(user.id) === -1) || member.notify_level === "none" || member.notify_level === "quiet") {
return;
}
var username = "Someone";
if (UserStore.hasProfile(msg.user_id)) {
username = UserStore.getProfile(msg.user_id).username;
}
var title = channel ? channel.display_name : "Posted";
var repRegex = new RegExp("
", "g");
var post = JSON.parse(msg.props.post);
var msgProps = msg.props;
var msg = post.message.replace(repRegex, "\n").replace(/\n+/g, " ").replace("