From 0ac4116b24eefa1d53ea09073ae3cbcb231fdb82 Mon Sep 17 00:00:00 2001 From: Jorik Schellekens Date: Tue, 18 Aug 2020 11:07:26 +0100 Subject: [PATCH] Implement better fallbacks --- src/components/Avatar.tsx | 22 +---- src/components/LinkPreview.tsx | 55 ++++--------- src/utils/cypher-wrapper.ts | 142 ++++++++++++++++++++++++--------- 3 files changed, 122 insertions(+), 97 deletions(-) diff --git a/src/components/Avatar.tsx b/src/components/Avatar.tsx index 5651cc6..a3de53b 100644 --- a/src/components/Avatar.tsx +++ b/src/components/Avatar.tsx @@ -18,7 +18,7 @@ import React, { useEffect, useState } from "react"; import classNames from "classnames"; import { Room, User } from "matrix-cypher"; -import { convertMXCtoMediaQuery } from "../utils/cypher-wrapper"; +import { getMediaQueryFromMCX } from "../utils/cypher-wrapper"; import logo from "../imgs/matrix-logo.svg"; import "./Avatar.scss"; @@ -55,15 +55,7 @@ export const UserAvatar: React.FC = ({ userId, }: IPropsUserAvatar) => ( ); @@ -76,15 +68,7 @@ export const RoomAvatar: React.FC = ({ room, }: IPropsRoomAvatar) => ( ); diff --git a/src/components/LinkPreview.tsx b/src/components/LinkPreview.tsx index 91b4193..2f78c76 100644 --- a/src/components/LinkPreview.tsx +++ b/src/components/LinkPreview.tsx @@ -15,46 +15,36 @@ limitations under the License. */ import React, { useState, useEffect } from "react"; -import { Client, getEvent, client } from "matrix-cypher"; +import { getEvent, client } from "matrix-cypher"; -import { - getRoomIdFromAlias, - searchPublicRooms, - getUserDetails, -} from "../utils/cypher-wrapper"; import { RoomPreviewWithTopic } from "./RoomPreview"; import InviteTile from "./InviteTile"; - -import { SafeLink } from "../parser/types"; -import { LinkKind } from "../parser/types"; +import { SafeLink, LinkKind } from "../parser/types"; import UserPreview from "./UserPreview"; import EventPreview from "./EventPreview"; import Clients from "../clients"; +import { + getRoomFromId, + getRoomFromAlias, + getRoomFromPermalink, + getUser, +} from "../utils/cypher-wrapper"; interface IProps { link: SafeLink; } -// TODO: replace with client fetch -const defaultClient: () => Promise = () => client("https://matrix.org"); - const LOADING: JSX.Element = <>Generating invite; const invite = async ({ link }: { link: SafeLink }): Promise => { + // TODO: replace with client fetch + const defaultClient = await client("https://matrix.org"); switch (link.kind) { case LinkKind.Alias: return ( ); @@ -62,24 +52,14 @@ const invite = async ({ link }: { link: SafeLink }): Promise => { case LinkKind.RoomId: return ( ); case LinkKind.UserId: return ( ); @@ -87,15 +67,10 @@ const invite = async ({ link }: { link: SafeLink }): Promise => { case LinkKind.Permalink: return ( { - return cGetUserDetails(client, userId).catch(() => ({ - displayname: userId, - })); -} +export const fallbackUser = (userId: string): User => ({ + displayname: userId, +}); -function defaultRoom(roomId: string): Room { +/* + * Returns an instance of Room with fallback information instead + * of fecthed metadata + */ +export const fallbackRoom = ({ + identifier, + roomId, + roomAlias, +}: { + identifier: string; + roomId?: string; + roomAlias?: string; +}): Room => { + const roomId_ = roomId ? roomId : identifier; + const roomAlias_ = roomAlias ? roomAlias : identifier; return { - aliases: [roomId], + aliases: [roomAlias_], topic: "Unable to find room details.", - canonical_alias: roomId, - name: roomId, + canonical_alias: roomAlias_, + name: roomAlias_, num_joined_members: 0, - room_id: roomId, + room_id: roomId_, guest_can_join: true, avatar_url: "", world_readable: false, }; -} +}; /* - * Gets the details of a room if that room is public + * Tries to fetch room details from an alias. If it fails it uses + * a `fallbackRoom` */ -export function getRoomDetails( - clients: Client[], - roomId: string +export async function getRoomFromAlias( + client: Client, + roomAlias: string ): Promise { - return cGetRoomDetails(clients, roomId).catch(() => defaultRoom(roomId)); + let resolvedRoomAlias: RoomAlias; + try { + resolvedRoomAlias = await getRoomIdFromAlias(client, roomAlias); + } catch { + return fallbackRoom({ identifier: roomAlias }); + } + + try { + return await searchPublicRooms(client, resolvedRoomAlias.room_id); + } catch { + return fallbackRoom({ + identifier: roomAlias, + roomId: resolvedRoomAlias.room_id, + roomAlias: roomAlias, + }); + } } /* - * Searches the public rooms of a homeserver for the metadata of a particular + * Tries to fetch room details from a roomId. If it fails it uses + * a `fallbackRoom` */ -export function searchPublicRooms( +export async function getRoomFromId( client: Client, roomId: string ): Promise { - return cSearchPublicRooms(client, roomId).catch(() => defaultRoom(roomId)); + try { + return await searchPublicRooms(client, roomId); + } catch { + return fallbackRoom({ identifier: roomId }); + } } -export function convertMXCtoMediaQuery(clientURL: string, mxc: string): string { +/* + * Tries to fetch user details. If it fails it uses a `fallbackUser` + */ +export async function getUser(client: Client, userId: string): Promise { try { - return cConvertMXCtoMediaQuery(clientURL, mxc); + return await getUserDetails(client, userId); + } catch { + return fallbackUser(userId); + } +} + +/* + * Tries to fetch room details from a permalink. If it fails it uses + * a `fallbackRoom` + */ +export async function getRoomFromPermalink( + client: Client, + link: Permalink +): Promise { + switch (link.roomKind) { + case LinkKind.Alias: + return getRoomFromAlias(client, link.roomLink); + case LinkKind.RoomId: + return getRoomFromId(client, link.roomLink); + } +} + +/* + * tries to convert an mxc to a media query. If it fails it + * uses the empty string + */ +export function getMediaQueryFromMCX(mxc?: string): string { + if (!mxc) { + return ""; + } + try { + return convertMXCtoMediaQuery( + // TODO: replace with correct client + "https://matrix.org", + mxc + ); } catch { return ""; } } - -export function getRoomIdFromAlias( - client: Client, - roomAlias: string -): Promise { - return cGetRoomIdFromAlias(client, roomAlias).catch(() => ({ - room_id: roomAlias, - servers: [], - })); -}