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 PostHeader from './post_header.jsx';
|
|
|
|
|
import PostBody from './post_body.jsx';
|
|
|
|
|
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
|
|
|
|
|
import Constants from '../utils/constants.jsx';
|
|
|
|
|
import UserStore from '../stores/user_store.jsx';
|
|
|
|
|
import PostStore from '../stores/post_store.jsx';
|
|
|
|
|
import ChannelStore from '../stores/channel_store.jsx';
|
|
|
|
|
import * as client from '../utils/client.jsx';
|
|
|
|
|
import * as AsyncClient from '../utils/async_client.jsx';
|
2015-06-14 23:53:32 -08:00
|
|
|
var ActionTypes = Constants.ActionTypes;
|
2015-11-19 21:12:56 -05:00
|
|
|
import * as utils from '../utils/utils.jsx';
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
export default class Post extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.handleCommentClick = this.handleCommentClick.bind(this);
|
|
|
|
|
this.forceUpdateInfo = this.forceUpdateInfo.bind(this);
|
|
|
|
|
this.retryPost = this.retryPost.bind(this);
|
|
|
|
|
|
|
|
|
|
this.state = {};
|
|
|
|
|
}
|
|
|
|
|
handleCommentClick(e) {
|
2015-06-14 23:53:32 -08:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
2015-07-11 08:32:02 -07:00
|
|
|
var data = {};
|
2015-06-14 23:53:32 -08:00
|
|
|
data.order = [this.props.post.id];
|
|
|
|
|
data.posts = this.props.posts;
|
|
|
|
|
|
|
|
|
|
AppDispatcher.handleServerAction({
|
|
|
|
|
type: ActionTypes.RECIEVED_POST_SELECTED,
|
|
|
|
|
post_list: data
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AppDispatcher.handleServerAction({
|
|
|
|
|
type: ActionTypes.RECIEVED_SEARCH,
|
|
|
|
|
results: null
|
|
|
|
|
});
|
2015-08-31 10:10:22 -07:00
|
|
|
}
|
|
|
|
|
forceUpdateInfo() {
|
2015-07-20 08:58:18 -04:00
|
|
|
this.refs.info.forceUpdate();
|
|
|
|
|
this.refs.header.forceUpdate();
|
2015-08-31 10:10:22 -07:00
|
|
|
}
|
|
|
|
|
retryPost(e) {
|
2015-07-23 09:39:29 -04:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
var post = this.props.post;
|
|
|
|
|
client.createPost(post, post.channel_id,
|
2015-09-23 12:25:01 -04:00
|
|
|
(data) => {
|
2015-08-26 12:09:01 -04:00
|
|
|
AsyncClient.getPosts();
|
2015-07-23 09:39:29 -04:00
|
|
|
|
2015-08-12 12:45:15 -04:00
|
|
|
var channel = ChannelStore.get(post.channel_id);
|
2015-07-23 09:39:29 -04:00
|
|
|
var member = ChannelStore.getMember(post.channel_id);
|
|
|
|
|
member.msg_count = channel.total_msg_count;
|
2015-08-31 10:10:22 -07:00
|
|
|
member.last_viewed_at = utils.getTimestamp();
|
2015-07-23 09:39:29 -04:00
|
|
|
ChannelStore.setChannelMember(member);
|
2015-08-12 14:56:37 -04:00
|
|
|
|
|
|
|
|
AppDispatcher.handleServerAction({
|
|
|
|
|
type: ActionTypes.RECIEVED_POST,
|
|
|
|
|
post: data
|
|
|
|
|
});
|
2015-08-31 10:10:22 -07:00
|
|
|
},
|
2015-09-23 12:25:01 -04:00
|
|
|
() => {
|
2015-08-12 12:45:15 -04:00
|
|
|
post.state = Constants.POST_FAILED;
|
2015-07-23 09:39:29 -04:00
|
|
|
PostStore.updatePendingPost(post);
|
|
|
|
|
this.forceUpdate();
|
2015-09-23 12:25:01 -04:00
|
|
|
}
|
2015-07-23 09:39:29 -04:00
|
|
|
);
|
|
|
|
|
|
2015-08-12 12:45:15 -04:00
|
|
|
post.state = Constants.POST_LOADING;
|
2015-07-23 09:39:29 -04:00
|
|
|
PostStore.updatePendingPost(post);
|
|
|
|
|
this.forceUpdate();
|
2015-08-31 10:10:22 -07:00
|
|
|
}
|
|
|
|
|
shouldComponentUpdate(nextProps) {
|
2015-11-12 11:19:59 -05:00
|
|
|
if (!utils.areObjectsEqual(nextProps.post, this.props.post)) {
|
2015-08-26 12:09:01 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 12:25:01 -04:00
|
|
|
if (nextProps.sameRoot !== this.props.sameRoot) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-09-23 12:25:01 -04:00
|
|
|
if (nextProps.sameUser !== this.props.sameUser) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 17:14:13 -08:00
|
|
|
if (nextProps.displayNameType !== this.props.displayNameType) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 12:25:01 -04:00
|
|
|
if (this.getCommentCount(nextProps) !== this.getCommentCount(this.props)) {
|
|
|
|
|
return true;
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-12-03 09:22:20 -05:00
|
|
|
if (nextProps.shouldHighlight !== this.props.shouldHighlight) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 12:25:01 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
getCommentCount(props) {
|
|
|
|
|
const post = props.post;
|
|
|
|
|
const parentPost = props.parentPost;
|
|
|
|
|
const posts = props.posts;
|
|
|
|
|
|
|
|
|
|
let commentCount = 0;
|
|
|
|
|
let commentRootId;
|
2015-08-31 10:10:22 -07:00
|
|
|
if (parentPost) {
|
|
|
|
|
commentRootId = post.root_id;
|
|
|
|
|
} else {
|
|
|
|
|
commentRootId = post.id;
|
|
|
|
|
}
|
2015-11-18 17:29:06 -05:00
|
|
|
for (const postId in posts) {
|
2015-08-31 10:10:22 -07:00
|
|
|
if (posts[postId].root_id === commentRootId) {
|
2015-06-14 23:53:32 -08:00
|
|
|
commentCount += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 12:25:01 -04:00
|
|
|
return commentCount;
|
|
|
|
|
}
|
|
|
|
|
render() {
|
2015-11-18 17:29:06 -05:00
|
|
|
const post = this.props.post;
|
|
|
|
|
const parentPost = this.props.parentPost;
|
|
|
|
|
const posts = this.props.posts;
|
2015-09-23 12:25:01 -04:00
|
|
|
|
2015-10-22 08:45:28 -04:00
|
|
|
if (!post.props) {
|
|
|
|
|
post.props = {};
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
let type = 'Post';
|
2015-09-23 12:25:01 -04:00
|
|
|
if (post.root_id && post.root_id.length > 0) {
|
|
|
|
|
type = 'Comment';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const commentCount = this.getCommentCount(this.props);
|
|
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
let rootUser;
|
2015-08-31 10:10:22 -07:00
|
|
|
if (this.props.sameRoot) {
|
|
|
|
|
rootUser = 'same--root';
|
|
|
|
|
} else {
|
|
|
|
|
rootUser = 'other--root';
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
let postType = '';
|
2015-08-31 10:10:22 -07:00
|
|
|
if (type !== 'Post') {
|
2015-08-26 12:09:01 -04:00
|
|
|
postType = 'post--comment';
|
2015-11-19 01:11:06 +05:00
|
|
|
} else if (commentCount > 0) {
|
|
|
|
|
postType = 'post--root';
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
let currentUserCss = '';
|
2015-12-04 03:39:37 +01:00
|
|
|
if (UserStore.getCurrentId() === post.user_id && !post.props.from_webhook && !utils.isSystemMessage(post)) {
|
2015-08-26 12:09:01 -04:00
|
|
|
currentUserCss = 'current--user';
|
2015-07-02 11:13:38 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
const userProfile = UserStore.getProfile(post.user_id);
|
2015-08-24 12:58:04 -07:00
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
let timestamp = UserStore.getCurrentUser().update_at;
|
2015-08-24 12:58:04 -07:00
|
|
|
if (userProfile) {
|
|
|
|
|
timestamp = userProfile.update_at;
|
|
|
|
|
}
|
2015-07-09 16:37:03 -07:00
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
let sameUserClass = '';
|
2015-08-26 12:09:01 -04:00
|
|
|
if (this.props.sameUser) {
|
|
|
|
|
sameUserClass = 'same--user';
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
let shouldHighlightClass = '';
|
|
|
|
|
if (this.props.shouldHighlight) {
|
|
|
|
|
shouldHighlightClass = 'post--highlight';
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 02:07:47 +01:00
|
|
|
let systemMessageClass = '';
|
|
|
|
|
if (utils.isSystemMessage(post)) {
|
|
|
|
|
systemMessageClass = 'post--system';
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 17:29:06 -05:00
|
|
|
let profilePic = null;
|
2015-12-10 08:32:30 -05:00
|
|
|
if (!this.props.hideProfilePic) {
|
2015-10-20 14:49:42 -07:00
|
|
|
let src = '/api/v1/users/' + post.user_id + '/image?time=' + timestamp + '&' + utils.getSessionIndex();
|
2015-10-16 09:10:54 -07:00
|
|
|
if (post.props && post.props.from_webhook && global.window.mm_config.EnablePostIconOverride === 'true') {
|
2015-10-05 08:46:23 -04:00
|
|
|
if (post.props.override_icon_url) {
|
|
|
|
|
src = post.props.override_icon_url;
|
|
|
|
|
}
|
2015-12-04 02:07:47 +01:00
|
|
|
} else if (utils.isSystemMessage(post)) {
|
|
|
|
|
src = Constants.SYSTEM_MESSAGE_PROFILE_IMAGE;
|
2015-10-02 11:08:00 -04:00
|
|
|
}
|
|
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
profilePic = (
|
2015-11-19 01:11:06 +05:00
|
|
|
<img
|
|
|
|
|
src={src}
|
|
|
|
|
height='36'
|
|
|
|
|
width='36'
|
|
|
|
|
/>
|
2015-08-31 10:10:22 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
return (
|
|
|
|
|
<div>
|
2015-08-31 10:10:22 -07:00
|
|
|
<div
|
2015-09-11 08:38:18 -04:00
|
|
|
id={'post_' + post.id}
|
2015-12-04 02:07:47 +01:00
|
|
|
className={'post ' + sameUserClass + ' ' + rootUser + ' ' + postType + ' ' + currentUserCss + ' ' + shouldHighlightClass + ' ' + systemMessageClass}
|
2015-09-03 10:49:36 -04:00
|
|
|
>
|
2015-08-26 12:09:01 -04:00
|
|
|
<div className='post__content'>
|
2015-11-19 01:11:06 +05:00
|
|
|
<div className='post__img'>{profilePic}</div>
|
|
|
|
|
<div>
|
|
|
|
|
<PostHeader
|
|
|
|
|
ref='header'
|
|
|
|
|
post={post}
|
|
|
|
|
sameRoot={this.props.sameRoot}
|
|
|
|
|
commentCount={commentCount}
|
|
|
|
|
handleCommentClick={this.handleCommentClick}
|
|
|
|
|
isLastComment={this.props.isLastComment}
|
|
|
|
|
/>
|
|
|
|
|
<PostBody
|
|
|
|
|
post={post}
|
|
|
|
|
sameRoot={this.props.sameRoot}
|
|
|
|
|
parentPost={parentPost}
|
|
|
|
|
posts={posts}
|
|
|
|
|
handleCommentClick={this.handleCommentClick}
|
|
|
|
|
retryPost={this.retryPost}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2015-06-14 23:53:32 -08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-08-31 10:10:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Post.propTypes = {
|
|
|
|
|
post: React.PropTypes.object,
|
|
|
|
|
posts: React.PropTypes.object,
|
|
|
|
|
parentPost: React.PropTypes.object,
|
|
|
|
|
sameUser: React.PropTypes.bool,
|
|
|
|
|
sameRoot: React.PropTypes.bool,
|
|
|
|
|
hideProfilePic: React.PropTypes.bool,
|
2015-11-18 17:29:06 -05:00
|
|
|
isLastComment: React.PropTypes.bool,
|
2015-11-23 17:14:13 -08:00
|
|
|
shouldHighlight: React.PropTypes.bool,
|
|
|
|
|
displayNameType: React.PropTypes.string
|
2015-08-31 10:10:22 -07:00
|
|
|
};
|