diff --git a/ts/session/crypto/MessageEncrypter.ts b/ts/session/crypto/MessageEncrypter.ts index 660c2448f..2919d754a 100644 --- a/ts/session/crypto/MessageEncrypter.ts +++ b/ts/session/crypto/MessageEncrypter.ts @@ -4,6 +4,10 @@ import { libloki, libsignal, Signal, textsecure } from '../../window'; import { UserUtil } from '../../util'; import { CipherTextObject } from '../../../libtextsecure/libsignal-protocol'; +/** + * Add padding to a message buffer + * @param messageBuffer The buffer to add padding to. + */ export function padPlainTextBuffer(messageBuffer: Uint8Array): Uint8Array { const plaintext = new Uint8Array( getPaddedMessageLength(messageBuffer.byteLength + 1) - 1 diff --git a/ts/session/sending/MessageSender.ts b/ts/session/sending/MessageSender.ts index 58c070d72..c6753a04c 100644 --- a/ts/session/sending/MessageSender.ts +++ b/ts/session/sending/MessageSender.ts @@ -10,11 +10,19 @@ import pRetry from 'p-retry'; // ================ Regular ================ +/** + * Check if we can send to service nodes. + */ export function canSendToSnode(): boolean { // Seems like lokiMessageAPI is not always guaranteed to be initialized return Boolean(lokiMessageAPI); } +/** + * Send a message via service nodes. + * @param message The message to send. + * @param retries The amount of times to retry sending. + */ export async function send( { device, plainTextBuffer, encryption, timestamp, ttl }: RawMessage, retries: number = 3 @@ -31,7 +39,9 @@ export async function send( const envelope = await buildEnvelope(envelopeType, timestamp, cipherText); const data = wrapEnvelope(envelope); - // pRetry doesn't count the first call as a retry + // pRetry counts retries after making the first call. + // So a retry couunt of 3 means you make a request then if it fails you make another request 3 times until it succeeds. + // This means a total of 4 requests are being sent, where as for us when we want 3 retries we only want 3 requests sent. return pRetry( async () => lokiMessageAPI.sendMessage(device, data, timestamp, ttl), { @@ -80,9 +90,18 @@ function wrapEnvelope(envelope: SignalService.Envelope): Uint8Array { // ================ Open Group ================ +/** + * Send a message to an open group. + * @param message The open group message. + */ export async function sendToOpenGroup( message: OpenGroupMessage ): Promise { + /* + Note: Retrying wasn't added to this but it can be added in the future if needed. + The only problem is that `channelAPI.sendMessage` returns true/false and doesn't throw any error so we can never be sure why sending failed. + This should be fixed and we shouldn't rely on returning true/false, rather return nothing (success) or throw an error (failure) + */ const { group, quote, attachments, preview, body } = message; const channelAPI = await lokiPublicChatAPI.findOrCreateChannel( group.server, @@ -91,7 +110,6 @@ export async function sendToOpenGroup( ); // Don't think returning true/false on `sendMessage` is a good way - // We should either: return nothing (success) or throw an error (failure) return channelAPI.sendMessage({ quote, attachments: attachments || [],