Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 8093f4b

Browse files
committed
Start DM on first message
Signed-off-by: Michael Weimann <[email protected]>
1 parent 6f85110 commit 8093f4b

File tree

14 files changed

+224
-17
lines changed

14 files changed

+224
-17
lines changed

src/PageTypes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
enum PageType {
2020
HomePage = "home_page",
2121
RoomView = "room_view",
22+
LocalRoomView = "local_room_view",
2223
UserView = "user_view",
2324
LegacyGroupView = "legacy_group_view",
2425
}

src/PosthogTrackers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const notLoggedInMap: Record<Exclude<Views, Views.LOGGED_IN>, ScreenName> = {
3939
const loggedInPageTypeMap: Record<PageType, ScreenName> = {
4040
[PageType.HomePage]: "Home",
4141
[PageType.RoomView]: "Room",
42+
[PageType.LocalRoomView]: "Room",
4243
[PageType.UserView]: "User",
4344
[PageType.LegacyGroupView]: "Group",
4445
};

src/components/structures/LoggedInView.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import classNames from 'classnames';
2222
import { ISyncStateData, SyncState } from 'matrix-js-sdk/src/sync';
2323
import { IUsageLimit } from 'matrix-js-sdk/src/@types/partials';
2424
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
25+
import { ISendEventResponse } from "matrix-js-sdk/src/@types/requests";
26+
import { IContent } from "matrix-js-sdk/src/models/event";
2527

2628
import { isOnlyCtrlOrCmdKeyEvent, Key } from '../../Keyboard';
2729
import PageTypes from '../../PageTypes';
@@ -71,6 +73,8 @@ import { SwitchSpacePayload } from "../../dispatcher/payloads/SwitchSpacePayload
7173
import LegacyGroupView from "./LegacyGroupView";
7274
import { IConfigOptions } from "../../IConfigOptions";
7375
import LeftPanelLiveShareWarning from '../views/beacon/LeftPanelLiveShareWarning';
76+
import { startDm } from '../../utils/direct-messages';
77+
import { LocalRoom } from '../../models/LocalRoom';
7478

7579
// We need to fetch each pinned message individually (if we don't already have it)
7680
// so each pinned message may trigger a request. Limit the number per room for sanity.
@@ -619,8 +623,27 @@ class LoggedInView extends React.Component<IProps, IState> {
619623

620624
render() {
621625
let pageElement;
626+
let messageComposerHandlers;
622627

623628
switch (this.props.page_type) {
629+
case PageTypes.LocalRoomView:
630+
messageComposerHandlers = {
631+
sendMessage: async (
632+
localRoomId: string,
633+
threadId: string | null,
634+
content: IContent,
635+
): Promise<ISendEventResponse> => {
636+
const room = this._matrixClient.store.getRoom(localRoomId);
637+
638+
if (!(room instanceof LocalRoom)) {
639+
return;
640+
}
641+
642+
const rooomId = await startDm(this._matrixClient, room.targets);
643+
return this._matrixClient.sendMessage(rooomId, threadId, content);
644+
},
645+
};
646+
// fallthrough
624647
case PageTypes.RoomView:
625648
pageElement = <RoomView
626649
ref={this._roomView}
@@ -631,6 +654,7 @@ class LoggedInView extends React.Component<IProps, IState> {
631654
resizeNotifier={this.props.resizeNotifier}
632655
justCreatedOpts={this.props.roomJustCreatedOpts}
633656
forceTimeline={this.props.forceTimeline}
657+
messageComposerHandlers={messageComposerHandlers}
634658
/>;
635659
break;
636660

src/components/structures/MatrixChat.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,15 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
651651
case 'view_user_info':
652652
this.viewUser(payload.userId, payload.subAction);
653653
break;
654+
case Action.ViewLocalRoom:
655+
this.viewRoom(payload as ViewRoomPayload, PageType.LocalRoomView);
656+
break;
654657
case Action.ViewRoom: {
655658
// Takes either a room ID or room alias: if switching to a room the client is already
656659
// known to be in (eg. user clicks on a room in the recents panel), supply the ID
657660
// If the user is clicking on a room in the context of the alias being presented
658661
// to them, supply the room alias. If both are supplied, the room ID will be ignored.
659-
const promise = this.viewRoom(payload as ViewRoomPayload);
662+
const promise = this.viewRoom(payload as ViewRoomPayload, PageType.RoomView);
660663
if (payload.deferred_action) {
661664
promise.then(() => {
662665
dis.dispatch(payload.deferred_action);
@@ -854,7 +857,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
854857
}
855858

856859
// switch view to the given room
857-
private async viewRoom(roomInfo: ViewRoomPayload) {
860+
private async viewRoom(roomInfo: ViewRoomPayload, pageType: PageType) {
858861
this.focusComposer = true;
859862

860863
if (roomInfo.room_alias) {
@@ -913,7 +916,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
913916
this.setState({
914917
view: Views.LOGGED_IN,
915918
currentRoomId: roomInfo.room_id || null,
916-
page_type: PageType.RoomView,
919+
page_type: pageType,
917920
threepidInvite: roomInfo.threepid_invite,
918921
roomOobData: roomInfo.oob_data,
919922
forceTimeline: roomInfo.forceTimeline,

src/components/structures/RoomView.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ import SearchResultTile from '../views/rooms/SearchResultTile';
9393
import Spinner from "../views/elements/Spinner";
9494
import UploadBar from './UploadBar';
9595
import RoomStatusBar from "./RoomStatusBar";
96-
import MessageComposer from '../views/rooms/MessageComposer';
96+
import MessageComposer, { IMessageComposerHandlers } from '../views/rooms/MessageComposer';
9797
import JumpToBottomButton from "../views/rooms/JumpToBottomButton";
9898
import TopUnreadMessagesBar from "../views/rooms/TopUnreadMessagesBar";
9999
import { showThread } from '../../dispatcher/dispatch-actions/threads';
@@ -132,6 +132,8 @@ interface IRoomProps extends MatrixClientProps {
132132

133133
// Called with the credentials of a registered user (if they were a ROU that transitioned to PWLU)
134134
onRegistered?(credentials: IMatrixClientCreds): void;
135+
136+
messageComposerHandlers?: IMessageComposerHandlers;
135137
}
136138

137139
// This defines the content of the mainSplit.
@@ -2013,6 +2015,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
20132015
resizeNotifier={this.props.resizeNotifier}
20142016
replyToEvent={this.state.replyToEvent}
20152017
permalinkCreator={this.getPermalinkCreatorForRoom(this.state.room)}
2018+
handlers={this.props.messageComposerHandlers}
20162019
/>;
20172020
}
20182021

@@ -2066,7 +2069,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
20662069
showReadReceipts={this.state.showReadReceipts}
20672070
manageReadReceipts={!this.state.isPeeking}
20682071
sendReadReceiptOnLoad={!this.state.wasContextSwitch}
2069-
manageReadMarkers={!this.state.isPeeking}
2072+
manageReadMarkers={false}
20702073
hidden={hideMessagePanel}
20712074
highlightedEventId={highlightedEventId}
20722075
eventId={this.state.initialEventId}

src/components/views/dialogs/InviteDialog.tsx

+15-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ import CopyableText from "../elements/CopyableText";
5858
import { ScreenName } from '../../../PosthogTrackers';
5959
import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
6060
import { getKeyBindingsManager } from "../../../KeyBindingsManager";
61-
import { DirectoryMember, IDMUserTileProps, Member, startDm, ThreepidMember } from "../../../utils/direct-messages";
61+
import {
62+
createDmLocalRoom,
63+
DirectoryMember,
64+
IDMUserTileProps,
65+
Member,
66+
ThreepidMember,
67+
} from "../../../utils/direct-messages";
6268
import { AnyInviteKind, KIND_CALL_TRANSFER, KIND_DM, KIND_INVITE } from './InviteDialogTypes';
6369
import Modal from '../../../Modal';
6470
import dis from "../../../dispatcher/dispatcher";
@@ -563,20 +569,23 @@ export default class InviteDialog extends React.PureComponent<IInviteDialogProps
563569
}
564570

565571
private startDm = async () => {
566-
this.setState({ busy: true });
567572
try {
568-
const cli = MatrixClientPeg.get();
569573
const targets = this.convertFilter();
570-
await startDm(cli, targets);
574+
const client = MatrixClientPeg.get();
575+
createDmLocalRoom(client, targets);
576+
dis.dispatch({
577+
action: Action.ViewLocalRoom,
578+
room_id: 'local_room',
579+
joining: false,
580+
targets,
581+
});
571582
this.props.onFinished(true);
572583
} catch (err) {
573584
logger.error(err);
574585
this.setState({
575586
busy: false,
576587
errorText: _t("We couldn't create your DM."),
577588
});
578-
} finally {
579-
this.setState({ busy: false });
580589
}
581590
};
582591

src/components/views/rooms/MessageComposer.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { RoomMember } from "matrix-js-sdk/src/models/room-member";
2222
import { EventType } from 'matrix-js-sdk/src/@types/event';
2323
import { Optional } from "matrix-events-sdk";
2424
import { THREAD_RELATION_TYPE } from 'matrix-js-sdk/src/models/thread';
25+
import { ISendEventResponse } from 'matrix-js-sdk/src/@types/requests';
26+
import { IContent } from 'matrix-js-sdk/src/models/event';
2527

2628
import { _t } from '../../../languageHandler';
2729
import { MatrixClientPeg } from '../../../MatrixClientPeg';
@@ -77,6 +79,7 @@ interface IProps {
7779
relation?: IEventRelation;
7880
e2eStatus?: E2EStatus;
7981
compact?: boolean;
82+
handlers?: IMessageComposerHandlers;
8083
}
8184

8285
interface IState {
@@ -90,6 +93,14 @@ interface IState {
9093
showPollsButton: boolean;
9194
}
9295

96+
export interface IMessageComposerHandlers {
97+
sendMessage: (
98+
roomId: string,
99+
threadId: string | null,
100+
content: IContent,
101+
) => Promise<ISendEventResponse>;
102+
}
103+
93104
export default class MessageComposer extends React.Component<IProps, IState> {
94105
private dispatcherRef: string;
95106
private messageComposerInput = createRef<SendMessageComposerClass>();
@@ -377,6 +388,7 @@ export default class MessageComposer extends React.Component<IProps, IState> {
377388
onChange={this.onChange}
378389
disabled={this.state.haveRecording}
379390
toggleStickerPickerOpen={this.toggleStickerPickerOpen}
391+
handlers={this.props.handlers}
380392
/>,
381393
);
382394

src/components/views/rooms/SendMessageComposer.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { getSlashCommand, isSlashCommand, runSlashCommand, shouldSendAnyway } fr
5858
import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
5959
import { PosthogAnalytics } from "../../../PosthogAnalytics";
6060
import { addReplyToMessageContent } from '../../../utils/Reply';
61+
import { IMessageComposerHandlers } from './MessageComposer';
6162

6263
// Merges favouring the given relation
6364
export function attachRelation(content: IContent, relation?: IEventRelation): void {
@@ -139,6 +140,7 @@ interface ISendMessageComposerProps extends MatrixClientProps {
139140
onChange?(model: EditorModel): void;
140141
includeReplyLegacyFallback?: boolean;
141142
toggleStickerPickerOpen: () => void;
143+
handlers?: IMessageComposerHandlers;
142144
}
143145

144146
export class SendMessageComposer extends React.Component<ISendMessageComposerProps> {
@@ -401,7 +403,10 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
401403
? this.props.relation.event_id
402404
: null;
403405

404-
const prom = this.props.mxClient.sendMessage(roomId, threadId, content);
406+
const prom = this.props.handlers
407+
? this.props.handlers.sendMessage(roomId, threadId, content)
408+
: this.props.mxClient.sendMessage(roomId, threadId, content);
409+
405410
if (replyToEvent) {
406411
// Clear reply_to_event as we put the message into the queue
407412
// if the send fails, retry will handle resending.

src/dispatcher/actions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ export enum Action {
106106
*/
107107
ViewRoom = "view_room",
108108

109+
ViewLocalRoom = "view_local_room",
110+
109111
/**
110112
* Changes room based on room list order and payload parameters. Should be used with ViewRoomDeltaPayload.
111113
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { Member } from '../../utils/direct-messages';
18+
import { ViewRoomPayload } from './ViewRoomPayload';
19+
20+
export interface ViewLocalRoomPayload extends ViewRoomPayload {
21+
targets: Member[];
22+
}

src/models/LocalRoom.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { Room } from "matrix-js-sdk/src/models/room";
18+
19+
import { Member } from "../utils/direct-messages";
20+
21+
export class LocalRoom extends Room {
22+
targets: Member[];
23+
}

src/stores/RoomViewStore.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export class RoomViewStore extends Store<ActionPayload> {
171171
// - event_offset: 100
172172
// - highlighted: true
173173
case Action.ViewRoom:
174+
case Action.ViewLocalRoom:
174175
this.viewRoom(payload);
175176
break;
176177
// for these events blank out the roomId as we are no longer in the RoomView

0 commit comments

Comments
 (0)