Fix sync message issue

This commit is contained in:
Mikunj 2020-07-03 13:53:17 +10:00
parent 269c87a42e
commit fd547941d6
4 changed files with 37 additions and 1 deletions

View file

@ -297,6 +297,11 @@
const msg = conv.messageCollection.models.find(
convMsg => convMsg.id === tmpMsg.id
);
if (!msg) {
return null;
}
return { msg };
},

View file

@ -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;

View file

@ -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;
}

View file

@ -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
};