|
|
|
@ -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<boolean> {
|
|
|
|
|
/*
|
|
|
|
|
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 || [],
|
|
|
|
|