matrix.to/src/utils/cypher-wrapper.ts

172 lines
4.2 KiB
TypeScript
Raw Normal View History

/*
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.
*/
2020-08-18 06:16:31 -04:00
// disable camelcase check because our object keys come
// from the matrix spec
/* eslint-disable @typescript-eslint/camelcase */
import {
Client,
2020-09-15 19:19:52 -04:00
client,
Room,
RoomAlias,
2020-08-18 06:07:26 -04:00
User,
getRoomIdFromAlias,
searchPublicRooms,
getUserDetails,
convertMXCtoMediaQuery,
2020-08-18 06:16:31 -04:00
} from 'matrix-cypher';
import { LinkKind, Permalink } from '../parser/types';
2020-08-18 06:07:26 -04:00
/* This is a collection of methods for providing fallback metadata
* for cypher queries
*/
/*
2020-08-18 06:07:26 -04:00
* Returns an instance of User with fallback information instead
* of fetched metadata
*/
2020-08-18 06:07:26 -04:00
export const fallbackUser = (userId: string): User => ({
displayname: userId,
});
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;
return {
2020-08-18 06:07:26 -04:00
aliases: [roomAlias_],
2020-09-15 19:19:52 -04:00
topic:
'No details available. This might be a private room. You can still join below.',
2020-08-18 06:07:26 -04:00
canonical_alias: roomAlias_,
name: roomAlias_,
num_joined_members: 0,
2020-08-18 06:07:26 -04:00
room_id: roomId_,
guest_can_join: true,
2020-08-18 06:16:31 -04:00
avatar_url: '',
world_readable: false,
};
2020-08-18 06:07:26 -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-18 06:07:26 -04:00
export async function getRoomFromAlias(
2020-09-15 19:19:52 -04:00
clientURL: string,
2020-08-18 06:07:26 -04:00
roomAlias: string
): Promise<Room> {
2020-08-18 06:07:26 -04:00
let resolvedRoomAlias: RoomAlias;
2020-09-15 19:19:52 -04:00
let resolvedClient: Client;
2020-08-18 06:07:26 -04:00
try {
2020-09-15 19:19:52 -04:00
resolvedClient = await client(clientURL);
resolvedRoomAlias = await getRoomIdFromAlias(resolvedClient, roomAlias);
2020-08-18 06:07:26 -04:00
} catch {
return fallbackRoom({ identifier: roomAlias });
}
try {
2020-09-15 19:19:52 -04:00
return await searchPublicRooms(
resolvedClient,
resolvedRoomAlias.room_id
);
2020-08-18 06:07:26 -04:00
} catch {
return fallbackRoom({
identifier: roomAlias,
roomId: resolvedRoomAlias.room_id,
roomAlias: roomAlias,
});
}
}
/*
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-18 06:07:26 -04:00
export async function getRoomFromId(
2020-09-15 19:19:52 -04:00
clientURL: string,
roomId: string
): Promise<Room> {
2020-08-18 06:07:26 -04:00
try {
2020-09-15 19:19:52 -04:00
const resolvedClient = await client(clientURL);
return await searchPublicRooms(resolvedClient, roomId);
2020-08-18 06:07:26 -04:00
} catch {
return fallbackRoom({ identifier: roomId });
}
}
2020-08-18 06:07:26 -04:00
/*
* Tries to fetch user details. If it fails it uses a `fallbackUser`
*/
2020-09-15 19:19:52 -04:00
export async function getUser(
clientURL: string,
userId: string
): Promise<User> {
try {
2020-09-15 19:19:52 -04:00
const resolvedClient = await client(clientURL);
return await getUserDetails(resolvedClient, userId);
} catch {
2020-08-18 06:07:26 -04:00
return fallbackUser(userId);
}
}
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-09-15 19:19:52 -04:00
client: string,
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) {
2020-08-18 06:16:31 -04:00
return '';
2020-08-18 06:07:26 -04:00
}
try {
return convertMXCtoMediaQuery(
// TODO: replace with correct client
2020-08-18 06:16:31 -04:00
'https://matrix.org',
2020-08-18 06:07:26 -04:00
mxc
);
} catch {
2020-08-18 06:16:31 -04:00
return '';
2020-08-18 06:07:26 -04:00
}
}