2023-07-11 13:53:33 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2020-2023 The Matrix.org Foundation C.I.C.
|
2023-07-11 13:53:33 +01:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2023-07-11 13:53:33 +01:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-07-11 13:53:33 +01:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import { EventType, type MatrixClient } from "matrix-js-sdk/src/matrix";
|
2023-07-11 13:53:33 +01:00
|
|
|
|
|
|
|
|
import { SdkContextClass } from "../contexts/SDKContext";
|
|
|
|
|
import { isLocalRoom } from "../utils/localRoom/isLocalRoom";
|
|
|
|
|
import Modal from "../Modal";
|
|
|
|
|
import UploadConfirmDialog from "../components/views/dialogs/UploadConfirmDialog";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type RunResult } from "./interface";
|
2023-07-11 13:53:33 +01:00
|
|
|
|
|
|
|
|
export function reject(error?: any): RunResult {
|
|
|
|
|
return { error };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function success(promise: Promise<any> = Promise.resolve()): RunResult {
|
|
|
|
|
return { promise };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function successSync(value: any): RunResult {
|
|
|
|
|
return success(Promise.resolve(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const canAffectPowerlevels = (cli: MatrixClient | null): boolean => {
|
|
|
|
|
const roomId = SdkContextClass.instance.roomViewStore.getRoomId();
|
|
|
|
|
if (!cli || !roomId) return false;
|
|
|
|
|
const room = cli?.getRoom(roomId);
|
|
|
|
|
return !!room?.currentState.maySendStateEvent(EventType.RoomPowerLevels, cli.getSafeUserId()) && !isLocalRoom(room);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816
|
|
|
|
|
interface HTMLInputEvent extends Event {
|
|
|
|
|
target: HTMLInputElement & EventTarget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const singleMxcUpload = async (cli: MatrixClient): Promise<string | null> => {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const fileSelector = document.createElement("input");
|
|
|
|
|
fileSelector.setAttribute("type", "file");
|
|
|
|
|
fileSelector.onchange = (ev: Event) => {
|
|
|
|
|
const file = (ev as HTMLInputEvent).target.files?.[0];
|
|
|
|
|
if (!file) return;
|
|
|
|
|
|
2025-04-30 16:56:21 +01:00
|
|
|
const { finished } = Modal.createDialog(UploadConfirmDialog, { file });
|
|
|
|
|
finished.then(async ([shouldContinue]) => {
|
|
|
|
|
if (shouldContinue) {
|
|
|
|
|
const { content_uri: uri } = await cli.uploadContent(file);
|
|
|
|
|
resolve(uri);
|
|
|
|
|
} else {
|
|
|
|
|
resolve(null);
|
|
|
|
|
}
|
2023-07-11 13:53:33 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fileSelector.click();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isCurrentLocalRoom = (cli: MatrixClient | null): boolean => {
|
|
|
|
|
const roomId = SdkContextClass.instance.roomViewStore.getRoomId();
|
|
|
|
|
if (!roomId) return false;
|
|
|
|
|
const room = cli?.getRoom(roomId);
|
|
|
|
|
if (!room) return false;
|
|
|
|
|
return isLocalRoom(room);
|
|
|
|
|
};
|