2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2022-09-28 12:50:47 +00:00
|
|
|
CreatePrivateMessageReport,
|
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";
|
2023-01-04 16:56:24 +00:00
|
|
|
import { mdToHtml, myAuth, toast, wsClient } from "../../utils";
|
2021-07-17 20:42:55 +00:00
|
|
|
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;
|
2022-09-28 12:50:47 +00:00
|
|
|
showReportDialog: boolean;
|
2023-01-04 16:56:24 +00:00
|
|
|
reportReason?: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
> {
|
2023-01-04 16:56:24 +00:00
|
|
|
state: PrivateMessageState = {
|
2020-09-06 16:15:25 +00:00
|
|
|
showReply: false,
|
|
|
|
showEdit: false,
|
|
|
|
collapsed: false,
|
|
|
|
viewSource: false,
|
2022-09-28 12:50:47 +00:00
|
|
|
showReportDialog: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
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 {
|
2023-01-04 16:56:24 +00:00
|
|
|
return (
|
|
|
|
UserService.Instance.myUserInfo?.local_user_view.person.id ==
|
|
|
|
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;
|
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 (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="border-top border-light">
|
2020-09-06 16:15:25 +00:00
|
|
|
<div>
|
2022-09-22 15:03:35 +00:00
|
|
|
<ul className="list-inline mb-0 text-muted small">
|
2020-09-06 16:15:25 +00:00
|
|
|
{/* 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>
|
2022-06-21 21:42:29 +00:00
|
|
|
<MomentTime
|
|
|
|
published={message_view.private_message.published}
|
|
|
|
updated={message_view.private_message.updated}
|
|
|
|
/>
|
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}
|
2023-01-04 16:56:24 +00:00
|
|
|
privateMessageView={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)}
|
|
|
|
/>
|
|
|
|
)}
|
2022-09-22 15:03:35 +00:00
|
|
|
<ul className="list-inline mb-0 text-muted font-weight-bold">
|
2020-09-06 16:15:25 +00:00
|
|
|
{!this.mine && (
|
|
|
|
<>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-link btn-animate text-muted"
|
2020-09-06 16:15:25 +00:00
|
|
|
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>
|
2022-09-28 12:50:47 +00:00
|
|
|
<li className="list-inline-item">{this.reportButton}</li>
|
2020-09-06 16:15:25 +00:00
|
|
|
<li className="list-inline-item">
|
|
|
|
<button
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-link btn-animate text-muted"
|
2020-09-06 16:15:25 +00:00
|
|
|
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
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-link btn-animate text-muted"
|
2020-09-06 16:15:25 +00:00
|
|
|
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
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-link btn-animate text-muted"
|
2020-09-06 16:15:25 +00:00
|
|
|
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
|
2022-09-22 15:03:35 +00:00
|
|
|
className="btn btn-link btn-animate text-muted"
|
2020-09-06 16:15:25 +00:00
|
|
|
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>
|
2022-09-28 12:50:47 +00:00
|
|
|
{this.state.showReportDialog && (
|
|
|
|
<form
|
|
|
|
className="form-inline"
|
|
|
|
onSubmit={linkEvent(this, this.handleReportSubmit)}
|
|
|
|
>
|
|
|
|
<label className="sr-only" htmlFor="pm-report-reason">
|
|
|
|
{i18n.t("reason")}
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="pm-report-reason"
|
|
|
|
className="form-control mr-2"
|
|
|
|
placeholder={i18n.t("reason")}
|
|
|
|
required
|
2023-01-04 16:56:24 +00:00
|
|
|
value={this.state.reportReason}
|
2022-09-28 12:50:47 +00:00
|
|
|
onInput={linkEvent(this, this.handleReportReasonChange)}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-secondary"
|
|
|
|
aria-label={i18n.t("create_report")}
|
|
|
|
>
|
|
|
|
{i18n.t("create_report")}
|
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
{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 */}
|
2022-09-22 15:03:35 +00:00
|
|
|
{this.state.collapsed && <div className="row col-12"></div>}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:50:47 +00:00
|
|
|
get reportButton() {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className="btn btn-link btn-animate text-muted py-0"
|
|
|
|
onClick={linkEvent(this, this.handleShowReportDialog)}
|
|
|
|
data-tippy-content={i18n.t("show_report_dialog")}
|
|
|
|
aria-label={i18n.t("show_report_dialog")}
|
|
|
|
>
|
|
|
|
<Icon icon="flag" inline />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
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) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ showReply: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleEditClick(i: PrivateMessage) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ showEdit: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDeleteClick(i: PrivateMessage) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let auth = myAuth();
|
|
|
|
if (auth) {
|
|
|
|
let form: DeletePrivateMessage = {
|
|
|
|
private_message_id: i.props.private_message_view.private_message.id,
|
|
|
|
deleted: !i.props.private_message_view.private_message.deleted,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(wsClient.deletePrivateMessage(form));
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyCancel() {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ showReply: false, showEdit: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMarkRead(i: PrivateMessage) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let auth = myAuth();
|
|
|
|
if (auth) {
|
|
|
|
let form: MarkPrivateMessageAsRead = {
|
|
|
|
private_message_id: i.props.private_message_view.private_message.id,
|
|
|
|
read: !i.props.private_message_view.private_message.read,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(wsClient.markPrivateMessageAsRead(form));
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMessageCollapse(i: PrivateMessage) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ collapsed: !i.state.collapsed });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleViewSource(i: PrivateMessage) {
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ viewSource: !i.state.viewSource });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 12:50:47 +00:00
|
|
|
handleShowReportDialog(i: PrivateMessage) {
|
|
|
|
i.setState({ showReportDialog: !i.state.showReportDialog });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleReportReasonChange(i: PrivateMessage, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState({ reportReason: event.target.value });
|
2022-09-28 12:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleReportSubmit(i: PrivateMessage, event: any) {
|
|
|
|
event.preventDefault();
|
2023-01-04 16:56:24 +00:00
|
|
|
let auth = myAuth();
|
|
|
|
let reason = i.state.reportReason;
|
|
|
|
if (auth && reason) {
|
|
|
|
let form: CreatePrivateMessageReport = {
|
|
|
|
private_message_id: i.props.private_message_view.private_message.id,
|
|
|
|
reason,
|
|
|
|
auth,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.send(wsClient.createPrivateMessageReport(form));
|
2022-09-28 12:50:47 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState({ showReportDialog: false });
|
|
|
|
}
|
2022-09-28 12:50:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handlePrivateMessageEdit() {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.setState({ showEdit: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
handlePrivateMessageCreate(message: PrivateMessageView) {
|
2023-01-04 16:56:24 +00:00
|
|
|
if (
|
|
|
|
message.creator.id ==
|
|
|
|
UserService.Instance.myUserInfo?.local_user_view.person.id
|
|
|
|
) {
|
|
|
|
this.setState({ showReply: false });
|
|
|
|
toast(i18n.t("message_sent"));
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|