2020-08-17 12:48:13 -04:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the 'License');
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an 'AS IS' BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {
|
|
|
|
Client,
|
|
|
|
Room,
|
|
|
|
RoomAlias,
|
2020-08-18 06:07:26 -04:00
|
|
|
User,
|
|
|
|
getRoomIdFromAlias,
|
|
|
|
searchPublicRooms,
|
|
|
|
getUserDetails,
|
|
|
|
convertMXCtoMediaQuery,
|
2020-08-17 12:48:13 -04:00
|
|
|
} from "matrix-cypher";
|
2020-08-18 06:07:26 -04:00
|
|
|
import { LinkKind, Permalink } from "../parser/types";
|
|
|
|
|
|
|
|
/* This is a collection of methods for providing fallback metadata
|
|
|
|
* for cypher queries
|
|
|
|
*/
|
2020-08-17 12:48:13 -04:00
|
|
|
|
|
|
|
/*
|
2020-08-18 06:07:26 -04:00
|
|
|
* Returns an instance of User with fallback information instead
|
|
|
|
* of fetched metadata
|
2020-08-17 12:48:13 -04:00
|
|
|
*/
|
2020-08-18 06:07:26 -04:00
|
|
|
export const fallbackUser = (userId: string): User => ({
|
|
|
|
displayname: userId,
|
|
|
|
});
|
2020-08-17 12:48:13 -04:00
|
|
|
|
2020-08-18 06:07:26 -04:00
|
|
|
/*
|
|
|
|
* 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;
|
2020-08-17 12:48:13 -04:00
|
|
|
return {
|
2020-08-18 06:07:26 -04:00
|
|
|
aliases: [roomAlias_],
|
2020-08-17 12:48:13 -04:00
|
|
|
topic: "Unable to find room details.",
|
2020-08-18 06:07:26 -04:00
|
|
|
canonical_alias: roomAlias_,
|
|
|
|
name: roomAlias_,
|
2020-08-17 12:48:13 -04:00
|
|
|
num_joined_members: 0,
|
2020-08-18 06:07:26 -04:00
|
|
|
room_id: roomId_,
|
2020-08-17 12:48:13 -04:00
|
|
|
guest_can_join: true,
|
|
|
|
avatar_url: "",
|
|
|
|
world_readable: false,
|
|
|
|
};
|
2020-08-18 06:07:26 -04:00
|
|
|
};
|
2020-08-17 12:48:13 -04:00
|
|
|
|
|
|
|
/*
|
2020-08-18 06:07:26 -04:00
|
|
|
* Tries to fetch room details from an alias. If it fails it uses
|
|
|
|
* a `fallbackRoom`
|
2020-08-17 12:48:13 -04:00
|
|
|
*/
|
2020-08-18 06:07:26 -04:00
|
|
|
export async function getRoomFromAlias(
|
|
|
|
client: Client,
|
|
|
|
roomAlias: string
|
2020-08-17 12:48:13 -04:00
|
|
|
): Promise<Room> {
|
2020-08-18 06:07:26 -04:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2020-08-17 12:48:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-08-18 06:07:26 -04:00
|
|
|
* Tries to fetch room details from a roomId. If it fails it uses
|
|
|
|
* a `fallbackRoom`
|
2020-08-17 12:48:13 -04:00
|
|
|
*/
|
2020-08-18 06:07:26 -04:00
|
|
|
export async function getRoomFromId(
|
2020-08-17 12:48:13 -04:00
|
|
|
client: Client,
|
|
|
|
roomId: string
|
|
|
|
): Promise<Room> {
|
2020-08-18 06:07:26 -04:00
|
|
|
try {
|
|
|
|
return await searchPublicRooms(client, roomId);
|
|
|
|
} catch {
|
|
|
|
return fallbackRoom({ identifier: roomId });
|
|
|
|
}
|
2020-08-17 12:48:13 -04:00
|
|
|
}
|
|
|
|
|
2020-08-18 06:07:26 -04:00
|
|
|
/*
|
|
|
|
* Tries to fetch user details. If it fails it uses a `fallbackUser`
|
|
|
|
*/
|
|
|
|
export async function getUser(client: Client, userId: string): Promise<User> {
|
2020-08-17 12:48:13 -04:00
|
|
|
try {
|
2020-08-18 06:07:26 -04:00
|
|
|
return await getUserDetails(client, userId);
|
2020-08-17 12:48:13 -04:00
|
|
|
} catch {
|
2020-08-18 06:07:26 -04:00
|
|
|
return fallbackUser(userId);
|
2020-08-17 12:48:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:07:26 -04:00
|
|
|
/*
|
|
|
|
* Tries to fetch room details from a permalink. If it fails it uses
|
|
|
|
* a `fallbackRoom`
|
|
|
|
*/
|
|
|
|
export async function getRoomFromPermalink(
|
2020-08-17 12:48:13 -04:00
|
|
|
client: Client,
|
2020-08-18 06:07:26 -04:00
|
|
|
link: Permalink
|
|
|
|
): Promise<Room> {
|
|
|
|
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 "";
|
|
|
|
}
|
2020-08-17 12:48:13 -04:00
|
|
|
}
|