diff --git a/ts/hooks/useParamSelector.ts b/ts/hooks/useParamSelector.ts index e35ae40dd..6f5954029 100644 --- a/ts/hooks/useParamSelector.ts +++ b/ts/hooks/useParamSelector.ts @@ -212,7 +212,7 @@ export function useIsKickedFromGroup(convoId?: string) { export function useIsGroupDestroyed(convoId?: string) { const libIsDestroyed = useLibGroupDestroyed(convoId); if (convoId && PubKey.is03Pubkey(convoId)) { - return libIsDestroyed || false; + return !!libIsDestroyed; } return false; } diff --git a/ts/session/apis/snode_api/batchRequest.ts b/ts/session/apis/snode_api/batchRequest.ts index 64652bcc4..e9aecca64 100644 --- a/ts/session/apis/snode_api/batchRequest.ts +++ b/ts/session/apis/snode_api/batchRequest.ts @@ -2,7 +2,6 @@ import { isArray } from 'lodash'; import { AbortController } from 'abort-controller'; import { MessageSender } from '../../sending'; -import { Snode } from '../../../data/types'; import { SnodeResponseError } from '../../utils/errors'; import { processOnionRequestErrorAtDestination, SnodeResponse } from './onions'; import { SessionRpc } from './sessionRpc'; @@ -15,15 +14,12 @@ import { } from './SnodeRequestTypes'; import { NotEmptyArrayOfBatchResults } from './BatchResultEntry'; import { MergedAbortSignal, WithTimeoutMs } from './requestWith'; -import { WithAllow401s } from '../../types/with'; +import { WithAllow401s, WithAssociatedWith, WithTargetNode } from '../../types/with'; function logSubRequests(requests: Array) { return `[${requests.map(builtRequestToLoggingId).join(', ')}]`; } -type WithTargetNode = { targetNode: Snode }; -type WithAssociatedWith = { associatedWith: string | null }; - /** * This is the equivalent to the batch send on sogs. The target node runs each sub request and returns a list of all the sub status and bodies. * If the global status code is not 200, an exception is thrown. diff --git a/ts/session/apis/snode_api/onions.ts b/ts/session/apis/snode_api/onions.ts index a99e36252..73c4e955b 100644 --- a/ts/session/apis/snode_api/onions.ts +++ b/ts/session/apis/snode_api/onions.ts @@ -21,7 +21,13 @@ import { fileServerHost } from '../file_server_api/FileServerApi'; import { hrefPnServerProd } from '../push_notification_api/PnServer'; import { ERROR_CODE_NO_CONNECT } from './SNodeAPI'; import { MergedAbortSignal, WithAbortSignal, WithTimeoutMs } from './requestWith'; -import { WithAllow401s } from '../../types/with'; +import { + WithAllow401s, + WithAssociatedWith, + WithDestinationEd25519, + WithGuardNode, + WithSymmetricKey, +} from '../../types/with'; // hold the ed25519 key of a snode against the time it fails. Used to remove a snode only after a few failures (snodeFailureThreshold failures) let snodeFailureCount: Record = {}; @@ -311,12 +317,11 @@ export async function processOnionRequestErrorAtDestination({ destinationSnodeEd25519, associatedWith, allow401s, -}: WithAllow401s & { - statusCode: number; - body: string; - destinationSnodeEd25519?: string; - associatedWith?: string; -}) { +}: WithAllow401s & + Partial & { + statusCode: number; + body: string; + }) { if (statusCode === 200) { return; } @@ -328,13 +333,13 @@ export async function processOnionRequestErrorAtDestination({ process401Error(statusCode); } processOxenServerError(statusCode, body); - await process421Error(statusCode, body, associatedWith, destinationSnodeEd25519); + await process421Error(statusCode, body, associatedWith || undefined, destinationSnodeEd25519); if (destinationSnodeEd25519) { await processAnyOtherErrorAtDestination( statusCode, body, destinationSnodeEd25519, - associatedWith + associatedWith || undefined ); } } @@ -342,9 +347,8 @@ export async function processOnionRequestErrorAtDestination({ async function handleNodeNotFound({ ed25519NotFound, associatedWith, -}: { +}: Partial & { ed25519NotFound: string; - associatedWith?: string; }) { const shortNodeNotFound = ed25519Str(ed25519NotFound); window?.log?.warn('Handling NODE NOT FOUND with: ', shortNodeNotFound); @@ -526,13 +530,10 @@ async function processOnionResponse({ associatedWith, destinationSnodeEd25519, allow401s, -}: Partial & - WithAllow401s & { +}: Partial & + WithAllow401s & + WithGuardNode & { response?: { text: () => Promise; status: number }; - symmetricKey?: ArrayBuffer; - guardNode: Snode; - destinationSnodeEd25519?: string; - associatedWith?: string; }): Promise { let ciphertext = ''; @@ -549,7 +550,7 @@ async function processOnionResponse({ ciphertext, guardNode.pubkey_ed25519, destinationSnodeEd25519, - associatedWith + associatedWith || undefined ); if (!ciphertext) { @@ -598,7 +599,7 @@ async function processOnionResponse({ } return value; }) as Record; - + // TODO: type those status const status = jsonRes.status_code || jsonRes.status; await processOnionRequestErrorAtDestination({ @@ -642,13 +643,10 @@ async function processOnionResponseV4({ guardNode, destinationSnodeEd25519, associatedWith, -}: Partial & { - response?: Response; - symmetricKey?: ArrayBuffer; - guardNode: Snode; - destinationSnodeEd25519?: string; - associatedWith?: string; -}): Promise { +}: Partial & + WithGuardNode & { + response?: Response; + }): Promise { processAbortedRequest(abortSignal); const validSymmetricKey = await processNoSymmetricKeyError(guardNode, symmetricKey); @@ -669,7 +667,7 @@ async function processOnionResponseV4({ cipherText, guardNode.pubkey_ed25519, destinationSnodeEd25519, - associatedWith + associatedWith || undefined ); const plaintextBuffer = await callUtilsWorker( @@ -705,9 +703,8 @@ export type FinalRelayOptions = { port?: number; // default to 443 }; -export type DestinationContext = { +export type DestinationContext = WithSymmetricKey & { ciphertext: Uint8Array; - symmetricKey: ArrayBuffer; ephemeralKey: ArrayBuffer; }; @@ -721,10 +718,8 @@ async function handle421InvalidSwarm({ body, destinationSnodeEd25519, associatedWith, -}: { +}: Partial & { body: string; - destinationSnodeEd25519?: string; - associatedWith?: string; }) { if (!destinationSnodeEd25519 || !associatedWith) { // The snode isn't associated with the given public key anymore @@ -784,9 +779,8 @@ async function handle421InvalidSwarm({ async function incrementBadSnodeCountOrDrop({ snodeEd25519, associatedWith, -}: { +}: Partial & { snodeEd25519: string; - associatedWith?: string; }) { const oldFailureCount = snodeFailureCount[snodeEd25519] || 0; const newFailureCount = oldFailureCount + 1; @@ -829,12 +823,12 @@ async function sendOnionRequestHandlingSnodeEjectNoRetries({ timeoutMs, }: WithAbortSignal & WithTimeoutMs & - WithAllow401s & { + WithAllow401s & + Partial & { nodePath: Array; destSnodeX25519: string; finalDestOptions: FinalDestOptions; finalRelayOptions?: FinalRelayOptions; - associatedWith?: string; useV4: boolean; throwErrors: boolean; }): Promise { @@ -1119,12 +1113,12 @@ async function sendOnionRequestSnodeDestNoRetries({ associatedWith, }: WithTimeoutMs & WithAbortSignal & - WithAllow401s & { + WithAllow401s & + Partial & { onionPath: Array; targetNode: Snode; headers: Record; plaintext: string | null; - associatedWith?: string; }) { return Onions.sendOnionRequestHandlingSnodeEjectNoRetries({ nodePath: onionPath, @@ -1156,11 +1150,11 @@ async function lokiOnionFetchNoRetries({ timeoutMs, }: WithTimeoutMs & WithAbortSignal & - WithAllow401s & { + WithAllow401s & + Partial & { targetNode: Snode; headers: Record; body: string | null; - associatedWith?: string; }): Promise { try { // Get a path excluding `targetNode`: diff --git a/ts/session/types/with.ts b/ts/session/types/with.ts index 37db18903..edc9f8bd0 100644 --- a/ts/session/types/with.ts +++ b/ts/session/types/with.ts @@ -1,4 +1,5 @@ import { PubkeyType } from 'libsession_util_nodejs'; +import { Snode } from '../../data/types'; export type WithMessageHash = { messageHash: string }; export type WithTimestamp = { timestamp: number }; @@ -26,3 +27,10 @@ export type ShortenOrExtend = 'extend' | 'shorten' | ''; export type WithShortenOrExtend = { shortenOrExtend: ShortenOrExtend }; export type WithMessagesHashes = { messagesHashes: Array }; export type WithAllow401s = { allow401s: boolean }; + +export type WithDestinationEd25519 = { destinationSnodeEd25519: string }; +export type WithAssociatedWith = { associatedWith: string | null }; +export type WithTargetNode = { targetNode: Snode }; +export type WithGuardNode = { guardNode: Snode }; + +export type WithSymmetricKey = { symmetricKey: ArrayBuffer };