2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2020-12-24 01:58:27 +00:00
|
|
|
DeletePrivateMessage,
|
|
|
|
MarkPrivateMessageAsRead,
|
2021-03-15 18:09:31 +00:00
|
|
|
PersonSafe,
|
2021-07-17 20:42:55 +00:00
|
|
|
PrivateMessageView,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
|
|
|
import { authField, mdToHtml, toast, wsClient } from "../../utils";
|
|
|
|
import { Icon } from "../common/icon";
|
|
|
|
import { MomentTime } from "../common/moment-time";
|
|
|
|
import { PersonListing } from "../person/person-listing";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { PrivateMessageForm } from "./private-message-form";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface PrivateMessageState {
|
|
|
|
showReply: boolean;
|
|
|
|
showEdit: boolean;
|
|
|
|
collapsed: boolean;
|
|
|
|
viewSource: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PrivateMessageProps {
|
2020-12-24 01:58:27 +00:00
|
|
|
private_message_view: PrivateMessageView;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PrivateMessage extends Component<
|
|
|
|
PrivateMessageProps,
|
|
|
|
PrivateMessageState
|
|
|
|
> {
|
|
|
|
private emptyState: PrivateMessageState = {
|
|
|
|
showReply: false,
|
|
|
|
showEdit: false,
|
|
|
|
collapsed: false,
|
|
|
|
viewSource: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.handleReplyCancel = this.handleReplyCancel.bind(this);
|
2021-07-17 20:42:55 +00:00
|
|
|
this.handlePrivateMessageCreate =
|
|
|
|
this.handlePrivateMessageCreate.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.handlePrivateMessageEdit = this.handlePrivateMessageEdit.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
get mine(): boolean {
|
|
|
|
return (
|
2021-08-20 02:56:18 +00:00
|
|
|
UserService.Instance.myUserInfo &&
|
|
|
|
UserService.Instance.myUserInfo.local_user_view.person.id ==
|
2021-03-15 18:09:31 +00:00
|
|
|
this.props.private_message_view.creator.id
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-12-24 01:58:27 +00:00
|
|
|
let message_view = this.props.private_message_view;
|
|
|
|
// TODO check this again
|
2021-03-15 18:09:31 +00:00
|
|
|
let otherPerson: PersonSafe = this.mine
|
2020-12-24 01:58:27 +00:00
|
|
|
? message_view.recipient
|
|
|
|
: message_view.creator;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div class="border-top border-light">
|
|
|
|
<div>
|
|
|
|
<ul class="list-inline mb-0 text-muted small">
|
|
|
|
{/* TODO refactor this */}
|
|
|
|
<li className="list-inline-item">
|
2021-02-22 02:39:04 +00:00
|
|
|
{this.mine ? i18n.t("to") : i18n.t("from")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
2021-03-15 18:09:31 +00:00
|
|
|
<PersonListing person={otherPerson} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span>
|
2020-12-24 01:58:27 +00:00
|
|
|
<MomentTime data={message_view.private_message} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<div
|
2021-02-06 20:20:41 +00:00
|
|
|
role="button"
|
2020-09-06 16:15:25 +00:00
|
|
|
className="pointer text-monospace"
|
|
|
|
onClick={linkEvent(this, this.handleMessageCollapse)}
|
|
|
|
>
|
|
|
|
{this.state.collapsed ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="plus-square" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="minus-square" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
{this.state.showEdit && (
|
|
|
|
<PrivateMessageForm
|
2021-03-15 18:09:31 +00:00
|
|
|
recipient={otherPerson}
|
2020-12-24 01:58:27 +00:00
|
|
|
privateMessage={message_view}
|
2020-09-06 16:15:25 +00:00
|
|
|
onEdit={this.handlePrivateMessageEdit}
|
|
|
|
onCreate={this.handlePrivateMessageCreate}
|
|
|
|
onCancel={this.handleReplyCancel}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{!this.state.showEdit && !this.state.collapsed && (
|
|
|
|
<div>
|
|
|
|
{this.state.viewSource ? (
|
|
|
|
<pre>{this.messageUnlessRemoved}</pre>
|
|
|
|
) : (
|
|
|
|
<div
|
|
|
|
className="md-div"
|
|
|
|
dangerouslySetInnerHTML={mdToHtml(this.messageUnlessRemoved)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<ul class="list-inline mb-0 text-muted font-weight-bold">
|
|
|
|
{!this.mine && (
|
|
|
|
<>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<button
|
|
|
|
class="btn btn-link btn-animate text-muted"
|
|
|
|
onClick={linkEvent(this, this.handleMarkRead)}
|
|
|
|
data-tippy-content={
|
2020-12-24 01:58:27 +00:00
|
|
|
message_view.private_message.read
|
2021-02-22 02:39:04 +00:00
|
|
|
? i18n.t("mark_as_unread")
|
|
|
|
: i18n.t("mark_as_read")
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2021-02-06 20:20:41 +00:00
|
|
|
aria-label={
|
|
|
|
message_view.private_message.read
|
2021-02-22 02:39:04 +00:00
|
|
|
? i18n.t("mark_as_unread")
|
|
|
|
: i18n.t("mark_as_read")
|
2021-02-06 20:20:41 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon
|
|
|
|
icon="check"
|
|
|
|
classes={`icon-inline ${
|
2021-02-22 02:39:04 +00:00
|
|
|
message_view.private_message.read && "text-success"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
2021-02-11 20:35:27 +00:00
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<button
|
|
|
|
class="btn btn-link btn-animate text-muted"
|
|
|
|
onClick={linkEvent(this, this.handleReplyClick)}
|
2021-02-22 02:39:04 +00:00
|
|
|
data-tippy-content={i18n.t("reply")}
|
|
|
|
aria-label={i18n.t("reply")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="reply1" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{this.mine && (
|
|
|
|
<>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<button
|
|
|
|
class="btn btn-link btn-animate text-muted"
|
|
|
|
onClick={linkEvent(this, this.handleEditClick)}
|
2021-02-22 02:39:04 +00:00
|
|
|
data-tippy-content={i18n.t("edit")}
|
|
|
|
aria-label={i18n.t("edit")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="edit" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<button
|
|
|
|
class="btn btn-link btn-animate text-muted"
|
|
|
|
onClick={linkEvent(this, this.handleDeleteClick)}
|
|
|
|
data-tippy-content={
|
2020-12-24 01:58:27 +00:00
|
|
|
!message_view.private_message.deleted
|
2021-02-22 02:39:04 +00:00
|
|
|
? i18n.t("delete")
|
|
|
|
: i18n.t("restore")
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2021-02-06 20:20:41 +00:00
|
|
|
aria-label={
|
|
|
|
!message_view.private_message.deleted
|
2021-02-22 02:39:04 +00:00
|
|
|
? i18n.t("delete")
|
|
|
|
: i18n.t("restore")
|
2021-02-06 20:20:41 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon
|
|
|
|
icon="trash"
|
|
|
|
classes={`icon-inline ${
|
2020-12-24 01:58:27 +00:00
|
|
|
message_view.private_message.deleted &&
|
2021-02-22 02:39:04 +00:00
|
|
|
"text-danger"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
2021-02-11 20:35:27 +00:00
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<button
|
|
|
|
class="btn btn-link btn-animate text-muted"
|
|
|
|
onClick={linkEvent(this, this.handleViewSource)}
|
2021-02-22 02:39:04 +00:00
|
|
|
data-tippy-content={i18n.t("view_source")}
|
|
|
|
aria-label={i18n.t("view_source")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon
|
|
|
|
icon="file-text"
|
|
|
|
classes={`icon-inline ${
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.viewSource && "text-success"
|
2020-09-06 16:15:25 +00:00
|
|
|
}`}
|
2021-02-11 20:35:27 +00:00
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
{this.state.showReply && (
|
|
|
|
<PrivateMessageForm
|
2021-03-15 18:09:31 +00:00
|
|
|
recipient={otherPerson}
|
2020-09-06 16:15:25 +00:00
|
|
|
onCreate={this.handlePrivateMessageCreate}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{/* A collapsed clearfix */}
|
|
|
|
{this.state.collapsed && <div class="row col-12"></div>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get messageUnlessRemoved(): string {
|
2020-12-24 01:58:27 +00:00
|
|
|
let message = this.props.private_message_view.private_message;
|
2021-02-22 02:39:04 +00:00
|
|
|
return message.deleted ? `*${i18n.t("deleted")}*` : message.content;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyClick(i: PrivateMessage) {
|
|
|
|
i.state.showReply = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEditClick(i: PrivateMessage) {
|
|
|
|
i.state.showEdit = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDeleteClick(i: PrivateMessage) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let form: DeletePrivateMessage = {
|
2021-01-18 22:42:41 +00:00
|
|
|
private_message_id: i.props.private_message_view.private_message.id,
|
2020-12-24 01:58:27 +00:00
|
|
|
deleted: !i.props.private_message_view.private_message.deleted,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.deletePrivateMessage(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyCancel() {
|
|
|
|
this.state.showReply = false;
|
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMarkRead(i: PrivateMessage) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let form: MarkPrivateMessageAsRead = {
|
2021-01-18 22:42:41 +00:00
|
|
|
private_message_id: i.props.private_message_view.private_message.id,
|
2020-12-24 01:58:27 +00:00
|
|
|
read: !i.props.private_message_view.private_message.read,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.markPrivateMessageAsRead(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMessageCollapse(i: PrivateMessage) {
|
|
|
|
i.state.collapsed = !i.state.collapsed;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleViewSource(i: PrivateMessage) {
|
|
|
|
i.state.viewSource = !i.state.viewSource;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePrivateMessageEdit() {
|
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
handlePrivateMessageCreate(message: PrivateMessageView) {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (
|
2021-08-20 02:56:18 +00:00
|
|
|
UserService.Instance.myUserInfo &&
|
|
|
|
message.creator.id ==
|
|
|
|
UserService.Instance.myUserInfo.local_user_view.person.id
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
|
|
|
this.state.showReply = false;
|
|
|
|
this.setState(this.state);
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("message_sent"));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|