diff --git a/js/modules/loki_app_dot_net_api.d.ts b/js/modules/loki_app_dot_net_api.d.ts index f578c40ab..318facf52 100644 --- a/js/modules/loki_app_dot_net_api.d.ts +++ b/js/modules/loki_app_dot_net_api.d.ts @@ -30,7 +30,7 @@ export interface LokiPublicChannelAPI { body?: string; }, timestamp: number - ): Promise; + ): Promise; } declare class LokiAppDotNetServerAPI implements LokiAppDotNetServerInterface { diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index cc05254cc..526f1b44f 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -2355,7 +2355,7 @@ class LokiPublicChannelAPI { } // there's no retry on desktop // this is supposed to be after retries - return false; + return -1; } } diff --git a/ts/session/sending/MessageQueue.ts b/ts/session/sending/MessageQueue.ts index 228d9d8be..a92248378 100644 --- a/ts/session/sending/MessageQueue.ts +++ b/ts/session/sending/MessageQueue.ts @@ -61,10 +61,18 @@ export class MessageQueue implements MessageQueueInterface { // This is absolutely yucky ... we need to make it not use Promise try { const result = await MessageSender.sendToOpenGroup(message); - if (result) { - this.events.emit('success', message); - } else { + // sendToOpenGroup returns -1 if failed or an id if succeeded + if (result < 0) { this.events.emit('fail', message, error); + } else { + const messageEventData = { + pubKey: message.group.groupId, + timestamp: message.timestamp, + serverId: result, + }; + this.events.emit('success', message); + + window.Whisper.events.trigger('publicMessageSent', messageEventData); } } catch (e) { console.warn( diff --git a/ts/session/sending/MessageSender.ts b/ts/session/sending/MessageSender.ts index d138cba81..835f9468f 100644 --- a/ts/session/sending/MessageSender.ts +++ b/ts/session/sending/MessageSender.ts @@ -98,7 +98,7 @@ function wrapEnvelope(envelope: SignalService.Envelope): Uint8Array { */ export async function sendToOpenGroup( message: OpenGroupMessage -): Promise { +): 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. @@ -112,10 +112,10 @@ export async function sendToOpenGroup( ); if (!channelAPI) { - return false; + return -1; } - // Don't think returning true/false on `sendMessage` is a good way + // Returns -1 on fail or an id > 0 on success return channelAPI.sendMessage( { quote, @@ -125,18 +125,4 @@ export async function sendToOpenGroup( }, timestamp ); - - // TODO: The below should be handled in whichever class calls this - /* - const res = await sendToOpenGroup(message); - if (!res) { - throw new textsecure.PublicChatError('Failed to send public chat message'); - } - const messageEventData = { - pubKey, - timestamp: messageTimeStamp, - }; - messageEventData.serverId = res; - window.Whisper.events.trigger('publicMessageSent', messageEventData); - */ } diff --git a/ts/test/session/sending/MessageQueue_test.ts b/ts/test/session/sending/MessageQueue_test.ts index 4e1644d27..f0d94ac4a 100644 --- a/ts/test/session/sending/MessageQueue_test.ts +++ b/ts/test/session/sending/MessageQueue_test.ts @@ -324,12 +324,12 @@ describe('MessageQueue', () => { describe('open groups', async () => { let sendToOpenGroupStub: sinon.SinonStub< [OpenGroupMessage], - Promise + Promise >; beforeEach(() => { sendToOpenGroupStub = sandbox .stub(MessageSender, 'sendToOpenGroup') - .resolves(true); + .resolves(-1); }); it('can send to open group', async () => { @@ -339,6 +339,8 @@ describe('MessageQueue', () => { }); it('should emit a success event when send was successful', async () => { + sendToOpenGroupStub.resolves(123456); + const message = TestUtils.generateOpenGroupMessage(); const eventPromise = PromiseUtils.waitForTask(complete => { messageQueueStub.events.once('success', complete); @@ -349,7 +351,7 @@ describe('MessageQueue', () => { }); it('should emit a fail event if something went wrong', async () => { - sendToOpenGroupStub.resolves(false); + sendToOpenGroupStub.resolves(-1); const message = TestUtils.generateOpenGroupMessage(); const eventPromise = PromiseUtils.waitForTask(complete => { messageQueueStub.events.once('fail', complete);