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

Commit

Permalink
Remove welcome bot welcome_user_id support (#12153)
Browse files Browse the repository at this point in the history
* Remove welcome bot `welcome_user_id` support

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Iterate

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* i18n

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Remove unused prop

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
  • Loading branch information
t3chguy authored Feb 12, 2024
1 parent 86b3564 commit 9b5401d
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 137 deletions.
2 changes: 0 additions & 2 deletions src/IConfigOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ export interface IConfigOptions {
admin_message_md: string; // message for how to contact the server owner when reporting an event
};

welcome_user_id?: string;

room_directory?: {
servers: string[];
};
Expand Down
92 changes: 7 additions & 85 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ import "../../stores/LifecycleStore";
import "../../stores/AutoRageshakeStore";
import PageType from "../../PageTypes";
import createRoom, { IOpts } from "../../createRoom";
import { _t, _td, getCurrentLanguage } from "../../languageHandler";
import { _t, _td } from "../../languageHandler";
import SettingsStore from "../../settings/SettingsStore";
import ThemeController from "../../settings/controllers/ThemeController";
import { startAnyRegistrationFlow } from "../../Registration";
import ResizeNotifier from "../../utils/ResizeNotifier";
import AutoDiscoveryUtils from "../../utils/AutoDiscoveryUtils";
import DMRoomMap from "../../utils/DMRoomMap";
import ThemeWatcher from "../../settings/watchers/ThemeWatcher";
import { FontWatcher } from "../../settings/watchers/FontWatcher";
import { storeRoomAliasInCache } from "../../RoomAliasCache";
Expand Down Expand Up @@ -122,7 +121,6 @@ import { ViewHomePagePayload } from "../../dispatcher/payloads/ViewHomePagePaylo
import { AfterLeaveRoomPayload } from "../../dispatcher/payloads/AfterLeaveRoomPayload";
import { DoAfterSyncPreparedPayload } from "../../dispatcher/payloads/DoAfterSyncPreparedPayload";
import { ViewStartChatOrReusePayload } from "../../dispatcher/payloads/ViewStartChatOrReusePayload";
import { SnakedObject } from "../../utils/SnakedObject";
import { leaveRoomBehaviour } from "../../utils/leave-behaviour";
import { CallStore } from "../../stores/CallStore";
import { IRoomStateEventsActionPayload } from "../../actions/MatrixActionCreators";
Expand Down Expand Up @@ -1138,30 +1136,13 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}

