mirror of
https://github.com/vector-im/element-call.git
synced 2026-10-16 23:45:50 +00:00
sdk implementation
This commit is contained in:
+60
-10
@@ -10,25 +10,29 @@ import {
|
||||
createClient,
|
||||
type MatrixClient,
|
||||
MemoryStore,
|
||||
type Room,
|
||||
SyncState,
|
||||
} from "matrix-js-sdk";
|
||||
import { KnownMembership } from "matrix-js-sdk/lib/types";
|
||||
|
||||
/** Logs in with a password and returns a client that has finished its first sync. */
|
||||
export type Login =
|
||||
| { username: string; password: string }
|
||||
| { accessToken: string; userId: string; deviceId: string };
|
||||
|
||||
/**
|
||||
* Returns a client that has finished its first sync, logging in with a
|
||||
* password unless the caller already holds a token.
|
||||
*/
|
||||
export async function createSession(
|
||||
homeserver: string,
|
||||
username: string,
|
||||
password: string,
|
||||
login: Login,
|
||||
): Promise<MatrixClient> {
|
||||
const login = await createClient({ baseUrl: homeserver }).login(
|
||||
"m.login.password",
|
||||
{ identifier: { type: "m.id.user", user: username }, password },
|
||||
);
|
||||
const credentials =
|
||||
"accessToken" in login ? login : await logIn(homeserver, login);
|
||||
|
||||
const client = createClient({
|
||||
baseUrl: homeserver,
|
||||
accessToken: login.access_token,
|
||||
userId: login.user_id,
|
||||
deviceId: login.device_id,
|
||||
...credentials,
|
||||
store: new MemoryStore(),
|
||||
useAuthorizationHeader: true,
|
||||
fallbackICEServerAllowed: true,
|
||||
@@ -47,3 +51,49 @@ export async function createSession(
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins a room and returns it as the sync loop maintains it. The room object
|
||||
* `joinRoom` itself returns for a room joined just now is a detached copy
|
||||
* that never receives the state the sync delivers, and a session built on
|
||||
* it would never see a member.
|
||||
*/
|
||||
export async function joinRoom(
|
||||
client: MatrixClient,
|
||||
roomIdOrAlias: string,
|
||||
): Promise<Room> {
|
||||
const { roomId } = await client.joinRoom(roomIdOrAlias);
|
||||
const joinedRoom = (): Room | undefined => {
|
||||
const room = client.getRoom(roomId);
|
||||
return room?.hasMembershipState(client.getUserId()!, KnownMembership.Join)
|
||||
? room
|
||||
: undefined;
|
||||
};
|
||||
return (
|
||||
joinedRoom() ??
|
||||
new Promise<Room>((resolve) => {
|
||||
const onSync = (): void => {
|
||||
const room = joinedRoom();
|
||||
if (room === undefined) return;
|
||||
client.off(ClientEvent.Sync, onSync);
|
||||
resolve(room);
|
||||
};
|
||||
client.on(ClientEvent.Sync, onSync);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async function logIn(
|
||||
homeserver: string,
|
||||
{ username, password }: { username: string; password: string },
|
||||
): Promise<{ accessToken: string; userId: string; deviceId: string }> {
|
||||
const login = await createClient({ baseUrl: homeserver }).login(
|
||||
"m.login.password",
|
||||
{ identifier: { type: "m.id.user", user: username }, password },
|
||||
);
|
||||
return {
|
||||
accessToken: login.access_token,
|
||||
userId: login.user_id,
|
||||
deviceId: login.device_id,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user