Initial barebone commit for messages refactor
parent
c7b76dfebb
commit
9bd08ba930
@ -0,0 +1,4 @@
|
|||||||
|
import * as Outgoing from './outgoing';
|
||||||
|
|
||||||
|
// For Audric: Do you think we need to namespace?
|
||||||
|
export { Outgoing };
|
@ -0,0 +1,25 @@
|
|||||||
|
import { OutgoingMessage } from './OutgoingMessage';
|
||||||
|
import { AttachmentType } from '../../../types/Attachment';
|
||||||
|
import { QuotedAttachmentType } from '../../../components/conversation/Quote';
|
||||||
|
|
||||||
|
export class OpenGroupMessage implements OutgoingMessage {
|
||||||
|
public timestamp: number;
|
||||||
|
public server: string;
|
||||||
|
public body?: string;
|
||||||
|
public attachments: [AttachmentType]; // TODO: Not sure if we should only use a subset of this type
|
||||||
|
public quote?: QuotedAttachmentType;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
timestamp: number,
|
||||||
|
server: string,
|
||||||
|
attachments: [AttachmentType],
|
||||||
|
body?: string,
|
||||||
|
quote?: QuotedAttachmentType
|
||||||
|
) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
this.server = server;
|
||||||
|
this.body = body;
|
||||||
|
this.attachments = attachments;
|
||||||
|
this.quote = quote;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
import { OutgoingMessage } from './OutgoingMessage';
|
||||||
|
import { SignalService } from '../../../protobuf';
|
||||||
|
|
||||||
|
export class OutgoingContentMessage implements OutgoingMessage {
|
||||||
|
public timestamp: number;
|
||||||
|
public identifier: string;
|
||||||
|
public category: OutgoingContentMessage.MessageCategory;
|
||||||
|
public ttl: number;
|
||||||
|
constructor(
|
||||||
|
timestamp: number,
|
||||||
|
identifier: string,
|
||||||
|
category: OutgoingContentMessage.MessageCategory,
|
||||||
|
ttl: number
|
||||||
|
) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
this.identifier = identifier;
|
||||||
|
this.category = category;
|
||||||
|
this.ttl = ttl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public plainTextBuffer(): Uint8Array {
|
||||||
|
const encoded = SignalService.Content.encode(this.contentProto()).finish();
|
||||||
|
|
||||||
|
return this.processPlainTextBuffer(encoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
public contentProto(): SignalService.Content {
|
||||||
|
throw new Error('contentProto() needs to be implemented.');
|
||||||
|
}
|
||||||
|
|
||||||
|
private processPlainTextBuffer(buffer: Uint8Array): Uint8Array {
|
||||||
|
const paddedMessageLength = this.getPaddedMessageLength(
|
||||||
|
buffer.byteLength + 1
|
||||||
|
);
|
||||||
|
const plainText = new Uint8Array(paddedMessageLength - 1);
|
||||||
|
plainText.set(new Uint8Array(buffer));
|
||||||
|
plainText[buffer.byteLength] = 0x80;
|
||||||
|
|
||||||
|
return plainText;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getPaddedMessageLength(length: number): number {
|
||||||
|
const messageLengthWithTerminator = length + 1;
|
||||||
|
let messagePartCount = Math.floor(messageLengthWithTerminator / 160);
|
||||||
|
|
||||||
|
if (messageLengthWithTerminator % 160 !== 0) {
|
||||||
|
messagePartCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return messagePartCount * 160;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export namespace OutgoingContentMessage {
|
||||||
|
export enum MessageCategory {
|
||||||
|
Secure,
|
||||||
|
SessionReset,
|
||||||
|
MediumGroup,
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
import { OutgoingContentMessage } from './OutgoingContentMessage';
|
||||||
|
import { SignalService } from '../../../protobuf';
|
||||||
|
|
||||||
|
export class OutgoingDataMessage extends OutgoingContentMessage {
|
||||||
|
public contentProto(): SignalService.Content {
|
||||||
|
return new SignalService.Content({
|
||||||
|
dataMessage: this.dataProto(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected dataProto(): SignalService.DataMessage {
|
||||||
|
throw new Error('dataProto() needs to be implemented.');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
export interface OutgoingMessage {
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove me once this is read
|
||||||
|
// Note for Audric, we don't have a `plainText()` function here because i realised that we use Uint8Arrays when encrypting
|
||||||
|
// It wouldn't make sense for this then to be `plainTextBuffer(): UInt8Array` as that's specific to OutgoingContentMessage.
|
||||||
|
// Thus i've left it out and moved it to outgoing content message
|
@ -0,0 +1,11 @@
|
|||||||
|
import { OutgoingMessage } from './OutgoingMessage';
|
||||||
|
import { OutgoingContentMessage } from './OutgoingContentMessage';
|
||||||
|
import { OutgoingDataMessage } from './OutgoingDataMessage';
|
||||||
|
import { OpenGroupMessage } from './OpenGroupMessage';
|
||||||
|
|
||||||
|
export {
|
||||||
|
OutgoingMessage,
|
||||||
|
OutgoingContentMessage,
|
||||||
|
OutgoingDataMessage,
|
||||||
|
OpenGroupMessage,
|
||||||
|
};
|
Loading…
Reference in New Issue