2016-01-22 10:12:23 +00:00
|
|
|
const React = require("react");
|
2016-02-12 16:14:30 +00:00
|
|
|
const moment = require("moment");
|
2016-01-26 10:44:34 +00:00
|
|
|
const ReactDOM = require("react-dom");
|
2016-02-03 16:12:08 +00:00
|
|
|
const Ps = require("perfect-scrollbar");
|
2016-01-22 21:39:03 +00:00
|
|
|
const ReactEmoji = require("react-emoji");
|
2016-01-26 10:44:34 +00:00
|
|
|
const ReactAutolink = require("react-autolink");
|
2016-02-03 16:12:08 +00:00
|
|
|
|
2015-12-29 02:20:34 +00:00
|
|
|
const MessageBrowser = React.createClass({
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
browserState: "",
|
|
|
|
messages: [],
|
|
|
|
page: 0,
|
|
|
|
limit: 250,
|
|
|
|
search: ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleNextPage(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const page = this.state.page;
|
|
|
|
this.setState({ page: page + 1 });
|
|
|
|
this.loadMessages();
|
|
|
|
},
|
|
|
|
|
|
|
|
handlePreviousPage(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const page = this.state.page;
|
|
|
|
if (page < 1) return;
|
|
|
|
this.setState({ page: page - 1 });
|
|
|
|
this.loadMessages();
|
|
|
|
},
|
|
|
|
|
|
|
|
pageHandlers() {
|
|
|
|
let previous;
|
|
|
|
if (this.state.page > 0) {
|
|
|
|
previous = (
|
|
|
|
<a className="btn btn-xs btn-primary add-right"
|
|
|
|
onClick={this.handlePreviousPage}>Prev</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let next;
|
|
|
|
if (this.state.messages.length === this.state.limit) {
|
|
|
|
next = (
|
|
|
|
<a className="btn btn-xs btn-primary"
|
|
|
|
onClick={this.handleNextPage}>Next</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{previous}
|
|
|
|
<span className="add-right">
|
|
|
|
{this.state.page}
|
|
|
|
</span>
|
|
|
|
{next}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
loadMessages() {
|
|
|
|
const limit = this.state.limit;
|
|
|
|
const page = this.state.page;
|
|
|
|
let data = {
|
|
|
|
limit: limit,
|
|
|
|
page: page
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.state.search.length) {
|
|
|
|
data.query = this.state.search;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ browserState: "Retrieving messages"});
|
|
|
|
$.ajax({
|
|
|
|
url: "/api/messages",
|
|
|
|
data: data
|
|
|
|
})
|
|
|
|
.done(data => {
|
|
|
|
this.setState({
|
|
|
|
messages: data.messages,
|
|
|
|
browserState: ""
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.fail(error => {
|
|
|
|
console.error(error);
|
|
|
|
this.setState({
|
|
|
|
browserState: `Unable to retrieve messages.`
|
|
|
|
});
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.loadMessages();
|
|
|
|
},
|
|
|
|
|
|
|
|
updateLimit(e) {
|
|
|
|
let newLimit = parseInt(e.target.value, 10);
|
|
|
|
if (isNaN(newLimit) || newLimit > 250) newLimit = 250;
|
|
|
|
this.setState({ limit: newLimit });
|
|
|
|
},
|
|
|
|
|
|
|
|
updateSearch(e) {
|
|
|
|
this.setState({ search: e.target.value });
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let browserState;
|
|
|
|
if (this.state.browserState.length) {
|
|
|
|
browserState = (
|
|
|
|
<div className="col-xs-7">
|
|
|
|
<div className="well">{this.state.browserState}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const messages = this.state.messages.map(message => {
|
|
|
|
return (
|
|
|
|
<tr key={message._id}>
|
2017-03-10 15:39:32 +00:00
|
|
|
<td className="col-xs-2">{(new Date(message.createdAt)).toString()}</td>
|
2015-12-29 02:20:34 +00:00
|
|
|
<td className="col-xs-3">{message.author.username}</td>
|
|
|
|
<td className="col-xs-5">{message.content}</td>
|
|
|
|
<td className="col-xs-2">{message._id}</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-xs-5">
|
|
|
|
<div className="form-horizontal">
|
|
|
|
<div className="form-group">
|
|
|
|
<label className="col-sm-3 control-label">Max Results</label>
|
|
|
|
<div className="col-sm-9">
|
|
|
|
<input type="number" className="form-control"
|
|
|
|
onChange={this.updateLimit}
|
|
|
|
value={this.state.limit}></input>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="form-group">
|
|
|
|
<label className="col-sm-3 control-label">Search Filter</label>
|
|
|
|
<div className="col-sm-9">
|
|
|
|
<input type="text" className="form-control"
|
|
|
|
onChange={this.updateSearch}
|
|
|
|
value={this.state.search}></input>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="form-group">
|
|
|
|
<div className="col-sm-offset-3 col-sm-9">
|
|
|
|
<button
|
|
|
|
className="btn btn-primary"
|
|
|
|
onClick={this.loadMessages}>Search</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-sm-offset-3 col-sm-9">
|
|
|
|
<p>Page Control</p>
|
|
|
|
{this.pageHandlers()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{browserState}
|
|
|
|
<div className="col-xs-12">
|
|
|
|
<table className="table">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Date</th>
|
|
|
|
<th>Author</th>
|
|
|
|
<th>Message</th>
|
|
|
|
<th>ID</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{messages}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-26 10:44:34 +00:00
|
|
|
const Chatroom = exports.Chatroom = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
messages: React.PropTypes.array.isRequired,
|
|
|
|
socket: React.PropTypes.object.isRequired,
|
|
|
|
user: React.PropTypes.object.isRequired
|
|
|
|
},
|
|
|
|
|
2015-10-30 12:20:24 +00:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
autoScroll: true
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:31:03 +00:00
|
|
|
componentDidMount() {
|
2015-10-30 12:20:24 +00:00
|
|
|
let self = this;
|
|
|
|
|
|
|
|
this.scrollListener = _.debounce((event) => {
|
|
|
|
self.temporarilyDisableAutoScroll(event);
|
|
|
|
}, 300, {
|
|
|
|
leading: false,
|
|
|
|
trailing: true
|
|
|
|
});
|
|
|
|
|
2016-01-26 10:44:34 +00:00
|
|
|
let node = ReactDOM.findDOMNode(this.refs.messageContainer);
|
2015-10-30 12:20:24 +00:00
|
|
|
node.addEventListener('scroll', this.scrollListener);
|
|
|
|
|
2016-04-18 23:44:24 +00:00
|
|
|
$(window).on("load", this.scrollToBottom);
|
2015-07-28 15:54:29 +00:00
|
|
|
},
|
|
|
|
|
2015-10-30 12:20:24 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
node.removeEventListener('scroll', this.scrollListener);
|
|
|
|
clearTimeout(this.disableScrollTimer);
|
|
|
|
},
|
|
|
|
|
2015-09-20 16:42:34 +00:00
|
|
|
loadMoreMessages() {
|
2016-01-26 10:44:34 +00:00
|
|
|
const earliestMessage = this.props.messages[0];
|
2015-09-20 17:05:22 +00:00
|
|
|
if (earliestMessage === undefined) return;
|
2016-01-26 10:44:34 +00:00
|
|
|
this.props.socket.emit("message:refresh", {
|
2015-09-20 17:05:22 +00:00
|
|
|
before: earliestMessage.createdAt
|
2015-09-20 16:42:34 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:31:03 +00:00
|
|
|
sendMessage(message) {
|
2016-01-26 10:44:34 +00:00
|
|
|
this.props.socket.emit("newMessage", {message: message});
|
2015-08-11 00:37:43 +00:00
|
|
|
},
|
2015-08-11 00:31:03 +00:00
|
|
|
|
2015-10-30 12:20:24 +00:00
|
|
|
clearAutoScrollTimeout() {
|
|
|
|
if (this.disableScrollTimer) clearTimeout(this.disableScrollTimer);
|
|
|
|
},
|
|
|
|
|
|
|
|
temporarilyDisableAutoScroll(event) {
|
|
|
|
let self = this;
|
|
|
|
let node = event.target;
|
|
|
|
if (node) {
|
|
|
|
if (node.scrollHeight - node.scrollTop === node.clientHeight) {
|
|
|
|
this.setState({ autoScroll: true });
|
|
|
|
this.clearAutoScrollTimeout();
|
|
|
|
}
|
|
|
|
if (node.scrollHeight - node.scrollTop - node.clientHeight < 50) return;
|
2015-10-01 13:50:31 +00:00
|
|
|
}
|
2015-10-30 12:20:24 +00:00
|
|
|
this.setState({ autoScroll: false });
|
|
|
|
this.clearAutoScrollTimeout();
|
|
|
|
this.disableScrollTimer = setTimeout(() => {
|
|
|
|
self.setState({
|
|
|
|
autoScroll: true
|
|
|
|
})
|
|
|
|
}, 10000);
|
|
|
|
},
|
|
|
|
|
2016-04-18 22:18:57 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (prevProps.messages.length < this.props.messages.length) {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
2015-10-01 13:50:31 +00:00
|
|
|
},
|
|
|
|
|
2015-08-11 00:31:03 +00:00
|
|
|
scrollToBottom() {
|
2015-10-30 12:20:24 +00:00
|
|
|
if (!this.state.autoScroll) return;
|
2016-01-26 10:44:34 +00:00
|
|
|
let node = ReactDOM.findDOMNode(this.refs.messageContainer);
|
2015-07-28 15:54:29 +00:00
|
|
|
node.scrollTop = node.scrollHeight;
|
|
|
|
},
|
2015-08-11 00:31:03 +00:00
|
|
|
|
|
|
|
render() {
|
2016-01-26 10:44:34 +00:00
|
|
|
const socket = this.props.socket;
|
|
|
|
const messages = this.props.messages.map(message => {
|
2015-10-01 13:28:58 +00:00
|
|
|
if (message) {
|
|
|
|
return <ChatMessage message={message}
|
2015-10-18 15:23:39 +00:00
|
|
|
key={message._id}
|
2016-01-26 10:44:34 +00:00
|
|
|
socket={socket}
|
2015-10-01 14:03:48 +00:00
|
|
|
user={this.props.user} />
|
2015-10-01 13:28:58 +00:00
|
|
|
}
|
|
|
|
});
|
2015-07-28 15:54:29 +00:00
|
|
|
return (
|
2016-02-03 16:12:08 +00:00
|
|
|
<div>
|
2016-04-18 23:16:10 +00:00
|
|
|
<ul className="chat" id="chatmessages" ref="messageContainer"
|
|
|
|
style={{height: this.props.containerHeight - 170}}>
|
2016-04-19 11:37:59 +00:00
|
|
|
<li className="text-center ">
|
2016-02-03 16:12:08 +00:00
|
|
|
<a href="#"
|
|
|
|
onClick={this.loadMoreMessages}
|
|
|
|
className="btn btn-primary btn-xs">
|
|
|
|
Load more messages
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
{messages}
|
|
|
|
</ul>
|
|
|
|
<div>
|
2016-01-26 10:44:34 +00:00
|
|
|
<MessageBar socket={socket}/>
|
2015-07-28 15:54:29 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-26 10:44:34 +00:00
|
|
|
const imgurRegex = /^(https?:\/\/i\.imgur\.com\/\S*\.(jpg|png))$/i;
|
|
|
|
|
|
|
|
const ChatMessage = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
user: React.PropTypes.object.isRequired,
|
|
|
|
socket: React.PropTypes.object.isRequired,
|
|
|
|
message: React.PropTypes.object.isRequired
|
|
|
|
},
|
2015-10-03 16:23:59 +00:00
|
|
|
|
2015-09-20 16:20:20 +00:00
|
|
|
mixins: [
|
2015-09-23 16:00:15 +00:00
|
|
|
ReactAutolink,
|
|
|
|
ReactEmoji
|
2015-09-20 16:20:20 +00:00
|
|
|
],
|
|
|
|
|
2015-09-20 16:33:43 +00:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
createdAt: ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-09-20 16:20:20 +00:00
|
|
|
messageContent: function () {
|
2015-09-23 16:00:15 +00:00
|
|
|
let self = this;
|
2015-10-03 16:23:59 +00:00
|
|
|
let message = self.props.message.content
|
|
|
|
if (message.match(imgurRegex)) {
|
|
|
|
return (
|
|
|
|
<div className="imgur-container">
|
|
|
|
<a href={message} target="_blank">
|
|
|
|
<img className="imgur-chat" src={message} />
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:02:26 +00:00
|
|
|
return (
|
|
|
|
<p className="wordwrap">
|
|
|
|
{
|
|
|
|
self.autolink(message, {
|
|
|
|
target: "_blank",
|
|
|
|
rel: "nofollow"
|
|
|
|
}).map((elem) => {
|
|
|
|
if (_.isString(elem)) {
|
|
|
|
return self.emojify(elem);
|
|
|
|
} else {
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
})
|
2015-09-23 16:00:15 +00:00
|
|
|
}
|
2016-04-01 13:02:26 +00:00
|
|
|
</p>
|
|
|
|
);
|
2015-09-20 16:20:20 +00:00
|
|
|
},
|
|
|
|
|
2017-03-10 21:09:12 +00:00
|
|
|
messageTime() {
|
|
|
|
let self = this;
|
|
|
|
let ts = new Date(self.props.message.createdAt);
|
|
|
|
let t = ts.toLocaleTimeString(undefined,{hour:"2-digit", minute:"2-digit"});
|
|
|
|
let d = ts.toLocaleDateString(undefined,{month: "2-digit", day:"2-digit"});
|
|
|
|
let r = "";
|
|
|
|
return r.concat(d," ", t);
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:37:43 +00:00
|
|
|
render() {
|
2015-09-14 21:03:39 +00:00
|
|
|
let deleteButton;
|
2015-10-01 10:22:23 +00:00
|
|
|
let user = this.props.user;
|
|
|
|
if (user && user.admin) {
|
2016-01-26 10:44:34 +00:00
|
|
|
deleteButton = <DeleteMessageButton messageId={this.props.message._id}
|
|
|
|
socket={this.props.socket}/>;
|
2015-09-14 21:03:39 +00:00
|
|
|
}
|
2015-07-28 15:54:29 +00:00
|
|
|
return (
|
2016-04-19 11:37:59 +00:00
|
|
|
<li className="left clearfix chat-message list-unstyled">
|
2015-07-28 15:54:29 +00:00
|
|
|
<span className="chat-img pull-left">
|
|
|
|
<img
|
2015-07-31 11:21:07 +00:00
|
|
|
src={this.props.message.author.avatar}
|
2015-07-28 15:54:29 +00:00
|
|
|
alt="User Avatar"
|
2016-02-12 16:14:30 +00:00
|
|
|
className="chat-avatar" />
|
2015-07-28 15:54:29 +00:00
|
|
|
</span>
|
|
|
|
<div className="chat-body clearfix">
|
|
|
|
<div className="header">
|
2015-09-17 20:40:54 +00:00
|
|
|
<strong className="primary-font">
|
|
|
|
{this.props.message.author.username}
|
|
|
|
</strong>
|
2015-07-28 15:54:29 +00:00
|
|
|
<small className="pull-right text-muted">
|
2015-10-03 15:23:32 +00:00
|
|
|
<span className="hidden-xs">
|
2017-03-10 21:09:12 +00:00
|
|
|
{this.messageTime()}
|
2015-10-03 15:23:32 +00:00
|
|
|
</span>
|
2016-02-12 17:05:46 +00:00
|
|
|
{deleteButton}
|
2015-07-28 15:54:29 +00:00
|
|
|
</small>
|
|
|
|
</div>
|
2016-04-01 13:02:26 +00:00
|
|
|
{this.messageContent()}
|
2015-07-28 15:54:29 +00:00
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-26 10:44:34 +00:00
|
|
|
const DeleteMessageButton = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
socket: React.PropTypes.object.isRequired
|
|
|
|
},
|
|
|
|
|
2015-09-14 21:03:39 +00:00
|
|
|
handleClick (e) {
|
|
|
|
e.preventDefault();
|
2016-01-26 10:44:34 +00:00
|
|
|
this.props.socket.emit("message:delete", {
|
2015-09-14 21:03:39 +00:00
|
|
|
id: this.props.messageId
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<a href="#" onClick={this.handleClick}>
|
|
|
|
<i className="fa fa-trash-o"></i>
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-01-26 10:44:34 +00:00
|
|
|
const MessageBar = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
socket: React.PropTypes.object.isRequired
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:37:43 +00:00
|
|
|
sendMessage(content) {
|
2016-01-26 10:44:34 +00:00
|
|
|
this.props.socket.emit("message:new", {
|
2015-07-28 15:54:29 +00:00
|
|
|
content: content
|
|
|
|
});
|
|
|
|
},
|
2015-08-11 00:37:43 +00:00
|
|
|
|
2015-11-15 17:53:12 +00:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
statusMessage: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
checkInputLength() {
|
2016-01-26 10:44:34 +00:00
|
|
|
const input = ReactDOM.findDOMNode(this.refs.content).value;
|
2015-11-15 17:53:12 +00:00
|
|
|
const currentStatusMessage = this.state.statusMessage;
|
|
|
|
if (input.length > 256) {
|
|
|
|
return this.setState({
|
|
|
|
statusMessage: "Maximum of 256 characters will be saved"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (currentStatusMessage !== null) {
|
|
|
|
this.setState({
|
|
|
|
statusMessage: null
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleInputChange() {
|
|
|
|
// Noop, later assigned as debounced method in componentWillMount
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:37:43 +00:00
|
|
|
handleSubmit(e) {
|
2015-07-28 15:54:29 +00:00
|
|
|
e.preventDefault();
|
2016-01-26 10:44:34 +00:00
|
|
|
let content = ReactDOM.findDOMNode(this.refs.content).value.trim();
|
2015-07-28 15:54:29 +00:00
|
|
|
if (!content) return;
|
2016-01-26 10:44:34 +00:00
|
|
|
ReactDOM.findDOMNode(this.refs.content).value = '';
|
2015-07-28 15:54:29 +00:00
|
|
|
this.sendMessage(content);
|
|
|
|
return;
|
|
|
|
},
|
2015-08-11 00:37:43 +00:00
|
|
|
|
2015-11-15 17:53:12 +00:00
|
|
|
componentWillMount() {
|
|
|
|
this.handleInputChange = _.debounce(this.checkInputLength, {
|
|
|
|
leading: false,
|
|
|
|
trailing: true
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-11 00:37:43 +00:00
|
|
|
render() {
|
2015-11-15 17:53:12 +00:00
|
|
|
let statusMessage;
|
|
|
|
if (this.state.statusMessage !== null) {
|
|
|
|
statusMessage = <div className="input-group">
|
|
|
|
<small>{this.state.statusMessage}</small>
|
|
|
|
</div>;
|
|
|
|
}
|
2015-07-28 15:54:29 +00:00
|
|
|
return (
|
2016-04-18 23:44:24 +00:00
|
|
|
|
2015-11-15 17:53:12 +00:00
|
|
|
<form onSubmit={this.handleSubmit} autoComplete="off">
|
2015-07-28 15:54:29 +00:00
|
|
|
<div className="input-group">
|
|
|
|
<input
|
2016-04-18 23:44:24 +00:00
|
|
|
id="message-input"
|
2015-07-28 15:54:29 +00:00
|
|
|
type="text"
|
2016-04-18 23:44:24 +00:00
|
|
|
className="form-control message-input"
|
2015-07-28 15:54:29 +00:00
|
|
|
ref="content"
|
2015-11-15 17:53:12 +00:00
|
|
|
onChange={this.handleInputChange}
|
|
|
|
autoComplete="off"
|
2015-07-28 15:54:29 +00:00
|
|
|
placeholder="Be polite please..." />
|
|
|
|
<span className="input-group-btn">
|
|
|
|
<input
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-primary"
|
|
|
|
id="btn-chat"
|
|
|
|
value="Send" />
|
|
|
|
</span>
|
|
|
|
</div>
|
2015-11-15 17:53:12 +00:00
|
|
|
{statusMessage}
|
2015-07-28 15:54:29 +00:00
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|