Use websocket for all chat service requests

This commit is contained in:
ayumi-signal 2024-11-12 11:43:02 -08:00 committed by GitHub
parent fbf39a36fb
commit b517bb817f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 24 additions and 71 deletions

View file

@ -672,55 +672,8 @@ const URL_CALLS = {
whoami: 'v1/accounts/whoami',
};
const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
// MessageController
'messages',
'multiRecipient',
'reportMessage',
// ProfileController
'profile',
// AttachmentControllerV3
'attachmentUploadForm',
// RemoteConfigController
'config',
// Certificate
'deliveryCert',
'getGroupCredentials',
// Devices
'devices',
'linkDevice',
'registerCapabilities',
'transferArchive',
// Directory
'directoryAuthV2',
// Storage
'storageToken',
// Account V2
'phoneNumberDiscoverability',
// Backups
'getBackupCredentials',
'getBackupCDNCredentials',
'getBackupMediaUploadForm',
'getBackupUploadForm',
'backup',
'backupMedia',
'backupMediaBatch',
'backupMediaDelete',
'setBackupId',
'setBackupSignatureKey',
]);
type InitializeOptionsType = {
url: string;
chatServiceUrl: string;
storageUrl: string;
updatesUrl: string;
resourcesUrl: string;
@ -1623,7 +1576,7 @@ type InflightCallback = (error: Error) => unknown;
// We first set up the data that won't change during this session of the app
export function initialize({
url,
chatServiceUrl,
storageUrl,
updatesUrl,
resourcesUrl,
@ -1635,8 +1588,8 @@ export function initialize({
version,
disableIPv6,
}: InitializeOptionsType): WebAPIConnectType {
if (!isString(url)) {
throw new Error('WebAPI.initialize: Invalid server url');
if (!isString(chatServiceUrl)) {
throw new Error('WebAPI.initialize: Invalid chatServiceUrl');
}
if (!isString(storageUrl)) {
throw new Error('WebAPI.initialize: Invalid storageUrl');
@ -1676,7 +1629,11 @@ export function initialize({
// for providing network layer API and related functionality.
// It's important to have a single instance of this class as it holds
// resources that are shared across all other use cases.
const libsignalNet = resolveLibsignalNet(url, version, certificateAuthority);
const libsignalNet = resolveLibsignalNet(
chatServiceUrl,
version,
certificateAuthority
);
libsignalNet.setIpv6Enabled(!disableIPv6);
// Thanks to function-hoisting, we can put this return statement before all of the
@ -1703,7 +1660,7 @@ export function initialize({
let activeRegistration: ExplodePromiseResultType<void> | undefined;
const socketManager = new SocketManager(libsignalNet, {
url,
url: chatServiceUrl,
certificateAuthority,
version,
proxyUrl,
@ -1931,8 +1888,11 @@ export function initialize({
param.urlParameters = '';
}
// When host is not provided, assume chat service
const host = param.host || chatServiceUrl;
const useWebSocketForEndpoint =
useWebSocket && WEBSOCKET_CALLS.has(param.call);
useWebSocket &&
(!param.host || (host === chatServiceUrl && !isMockServer(host)));
const outerParams = {
socketManager: useWebSocketForEndpoint ? socketManager : undefined,
@ -1943,7 +1903,7 @@ export function initialize({
param.data ||
(param.jsonData ? JSON.stringify(param.jsonData) : undefined),
headers: param.headers,
host: param.host || url,
host,
password: param.password ?? password,
path: URL_CALLS[param.call] + param.urlParameters,
proxyUrl,
@ -1952,7 +1912,7 @@ export function initialize({
type: param.httpType,
user: param.username ?? username,
redactUrl: param.redactUrl,
serverUrl: url,
serverUrl: chatServiceUrl,
validateResponse: param.validateResponse,
version,
unauthenticated: param.unauthenticated,