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-12-09 20:58:30 +00:00
|
|
|
import React, {
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
createContext,
|
|
|
|
useMemo,
|
|
|
|
useContext,
|
|
|
|
} from "react";
|
2021-09-29 21:34:29 +00:00
|
|
|
import matrix from "matrix-js-sdk/src/browser-index";
|
2021-12-09 20:58:30 +00:00
|
|
|
import {
|
|
|
|
GroupCallIntent,
|
|
|
|
GroupCallType,
|
|
|
|
} from "matrix-js-sdk/src/browser-index";
|
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
import { randomString } from "matrix-js-sdk/src/randomstring";
|
|
|
|
|
|
|
|
const ClientContext = createContext();
|
2021-08-05 00:40:25 +00:00
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
function waitForSync(client) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-17 23:32:46 +00:00
|
|
|
const onSync = (state, _old, data) => {
|
2021-09-10 19:20:17 +00:00
|
|
|
if (state === "PREPARED") {
|
|
|
|
resolve();
|
|
|
|
client.removeListener("sync", onSync);
|
2021-11-17 23:32:46 +00:00
|
|
|
} else if (state === "ERROR") {
|
|
|
|
reject(data?.error);
|
2021-11-18 00:39:59 +00:00
|
|
|
client.removeListener("sync", onSync);
|
2021-09-10 19:20:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
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-11-17 23:22:27 +00:00
|
|
|
export async function fetchGroupCall(
|
|
|
|
client,
|
|
|
|
roomIdOrAlias,
|
|
|
|
viaServers = undefined,
|
|
|
|
timeout = 5000
|
|
|
|
) {
|
|
|
|
const { roomId } = await client.joinRoom(roomIdOrAlias, { viaServers });
|
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-12-09 20:58:30 +00:00
|
|
|
export function ClientProvider({ homeserverUrl, children }) {
|
|
|
|
const history = useHistory();
|
2021-12-13 19:55:10 +00:00
|
|
|
const [
|
2021-12-13 22:54:44 +00:00
|
|
|
{
|
|
|
|
loading,
|
|
|
|
isAuthenticated,
|
|
|
|
isPasswordlessUser,
|
|
|
|
isGuest,
|
|
|
|
client,
|
|
|
|
userName,
|
|
|
|
displayName,
|
|
|
|
},
|
2021-12-13 19:55:10 +00:00
|
|
|
setState,
|
|
|
|
] = useState({
|
|
|
|
loading: true,
|
|
|
|
isAuthenticated: false,
|
|
|
|
isPasswordlessUser: false,
|
|
|
|
isGuest: false,
|
|
|
|
client: undefined,
|
|
|
|
userName: null,
|
2021-12-13 22:54:44 +00:00
|
|
|
displayName: null,
|
2021-12-13 19:55:10 +00:00
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
async function restore() {
|
|
|
|
try {
|
|
|
|
const authStore = localStorage.getItem("matrix-auth-store");
|
|
|
|
|
|
|
|
if (authStore) {
|
2021-12-13 19:55:10 +00:00
|
|
|
const { user_id, device_id, access_token, guest, passwordlessUser } =
|
2021-10-06 21:19:34 +00:00
|
|
|
JSON.parse(authStore);
|
|
|
|
|
|
|
|
const client = await initClient(
|
|
|
|
{
|
|
|
|
baseUrl: homeserverUrl,
|
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
|
|
|
},
|
|
|
|
guest
|
|
|
|
);
|
2021-09-10 19:20:17 +00:00
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
"matrix-auth-store",
|
2021-12-13 19:55:10 +00:00
|
|
|
JSON.stringify({
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
access_token,
|
|
|
|
guest,
|
|
|
|
passwordlessUser,
|
|
|
|
})
|
2021-09-10 19:20:17 +00:00
|
|
|
);
|
|
|
|
|
2021-12-13 19:55:10 +00:00
|
|
|
return { client, guest, passwordlessUser };
|
2021-09-10 19:20:17 +00:00
|
|
|
}
|
2021-12-09 20:58:30 +00:00
|
|
|
|
|
|
|
return { client: undefined, guest: false };
|
2021-09-10 19:20:17 +00:00
|
|
|
} 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()
|
2021-12-13 19:55:10 +00:00
|
|
|
.then(({ client, guest, passwordlessUser }) => {
|
2021-12-09 20:58:30 +00:00
|
|
|
setState({
|
|
|
|
client,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: !!client,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser: !!passwordlessUser,
|
2021-12-09 20:58:30 +00:00
|
|
|
isGuest: guest,
|
|
|
|
userName: client?.getUserIdLocalpart(),
|
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
})
|
|
|
|
.catch(() => {
|
2021-12-09 20:58:30 +00:00
|
|
|
setState({
|
|
|
|
client: undefined,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: false,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser: false,
|
2021-12-09 20:58:30 +00:00
|
|
|
isGuest: false,
|
|
|
|
userName: null,
|
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2021-10-14 18:48:05 +00:00
|
|
|
const login = useCallback(async (homeserver, username, password) => {
|
2021-09-10 19:20:17 +00:00
|
|
|
try {
|
2021-10-14 18:48:05 +00:00
|
|
|
let loginHomeserverUrl = homeserver.trim();
|
|
|
|
|
|
|
|
if (!loginHomeserverUrl.includes("://")) {
|
|
|
|
loginHomeserverUrl = "https://" + loginHomeserverUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const wellKnownUrl = new URL(
|
|
|
|
"/.well-known/matrix/client",
|
|
|
|
window.location
|
|
|
|
);
|
|
|
|
const response = await fetch(wellKnownUrl);
|
|
|
|
const config = await response.json();
|
|
|
|
|
|
|
|
if (config["m.homeserver"]) {
|
|
|
|
loginHomeserverUrl = config["m.homeserver"];
|
|
|
|
}
|
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
const registrationClient = matrix.createClient(loginHomeserverUrl);
|
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({
|
2021-10-14 18:48:05 +00:00
|
|
|
baseUrl: loginHomeserverUrl,
|
2021-09-10 19:20:17 +00:00
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
"matrix-auth-store",
|
|
|
|
JSON.stringify({ user_id, device_id, access_token })
|
|
|
|
);
|
|
|
|
|
2021-12-09 20:58:30 +00:00
|
|
|
setState({
|
|
|
|
client,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: true,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser: false,
|
2021-12-09 20:58:30 +00:00
|
|
|
isGuest: false,
|
|
|
|
userName: client.getUserIdLocalpart(),
|
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
} catch (err) {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
2021-12-09 20:58:30 +00:00
|
|
|
setState({
|
|
|
|
client: undefined,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: false,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser: false,
|
2021-12-09 20:58:30 +00:00
|
|
|
isGuest: false,
|
|
|
|
userName: null,
|
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
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-10-06 21:19:34 +00:00
|
|
|
const registerGuest = useCallback(async () => {
|
2021-09-10 19:20:17 +00:00
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2021-10-06 21:19:34 +00:00
|
|
|
await client.setProfileInfo("displayname", {
|
|
|
|
displayname: `Guest ${client.getUserIdLocalpart()}`,
|
|
|
|
});
|
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
localStorage.setItem(
|
|
|
|
"matrix-auth-store",
|
2021-10-06 21:19:34 +00:00
|
|
|
JSON.stringify({ user_id, device_id, access_token, guest: true })
|
2021-09-10 19:20:17 +00:00
|
|
|
);
|
|
|
|
|
2021-12-09 20:58:30 +00:00
|
|
|
setState({
|
|
|
|
client,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: true,
|
|
|
|
isGuest: true,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser: false,
|
2021-12-09 20:58:30 +00:00
|
|
|
userName: client.getUserIdLocalpart(),
|
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
} catch (err) {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
2021-12-09 20:58:30 +00:00
|
|
|
setState({
|
|
|
|
client: undefined,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: false,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser: false,
|
2021-12-09 20:58:30 +00:00
|
|
|
isGuest: false,
|
|
|
|
userName: null,
|
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}, []);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-12-13 19:55:10 +00:00
|
|
|
const register = useCallback(async (username, password, passwordlessUser) => {
|
2021-09-10 19:20:17 +00:00
|
|
|
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",
|
2021-12-13 19:55:10 +00:00
|
|
|
JSON.stringify({ user_id, device_id, access_token, passwordlessUser })
|
2021-09-10 19:20:17 +00:00
|
|
|
);
|
2021-07-27 20:26:18 +00:00
|
|
|
|
2021-12-09 20:58:30 +00:00
|
|
|
setState({
|
|
|
|
client,
|
|
|
|
loading: false,
|
|
|
|
isGuest: false,
|
|
|
|
isAuthenticated: true,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser: passwordlessUser,
|
2021-12-09 20:58:30 +00:00
|
|
|
userName: client.getUserIdLocalpart(),
|
|
|
|
});
|
|
|
|
|
|
|
|
return client;
|
2021-09-10 19:20:17 +00:00
|
|
|
} catch (err) {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
2021-12-09 20:58:30 +00:00
|
|
|
setState({
|
|
|
|
client: undefined,
|
|
|
|
loading: false,
|
|
|
|
isGuest: false,
|
|
|
|
isAuthenticated: false,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser: false,
|
2021-12-09 20:58:30 +00:00
|
|
|
userName: null,
|
|
|
|
});
|
2021-09-10 19:20:17 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const logout = useCallback(() => {
|
|
|
|
localStorage.removeItem("matrix-auth-store");
|
2021-12-03 01:21:37 +00:00
|
|
|
window.location = "/";
|
|
|
|
}, [history]);
|
2021-07-27 20:26:18 +00:00
|
|
|
|
2021-12-09 20:58:30 +00:00
|
|
|
const context = useMemo(
|
|
|
|
() => ({
|
|
|
|
loading,
|
|
|
|
isAuthenticated,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser,
|
2021-12-09 20:58:30 +00:00
|
|
|
isGuest,
|
|
|
|
client,
|
|
|
|
login,
|
|
|
|
registerGuest,
|
|
|
|
register,
|
|
|
|
logout,
|
|
|
|
userName,
|
|
|
|
}),
|
|
|
|
[
|
|
|
|
loading,
|
|
|
|
isAuthenticated,
|
2021-12-13 19:55:10 +00:00
|
|
|
isPasswordlessUser,
|
2021-12-09 20:58:30 +00:00
|
|
|
isGuest,
|
|
|
|
client,
|
|
|
|
login,
|
|
|
|
registerGuest,
|
|
|
|
register,
|
|
|
|
logout,
|
|
|
|
userName,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ClientContext.Provider value={context}>{children}</ClientContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useClient() {
|
|
|
|
return useContext(ClientContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
function roomAliasFromRoomName(roomName) {
|
|
|
|
return roomName
|
|
|
|
.trim()
|
|
|
|
.replace(/\s/g, "-")
|
|
|
|
.replace(/[^\w-]/g, "")
|
|
|
|
.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createRoom(client, name) {
|
|
|
|
const { room_id, room_alias } = await client.createRoom({
|
|
|
|
visibility: "private",
|
|
|
|
preset: "public_chat",
|
|
|
|
name,
|
|
|
|
room_alias_name: roomAliasFromRoomName(name),
|
|
|
|
power_level_content_override: {
|
|
|
|
invite: 100,
|
|
|
|
kick: 100,
|
|
|
|
ban: 100,
|
|
|
|
redact: 50,
|
|
|
|
state_default: 0,
|
|
|
|
events_default: 0,
|
|
|
|
users_default: 0,
|
|
|
|
events: {
|
|
|
|
"m.room.power_levels": 100,
|
|
|
|
"m.room.history_visibility": 100,
|
|
|
|
"m.room.tombstone": 100,
|
|
|
|
"m.room.encryption": 100,
|
|
|
|
"m.room.name": 50,
|
|
|
|
"m.room.message": 0,
|
|
|
|
"m.room.encrypted": 50,
|
|
|
|
"m.sticker": 50,
|
|
|
|
"org.matrix.msc3401.call.member": 0,
|
|
|
|
},
|
|
|
|
users: {
|
|
|
|
[client.getUserId()]: 100,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await client.setGuestAccess(room_id, {
|
|
|
|
allowJoin: true,
|
|
|
|
allowRead: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await client.createGroupCall(
|
|
|
|
room_id,
|
|
|
|
GroupCallType.Video,
|
|
|
|
GroupCallIntent.Prompt
|
|
|
|
);
|
|
|
|
|
|
|
|
return room_alias || room_id;
|
|
|
|
}
|
|
|
|
|
2021-12-10 18:54:18 +00:00
|
|
|
export function useCreateRoom() {
|
|
|
|
const { register, client } = useClient();
|
2021-12-09 20:58:30 +00:00
|
|
|
const [creatingRoom, setCreatingRoom] = useState(false);
|
|
|
|
const [createRoomError, setCreateRoomError] = useState();
|
|
|
|
|
|
|
|
const onCreateRoom = useCallback(
|
|
|
|
(roomName, userName) => {
|
|
|
|
async function onCreateRoom(roomName, userName) {
|
2021-12-10 18:54:18 +00:00
|
|
|
let _client = client;
|
|
|
|
|
|
|
|
if (!_client) {
|
2021-12-13 19:55:10 +00:00
|
|
|
_client = await register(userName, randomString(16), true);
|
2021-12-10 18:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return await createRoom(_client, roomName);
|
2021-12-09 20:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setCreateRoomError(undefined);
|
|
|
|
setCreatingRoom(true);
|
|
|
|
|
|
|
|
return onCreateRoom(roomName, userName).catch((error) => {
|
|
|
|
setCreateRoomError(error);
|
|
|
|
setCreatingRoom(false);
|
|
|
|
});
|
|
|
|
},
|
2021-12-10 18:54:18 +00:00
|
|
|
[register, client]
|
2021-12-09 20:58:30 +00:00
|
|
|
);
|
|
|
|
|
2021-09-10 19:20:17 +00:00
|
|
|
return {
|
2021-12-09 20:58:30 +00:00
|
|
|
creatingRoom,
|
|
|
|
createRoomError,
|
|
|
|
createRoom: onCreateRoom,
|
2021-09-10 19:20:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:58:30 +00:00
|
|
|
export function useLoadGroupCall(client, roomId, viaServers) {
|
|
|
|
const [state, setState] = useState({
|
|
|
|
loading: true,
|
|
|
|
error: undefined,
|
|
|
|
groupCall: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setState({ loading: true });
|
|
|
|
fetchGroupCall(client, roomId, viaServers, 30000)
|
|
|
|
.then((groupCall) => setState({ loading: false, groupCall }))
|
|
|
|
.catch((error) => setState({ loading: false, error }));
|
|
|
|
}, [client, roomId]);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
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-10-01 23:17:47 +00:00
|
|
|
export function useGroupCallRooms(client) {
|
2021-07-27 18:55:45 +00:00
|
|
|
const [rooms, setRooms] = useState([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function updateRooms() {
|
2021-10-01 23:17:47 +00:00
|
|
|
const groupCalls = client.groupCallEventHandler.groupCalls.values();
|
|
|
|
const rooms = Array.from(groupCalls).map((groupCall) => groupCall.room);
|
|
|
|
const sortedRooms = sortRooms(client, rooms);
|
2021-10-04 22:37:23 +00:00
|
|
|
const items = sortedRooms.map((room) => {
|
|
|
|
const groupCall = client.getGroupCallForRoom(room.roomId);
|
|
|
|
|
|
|
|
return {
|
2021-12-10 21:44:06 +00:00
|
|
|
roomId: room.getCanonicalAlias() || room.roomId,
|
2021-12-09 20:58:30 +00:00
|
|
|
roomName: room.name,
|
|
|
|
avatarUrl: null,
|
2021-10-04 22:37:23 +00:00
|
|
|
room,
|
|
|
|
groupCall,
|
|
|
|
participants: [...groupCall.participants],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
setRooms(items);
|
2021-07-27 18:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateRooms();
|
|
|
|
|
2021-10-01 23:17:47 +00:00
|
|
|
client.on("GroupCall.incoming", updateRooms);
|
2021-10-04 22:37:23 +00:00
|
|
|
client.on("GroupCall.participants", updateRooms);
|
2021-07-27 18:55:45 +00:00
|
|
|
|
|
|
|
return () => {
|
2021-10-01 23:17:47 +00:00
|
|
|
client.removeListener("GroupCall.incoming", updateRooms);
|
2021-10-04 22:37:23 +00:00
|
|
|
client.removeListener("GroupCall.participants", updateRooms);
|
2021-07-27 18:55:45 +00:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return rooms;
|
|
|
|
}
|
2021-10-01 23:17:47 +00:00
|
|
|
|
|
|
|
export function usePublicRooms(client, publicSpaceRoomId, maxRooms = 50) {
|
|
|
|
const [rooms, setRooms] = useState([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (publicSpaceRoomId) {
|
|
|
|
client.getRoomHierarchy(publicSpaceRoomId, maxRooms).then(({ rooms }) => {
|
2021-12-09 20:58:30 +00:00
|
|
|
const filteredRooms = rooms
|
|
|
|
.filter((room) => room.room_type !== "m.space")
|
|
|
|
.map((room) => ({
|
2021-12-10 21:44:06 +00:00
|
|
|
roomId: room.room_alias || room.room_id,
|
2021-12-09 20:58:30 +00:00
|
|
|
roomName: room.name,
|
|
|
|
avatarUrl: null,
|
|
|
|
room,
|
|
|
|
participants: [],
|
|
|
|
}));
|
2021-10-01 23:17:47 +00:00
|
|
|
|
|
|
|
setRooms(filteredRooms);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setRooms([]);
|
|
|
|
}
|
|
|
|
}, [publicSpaceRoomId]);
|
|
|
|
|
|
|
|
return rooms;
|
|
|
|
}
|
2021-12-10 21:44:06 +00:00
|
|
|
|
|
|
|
export function getRoomUrl(roomId) {
|
|
|
|
if (roomId.startsWith("#")) {
|
|
|
|
const [localPart, host] = roomId.replace("#", "").split(":");
|
|
|
|
|
|
|
|
if (host !== window.location.host) {
|
2021-12-10 22:03:20 +00:00
|
|
|
return `${window.location.host}/room/${roomId}`;
|
2021-12-10 21:44:06 +00:00
|
|
|
} else {
|
|
|
|
return `${window.location.host}/${localPart}`;
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-10 22:03:20 +00:00
|
|
|
return `${window.location.host}/room/${roomId}`;
|
2021-12-10 21:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-13 22:54:44 +00:00
|
|
|
|
|
|
|
export function useDisplayName(client) {
|
|
|
|
const [{ loading, displayName, error, success }, setState] = useState(() => ({
|
|
|
|
success: false,
|
|
|
|
loading: false,
|
|
|
|
displayName: client?.getUser(client.getUserId())?.displayName,
|
|
|
|
error: null,
|
|
|
|
}));
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const onChangeDisplayName = (_event, { displayName }) => {
|
|
|
|
setState({ success: false, loading: false, displayName, error: null });
|
|
|
|
};
|
|
|
|
|
|
|
|
let user;
|
|
|
|
|
|
|
|
if (client) {
|
|
|
|
const userId = client.getUserId();
|
|
|
|
user = client.getUser(userId);
|
|
|
|
user.on("User.displayName", onChangeDisplayName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (user) {
|
|
|
|
user.removeListener("User.displayName", onChangeDisplayName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [client]);
|
|
|
|
|
|
|
|
const setDisplayName = useCallback(
|
|
|
|
(displayName) => {
|
|
|
|
if (client) {
|
|
|
|
setState((prev) => ({
|
|
|
|
...prev,
|
|
|
|
loading: true,
|
|
|
|
error: null,
|
|
|
|
success: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
client
|
|
|
|
.setDisplayName(displayName)
|
|
|
|
.then(() => {
|
|
|
|
setState((prev) => ({
|
|
|
|
...prev,
|
|
|
|
displayName,
|
|
|
|
loading: false,
|
|
|
|
success: true,
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setState((prev) => ({
|
|
|
|
...prev,
|
|
|
|
loading: false,
|
|
|
|
error,
|
|
|
|
success: false,
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.error("Client not initialized before calling setDisplayName");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[client]
|
|
|
|
);
|
|
|
|
|
|
|
|
return { loading, error, displayName, setDisplayName, success };
|
|
|
|
}
|