You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/session/utils/Messages.ts

41 lines
1.2 KiB
TypeScript

import { RawMessage } from '../types/RawMessage';
import {
ContentMessage,
MediumGroupChatMessage,
SessionRequestMessage,
} from '../messages/outgoing';
import { EncryptionType, PubKey } from '../types';
import { SessionProtocol } from '../protocols';
export async function toRawMessage(
device: PubKey,
message: ContentMessage
): Promise<RawMessage> {
const timestamp = message.timestamp;
const ttl = message.ttl();
const plainTextBuffer = message.plainTextBuffer();
let encryption: EncryptionType;
if (message instanceof MediumGroupChatMessage) {
encryption = EncryptionType.MediumGroup;
} else if (message instanceof SessionRequestMessage) {
encryption = EncryptionType.Fallback;
} else {
// If we don't have a session yet then send using fallback encryption until we have a session
const hasSession = await SessionProtocol.hasSession(device);
encryption = hasSession ? EncryptionType.Signal : EncryptionType.Fallback;
}
// tslint:disable-next-line: no-unnecessary-local-variable
const rawMessage: RawMessage = {
identifier: message.identifier,
plainTextBuffer,
timestamp,
device: device.key,
ttl,
encryption,
};
return rawMessage;
}