From fd547941d66f09c2778c14afe6223ae5c724ff88 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 3 Jul 2020 13:53:17 +1000 Subject: [PATCH] Fix sync message issue --- js/views/inbox_view.js | 5 ++++ .../outgoing/content/sync/SentSyncMessage.ts | 3 +- ts/session/utils/Protobuf.ts | 28 +++++++++++++++++++ ts/session/utils/index.ts | 2 ++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 ts/session/utils/Protobuf.ts diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 0e4c89681..8ec8e024b 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -297,6 +297,11 @@ const msg = conv.messageCollection.models.find( convMsg => convMsg.id === tmpMsg.id ); + + if (!msg) { + return null; + } + return { msg }; }, diff --git a/ts/session/messages/outgoing/content/sync/SentSyncMessage.ts b/ts/session/messages/outgoing/content/sync/SentSyncMessage.ts index 328fd9958..8778e9938 100644 --- a/ts/session/messages/outgoing/content/sync/SentSyncMessage.ts +++ b/ts/session/messages/outgoing/content/sync/SentSyncMessage.ts @@ -2,6 +2,7 @@ import { SyncMessage } from './SyncMessage'; import { SignalService } from '../../../../../protobuf'; import { MessageParams } from '../../Message'; import { PubKey } from '../../../../types'; +import { ProtobufUtils } from '../../../../utils'; interface SentSyncMessageParams extends MessageParams { dataMessage: SignalService.IDataMessage; @@ -21,7 +22,7 @@ export class SentSyncMessage extends SyncMessage { constructor(params: SentSyncMessageParams) { super({ timestamp: params.timestamp, identifier: params.identifier }); - this.dataMessage = params.dataMessage; + this.dataMessage = ProtobufUtils.convertToTS(params.dataMessage); this.expirationStartTimestamp = params.expirationStartTimestamp; this.sentTo = params.sentTo; this.unidentifiedDeliveries = params.unidentifiedDeliveries; diff --git a/ts/session/utils/Protobuf.ts b/ts/session/utils/Protobuf.ts new file mode 100644 index 000000000..ac9023803 --- /dev/null +++ b/ts/session/utils/Protobuf.ts @@ -0,0 +1,28 @@ +import ByteBuffer from 'bytebuffer'; + +/** + * Converts any object to a valid ts protobuf object. + * + * This is needed because there's a very jarring difference between `protobufjs` and `protobufts`. + * `protobufjs` returns all `bytes` as `ByteBuffer` where as `protobufts` returns all `bytes` as `Uint8Array`. + */ +export function convertToTS(object: any): any { + // No idea why js `ByteBuffer` and ts `ByteBuffer` differ ... + if (object && object.constructor && object.constructor.name === 'ByteBuffer') { + return new Uint8Array(object.toArrayBuffer()); + } else if (object instanceof ByteBuffer || object instanceof Buffer || object instanceof ArrayBuffer || object instanceof SharedArrayBuffer) { + const arrayBuffer = ByteBuffer.wrap(object).toArrayBuffer(); + return new Uint8Array(arrayBuffer); + } else if (Array.isArray(object)) { + return object.map(convertToTS); + } else if (object && typeof object === 'object') { + const keys = Object.keys(object); + const values: { [key: string]: any } = {}; + for (const key of keys) { + values[key] = convertToTS(object[key]); + } + return values; + } + + return object; +} diff --git a/ts/session/utils/index.ts b/ts/session/utils/index.ts index 73a11d4de..2db75fa65 100644 --- a/ts/session/utils/index.ts +++ b/ts/session/utils/index.ts @@ -3,6 +3,7 @@ import * as GroupUtils from './Groups'; import * as SyncMessageUtils from './SyncMessage'; import * as StringUtils from './String'; import * as PromiseUtils from './Promise'; +import * as ProtobufUtils from './Protobuf'; export * from './Attachments'; export * from './TypedEmitter'; @@ -14,4 +15,5 @@ export { GroupUtils, StringUtils, PromiseUtils, + ProtobufUtils };