2019-04-03 06:49:32 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
|
|
|
import { Link } from 'inferno-router';
|
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-04-20 07:24:59 +00:00
|
|
|
import { Post, CreatePostLikeForm, PostForm as PostFormI, SavePostForm, CommunityUser, UserView } from '../interfaces';
|
2019-04-03 06:49:32 +00:00
|
|
|
import { MomentTime } from './moment-time';
|
2019-04-03 20:59:37 +00:00
|
|
|
import { PostForm } from './post-form';
|
2019-04-24 16:28:20 +00:00
|
|
|
import { mdToHtml, canMod, isMod, isImage } from '../utils';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
import { T } from 'inferno-i18next';
|
2019-04-03 06:49:32 +00:00
|
|
|
|
|
|
|
interface PostListingState {
|
2019-04-03 20:59:37 +00:00
|
|
|
showEdit: boolean;
|
2019-04-15 23:12:06 +00:00
|
|
|
showRemoveDialog: boolean;
|
|
|
|
removeReason: string;
|
2019-04-24 16:28:20 +00:00
|
|
|
imageExpanded: boolean;
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PostListingProps {
|
|
|
|
post: Post;
|
2019-04-03 20:59:37 +00:00
|
|
|
editable?: boolean;
|
2019-04-03 06:49:32 +00:00
|
|
|
showCommunity?: boolean;
|
|
|
|
showBody?: boolean;
|
2019-04-08 05:19:02 +00:00
|
|
|
viewOnly?: boolean;
|
2019-04-20 07:24:59 +00:00
|
|
|
moderators?: Array<CommunityUser>;
|
|
|
|
admins?: Array<UserView>;
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PostListing extends Component<PostListingProps, PostListingState> {
|
|
|
|
|
|
|
|
private emptyState: PostListingState = {
|
2019-04-05 00:01:10 +00:00
|
|
|
showEdit: false,
|
2019-04-15 23:12:06 +00:00
|
|
|
showRemoveDialog: false,
|
|
|
|
removeReason: null,
|
2019-04-24 16:28:20 +00:00
|
|
|
imageExpanded: false
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-04-03 06:49:32 +00:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.handlePostLike = this.handlePostLike.bind(this);
|
|
|
|
this.handlePostDisLike = this.handlePostDisLike.bind(this);
|
2019-04-03 20:59:37 +00:00
|
|
|
this.handleEditPost = this.handleEditPost.bind(this);
|
2019-04-05 02:08:21 +00:00
|
|
|
this.handleEditCancel = this.handleEditCancel.bind(this);
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2019-08-18 05:00:11 +00:00
|
|
|
<div class="row col-12">
|
2019-04-04 22:29:14 +00:00
|
|
|
{!this.state.showEdit
|
|
|
|
? this.listing()
|
2019-04-05 02:08:21 +00:00
|
|
|
: <PostForm post={this.props.post} onEdit={this.handleEditPost} onCancel={this.handleEditCancel}/>
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listing() {
|
2019-04-03 06:49:32 +00:00
|
|
|
let post = this.props.post;
|
|
|
|
return (
|
|
|
|
<div class="listing">
|
2019-08-18 02:31:32 +00:00
|
|
|
<div className={`vote-bar mr-1 float-left small text-center ${this.props.viewOnly && 'no-click'}`}>
|
2019-04-24 15:26:49 +00:00
|
|
|
<div className={`pointer ${post.my_vote == 1 ? 'text-info' : 'text-muted'}`} onClick={linkEvent(this, this.handlePostLike)}>
|
2019-07-23 03:15:00 +00:00
|
|
|
<svg class="pointer icon upvote"><use xlinkHref="#icon-arrow-up"></use></svg>
|
2019-04-24 15:26:49 +00:00
|
|
|
</div>
|
2019-05-05 22:47:35 +00:00
|
|
|
<div class={`font-weight-bold text-muted`}>{post.score}</div>
|
2019-04-25 01:03:21 +00:00
|
|
|
<div className={`pointer ${post.my_vote == -1 ? 'text-danger' : 'text-muted'}`} onClick={linkEvent(this, this.handlePostDisLike)}>
|
2019-07-23 03:15:00 +00:00
|
|
|
<svg class="pointer icon downvote"><use xlinkHref="#icon-arrow-down"></use></svg>
|
2019-04-24 15:26:49 +00:00
|
|
|
</div>
|
2019-04-03 06:49:32 +00:00
|
|
|
</div>
|
2019-04-24 17:26:15 +00:00
|
|
|
{post.url && isImage(post.url) &&
|
2019-08-18 02:17:12 +00:00
|
|
|
<span title={i18n.t('expand_here')} class="pointer" onClick={linkEvent(this, this.handleImageExpandClick)}><img class="mx-2 mt-1 float-left img-fluid thumbnail rounded" src={post.url} /></span>
|
2019-04-24 17:26:15 +00:00
|
|
|
}
|
2019-04-24 15:26:49 +00:00
|
|
|
<div className="ml-4">
|
2019-08-18 04:41:55 +00:00
|
|
|
<div className="post-title">
|
2019-08-19 21:13:15 +00:00
|
|
|
<h5 className="mb-0 d-inline">
|
2019-04-25 22:00:05 +00:00
|
|
|
{post.url ?
|
|
|
|
<a className="text-white" href={post.url} target="_blank" title={post.url}>{post.name}</a> :
|
2019-08-10 00:14:43 +00:00
|
|
|
<Link className="text-white" to={`/post/${post.id}`} title={i18n.t('comments')}>{post.name}</Link>
|
2019-04-25 22:00:05 +00:00
|
|
|
}
|
2019-08-19 21:13:15 +00:00
|
|
|
</h5>
|
2019-04-25 03:57:07 +00:00
|
|
|
{post.url &&
|
|
|
|
<small>
|
|
|
|
<a className="ml-2 text-muted font-italic" href={post.url} target="_blank" title={post.url}>{(new URL(post.url)).hostname}</a>
|
|
|
|
</small>
|
|
|
|
}
|
|
|
|
{ post.url && isImage(post.url) &&
|
|
|
|
<>
|
|
|
|
{ !this.state.imageExpanded
|
2019-08-10 00:14:43 +00:00
|
|
|
? <span class="text-monospace pointer ml-2 text-muted small" title={i18n.t('expand_here')} onClick={linkEvent(this, this.handleImageExpandClick)}>[+]</span>
|
2019-04-25 03:57:07 +00:00
|
|
|
:
|
|
|
|
<span>
|
2019-07-23 03:15:00 +00:00
|
|
|
<span class="text-monospace pointer ml-2 text-muted small" onClick={linkEvent(this, this.handleImageExpandClick)}>[-]</span>
|
2019-04-25 03:57:07 +00:00
|
|
|
<div>
|
2019-05-05 21:50:17 +00:00
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleImageExpandClick)}><img class="img-fluid" src={post.url} /></span>
|
2019-04-25 03:57:07 +00:00
|
|
|
</div>
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
</>
|
|
|
|
}
|
2019-08-17 20:18:25 +00:00
|
|
|
{post.removed &&
|
|
|
|
<small className="ml-2 text-muted font-italic"><T i18nKey="removed">#</T></small>
|
|
|
|
}
|
|
|
|
{post.deleted &&
|
|
|
|
<small className="ml-2 text-muted font-italic"><T i18nKey="deleted">#</T></small>
|
|
|
|
}
|
|
|
|
{post.locked &&
|
|
|
|
<small className="ml-2 text-muted font-italic"><T i18nKey="locked">#</T></small>
|
|
|
|
}
|
|
|
|
{post.nsfw &&
|
|
|
|
<small className="ml-2 text-muted font-italic"><T i18nKey="nsfw">#</T></small>
|
|
|
|
}
|
2019-04-25 03:57:07 +00:00
|
|
|
</div>
|
2019-04-03 06:49:32 +00:00
|
|
|
</div>
|
2019-08-18 04:47:25 +00:00
|
|
|
<div className="details ml-4">
|
2019-04-03 06:49:32 +00:00
|
|
|
<ul class="list-inline mb-0 text-muted small">
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span>by </span>
|
2019-04-25 21:52:18 +00:00
|
|
|
<Link className="text-info" to={`/u/${post.creator_name}`}>{post.creator_name}</Link>
|
2019-04-20 07:24:59 +00:00
|
|
|
{this.isMod &&
|
2019-08-10 00:14:43 +00:00
|
|
|
<span className="mx-1 badge badge-light"><T i18nKey="mod">#</T></span>
|
2019-04-20 07:24:59 +00:00
|
|
|
}
|
|
|
|
{this.isAdmin &&
|
2019-08-10 00:14:43 +00:00
|
|
|
<span className="mx-1 badge badge-light"><T i18nKey="admin">#</T></span>
|
2019-04-20 07:24:59 +00:00
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
{this.props.showCommunity &&
|
|
|
|
<span>
|
|
|
|
<span> to </span>
|
2019-04-29 02:05:11 +00:00
|
|
|
<Link to={`/c/${post.community_name}`}>{post.community_name}</Link>
|
2019-04-03 06:49:32 +00:00
|
|
|
</span>
|
|
|
|
}
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span><MomentTime data={post} /></span>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span>(
|
|
|
|
<span className="text-info">+{post.upvotes}</span>
|
|
|
|
<span> | </span>
|
|
|
|
<span className="text-danger">-{post.downvotes}</span>
|
|
|
|
<span>) </span>
|
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
2019-08-10 00:14:43 +00:00
|
|
|
<Link className="text-muted" to={`/post/${post.id}`}><T i18nKey="number_of_comments" interpolation={{count: post.number_of_comments}}>#</T></Link>
|
2019-04-03 06:49:32 +00:00
|
|
|
</li>
|
|
|
|
</ul>
|
2019-04-20 04:06:25 +00:00
|
|
|
{UserService.Instance.user && this.props.editable &&
|
2019-04-03 20:59:37 +00:00
|
|
|
<ul class="list-inline mb-1 text-muted small font-weight-bold">
|
2019-04-20 04:06:25 +00:00
|
|
|
<li className="list-inline-item mr-2">
|
2019-08-10 00:14:43 +00:00
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleSavePostClick)}>{post.saved ? i18n.t('unsave') : i18n.t('save')}</span>
|
2019-04-20 04:06:25 +00:00
|
|
|
</li>
|
2019-04-15 23:12:06 +00:00
|
|
|
{this.myPost &&
|
2019-04-20 04:06:25 +00:00
|
|
|
<>
|
2019-04-15 23:12:06 +00:00
|
|
|
<li className="list-inline-item">
|
2019-08-10 00:14:43 +00:00
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleEditClick)}><T i18nKey="edit">#</T></span>
|
2019-04-15 23:12:06 +00:00
|
|
|
</li>
|
|
|
|
<li className="list-inline-item mr-2">
|
2019-04-29 19:14:54 +00:00
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>
|
2019-08-10 00:14:43 +00:00
|
|
|
{!post.deleted ? i18n.t('delete') : i18n.t('restore')}
|
2019-04-29 19:14:54 +00:00
|
|
|
</span>
|
2019-04-15 23:12:06 +00:00
|
|
|
</li>
|
2019-04-20 04:06:25 +00:00
|
|
|
</>
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
2019-04-20 07:24:59 +00:00
|
|
|
{this.canMod &&
|
2019-04-15 23:12:06 +00:00
|
|
|
<span>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
{!this.props.post.removed ?
|
2019-08-10 00:14:43 +00:00
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleModRemoveShow)}><T i18nKey="remove">#</T></span> :
|
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleModRemoveSubmit)}><T i18nKey="restore">#</T></span>
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
2019-08-10 00:14:43 +00:00
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleModLock)}>{this.props.post.locked ? i18n.t('unlock') : i18n.t('lock')}</span>
|
2019-04-15 23:12:06 +00:00
|
|
|
</li>
|
|
|
|
</span>
|
|
|
|
}
|
2019-04-03 20:59:37 +00:00
|
|
|
</ul>
|
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
{this.state.showRemoveDialog &&
|
|
|
|
<form class="form-inline" onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
|
2019-08-10 00:14:43 +00:00
|
|
|
<input type="text" class="form-control mr-2" placeholder={i18n.t('reason')} value={this.state.removeReason} onInput={linkEvent(this, this.handleModRemoveReasonChange)} />
|
|
|
|
<button type="submit" class="btn btn-secondary"><T i18nKey="remove_post">#</T></button>
|
2019-04-15 23:12:06 +00:00
|
|
|
</form>
|
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
{this.props.showBody && this.props.post.body && <div className="md-div" dangerouslySetInnerHTML={mdToHtml(post.body)} />}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
private get myPost(): boolean {
|
2019-04-16 23:04:23 +00:00
|
|
|
return UserService.Instance.user && this.props.post.creator_id == UserService.Instance.user.id;
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2019-04-21 20:52:55 +00:00
|
|
|
get isMod(): boolean {
|
|
|
|
return this.props.moderators && isMod(this.props.moderators.map(m => m.user_id), this.props.post.creator_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
get isAdmin(): boolean {
|
|
|
|
return this.props.admins && isMod(this.props.admins.map(a => a.id), this.props.post.creator_id);
|
|
|
|
}
|
|
|
|
|
2019-04-20 07:24:59 +00:00
|
|
|
get canMod(): boolean {
|
|
|
|
|
|
|
|
if (this.props.editable) {
|
|
|
|
let adminsThenMods = this.props.admins.map(a => a.id)
|
|
|
|
.concat(this.props.moderators.map(m => m.user_id));
|
|
|
|
|
|
|
|
return canMod(UserService.Instance.user, adminsThenMods, this.props.post.creator_id);
|
|
|
|
|
|
|
|
} else return false;
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handlePostLike(i: PostListing) {
|
2019-04-03 06:49:32 +00:00
|
|
|
|
|
|
|
let form: CreatePostLikeForm = {
|
|
|
|
post_id: i.props.post.id,
|
|
|
|
score: (i.props.post.my_vote == 1) ? 0 : 1
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.likePost(form);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handlePostDisLike(i: PostListing) {
|
2019-04-03 06:49:32 +00:00
|
|
|
let form: CreatePostLikeForm = {
|
|
|
|
post_id: i.props.post.id,
|
|
|
|
score: (i.props.post.my_vote == -1) ? 0 : -1
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.likePost(form);
|
|
|
|
}
|
2019-04-03 20:59:37 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleEditClick(i: PostListing) {
|
2019-04-03 20:59:37 +00:00
|
|
|
i.state.showEdit = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:08:21 +00:00
|
|
|
handleEditCancel() {
|
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
// The actual editing is done in the recieve for post
|
2019-04-08 05:19:02 +00:00
|
|
|
handleEditPost() {
|
2019-04-03 20:59:37 +00:00
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleDeleteClick(i: PostListing) {
|
2019-04-03 20:59:37 +00:00
|
|
|
let deleteForm: PostFormI = {
|
2019-04-29 19:14:54 +00:00
|
|
|
body: i.props.post.body,
|
2019-04-03 20:59:37 +00:00
|
|
|
community_id: i.props.post.community_id,
|
2019-04-29 19:14:54 +00:00
|
|
|
name: i.props.post.name,
|
|
|
|
url: i.props.post.url,
|
2019-04-03 20:59:37 +00:00
|
|
|
edit_id: i.props.post.id,
|
2019-04-15 23:12:06 +00:00
|
|
|
creator_id: i.props.post.creator_id,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: !i.props.post.deleted,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: i.props.post.nsfw,
|
2019-04-03 20:59:37 +00:00
|
|
|
auth: null
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.editPost(deleteForm);
|
|
|
|
}
|
2019-04-05 00:01:10 +00:00
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
handleSavePostClick(i: PostListing) {
|
|
|
|
let saved = (i.props.post.saved == undefined) ? true : !i.props.post.saved;
|
|
|
|
let form: SavePostForm = {
|
|
|
|
post_id: i.props.post.id,
|
|
|
|
save: saved
|
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketService.Instance.savePost(form);
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
handleModRemoveShow(i: PostListing) {
|
|
|
|
i.state.showRemoveDialog = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleModRemoveReasonChange(i: PostListing, event: any) {
|
|
|
|
i.state.removeReason = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleModRemoveSubmit(i: PostListing) {
|
2019-04-16 23:04:23 +00:00
|
|
|
event.preventDefault();
|
2019-04-15 23:12:06 +00:00
|
|
|
let form: PostFormI = {
|
|
|
|
name: i.props.post.name,
|
|
|
|
community_id: i.props.post.community_id,
|
|
|
|
edit_id: i.props.post.id,
|
|
|
|
creator_id: i.props.post.creator_id,
|
|
|
|
removed: !i.props.post.removed,
|
|
|
|
reason: i.state.removeReason,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: i.props.post.nsfw,
|
2019-04-15 23:12:06 +00:00
|
|
|
auth: null,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.editPost(form);
|
|
|
|
|
|
|
|
i.state.showRemoveDialog = false;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleModLock(i: PostListing) {
|
|
|
|
let form: PostFormI = {
|
|
|
|
name: i.props.post.name,
|
|
|
|
community_id: i.props.post.community_id,
|
|
|
|
edit_id: i.props.post.id,
|
|
|
|
creator_id: i.props.post.creator_id,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: i.props.post.nsfw,
|
2019-04-15 23:12:06 +00:00
|
|
|
locked: !i.props.post.locked,
|
|
|
|
auth: null,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.editPost(form);
|
|
|
|
}
|
|
|
|
|
2019-04-24 16:28:20 +00:00
|
|
|
handleImageExpandClick(i: PostListing) {
|
|
|
|
i.state.imageExpanded = !i.state.imageExpanded;
|
2019-04-05 00:01:10 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|