2021-07-27 19:27:59 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 New Vector Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-09-29 21:34:29 +00:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
import matrix from "matrix-js-sdk/src/browser-index";
|
2021-08-05 00:40:25 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
function waitForSync(client) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const onSync = (state) => {
|
|
|
|
if (state === "PREPARED") {
|
|
|
|
resolve();
|
|
|
|
client.removeListener("sync", onSync);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
client.on("sync", onSync);
|
2021-07-27 18:40:19 +00:00
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
}
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
async function initClient(clientOptions, guest) {
|
|
|
|
const client = matrix.createClient(clientOptions);
|
2021-08-10 01:38:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
if (guest) {
|
|
|
|
client.setGuest(true);
|
|
|
|
}
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
await client.startClient({
|
|
|
|
// dirty hack to reduce chance of gappy syncs
|
|
|
|
// should be fixed by spotting gaps and backpaginating
|
|
|
|
initialSyncLimit: 50,
|
|
|
|
});
|
2021-08-02 18:16:14 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
await waitForSync(client);
|
2021-08-02 18:16:14 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
return client;
|
2021-07-27 18:40:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:23:18 +00:00
|
|
|
export async function fetchGroupCall(client, roomIdOrAlias, timeout = 5000) {
|
2021-09-16 22:01:14 +00:00
|
|
|
const { roomId } = await client.joinRoom(roomIdOrAlias);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-07-27 18:40:19 +00:00
|
|
|
let timeoutId;
|
|
|
|
|
2021-09-29 23:23:18 +00:00
|
|
|
function onGroupCallIncoming(groupCall) {
|
|
|
|
if (groupCall && groupCall.room.roomId === roomId) {
|
2021-07-27 18:40:19 +00:00
|
|
|
clearTimeout(timeoutId);
|
2021-09-29 23:23:18 +00:00
|
|
|
client.removeListener("GroupCall.incoming", onGroupCallIncoming);
|
|
|
|
resolve(groupCall);
|
2021-07-27 18:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 23:23:18 +00:00
|
|
|
const groupCall = client.getGroupCallForRoom(roomId);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-29 23:23:18 +00:00
|
|
|
if (groupCall) {
|
|
|
|
resolve(groupCall);
|
2021-09-10 19:20:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:23:18 +00:00
|
|
|
client.on("GroupCall.incoming", onGroupCallIncoming);
|
2021-08-10 00:23:07 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
if (timeout) {
|
2021-08-10 00:23:07 +00:00
|
|
|
timeoutId = setTimeout(() => {
|
2021-09-29 23:23:18 +00:00
|
|
|
client.removeListener("GroupCall.incoming", onGroupCallIncoming);
|
|
|
|
reject(new Error("Fetching group call timed out."));
|
2021-08-10 00:23:07 +00:00
|
|
|
}, timeout);
|
|
|
|
}
|
2021-09-10 19:20:17 +00:00
|
|
|
});
|
|
|
|
}
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
export function useClient(homeserverUrl) {
|
|
|
|
const [{ loading, authenticated, client }, setState] = useState({
|
|
|
|
loading: true,
|
|
|
|
authenticated: false,
|
|
|
|
client: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
async function restore() {
|
|
|
|
try {
|
|
|
|
const authStore = localStorage.getItem("matrix-auth-store");
|
|
|
|
|
|
|
|
if (authStore) {
|
|
|
|
const { user_id, device_id, access_token } = JSON.parse(authStore);
|
|
|
|
|
|
|
|
const client = await initClient({
|
|
|
|
baseUrl: homeserverUrl,
|
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
"matrix-auth-store",
|
|
|
|
JSON.stringify({ user_id, device_id, access_token })
|
|
|
|
);
|
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
|
|
|
throw err;
|
|
|
|
}
|
2021-08-21 00:02:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
restore()
|
|
|
|
.then((client) => {
|
|
|
|
if (client) {
|
|
|
|
setState({ client, loading: false, authenticated: true });
|
|
|
|
} else {
|
|
|
|
setState({ client: undefined, loading: false, authenticated: false });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setState({ client: undefined, loading: false, authenticated: false });
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const login = useCallback(async (username, password) => {
|
|
|
|
try {
|
|
|
|
const registrationClient = matrix.createClient(homeserverUrl);
|
2021-08-21 00:02:47 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
const { user_id, device_id, access_token } =
|
|
|
|
await registrationClient.loginWithPassword(username, password);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
const client = await initClient({
|
|
|
|
baseUrl: homeserverUrl,
|
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
"matrix-auth-store",
|
|
|
|
JSON.stringify({ user_id, device_id, access_token })
|
|
|
|
);
|
|
|
|
|
|
|
|
setState({ client, loading: false, authenticated: true });
|
|
|
|
} catch (err) {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
|
|
|
setState({ client: undefined, loading: false, authenticated: false });
|
|
|
|
throw err;
|
2021-08-09 20:54:23 +00:00
|
|
|
}
|
2021-09-10 19:20:17 +00:00
|
|
|
}, []);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
const registerGuest = useCallback(async (displayName) => {
|
|
|
|
try {
|
|
|
|
const registrationClient = matrix.createClient(homeserverUrl);
|
|
|
|
|
|
|
|
const { user_id, device_id, access_token } =
|
|
|
|
await registrationClient.registerGuest({});
|
|
|
|
|
|
|
|
const client = await initClient(
|
|
|
|
{
|
|
|
|
baseUrl: homeserverUrl,
|
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
"matrix-auth-store",
|
|
|
|
JSON.stringify({ user_id, device_id, access_token })
|
|
|
|
);
|
|
|
|
|
|
|
|
setState({ client, loading: false, authenticated: true });
|
|
|
|
} catch (err) {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
|
|
|
setState({ client: undefined, loading: false, authenticated: false });
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}, []);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
const register = useCallback(async (username, password) => {
|
|
|
|
try {
|
|
|
|
const registrationClient = matrix.createClient(homeserverUrl);
|
|
|
|
|
|
|
|
const { user_id, device_id, access_token } =
|
|
|
|
await registrationClient.register(username, password, null, {
|
|
|
|
type: "m.login.dummy",
|
|
|
|
});
|
|
|
|
|
|
|
|
const client = await initClient({
|
|
|
|
baseUrl: homeserverUrl,
|
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
2021-08-05 17:39:49 +00:00
|
|
|
});
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
localStorage.setItem(
|
|
|
|
"matrix-auth-store",
|
|
|
|
JSON.stringify({ user_id, device_id, access_token })
|
|
|
|
);
|
2021-07-27 20:26:18 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
setState({ client, loading: false, authenticated: true });
|
|
|
|
} catch (err) {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
|
|
|
setState({ client: undefined, loading: false, authenticated: false });
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const logout = useCallback(() => {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
|
|
|
setState({ client: undefined, loading: false, authenticated: false });
|
|
|
|
}, []);
|
2021-07-27 20:26:18 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
return {
|
|
|
|
loading,
|
|
|
|
authenticated,
|
|
|
|
client,
|
|
|
|
login,
|
|
|
|
registerGuest,
|
|
|
|
register,
|
|
|
|
logout,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-23 19:32:32 +00:00
|
|
|
const tsCache = {};
|
|
|
|
|
|
|
|
function getLastTs(client, r) {
|
|
|
|
if (tsCache[r.roomId]) {
|
|
|
|
return tsCache[r.roomId];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r || !r.timeline) {
|
|
|
|
const ts = Number.MAX_SAFE_INTEGER;
|
|
|
|
tsCache[r.roomId] = ts;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
const myUserId = client.getUserId();
|
|
|
|
|
|
|
|
if (r.getMyMembership() !== "join") {
|
|
|
|
const membershipEvent = r.currentState.getStateEvents(
|
|
|
|
"m.room.member",
|
|
|
|
myUserId
|
|
|
|
);
|
|
|
|
|
|
|
|
if (membershipEvent && !Array.isArray(membershipEvent)) {
|
|
|
|
const ts = membershipEvent.getTs();
|
|
|
|
tsCache[r.roomId] = ts;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = r.timeline.length - 1; i >= 0; --i) {
|
|
|
|
const ev = r.timeline[i];
|
|
|
|
const ts = ev.getTs();
|
|
|
|
|
|
|
|
if (ts) {
|
|
|
|
tsCache[r.roomId] = ts;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ts = Number.MAX_SAFE_INTEGER;
|
|
|
|
tsCache[r.roomId] = ts;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortRooms(client, rooms) {
|
|
|
|
return rooms.sort((a, b) => {
|
|
|
|
return getLastTs(client, b) - getLastTs(client, a);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
export function useRooms(client) {
|
2021-07-27 18:55:45 +00:00
|
|
|
const [rooms, setRooms] = useState([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function updateRooms() {
|
2021-09-10 19:20:17 +00:00
|
|
|
const visibleRooms = client.getVisibleRooms();
|
|
|
|
const sortedRooms = sortRooms(client, visibleRooms);
|
2021-08-23 19:32:32 +00:00
|
|
|
setRooms(sortedRooms);
|
2021-07-27 18:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateRooms();
|
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
client.on("Room", updateRooms);
|
2021-07-27 18:55:45 +00:00
|
|
|
|
|
|
|
return () => {
|
2021-09-10 19:20:17 +00:00
|
|
|
client.removeListener("Room", updateRooms);
|
2021-07-27 18:55:45 +00:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return rooms;
|
|
|
|
}
|