diff --git a/src/Avatar.test.tsx b/src/Avatar.test.tsx index 3d75d4c1..debff26a 100644 --- a/src/Avatar.test.tsx +++ b/src/Avatar.test.tsx @@ -13,19 +13,14 @@ import { type FC, type PropsWithChildren } from "react"; import { ClientContextProvider } from "./ClientContext"; import { Avatar } from "./Avatar"; import { mockMatrixRoomMember, mockRtcMembership } from "./utils/test"; -import EventEmitter from "events"; import { widget } from "./widget"; +import { WidgetApi } from "matrix-widget-api"; const TestComponent: FC< PropsWithChildren<{ client: MatrixClient; - supportsAuthenticatedMedia?: boolean; }> -> = ({ - client, - children, - supportsAuthenticatedMedia: supportsAuthenticatedMedia, -}) => { +> = ({ client, children }) => { return ( ({ widget: { - api: { downloadFile: vi.fn() }, + api: null, // Ideally we'd only mock this in the as a widget test so the whole module is otherwise null, but just nulling `api` by default works well enough }, })); @@ -88,7 +82,7 @@ test("should just render a placeholder when the user has no avatar", () => { expect(client.mxcUrlToHttp).toBeCalledTimes(0); }); -test("should attempt to fetch authenticated media if supported", async () => { +test("should attempt to fetch authenticated media from the server", async () => { const expectedAuthUrl = "http://example.org/media/alice-avatar"; const expectedObjectURL = "my-object-url"; const accessToken = "my-access-token"; @@ -120,7 +114,7 @@ test("should attempt to fetch authenticated media if supported", async () => { ); const displayName = "Alice"; render( - + { }); }); -test("should attempt to use widget API if authenticate media is not supported", async () => { +test("should attempt to use widget API if running as a widget", async () => { const expectedMXCUrl = "mxc://example.org/alice-avatar"; const expectedObjectURL = "my-object-url"; const theBlob = new Blob([]); @@ -157,6 +151,7 @@ test("should attempt to use widget API if authenticate media is not supported", getAccessToken: () => undefined, } as unknown as MatrixClient); + widget!.api = { downloadFile: vi.fn() } as unknown as WidgetApi; vi.spyOn(widget!.api, "downloadFile").mockResolvedValue({ file: theBlob }); const member = mockMatrixRoomMember( mockRtcMembership("@alice:example.org", "AAAA"), @@ -166,7 +161,7 @@ test("should attempt to use widget API if authenticate media is not supported", ); const displayName = "Alice"; render( - + = ({ const [avatarUrl, setAvatarUrl] = useState(undefined); + // In theory, a change in `clientState` or `sizePx` could run extra getAvatarFromWidgetAPI calls, but in practice they should be stable long before this code runs. useEffect(() => { - if (!src || clientState?.state !== "valid") { + if (!src) { setAvatarUrl(undefined); return; } - const { authenticated, supportedFeatures } = clientState; let blob: Promise; - if ( - supportedFeatures.authenticatedMedia && - authenticated?.client && + if (widget?.api) { + blob = getAvatarFromWidgetAPI(widget.api, src); + } else if ( + clientState?.state === "valid" && + clientState.authenticated?.client && sizePx ) { - blob = getAvatarFromServer(authenticated.client, src, sizePx); - } else if (widget?.api) { - blob = getAvatarFromWidgetAPI(widget.api, src); + blob = getAvatarFromServer(clientState.authenticated.client, src, sizePx); } else { setAvatarUrl(undefined); return; diff --git a/src/ClientContext.tsx b/src/ClientContext.tsx index 7059cd69..f2ff3dd4 100644 --- a/src/ClientContext.tsx +++ b/src/ClientContext.tsx @@ -48,7 +48,6 @@ export type ValidClientState = { disconnected: boolean; supportedFeatures: { reactions: boolean; - authenticatedMedia: boolean; }; setClient: (client: MatrixClient, session: Session) => void; }; @@ -249,8 +248,6 @@ export const ClientProvider: FC = ({ children }) => { const [isDisconnected, setIsDisconnected] = useState(false); const [supportsReactions, setSupportsReactions] = useState(false); - const [supportsAuthenticatedMedia, setSupportsAuthenticatedMedia] = - useState(false); const state: ClientState | undefined = useMemo(() => { if (alreadyOpenedErr) { @@ -276,7 +273,6 @@ export const ClientProvider: FC = ({ children }) => { disconnected: isDisconnected, supportedFeatures: { reactions: supportsReactions, - authenticatedMedia: supportsAuthenticatedMedia, }, }; }, [ @@ -287,7 +283,6 @@ export const ClientProvider: FC = ({ children }) => { setClient, isDisconnected, supportsReactions, - supportsAuthenticatedMedia, ]); const onSync = useCallback( @@ -313,8 +308,6 @@ export const ClientProvider: FC = ({ children }) => { } if (initClientState.widgetApi) { - // There is currently no way for widgets to request authenticated media directly from the server. - setSupportsAuthenticatedMedia(false); const reactSend = initClientState.widgetApi.hasCapability( "org.matrix.msc2762.send.event:m.reaction", ); @@ -336,7 +329,6 @@ export const ClientProvider: FC = ({ children }) => { } } else { setSupportsReactions(true); - setSupportsAuthenticatedMedia(true); } return (): void => { diff --git a/src/settings/useSubmitRageshake.test.tsx b/src/settings/useSubmitRageshake.test.tsx index d69255e0..b5d07553 100644 --- a/src/settings/useSubmitRageshake.test.tsx +++ b/src/settings/useSubmitRageshake.test.tsx @@ -78,7 +78,6 @@ function renderWithMockClient( disconnected: false, supportedFeatures: { reactions: true, - authenticatedMedia: true, }, setClient: vi.fn(), authenticated: {