private chatCreateOrReuse(userId: string): void {
const snakedConfig = new SnakedObject(this.props.config);
// Use a deferred action to reshow the dialog once the user has registered
if (MatrixClientPeg.safeGet().isGuest()) {
// No point in making 2 DMs with welcome bot. This assumes view_set_mxid will
// result in a new DM with the welcome user.
if (userId !== snakedConfig.get("welcome_user_id")) {
dis.dispatch<DoAfterSyncPreparedPayload<ViewStartChatOrReusePayload>>({
action: Action.DoAfterSyncPrepared,
deferred_action: {
action: Action.ViewStartChatOrReuse,
user_id: userId,
},
});
}
dis.dispatch({
action: "require_registration",
// If the set_mxid dialog is cancelled, view /welcome because if the
// browser was pointing at /user/@someone:domain?action=chat, the URL
// needs to be reset so that they can revisit /user/.. // (and trigger
// `_chatCreateOrReuse` again)
go_welcome_on_cancel: true,
screen_after: {
screen: `user/${snakedConfig.get("welcome_user_id")}`,
params: { action: "chat" },
dis.dispatch<DoAfterSyncPreparedPayload<ViewStartChatOrReusePayload>>({
action: Action.DoAfterSyncPrepared,
deferred_action: {
action: Action.ViewStartChatOrReuse,
user_id: userId,
},
});
return;
Expand Down Expand Up @@ -1290,57 +1271,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}
}

/**
* Starts a chat with the welcome user, if the user doesn't already have one
* @returns {string} The room ID of the new room, or null if no room was created
*/
private async startWelcomeUserChat(): Promise<string | null> {
const snakedConfig = new SnakedObject(this.props.config);
const welcomeUserId = snakedConfig.get("welcome_user_id");
if (!welcomeUserId) return null;

// We can end up with multiple tabs post-registration where the user
// might then end up with a session and we don't want them all making
// a chat with the welcome user: try to de-dupe.
// We need to wait for the first sync to complete for this to
// work though.
let waitFor: Promise<void>;
if (!this.firstSyncComplete) {
waitFor = this.firstSyncPromise.promise;
} else {
waitFor = Promise.resolve();
}
await waitFor;

const welcomeUserRooms = DMRoomMap.shared().getDMRoomsForUserId(welcomeUserId);
if (welcomeUserRooms.length === 0) {
const roomId = await createRoom(MatrixClientPeg.safeGet(), {
dmUserId: snakedConfig.get("welcome_user_id"),
// Only view the welcome user if we're NOT looking at a room
andView: !this.state.currentRoomId,
spinner: false, // we're already showing one: we don't need another one
});
// This is a bit of a hack, but since the deduplication relies
// on m.direct being up to date, we need to force a sync
// of the database, otherwise if the user goes to the other
// tab before the next save happens (a few minutes), the
// saved sync will be restored from the db and this code will
// run without the update to m.direct, making another welcome
// user room (it doesn't wait for new data from the server, just
// the saved sync to be loaded).
const saveWelcomeUser = (ev: MatrixEvent): void => {
if (ev.getType() === EventType.Direct && ev.getContent()[welcomeUserId]) {
MatrixClientPeg.safeGet().store.save(true);
MatrixClientPeg.safeGet().removeListener(ClientEvent.AccountData, saveWelcomeUser);
}
};
MatrixClientPeg.safeGet().on(ClientEvent.AccountData, saveWelcomeUser);

return roomId;
}
return null;
}

/**
* Called when a new logged in session has started
*/
Expand Down Expand Up @@ -1390,15 +1320,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
} else if (MatrixClientPeg.currentUserIsJustRegistered()) {
MatrixClientPeg.setJustRegisteredUserId(null);

const snakedConfig = new SnakedObject(this.props.config);
if (snakedConfig.get("welcome_user_id") && getCurrentLanguage().startsWith("en")) {
const welcomeUserRoom = await this.startWelcomeUserChat();
if (welcomeUserRoom === null) {
// We didn't redirect to the welcome user room, so show
// the homepage.
dis.dispatch<ViewHomePagePayload>({ action: Action.ViewHomePage, justRegistered: true });
}
} else if (ThreepidInviteStore.instance.pickBestInvite()) {
if (ThreepidInviteStore.instance.pickBestInvite()) {
// The user has a 3pid invite pending - show them that
const threepidInvite = ThreepidInviteStore.instance.pickBestInvite();

Expand Down
4 changes: 0 additions & 4 deletions src/components/views/dialogs/InviteDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { _t, _td } from "../../../languageHandler";
import { MatrixClientPeg } from "../../../MatrixClientPeg";
import { makeRoomPermalink, makeUserPermalink } from "../../../utils/permalinks/Permalinks";
import DMRoomMap from "../../../utils/DMRoomMap";
import SdkConfig from "../../../SdkConfig";
import * as Email from "../../../email";
import { getDefaultIdentityServerUrl, setToDefaultIdentityServer } from "../../../utils/IdentityServerUtils";
import { buildActivityScores, buildMemberScores, compareMembers } from "../../../utils/SortMembers";
Expand Down Expand Up @@ -369,9 +368,6 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
this.profilesStore = SdkContextClass.instance.userProfilesStore;

const excludedIds = new Set([MatrixClientPeg.safeGet().getUserId()!]);
const welcomeUserId = SdkConfig.get("welcome_user_id");
if (welcomeUserId) excludedIds.add(welcomeUserId);

if (isRoomInvite(props)) {
const room = MatrixClientPeg.safeGet().getRoom(props.roomId);
const isFederated = room?.currentState.getStateEvents(EventType.RoomCreate, "")?.getContent()["m.federate"];
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/dialogs/UserSettingsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default class UserSettingsDialog extends React.Component<IProps, IState>
UserTab.Help,
_td("setting|help_about|title"),
"mx_UserSettingsDialog_helpIcon",
<HelpUserSettingsTab closeSettingsFn={() => this.props.onFinished()} />,
<HelpUserSettingsTab />,
"UserSettingsHelpAbout",
),
);
Expand Down
45 changes: 3 additions & 42 deletions src/components/views/settings/tabs/user/HelpUserSettingsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import React, { ReactNode } from "react";
import { logger } from "matrix-js-sdk/src/logger";

import AccessibleButton from "../../../elements/AccessibleButton";
import { _t, getCurrentLanguage } from "../../../../../languageHandler";
import { _t } from "../../../../../languageHandler";
import SdkConfig from "../../../../../SdkConfig";
import createRoom from "../../../../../createRoom";
import Modal from "../../../../../Modal";
import PlatformPeg from "../../../../../PlatformPeg";
import UpdateCheckButton from "../../UpdateCheckButton";
Expand All @@ -32,9 +31,7 @@ import SettingsSubsection, { SettingsSubsectionText } from "../../shared/Setting
import ExternalLink from "../../../elements/ExternalLink";
import MatrixClientContext from "../../../../../contexts/MatrixClientContext";

interface IProps {
closeSettingsFn: () => void;
}
interface IProps {}

interface IState {
appVersion: string | null;
Expand Down Expand Up @@ -96,14 +93,6 @@ export default class HelpUserSettingsTab extends React.Component<IProps, IState>
Modal.createDialog(BugReportDialog, {});
};

private onStartBotChat = (): void => {
this.props.closeSettingsFn();
createRoom(this.context, {
dmUserId: SdkConfig.get("welcome_user_id"),
andView: true,
});
};

private renderLegal(): ReactNode {
const tocLinks = SdkConfig.get().terms_and_conditions_links;
if (!tocLinks) return null;
Expand Down Expand Up @@ -224,7 +213,7 @@ export default class HelpUserSettingsTab extends React.Component<IProps, IState>
public render(): React.ReactNode {
const brand = SdkConfig.get().brand;

let faqText = _t(
const faqText = _t(
"setting|help_about|help_link",
{
brand,
Expand All @@ -233,34 +222,6 @@ export default class HelpUserSettingsTab extends React.Component<IProps, IState>
a: (sub) => <ExternalLink href={SdkConfig.get("help_url")}>{sub}</ExternalLink>,
},
);
if (SdkConfig.get("welcome_user_id") && getCurrentLanguage().startsWith("en")) {
faqText = (
<div>
{_t(
"setting|help_about|help_link_chat_bot",
{
brand,
},
{
a: (sub) => (
<ExternalLink
href={SdkConfig.get("help_url")}
rel="noreferrer noopener"
target="_blank"
>
{sub}
</ExternalLink>
),
},
)}
<div>
<AccessibleButton onClick={this.onStartBotChat} kind="primary">
{_t("setting|help_about|chat_bot", { brand })}
</AccessibleButton>
</div>
</div>
);
}

let updateButton: JSX.Element | undefined;
if (this.state.canUpdate) {
Expand Down
2 changes: 0 additions & 2 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2397,11 +2397,9 @@
"help_about": {
"access_token_detail": "Your access token gives full access to your account. Do not share it with anyone.",
"brand_version": "%(brand)s version:",
"chat_bot": "Chat with %(brand)s Bot",
"clear_cache_reload": "Clear cache and reload",
"crypto_version": "Crypto version:",
"help_link": "For help with using %(brand)s, click <a>here</a>.",
"help_link_chat_bot": "For help with using %(brand)s, click <a>here</a> or start a chat with our bot using the button below.",
"homeserver": "Homeserver is <code>%(homeserverUrl)s</code>",
"identity_server": "Identity server is <code>%(identityServerUrl)s</code>",
"title": "Help & About",
Expand Down
1 change: 0 additions & 1 deletion test/components/views/dialogs/InviteDialog-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ describe("InviteDialog", () => {
});

it("should not suggest users from other server when room has m.federate=false", async () => {
SdkConfig.add({ welcome_user_id: "@bot:example.org" });
room.currentState.setStateEvents([mkRoomCreateEvent(bobId, roomId, { "m.federate": false })]);

render(
Expand Down

0 comments on commit 9b5401d

Please sign in to comment.