session-desktop/ts/types/Message.ts

106 lines
2.4 KiB
TypeScript
Raw Normal View History

2018-04-10 01:26:48 +02:00
import { Attachment } from './Attachment';
import { Contact } from './Contact';
import { IndexableBoolean, IndexablePresence } from './IndexedDB';
2018-04-10 01:26:48 +02:00
export type Message = UserMessage | VerifiedChangeMessage;
export type UserMessage = IncomingMessage | OutgoingMessage;
2018-04-10 01:26:48 +02:00
2018-04-13 22:25:52 +02:00
export type IncomingMessage = Readonly<
{
type: 'incoming';
// Required
attachments: Array<Attachment>;
id: string;
received_at: number;
2018-04-10 01:26:48 +02:00
2018-04-13 22:25:52 +02:00
// Optional
body?: string;
decrypted_at?: number;
errors?: Array<any>;
expireTimer?: number;
2018-04-13 22:25:52 +02:00
flags?: number;
source?: string;
sourceDevice?: number;
} & SharedMessageProperties &
MessageSchemaVersion5 &
MessageSchemaVersion6 &
2018-04-13 22:25:52 +02:00
ExpirationTimerUpdate
>;
2018-04-13 22:25:52 +02:00
export type OutgoingMessage = Readonly<
{
type: 'outgoing';
2018-04-10 01:26:48 +02:00
2018-04-13 22:25:52 +02:00
// Required
attachments: Array<Attachment>;
delivered: number;
delivered_to: Array<string>;
destination: string; // PhoneNumber
expirationStartTimestamp: number;
id: string;
received_at: number;
sent: boolean;
sent_to: Array<string>; // Array<PhoneNumber>
2018-04-13 22:25:52 +02:00
// Optional
body?: string;
expires_at?: number;
expireTimer?: number;
recipients?: Array<string>; // Array<PhoneNumber>
synced: boolean;
} & SharedMessageProperties &
MessageSchemaVersion5 &
2018-04-13 22:25:52 +02:00
ExpirationTimerUpdate
>;
2018-04-13 22:25:52 +02:00
export type VerifiedChangeMessage = Readonly<
{
type: 'verified-change';
} & SharedMessageProperties &
MessageSchemaVersion5 &
2018-04-13 22:25:52 +02:00
ExpirationTimerUpdate
>;
2018-04-12 00:36:11 +02:00
type SharedMessageProperties = Readonly<{
conversationId: string;
sent_at: number;
2018-04-10 01:26:48 +02:00
timestamp: number;
2018-04-12 00:36:11 +02:00
}>;
2018-04-14 04:09:56 +02:00
type ExpirationTimerUpdate = Partial<
Readonly<{
expirationTimerUpdate: Readonly<{
expireTimer: number;
fromSync: boolean;
source: string; // PhoneNumber
}>;
}>
>;
2018-04-10 01:26:48 +02:00
type MessageSchemaVersion5 = Partial<
2018-04-14 04:09:56 +02:00
Readonly<{
hasAttachments: IndexableBoolean;
hasVisualMediaAttachments: IndexablePresence;
hasFileAttachments: IndexablePresence;
2018-04-14 04:09:56 +02:00
}>
>;
type MessageSchemaVersion6 = Partial<
Readonly<{
contact: Array<Contact>;
}>
>;
export const isUserMessage = (message: Message): message is UserMessage =>
message.type === 'incoming' || message.type === 'outgoing';
export const hasExpiration = (message: Message): boolean => {
if (!isUserMessage(message)) {
return false;
}
const { expireTimer } = message;
return typeof expireTimer === 'number' && expireTimer > 0;
};