Merge pull request #54 from status-im/no-text-media-content
This commit is contained in:
commit
738609d53f
|
@ -176,14 +176,18 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
|
||||||
|
|
||||||
const sendMessage = useCallback(
|
const sendMessage = useCallback(
|
||||||
async (messageText: string, image?: Uint8Array) => {
|
async (messageText: string, image?: Uint8Array) => {
|
||||||
const mediaContent = image
|
// TODO: A message can either contain text or media, not both.
|
||||||
|
const content = image
|
||||||
? {
|
? {
|
||||||
image,
|
image,
|
||||||
imageType: 1,
|
imageType: 1,
|
||||||
contentType: 1,
|
contentType: 2,
|
||||||
}
|
}
|
||||||
: undefined;
|
: {
|
||||||
await messenger?.sendMessage(messageText, chatId, mediaContent);
|
text: messageText,
|
||||||
|
contentType: 0,
|
||||||
|
};
|
||||||
|
await messenger?.sendMessage(chatId, content);
|
||||||
addNewMessageRaw(
|
addNewMessageRaw(
|
||||||
messenger?.identity.publicKey ?? new Uint8Array(),
|
messenger?.identity.publicKey ?? new Uint8Array(),
|
||||||
messageText,
|
messageText,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { ChatMessage, MediaContent } from "./chat_message";
|
import { ChatMessage, Content } from "./chat_message";
|
||||||
import { chatIdToContentTopic } from "./contentTopic";
|
import { chatIdToContentTopic } from "./contentTopic";
|
||||||
import { createSymKeyFromPassword } from "./encryption";
|
import { createSymKeyFromPassword } from "./encryption";
|
||||||
|
|
||||||
|
@ -24,15 +24,14 @@ export class Chat {
|
||||||
return chatIdToContentTopic(this.id);
|
return chatIdToContentTopic(this.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public createMessage(text: string, mediaContent?: MediaContent): ChatMessage {
|
public createMessage(content: Content): ChatMessage {
|
||||||
const { timestamp, clock } = this._nextClockAndTimestamp();
|
const { timestamp, clock } = this._nextClockAndTimestamp();
|
||||||
|
|
||||||
const message = ChatMessage.createMessage(
|
const message = ChatMessage.createMessage(
|
||||||
clock,
|
clock,
|
||||||
timestamp,
|
timestamp,
|
||||||
text,
|
|
||||||
this.id,
|
this.id,
|
||||||
mediaContent
|
content
|
||||||
);
|
);
|
||||||
|
|
||||||
this._updateClockFromMessage(message);
|
this._updateClockFromMessage(message);
|
||||||
|
|
|
@ -23,13 +23,7 @@ describe("Chat Message", () => {
|
||||||
contentType: ContentType.Image,
|
contentType: ContentType.Image,
|
||||||
};
|
};
|
||||||
|
|
||||||
const message = ChatMessage.createMessage(
|
const message = ChatMessage.createMessage(1, 1, "chat-id", imageContent);
|
||||||
1,
|
|
||||||
1,
|
|
||||||
"Some text",
|
|
||||||
"chat-id",
|
|
||||||
imageContent
|
|
||||||
);
|
|
||||||
|
|
||||||
const buf = message.encode();
|
const buf = message.encode();
|
||||||
const dec = ChatMessage.decode(buf);
|
const dec = ChatMessage.decode(buf);
|
||||||
|
@ -50,13 +44,7 @@ describe("Chat Message", () => {
|
||||||
contentType: ContentType.Audio,
|
contentType: ContentType.Audio,
|
||||||
};
|
};
|
||||||
|
|
||||||
const message = ChatMessage.createMessage(
|
const message = ChatMessage.createMessage(1, 1, "chat-id", audioContent);
|
||||||
1,
|
|
||||||
1,
|
|
||||||
"Some text",
|
|
||||||
"chat-id",
|
|
||||||
audioContent
|
|
||||||
);
|
|
||||||
|
|
||||||
const buf = message.encode();
|
const buf = message.encode();
|
||||||
const dec = ChatMessage.decode(buf);
|
const dec = ChatMessage.decode(buf);
|
||||||
|
@ -77,13 +65,7 @@ describe("Chat Message", () => {
|
||||||
contentType: ContentType.Sticker,
|
contentType: ContentType.Sticker,
|
||||||
};
|
};
|
||||||
|
|
||||||
const message = ChatMessage.createMessage(
|
const message = ChatMessage.createMessage(1, 1, "chat-id", stickerContent);
|
||||||
1,
|
|
||||||
1,
|
|
||||||
"Some text",
|
|
||||||
"chat-id",
|
|
||||||
stickerContent
|
|
||||||
);
|
|
||||||
|
|
||||||
const buf = message.encode();
|
const buf = message.encode();
|
||||||
const dec = ChatMessage.decode(buf);
|
const dec = ChatMessage.decode(buf);
|
||||||
|
|
|
@ -10,14 +10,24 @@ import {
|
||||||
} from "./proto/communities/v1/chat_message";
|
} from "./proto/communities/v1/chat_message";
|
||||||
import { ImageType, MessageType } from "./proto/communities/v1/enums";
|
import { ImageType, MessageType } from "./proto/communities/v1/enums";
|
||||||
|
|
||||||
export type MediaContent = StickerContent | ImageContent | AudioContent;
|
export type Content =
|
||||||
|
| TextContent
|
||||||
|
| StickerContent
|
||||||
|
| ImageContent
|
||||||
|
| AudioContent;
|
||||||
|
|
||||||
export enum ContentType {
|
export enum ContentType {
|
||||||
|
Text,
|
||||||
Sticker,
|
Sticker,
|
||||||
Image,
|
Image,
|
||||||
Audio,
|
Audio,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface TextContent {
|
||||||
|
text: string;
|
||||||
|
contentType: ContentType.Text;
|
||||||
|
}
|
||||||
|
|
||||||
export interface StickerContent {
|
export interface StickerContent {
|
||||||
hash: string;
|
hash: string;
|
||||||
pack: number;
|
pack: number;
|
||||||
|
@ -37,15 +47,19 @@ export interface AudioContent {
|
||||||
contentType: ContentType.Audio;
|
contentType: ContentType.Audio;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isSticker(content: MediaContent): content is StickerContent {
|
function isText(content: Content): content is TextContent {
|
||||||
|
return content.contentType === ContentType.Text;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSticker(content: Content): content is StickerContent {
|
||||||
return content.contentType === ContentType.Sticker;
|
return content.contentType === ContentType.Sticker;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isImage(content: MediaContent): content is ImageContent {
|
function isImage(content: Content): content is ImageContent {
|
||||||
return content.contentType === ContentType.Image;
|
return content.contentType === ContentType.Image;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAudio(content: MediaContent): content is AudioContent {
|
function isAudio(content: Content): content is AudioContent {
|
||||||
return content.contentType === ContentType.Audio;
|
return content.contentType === ContentType.Audio;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,45 +74,43 @@ export class ChatMessage {
|
||||||
public static createMessage(
|
public static createMessage(
|
||||||
clock: number,
|
clock: number,
|
||||||
timestamp: number,
|
timestamp: number,
|
||||||
text: string,
|
|
||||||
chatId: string,
|
chatId: string,
|
||||||
mediaContent?: MediaContent
|
content: Content
|
||||||
): ChatMessage {
|
): ChatMessage {
|
||||||
let sticker, image, audio;
|
let sticker,
|
||||||
|
image,
|
||||||
|
audio,
|
||||||
|
text = "Upgrade to the latest version to see this media content.";
|
||||||
let contentType = ChatMessage_ContentType.CONTENT_TYPE_TEXT_PLAIN;
|
let contentType = ChatMessage_ContentType.CONTENT_TYPE_TEXT_PLAIN;
|
||||||
|
|
||||||
if (mediaContent) {
|
if (isText(content)) {
|
||||||
if (isSticker(mediaContent)) {
|
if (!content.text) throw "Malformed Text Content";
|
||||||
if (!mediaContent.hash || !mediaContent.pack)
|
text = content.text;
|
||||||
throw "Malformed Sticker Content";
|
contentType = ChatMessage_ContentType.CONTENT_TYPE_TEXT_PLAIN;
|
||||||
|
} else if (isSticker(content)) {
|
||||||
|
if (!content.hash || !content.pack) throw "Malformed Sticker Content";
|
||||||
sticker = {
|
sticker = {
|
||||||
hash: mediaContent.hash,
|
hash: content.hash,
|
||||||
pack: mediaContent.pack,
|
pack: content.pack,
|
||||||
};
|
};
|
||||||
contentType = ChatMessage_ContentType.CONTENT_TYPE_STICKER;
|
contentType = ChatMessage_ContentType.CONTENT_TYPE_STICKER;
|
||||||
} else if (isImage(mediaContent)) {
|
} else if (isImage(content)) {
|
||||||
if (!mediaContent.image || !mediaContent.imageType)
|
if (!content.image || !content.imageType) throw "Malformed Image Content";
|
||||||
throw "Malformed Image Content";
|
|
||||||
image = {
|
image = {
|
||||||
payload: mediaContent.image,
|
payload: content.image,
|
||||||
type: mediaContent.imageType,
|
type: content.imageType,
|
||||||
};
|
};
|
||||||
contentType = ChatMessage_ContentType.CONTENT_TYPE_IMAGE;
|
contentType = ChatMessage_ContentType.CONTENT_TYPE_IMAGE;
|
||||||
} else if (isAudio(mediaContent)) {
|
} else if (isAudio(content)) {
|
||||||
if (
|
if (!content.audio || !content.audioType || !content.durationMs)
|
||||||
!mediaContent.audio ||
|
|
||||||
!mediaContent.audioType ||
|
|
||||||
!mediaContent.durationMs
|
|
||||||
)
|
|
||||||
throw "Malformed Audio Content";
|
throw "Malformed Audio Content";
|
||||||
audio = {
|
audio = {
|
||||||
payload: mediaContent.audio,
|
payload: content.audio,
|
||||||
type: mediaContent.audioType,
|
type: content.audioType,
|
||||||
durationMs: mediaContent.durationMs,
|
durationMs: content.durationMs,
|
||||||
};
|
};
|
||||||
contentType = ChatMessage_ContentType.CONTENT_TYPE_AUDIO;
|
contentType = ChatMessage_ContentType.CONTENT_TYPE_AUDIO;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const proto = {
|
const proto = {
|
||||||
clock, // ms?
|
clock, // ms?
|
||||||
|
|
|
@ -3,6 +3,7 @@ import debug from "debug";
|
||||||
import { utils } from "js-waku";
|
import { utils } from "js-waku";
|
||||||
|
|
||||||
import { ApplicationMetadataMessage } from "./application_metadata_message";
|
import { ApplicationMetadataMessage } from "./application_metadata_message";
|
||||||
|
import { ContentType } from "./chat_message";
|
||||||
import { Identity } from "./identity";
|
import { Identity } from "./identity";
|
||||||
import { Messenger } from "./messenger";
|
import { Messenger } from "./messenger";
|
||||||
|
|
||||||
|
@ -73,7 +74,10 @@ describe("Messenger", () => {
|
||||||
}, testChatId);
|
}, testChatId);
|
||||||
});
|
});
|
||||||
|
|
||||||
await messengerAlice.sendMessage(text, testChatId);
|
await messengerAlice.sendMessage(testChatId, {
|
||||||
|
text,
|
||||||
|
contentType: ContentType.Text,
|
||||||
|
});
|
||||||
|
|
||||||
const receivedMessage = await receivedMessagePromise;
|
const receivedMessage = await receivedMessagePromise;
|
||||||
|
|
||||||
|
@ -95,7 +99,10 @@ describe("Messenger", () => {
|
||||||
}, testChatId);
|
}, testChatId);
|
||||||
});
|
});
|
||||||
|
|
||||||
await messengerAlice.sendMessage(text, testChatId);
|
await messengerAlice.sendMessage(testChatId, {
|
||||||
|
text,
|
||||||
|
contentType: ContentType.Text,
|
||||||
|
});
|
||||||
|
|
||||||
const receivedMessage = await receivedMessagePromise;
|
const receivedMessage = await receivedMessagePromise;
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { CreateOptions as WakuCreateOptions } from "js-waku/build/main/lib/waku"
|
||||||
|
|
||||||
import { ApplicationMetadataMessage } from "./application_metadata_message";
|
import { ApplicationMetadataMessage } from "./application_metadata_message";
|
||||||
import { Chat } from "./chat";
|
import { Chat } from "./chat";
|
||||||
import { ChatMessage, MediaContent } from "./chat_message";
|
import { ChatMessage, Content } from "./chat_message";
|
||||||
import { Identity } from "./identity";
|
import { Identity } from "./identity";
|
||||||
import { ApplicationMetadataMessage_Type } from "./proto/status/v1/application_metadata_message";
|
import { ApplicationMetadataMessage_Type } from "./proto/status/v1/application_metadata_message";
|
||||||
|
|
||||||
|
@ -72,15 +72,11 @@ export class Messenger {
|
||||||
/**
|
/**
|
||||||
* Sends a message on the given chat Id.
|
* Sends a message on the given chat Id.
|
||||||
*/
|
*/
|
||||||
public async sendMessage(
|
public async sendMessage(chatId: string, content: Content): Promise<void> {
|
||||||
text: string,
|
|
||||||
chatId: string,
|
|
||||||
mediaContent?: MediaContent
|
|
||||||
): Promise<void> {
|
|
||||||
const chat = this.chatsById.get(chatId);
|
const chat = this.chatsById.get(chatId);
|
||||||
if (!chat) throw `Failed to send message, chat not joined: ${chatId}`;
|
if (!chat) throw `Failed to send message, chat not joined: ${chatId}`;
|
||||||
|
|
||||||
const chatMessage = chat.createMessage(text, mediaContent);
|
const chatMessage = chat.createMessage(content);
|
||||||
|
|
||||||
const appMetadataMessage = ApplicationMetadataMessage.create(
|
const appMetadataMessage = ApplicationMetadataMessage.create(
|
||||||
chatMessage.encode(),
|
chatMessage.encode(),
|
||||||
|
|
Loading…
Reference in New Issue