add ChatMessage with quotes and attachments

pull/1151/head
Audric Ackermann 5 years ago
parent e345f6a39f
commit 5cec07ef16
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -0,0 +1,157 @@
import { DataMessage } from './DataMessage';
import { SignalService } from '../../../../../protobuf';
import { MessageParams } from '../../Message';
import { LokiProfile } from '../../../../../types/Message';
interface AttachmentPointer {
id?: number;
contentType?: string;
key?: Uint8Array;
size?: number;
thumbnail?: Uint8Array;
digest?: Uint8Array;
filename?: string;
flags?: number;
width?: number;
height?: number;
caption?: string;
url?: string;
}
/** Properties of a Preview. */
interface Preview {
url?: string;
title?: string;
image?: AttachmentPointer;
}
interface QuotedAttachment {
contentType?: string;
fileName?: string;
thumbnail?: AttachmentPointer;
}
interface Quote {
id?: number;
author?: string;
text?: string;
attachments?: Array<QuotedAttachment>;
}
interface ChatMessageParams extends MessageParams {
attachments: Array<AttachmentPointer>;
body?: string;
quote?: Quote;
expireTimer?: number;
lokiProfile?: LokiProfile;
preview?: Array<Preview>;
}
export abstract class ChatMessage extends DataMessage {
private readonly attachments: Array<AttachmentPointer>;
private readonly body?: string;
private readonly quote?: Quote;
private readonly expireTimer?: number | null;
private readonly profileKey?: Uint8Array;
private readonly displayName?: string;
private readonly avatarPointer?: string;
private readonly preview?: Array<Preview>;
constructor(params: ChatMessageParams) {
super({ timestamp: params.timestamp, identifier: params.identifier });
this.attachments = params.attachments;
this.body = params.body;
this.quote = params.quote;
this.expireTimer = params.expireTimer;
this.profileKey = params.lokiProfile && params.lokiProfile.profileKey;
this.displayName = params.lokiProfile && params.lokiProfile.displayName;
this.avatarPointer = params.lokiProfile && params.lokiProfile.avatarPointer;
this.preview = params.preview;
}
public ttl(): number {
return this.getDefaultTTL();
}
public dataProto(): SignalService.DataMessage {
const dataMessage = new SignalService.DataMessage();
if (this.body) {
dataMessage.body = this.body;
}
dataMessage.attachments = this.attachments;
if (this.expireTimer) {
dataMessage.expireTimer = this.expireTimer;
}
if (this.profileKey) {
dataMessage.profileKey = this.profileKey;
}
if (this.preview) {
dataMessage.preview = this.preview;
}
if (this.avatarPointer || this.displayName) {
const profile = new SignalService.DataMessage.LokiProfile();
if (this.avatarPointer) {
profile.avatar = this.avatarPointer;
}
if (this.displayName) {
profile.displayName = this.displayName;
}
dataMessage.profile = profile;
}
if (this.profileKey) {
dataMessage.profileKey = this.profileKey;
}
if (this.quote) {
dataMessage.quote = new SignalService.DataMessage.Quote();
dataMessage.quote.id = this.quote.id;
dataMessage.quote.author = this.quote.author;
dataMessage.quote.text = this.quote.text;
if (this.quote.attachments) {
dataMessage.quote.attachments = this.quote.attachments.map((attachment: QuotedAttachment) => {
const quotedAttachment = new SignalService.DataMessage.Quote.QuotedAttachment();
if (attachment.contentType) {
quotedAttachment.contentType = attachment.contentType;
}
if (attachment.fileName) {
quotedAttachment.fileName = attachment.fileName;
}
if (attachment.thumbnail) {
quotedAttachment.thumbnail = attachment.thumbnail;
}
return quotedAttachment;
});
}
}
if (Array.isArray(this.preview)) {
dataMessage.preview = this.preview.map(preview => {
const item = new SignalService.DataMessage.Preview();
if (preview.title) {
item.title = preview.title;
}
if (preview.url) {
item.url = preview.url;
}
item.image = preview.image || null;
return item;
});
}
return dataMessage;
}
}

@ -0,0 +1,36 @@
import { DataMessage } from './DataMessage';
import { SignalService } from '../../../../../protobuf';
import { MessageParams } from '../../Message';
import { ChatMessage } from './ChatMessage';
interface ClosedGroupChatMessageParams extends MessageParams {
groupId: string;
chatMessage: ChatMessage;
}
export class ClosedGroupChatMessage extends DataMessage {
private readonly groupId: string;
private readonly chatMessage: ChatMessage;
constructor(params: ClosedGroupChatMessageParams) {
super({
timestamp: params.timestamp,
identifier: params.identifier,
});
this.groupId = params.groupId;
this.chatMessage = params.chatMessage;
}
public ttl(): number {
return this.getDefaultTTL();
}
protected dataProto(): SignalService.DataMessage {
const messageProto = this.chatMessage.dataProto();
const id = new TextEncoder().encode(this.groupId);
const type = SignalService.GroupContext.Type.DELIVER;
messageProto.group = new SignalService.GroupContext({id, type});
return messageProto;
}
}

@ -1,13 +0,0 @@
import { DataMessage } from './DataMessage';
import { SignalService } from '../../../../../protobuf';
export class ClosedGroupMessage extends DataMessage {
public ttl(): number {
return this.getDefaultTTL();
}
protected dataProto(): SignalService.DataMessage {
throw new Error('Not implemented');
}
}

@ -1,15 +0,0 @@
import { DataMessage } from './DataMessage';
import { SignalService } from '../../../../../protobuf';
// this message type is probably to sub divise again.
// should handle quote, body, attachmentsPointer, ... @see DataMessage in compiled.d.ts
// Also, find a better name
export abstract class RegularMessage extends DataMessage {
public ttl(): number {
return this.getDefaultTTL();
}
protected dataProto(): SignalService.DataMessage {
throw new Error('Not implemented');
}
}

@ -1,5 +1,5 @@
export * from './ClosedGroupMessage';
export * from './ClosedGroupChatMessage';
export * from './DataMessage';
export * from './DeviceUnlinkMessage';
export * from './GroupInvitationMessage';
export * from './RegularMessage';
export * from './ChatMessage';

Loading…
Cancel
Save