2019-08-06 18:03:44 +03:00
|
|
|
/*
|
2021-06-30 15:01:26 +03:00
|
|
|
Copyright 2019 - 2021 The Matrix.org Foundation C.I.C.
|
2019-08-06 18:03:44 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2021-06-30 15:01:26 +03:00
|
|
|
|
|
|
|
import React, { ClipboardEvent, createRef, KeyboardEvent } from 'react';
|
|
|
|
import EMOJI_REGEX from 'emojibase-regex';
|
|
|
|
import { IContent, MatrixEvent } from 'matrix-js-sdk/src/models/event';
|
2021-07-01 23:10:37 +03:00
|
|
|
import { DebouncedFunc, throttle } from 'lodash';
|
|
|
|
import { EventType, RelationType } from "matrix-js-sdk/src/@types/event";
|
2021-06-30 15:01:26 +03:00
|
|
|
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from '../../../dispatcher/dispatcher';
|
2019-08-06 18:03:44 +03:00
|
|
|
import EditorModel from '../../../editor/model';
|
2019-09-02 18:56:16 +03:00
|
|
|
import {
|
|
|
|
containsEmote,
|
2021-07-01 23:10:37 +03:00
|
|
|
htmlSerializeIfNeeded,
|
2020-01-21 18:55:21 +03:00
|
|
|
startsWith,
|
2021-07-01 23:10:37 +03:00
|
|
|
stripEmoteCommand,
|
2020-01-21 18:55:21 +03:00
|
|
|
stripPrefix,
|
2021-07-01 23:10:37 +03:00
|
|
|
textSerialize,
|
|
|
|
unescapeMessage,
|
2019-09-02 18:56:16 +03:00
|
|
|
} from '../../../editor/serialize';
|
2021-06-30 15:01:26 +03:00
|
|
|
import { CommandPartCreator, Part, PartCreator, SerializedPart } from '../../../editor/parts';
|
2019-08-06 18:03:44 +03:00
|
|
|
import BasicMessageComposer from "./BasicMessageComposer";
|
2019-08-20 12:41:13 +03:00
|
|
|
import ReplyThread from "../elements/ReplyThread";
|
2021-05-26 16:14:55 +03:00
|
|
|
import { findEditableEvent } from '../../../utils/EventUtils';
|
2019-08-21 16:34:49 +03:00
|
|
|
import SendHistoryManager from "../../../SendHistoryManager";
|
2021-06-30 15:01:26 +03:00
|
|
|
import { Command, CommandCategories, getCommand } from '../../../SlashCommands';
|
2019-08-21 12:26:21 +03:00
|
|
|
import Modal from '../../../Modal';
|
2021-05-26 16:14:55 +03:00
|
|
|
import { _t, _td } from '../../../languageHandler';
|
2019-08-29 17:19:05 +03:00
|
|
|
import ContentMessages from '../../../ContentMessages';
|
2019-12-17 20:26:12 +03:00
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
2021-05-26 16:14:55 +03:00
|
|
|
import { Action } from "../../../dispatcher/actions";
|
|
|
|
import { containsEmoji } from "../../../effects/utils";
|
|
|
|
import { CHAT_EFFECTS } from '../../../effects';
|
2020-10-29 18:53:14 +03:00
|
|
|
import CountlyAnalytics from "../../../CountlyAnalytics";
|
2021-05-26 16:14:55 +03:00
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
|
|
|
import { getKeyBindingsManager, MessageComposerAction } from '../../../KeyBindingsManager';
|
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-03-05 12:13:47 +03:00
|
|
|
import SettingsStore from '../../../settings/SettingsStore';
|
2021-06-30 15:01:26 +03:00
|
|
|
import { RoomPermalinkCreator } from "../../../utils/permalinks/Permalinks";
|
|
|
|
import { Room } from 'matrix-js-sdk/src/models/room';
|
|
|
|
import ErrorDialog from "../dialogs/ErrorDialog";
|
|
|
|
import QuestionDialog from "../dialogs/QuestionDialog";
|
|
|
|
import { ActionPayload } from "../../../dispatcher/payloads";
|
|
|
|
|
|
|
|
function addReplyToMessageContent(
|
|
|
|
content: IContent,
|
|
|
|
repliedToEvent: MatrixEvent,
|
|
|
|
permalinkCreator: RoomPermalinkCreator,
|
|
|
|
): void {
|
2019-08-20 12:41:13 +03:00
|
|
|
const replyContent = ReplyThread.makeReplyMixIn(repliedToEvent);
|
|
|
|
Object.assign(content, replyContent);
|
|
|
|
|
|
|
|
// Part of Replies fallback support - prepend the text we're sending
|
|
|
|
// with the text we're replying to
|
|
|
|
const nestedReply = ReplyThread.getNestedReplyText(repliedToEvent, permalinkCreator);
|
|
|
|
if (nestedReply) {
|
|
|
|
if (content.formatted_body) {
|
|
|
|
content.formatted_body = nestedReply.html + content.formatted_body;
|
|
|
|
}
|
|
|
|
content.body = nestedReply.body + content.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:56:27 +03:00
|
|
|
// exported for tests
|
2021-06-30 15:01:26 +03:00
|
|
|
export function createMessageContent(
|
|
|
|
model: EditorModel,
|
|
|
|
permalinkCreator: RoomPermalinkCreator,
|
|
|
|
replyToEvent: MatrixEvent,
|
|
|
|
): IContent {
|
2019-08-21 12:26:21 +03:00
|
|
|
const isEmote = containsEmote(model);
|
|
|
|
if (isEmote) {
|
|
|
|
model = stripEmoteCommand(model);
|
|
|
|
}
|
2020-01-21 18:55:21 +03:00
|
|
|
if (startsWith(model, "//")) {
|
|
|
|
model = stripPrefix(model, "/");
|
|
|
|
}
|
2019-09-02 18:53:14 +03:00
|
|
|
model = unescapeMessage(model);
|
2019-08-06 18:03:44 +03:00
|
|
|
|
|
|
|
const body = textSerialize(model);
|
2021-06-30 15:01:26 +03:00
|
|
|
const content: IContent = {
|
2019-08-21 12:26:21 +03:00
|
|
|
msgtype: isEmote ? "m.emote" : "m.text",
|
2019-08-20 12:41:13 +03:00
|
|
|
body: body,
|
2019-08-06 18:03:44 +03:00
|
|
|
};
|
2021-06-29 15:11:58 +03:00
|
|
|
const formattedBody = htmlSerializeIfNeeded(model, { forceHTML: !!replyToEvent });
|
2019-08-06 18:03:44 +03:00
|
|
|
if (formattedBody) {
|
|
|
|
content.format = "org.matrix.custom.html";
|
|
|
|
content.formatted_body = formattedBody;
|
|
|
|
}
|
2019-08-20 12:41:13 +03:00
|
|
|
|
2020-10-06 16:47:53 +03:00
|
|
|
if (replyToEvent) {
|
|
|
|
addReplyToMessageContent(content, replyToEvent, permalinkCreator);
|
2019-08-20 12:41:13 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:03:44 +03:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2020-11-18 01:36:58 +03:00
|
|
|
// exported for tests
|
2021-06-30 15:01:26 +03:00
|
|
|
export function isQuickReaction(model: EditorModel): boolean {
|
2020-11-18 01:36:58 +03:00
|
|
|
const parts = model.parts;
|
|
|
|
if (parts.length == 0) return false;
|
2020-12-02 23:01:44 +03:00
|
|
|
const text = textSerialize(model);
|
2020-11-18 01:36:58 +03:00
|
|
|
// shortcut takes the form "+:emoji:" or "+ :emoji:""
|
|
|
|
// can be in 1 or 2 parts
|
|
|
|
if (parts.length <= 2) {
|
|
|
|
const hasShortcut = text.startsWith("+") || text.startsWith("+ ");
|
|
|
|
const emojiMatch = text.match(EMOJI_REGEX);
|
|
|
|
if (hasShortcut && emojiMatch && emojiMatch.length == 1) {
|
|
|
|
return emojiMatch[0] === text.substring(1) ||
|
|
|
|
emojiMatch[0] === text.substring(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
interface IProps {
|
|
|
|
room: Room;
|
|
|
|
placeholder?: string;
|
|
|
|
permalinkCreator: RoomPermalinkCreator;
|
|
|
|
replyToEvent?: MatrixEvent;
|
|
|
|
disabled?: boolean;
|
|
|
|
onChange?(model: EditorModel): void;
|
|
|
|
}
|
2019-08-06 18:03:44 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
@replaceableComponent("views.rooms.SendMessageComposer")
|
|
|
|
export default class SendMessageComposer extends React.Component<IProps> {
|
2019-12-17 20:26:12 +03:00
|
|
|
static contextType = MatrixClientContext;
|
2021-06-30 15:45:43 +03:00
|
|
|
context!: React.ContextType<typeof MatrixClientContext>;
|
2019-08-06 18:03:44 +03:00
|
|
|
|
2021-07-01 20:35:38 +03:00
|
|
|
private readonly prepareToEncrypt?: DebouncedFunc<() => void>;
|
2021-06-30 15:01:26 +03:00
|
|
|
private readonly editorRef = createRef<BasicMessageComposer>();
|
|
|
|
private model: EditorModel = null;
|
|
|
|
private currentlyComposedEditorState: SerializedPart[] = null;
|
|
|
|
private dispatcherRef: string;
|
|
|
|
private sendHistoryManager: SendHistoryManager;
|
|
|
|
|
|
|
|
constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
|
2021-06-30 15:45:43 +03:00
|
|
|
super(props);
|
|
|
|
this.context = context; // otherwise React will only set it prior to render due to type def above
|
|
|
|
if (this.context.isCryptoEnabled() && this.context.isRoomEncrypted(this.props.room.roomId)) {
|
2021-07-01 20:35:38 +03:00
|
|
|
this.prepareToEncrypt = throttle(() => {
|
2020-10-07 02:09:09 +03:00
|
|
|
this.context.prepareToEncrypt(this.props.room);
|
2021-07-01 20:35:38 +03:00
|
|
|
}, 60000, { leading: true, trailing: false });
|
2020-03-19 02:23:36 +03:00
|
|
|
}
|
2020-10-06 16:47:53 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
window.addEventListener("beforeunload", this.saveStoredEditorState);
|
2019-08-06 18:03:44 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private onKeyDown = (event: KeyboardEvent): void => {
|
2019-09-25 11:33:52 +03:00
|
|
|
// ignore any keypress while doing IME compositions
|
2021-06-30 15:01:26 +03:00
|
|
|
if (this.editorRef.current?.isComposing(event)) {
|
2019-09-24 16:32:30 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-03-01 11:43:00 +03:00
|
|
|
const action = getKeyBindingsManager().getMessageComposerAction(event);
|
2021-02-14 05:56:55 +03:00
|
|
|
switch (action) {
|
2021-03-01 11:43:00 +03:00
|
|
|
case MessageComposerAction.Send:
|
2021-06-30 15:01:26 +03:00
|
|
|
this.sendMessage();
|
2021-02-14 05:56:55 +03:00
|
|
|
event.preventDefault();
|
|
|
|
break;
|
2021-03-01 11:43:00 +03:00
|
|
|
case MessageComposerAction.SelectPrevSendHistory:
|
|
|
|
case MessageComposerAction.SelectNextSendHistory: {
|
2021-02-14 05:56:55 +03:00
|
|
|
// Try select composer history
|
2021-03-01 11:43:00 +03:00
|
|
|
const selected = this.selectSendHistory(action === MessageComposerAction.SelectPrevSendHistory);
|
2021-02-14 05:56:55 +03:00
|
|
|
if (selected) {
|
2019-08-20 18:18:46 +03:00
|
|
|
// We're selecting history, so prevent the key event from doing anything else
|
2021-02-14 05:56:55 +03:00
|
|
|
event.preventDefault();
|
2019-08-20 18:18:46 +03:00
|
|
|
}
|
2021-02-14 05:56:55 +03:00
|
|
|
break;
|
2019-08-20 18:18:46 +03:00
|
|
|
}
|
2021-03-01 11:43:00 +03:00
|
|
|
case MessageComposerAction.EditPrevMessage:
|
2021-02-14 05:56:55 +03:00
|
|
|
// selection must be collapsed and caret at start
|
2021-06-30 15:01:26 +03:00
|
|
|
if (this.editorRef.current?.isSelectionCollapsed() && this.editorRef.current?.isCaretAtStart()) {
|
2021-02-14 05:56:55 +03:00
|
|
|
const editEvent = findEditableEvent(this.props.room, false);
|
|
|
|
if (editEvent) {
|
|
|
|
// We're selecting history, so prevent the key event from doing anything else
|
|
|
|
event.preventDefault();
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'edit_event',
|
|
|
|
event: editEvent,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-03-01 11:43:00 +03:00
|
|
|
case MessageComposerAction.CancelEditing:
|
2021-02-16 09:12:18 +03:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'reply_to_event',
|
|
|
|
event: null,
|
|
|
|
});
|
|
|
|
break;
|
2021-02-14 05:56:55 +03:00
|
|
|
default:
|
2021-06-30 15:01:26 +03:00
|
|
|
if (this.prepareToEncrypt) {
|
2021-02-14 05:56:55 +03:00
|
|
|
// This needs to be last!
|
2021-06-30 15:01:26 +03:00
|
|
|
this.prepareToEncrypt();
|
2019-08-20 18:18:46 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 05:56:55 +03:00
|
|
|
};
|
2019-08-20 18:18:46 +03:00
|
|
|
|
2019-08-21 12:26:21 +03:00
|
|
|
// we keep sent messages/commands in a separate history (separate from undo history)
|
|
|
|
// so you can alt+up/down in them
|
2021-06-30 15:03:29 +03:00
|
|
|
private selectSendHistory(up: boolean): boolean {
|
2019-08-20 18:18:46 +03:00
|
|
|
const delta = up ? -1 : 1;
|
|
|
|
// True if we are not currently selecting history, but composing a message
|
|
|
|
if (this.sendHistoryManager.currentIndex === this.sendHistoryManager.history.length) {
|
|
|
|
// We can't go any further - there isn't any more history, so nop.
|
|
|
|
if (!up) {
|
2021-06-30 15:03:29 +03:00
|
|
|
return false;
|
2019-08-20 18:18:46 +03:00
|
|
|
}
|
|
|
|
this.currentlyComposedEditorState = this.model.serializeParts();
|
|
|
|
} else if (this.sendHistoryManager.currentIndex + delta === this.sendHistoryManager.history.length) {
|
|
|
|
// True when we return to the message being composed currently
|
|
|
|
this.model.reset(this.currentlyComposedEditorState);
|
|
|
|
this.sendHistoryManager.currentIndex = this.sendHistoryManager.history.length;
|
2021-06-30 15:03:29 +03:00
|
|
|
return true;
|
2019-08-20 18:18:46 +03:00
|
|
|
}
|
2021-06-29 15:11:58 +03:00
|
|
|
const { parts, replyEventId } = this.sendHistoryManager.getItem(delta);
|
2020-10-06 16:47:53 +03:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'reply_to_event',
|
|
|
|
event: replyEventId ? this.props.room.findEventById(replyEventId) : null,
|
|
|
|
});
|
|
|
|
if (parts) {
|
|
|
|
this.model.reset(parts);
|
2021-06-30 15:01:26 +03:00
|
|
|
this.editorRef.current?.focus();
|
2019-08-06 18:03:44 +03:00
|
|
|
}
|
2021-06-30 15:03:29 +03:00
|
|
|
return true;
|
2019-08-06 18:03:44 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private isSlashCommand(): boolean {
|
2019-08-21 12:26:21 +03:00
|
|
|
const parts = this.model.parts;
|
2019-09-25 18:30:01 +03:00
|
|
|
const firstPart = parts[0];
|
|
|
|
if (firstPart) {
|
2020-01-21 18:58:51 +03:00
|
|
|
if (firstPart.type === "command" && firstPart.text.startsWith("/") && !firstPart.text.startsWith("//")) {
|
2019-09-25 18:30:01 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// be extra resilient when somehow the AutocompleteWrapperModel or
|
|
|
|
// CommandPartCreator fails to insert a command part, so we don't send
|
|
|
|
// a command as a message
|
2020-01-22 17:24:10 +03:00
|
|
|
if (firstPart.text.startsWith("/") && !firstPart.text.startsWith("//")
|
2020-01-21 18:58:51 +03:00
|
|
|
&& (firstPart.type === "plain" || firstPart.type === "pill-candidate")) {
|
2019-09-25 18:30:01 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2019-08-21 12:26:21 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private sendQuickReaction(): void {
|
2020-11-18 01:36:58 +03:00
|
|
|
const timeline = this.props.room.getLiveTimeline();
|
|
|
|
const events = timeline.getEvents();
|
|
|
|
const reaction = this.model.parts[1].text;
|
|
|
|
for (let i = events.length - 1; i >= 0; i--) {
|
2021-07-01 23:10:37 +03:00
|
|
|
if (events[i].getType() === EventType.RoomMessage) {
|
2020-11-18 01:36:58 +03:00
|
|
|
let shouldReact = true;
|
|
|
|
const lastMessage = events[i];
|
|
|
|
const userId = MatrixClientPeg.get().getUserId();
|
|
|
|
const messageReactions = this.props.room.getUnfilteredTimelineSet()
|
2021-07-01 23:10:37 +03:00
|
|
|
.getRelationsForEvent(lastMessage.getId(), RelationType.Annotation, EventType.Reaction);
|
2020-11-18 01:36:58 +03:00
|
|
|
|
|
|
|
// if we have already sent this reaction, don't redact but don't re-send
|
|
|
|
if (messageReactions) {
|
|
|
|
const myReactionEvents = messageReactions.getAnnotationsBySender()[userId] || [];
|
|
|
|
const myReactionKeys = [...myReactionEvents]
|
|
|
|
.filter(event => !event.isRedacted())
|
|
|
|
.map(event => event.getRelation().key);
|
2021-02-16 09:05:51 +03:00
|
|
|
shouldReact = !myReactionKeys.includes(reaction);
|
2020-11-18 01:36:58 +03:00
|
|
|
}
|
|
|
|
if (shouldReact) {
|
2021-07-01 23:10:37 +03:00
|
|
|
MatrixClientPeg.get().sendEvent(lastMessage.getRoomId(), EventType.Reaction, {
|
2020-11-18 01:36:58 +03:00
|
|
|
"m.relates_to": {
|
2021-07-01 23:10:37 +03:00
|
|
|
"rel_type": RelationType.Annotation,
|
2020-11-18 01:36:58 +03:00
|
|
|
"event_id": lastMessage.getId(),
|
|
|
|
"key": reaction,
|
|
|
|
},
|
|
|
|
});
|
2021-06-29 15:11:58 +03:00
|
|
|
dis.dispatch({ action: "message_sent" });
|
2020-11-18 01:36:58 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private getSlashCommand(): [Command, string, string] {
|
2019-08-21 12:26:21 +03:00
|
|
|
const commandText = this.model.parts.reduce((text, part) => {
|
2019-09-25 18:30:12 +03:00
|
|
|
// use mxid to textify user pills in a command
|
|
|
|
if (part.type === "user-pill") {
|
|
|
|
return text + part.resourceId;
|
|
|
|
}
|
2019-08-21 12:26:21 +03:00
|
|
|
return text + part.text;
|
|
|
|
}, "");
|
2021-06-29 15:11:58 +03:00
|
|
|
const { cmd, args } = getCommand(commandText);
|
2021-02-25 22:39:20 +03:00
|
|
|
return [cmd, args, commandText];
|
2020-01-21 19:57:07 +03:00
|
|
|
}
|
2019-10-31 13:49:24 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private async runSlashCommand(cmd: Command, args: string): Promise<void> {
|
2021-02-25 22:39:20 +03:00
|
|
|
const result = cmd.run(this.props.room.roomId, args);
|
|
|
|
let messageContent;
|
|
|
|
let error = result.error;
|
|
|
|
if (result.promise) {
|
2020-01-21 19:57:07 +03:00
|
|
|
try {
|
2021-02-25 22:39:20 +03:00
|
|
|
if (cmd.category === CommandCategories.messages) {
|
|
|
|
// The command returns a modified message that we need to pass on
|
|
|
|
messageContent = await result.promise;
|
|
|
|
} else {
|
|
|
|
await result.promise;
|
|
|
|
}
|
2020-01-21 19:57:07 +03:00
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
2019-08-21 12:26:21 +03:00
|
|
|
}
|
2020-01-21 19:57:07 +03:00
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
console.error("Command failure: %s", error);
|
|
|
|
// assume the error is a server error when the command is async
|
2021-02-25 22:39:20 +03:00
|
|
|
const isServerError = !!result.promise;
|
2020-01-21 19:57:07 +03:00
|
|
|
const title = isServerError ? _td("Server error") : _td("Command error");
|
2019-10-31 13:49:24 +03:00
|
|
|
|
2020-01-21 19:57:07 +03:00
|
|
|
let errText;
|
|
|
|
if (typeof error === 'string') {
|
|
|
|
errText = error;
|
|
|
|
} else if (error.message) {
|
|
|
|
errText = error.message;
|
2019-08-21 12:26:21 +03:00
|
|
|
} else {
|
2020-01-21 19:57:07 +03:00
|
|
|
errText = _t("Server unavailable, overloaded, or something else went wrong.");
|
2019-08-21 12:26:21 +03:00
|
|
|
}
|
2020-01-21 19:57:07 +03:00
|
|
|
|
|
|
|
Modal.createTrackedDialog(title, '', ErrorDialog, {
|
|
|
|
title: _t(title),
|
|
|
|
description: errText,
|
2020-01-21 19:50:04 +03:00
|
|
|
});
|
2020-01-21 19:57:07 +03:00
|
|
|
} else {
|
|
|
|
console.log("Command success.");
|
2021-02-25 22:39:20 +03:00
|
|
|
if (messageContent) return messageContent;
|
2019-08-21 12:26:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
public async sendMessage(): Promise<void> {
|
2019-09-02 15:36:31 +03:00
|
|
|
if (this.model.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-21 19:50:04 +03:00
|
|
|
|
2021-02-25 22:39:20 +03:00
|
|
|
const replyToEvent = this.props.replyToEvent;
|
2020-01-21 19:50:04 +03:00
|
|
|
let shouldSend = true;
|
2021-02-25 22:39:20 +03:00
|
|
|
let content;
|
2020-01-21 19:50:04 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
if (!containsEmote(this.model) && this.isSlashCommand()) {
|
|
|
|
const [cmd, args, commandText] = this.getSlashCommand();
|
2020-01-21 19:57:07 +03:00
|
|
|
if (cmd) {
|
2021-02-25 22:39:20 +03:00
|
|
|
if (cmd.category === CommandCategories.messages) {
|
2021-06-30 15:01:26 +03:00
|
|
|
content = await this.runSlashCommand(cmd, args);
|
2021-02-25 22:39:20 +03:00
|
|
|
if (replyToEvent) {
|
|
|
|
addReplyToMessageContent(content, replyToEvent, this.props.permalinkCreator);
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.runSlashCommand(cmd, args);
|
2021-02-25 22:39:20 +03:00
|
|
|
shouldSend = false;
|
|
|
|
}
|
2020-01-21 19:57:07 +03:00
|
|
|
} else {
|
2020-01-21 20:54:27 +03:00
|
|
|
// ask the user if their unknown command should be sent as a message
|
2021-06-29 15:11:58 +03:00
|
|
|
const { finished } = Modal.createTrackedDialog("Unknown command", "", QuestionDialog, {
|
2020-01-21 19:57:07 +03:00
|
|
|
title: _t("Unknown Command"),
|
2020-01-21 20:54:27 +03:00
|
|
|
description: <div>
|
|
|
|
<p>
|
2021-06-29 15:11:58 +03:00
|
|
|
{ _t("Unrecognised command: %(commandText)s", { commandText }) }
|
2020-01-21 20:54:27 +03:00
|
|
|
</p>
|
|
|
|
<p>
|
2020-01-21 21:03:01 +03:00
|
|
|
{ _t("You can use <code>/help</code> to list available commands. " +
|
|
|
|
"Did you mean to send this as a message?", {}, {
|
2020-01-21 20:54:27 +03:00
|
|
|
code: t => <code>{ t }</code>,
|
|
|
|
}) }
|
|
|
|
</p>
|
|
|
|
<p>
|
2020-01-21 20:58:53 +03:00
|
|
|
{ _t("Hint: Begin your message with <code>//</code> to start it with a slash.", {}, {
|
2020-01-21 20:54:27 +03:00
|
|
|
code: t => <code>{ t }</code>,
|
|
|
|
}) }
|
|
|
|
</p>
|
|
|
|
</div>,
|
2020-01-21 19:57:07 +03:00
|
|
|
button: _t('Send as message'),
|
|
|
|
});
|
|
|
|
const [sendAnyway] = await finished;
|
|
|
|
// if !sendAnyway bail to let the user edit the composer and try again
|
|
|
|
if (!sendAnyway) return;
|
2020-01-21 19:50:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 01:36:58 +03:00
|
|
|
if (isQuickReaction(this.model)) {
|
|
|
|
shouldSend = false;
|
2021-06-30 15:01:26 +03:00
|
|
|
this.sendQuickReaction();
|
2020-11-18 01:36:58 +03:00
|
|
|
}
|
|
|
|
|
2020-01-21 19:50:04 +03:00
|
|
|
if (shouldSend) {
|
2020-10-29 18:53:14 +03:00
|
|
|
const startTime = CountlyAnalytics.getTimestamp();
|
2021-06-29 15:11:58 +03:00
|
|
|
const { roomId } = this.props.room;
|
2021-02-25 22:39:20 +03:00
|
|
|
if (!content) {
|
|
|
|
content = createMessageContent(this.model, this.props.permalinkCreator, replyToEvent);
|
|
|
|
}
|
2020-11-03 19:06:45 +03:00
|
|
|
// don't bother sending an empty message
|
|
|
|
if (!content.body.trim()) return;
|
|
|
|
|
2020-10-29 18:53:14 +03:00
|
|
|
const prom = this.context.sendMessage(roomId, content);
|
2020-10-06 16:47:53 +03:00
|
|
|
if (replyToEvent) {
|
2019-08-21 12:26:21 +03:00
|
|
|
// Clear reply_to_event as we put the message into the queue
|
|
|
|
// if the send fails, retry will handle resending.
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'reply_to_event',
|
|
|
|
event: null,
|
|
|
|
});
|
|
|
|
}
|
2021-06-29 15:11:58 +03:00
|
|
|
dis.dispatch({ action: "message_sent" });
|
2020-11-27 16:54:21 +03:00
|
|
|
CHAT_EFFECTS.forEach((effect) => {
|
2020-10-21 14:37:36 +03:00
|
|
|
if (containsEmoji(content, effect.emojis)) {
|
2021-06-29 15:11:58 +03:00
|
|
|
dis.dispatch({ action: `effects.${effect.command}` });
|
2020-10-21 14:56:58 +03:00
|
|
|
}
|
2020-10-21 14:37:36 +03:00
|
|
|
});
|
2020-10-29 18:53:14 +03:00
|
|
|
CountlyAnalytics.instance.trackSendMessage(startTime, prom, roomId, false, !!replyToEvent, content);
|
2019-08-21 12:26:21 +03:00
|
|
|
}
|
2020-01-21 19:50:04 +03:00
|
|
|
|
2020-10-06 16:47:53 +03:00
|
|
|
this.sendHistoryManager.save(this.model, replyToEvent);
|
2019-08-21 12:26:21 +03:00
|
|
|
// clear composer
|
2019-08-06 18:03:44 +03:00
|
|
|
this.model.reset([]);
|
2021-06-30 15:01:26 +03:00
|
|
|
this.editorRef.current?.clearUndoHistory();
|
|
|
|
this.editorRef.current?.focus();
|
|
|
|
this.clearStoredEditorState();
|
2021-02-26 20:35:45 +03:00
|
|
|
if (SettingsStore.getValue("scrollToBottomOnMessageSent")) {
|
2021-06-29 15:11:58 +03:00
|
|
|
dis.dispatch({ action: "scroll_to_bottom" });
|
2021-02-26 20:35:45 +03:00
|
|
|
}
|
2019-08-06 18:03:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2019-08-07 16:14:16 +03:00
|
|
|
dis.unregister(this.dispatcherRef);
|
2021-06-30 15:01:26 +03:00
|
|
|
window.removeEventListener("beforeunload", this.saveStoredEditorState);
|
|
|
|
this.saveStoredEditorState();
|
2019-08-06 18:03:44 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 23:12:52 +03:00
|
|
|
// TODO: [REACT-WARNING] Move this to constructor
|
2020-03-31 23:21:12 +03:00
|
|
|
UNSAFE_componentWillMount() { // eslint-disable-line camelcase
|
2019-12-17 20:26:12 +03:00
|
|
|
const partCreator = new CommandPartCreator(this.props.room, this.context);
|
2021-06-30 15:01:26 +03:00
|
|
|
const parts = this.restoreStoredEditorState(partCreator) || [];
|
2019-08-21 18:42:09 +03:00
|
|
|
this.model = new EditorModel(parts, partCreator);
|
2019-08-07 16:14:16 +03:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2020-10-08 11:51:31 +03:00
|
|
|
this.sendHistoryManager = new SendHistoryManager(this.props.room.roomId, 'mx_cider_history_');
|
2019-08-06 18:03:44 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private get editorStateKey() {
|
2020-10-08 11:51:31 +03:00
|
|
|
return `mx_cider_state_${this.props.room.roomId}`;
|
2019-08-21 18:42:09 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private clearStoredEditorState(): void {
|
|
|
|
localStorage.removeItem(this.editorStateKey);
|
2019-08-21 18:42:09 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private restoreStoredEditorState(partCreator: PartCreator): Part[] {
|
|
|
|
const json = localStorage.getItem(this.editorStateKey);
|
2019-08-21 18:42:09 +03:00
|
|
|
if (json) {
|
2020-10-08 11:51:31 +03:00
|
|
|
try {
|
2021-06-29 15:11:58 +03:00
|
|
|
const { parts: serializedParts, replyEventId } = JSON.parse(json);
|
2021-06-30 15:01:26 +03:00
|
|
|
const parts: Part[] = serializedParts.map(p => partCreator.deserializePart(p));
|
2020-10-08 11:51:31 +03:00
|
|
|
if (replyEventId) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'reply_to_event',
|
|
|
|
event: this.props.room.findEventById(replyEventId),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2020-10-06 16:47:53 +03:00
|
|
|
}
|
2019-08-21 18:42:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 21:58:39 +03:00
|
|
|
// should save state when editor has contents or reply is open
|
2021-06-30 15:01:26 +03:00
|
|
|
private shouldSaveStoredEditorState = (): boolean => {
|
|
|
|
return !this.model.isEmpty || !!this.props.replyToEvent;
|
|
|
|
};
|
2021-03-25 21:58:39 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private saveStoredEditorState = (): void => {
|
|
|
|
if (this.shouldSaveStoredEditorState()) {
|
2020-10-06 16:47:53 +03:00
|
|
|
const item = SendHistoryManager.createItem(this.model, this.props.replyToEvent);
|
2021-06-30 15:01:26 +03:00
|
|
|
localStorage.setItem(this.editorStateKey, JSON.stringify(item));
|
2021-03-25 21:58:39 +03:00
|
|
|
} else {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.clearStoredEditorState();
|
2019-08-21 18:42:09 +03:00
|
|
|
}
|
2021-06-30 15:01:26 +03:00
|
|
|
};
|
2019-08-21 18:42:09 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private onAction = (payload: ActionPayload): void => {
|
2021-04-15 05:12:10 +03:00
|
|
|
// don't let the user into the composer if it is disabled - all of these branches lead
|
|
|
|
// to the cursor being in the composer
|
|
|
|
if (this.props.disabled) return;
|
|
|
|
|
2019-08-07 16:14:16 +03:00
|
|
|
switch (payload.action) {
|
2019-08-07 16:14:50 +03:00
|
|
|
case 'reply_to_event':
|
2021-07-08 18:36:31 +03:00
|
|
|
case Action.FocusSendMessageComposer:
|
2021-06-30 15:01:26 +03:00
|
|
|
this.editorRef.current?.focus();
|
2019-08-07 16:14:16 +03:00
|
|
|
break;
|
2021-04-13 17:09:37 +03:00
|
|
|
case "send_composer_insert":
|
|
|
|
if (payload.userId) {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.editorRef.current?.insertMention(payload.userId);
|
2021-04-13 17:09:37 +03:00
|
|
|
} else if (payload.event) {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.editorRef.current?.insertQuotedMessage(payload.event);
|
2021-04-13 17:09:37 +03:00
|
|
|
} else if (payload.text) {
|
2021-06-30 15:01:26 +03:00
|
|
|
this.editorRef.current?.insertPlaintext(payload.text);
|
2021-04-13 17:09:37 +03:00
|
|
|
}
|
2019-12-19 12:25:51 +03:00
|
|
|
break;
|
2019-08-07 16:14:16 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private onPaste = (event: ClipboardEvent<HTMLDivElement>): boolean => {
|
2021-06-29 15:11:58 +03:00
|
|
|
const { clipboardData } = event;
|
2020-06-10 23:48:39 +03:00
|
|
|
// Prioritize text on the clipboard over files as Office on macOS puts a bitmap
|
|
|
|
// in the clipboard as well as the content being copied.
|
|
|
|
if (clipboardData.files.length && !clipboardData.types.some(t => t === "text/plain")) {
|
2019-08-29 17:19:05 +03:00
|
|
|
// This actually not so much for 'files' as such (at time of writing
|
|
|
|
// neither chrome nor firefox let you paste a plain file copied
|
|
|
|
// from Finder) but more images copied from a different website
|
|
|
|
// / word processor etc.
|
|
|
|
ContentMessages.sharedInstance().sendContentListToRoom(
|
2019-12-17 20:26:12 +03:00
|
|
|
Array.from(clipboardData.files), this.props.room.roomId, this.context,
|
2019-08-29 17:19:05 +03:00
|
|
|
);
|
2020-06-01 17:00:55 +03:00
|
|
|
return true; // to skip internal onPaste handler
|
2019-08-29 17:19:05 +03:00
|
|
|
}
|
2021-06-30 15:01:26 +03:00
|
|
|
};
|
2019-08-29 17:19:05 +03:00
|
|
|
|
2021-06-30 15:01:26 +03:00
|
|
|
private onChange = (): void => {
|
2021-02-17 15:32:48 +03:00
|
|
|
if (this.props.onChange) this.props.onChange(this.model);
|
2021-06-30 15:01:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
private focusComposer = (): void => {
|
|
|
|
this.editorRef.current?.focus();
|
|
|
|
};
|
2021-02-12 17:16:07 +03:00
|
|
|
|
2019-08-06 18:03:44 +03:00
|
|
|
render() {
|
|
|
|
return (
|
2021-06-30 15:01:26 +03:00
|
|
|
<div className="mx_SendMessageComposer" onClick={this.focusComposer} onKeyDown={this.onKeyDown}>
|
2019-08-06 18:03:44 +03:00
|
|
|
<BasicMessageComposer
|
2021-02-12 17:16:07 +03:00
|
|
|
onChange={this.onChange}
|
2021-06-30 15:01:26 +03:00
|
|
|
ref={this.editorRef}
|
2019-08-06 18:03:44 +03:00
|
|
|
model={this.model}
|
|
|
|
room={this.props.room}
|
2019-08-06 18:53:23 +03:00
|
|
|
label={this.props.placeholder}
|
2019-08-06 18:52:47 +03:00
|
|
|
placeholder={this.props.placeholder}
|
2021-06-30 15:01:26 +03:00
|
|
|
onPaste={this.onPaste}
|
2021-03-16 07:16:58 +03:00
|
|
|
disabled={this.props.disabled}
|
2019-08-06 18:03:44 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|