Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private readonly sendMessage = (
context: any,
conversationId: ConversationId,
userId: UserId,
clientId: DeviceId,
destinationUserId: UserId,
destinationClientId: DeviceId,
payload: string,
): number => {
const protoCalling = new Calling({content: payload});
const genericMessage = new GenericMessage({
[GENERIC_MESSAGE_TYPE.CALLING]: protoCalling,
messageId: createRandomUuid(),
});
const call = this.findCall(conversationId);
if (call?.blockMessages) {
return 0;
}
const options = this.targetMessageRecipients(payload, destinationUserId, destinationClientId);
const eventInfoEntity = new EventInfoEntity(genericMessage, conversationId, options);
this.conversationRepository.sendCallingMessage(eventInfoEntity, conversationId).catch(() => {
if (call) {
// we flag the call in order to prevent sending further messages
call.blockMessages = true;
}
this.leaveCall(conversationId);
if (!data.title || !data.url) {
return;
}
const {description = '', site_name, title = '', url: dataUrl} = data;
const truncatedDescription = truncate(deArrayify(description), config.MAXIMUM_LINK_PREVIEW_CHARS);
const truncatedTitle = truncate(deArrayify(title), config.MAXIMUM_LINK_PREVIEW_CHARS);
const protoArticle = new Article({
permanentUrl: deArrayify(dataUrl),
summary: truncatedDescription,
title: truncatedTitle,
}); // deprecated format
const protoLinkPreview = new LinkPreview({
article: protoArticle,
permanentUrl: deArrayify(dataUrl),
summary: truncatedDescription,
title: truncatedTitle,
url: link,
urlOffset: offset,
});
if (deArrayify(site_name) === 'Twitter' && isTweetUrl(deArrayify(dataUrl))) {
const author = deArrayify(title)
.replace('on Twitter', '')
.trim();
const username = deArrayify(dataUrl).match(/com\/([^/]*)\//)[1];
const protoTweet = new Tweet({author, username});
protoLinkPreview[PROTO_MESSAGE_TYPE.TWEET] = protoTweet;
assetEntity.previews().map(linkPreview => {
linkPreview.obfuscate();
const protoArticle = new Article({permanentUrl: linkPreview.url, title: linkPreview.title}); // deprecated format
const linkPreviewProto = new LinkPreview({
article: protoArticle,
permanentUrl: linkPreview.url,
title: linkPreview.title,
url: linkPreview.url,
urlOffset: 0,
});
return arrayToBase64(LinkPreview.encode(linkPreviewProto).finish());
}),
);
if (isEmpty(data)) {
return;
}
data.url = data.url || link;
if (!data.title || !data.url) {
return;
}
const {description = '', site_name, title = '', url: dataUrl} = data;
const truncatedDescription = truncate(deArrayify(description), config.MAXIMUM_LINK_PREVIEW_CHARS);
const truncatedTitle = truncate(deArrayify(title), config.MAXIMUM_LINK_PREVIEW_CHARS);
const protoArticle = new Article({
permanentUrl: deArrayify(dataUrl),
summary: truncatedDescription,
title: truncatedTitle,
}); // deprecated format
const protoLinkPreview = new LinkPreview({
article: protoArticle,
permanentUrl: deArrayify(dataUrl),
summary: truncatedDescription,
title: truncatedTitle,
url: link,
urlOffset: offset,
});
if (deArrayify(site_name) === 'Twitter' && isTweetUrl(deArrayify(dataUrl))) {
const author = deArrayify(title)
assetEntity.previews().map(linkPreview => {
linkPreview.obfuscate();
const protoArticle = new Article({permanentUrl: linkPreview.url, title: linkPreview.title}); // deprecated format
const linkPreviewProto = new LinkPreview({
article: protoArticle,
permanentUrl: linkPreview.url,
title: linkPreview.title,
url: linkPreview.url,
urlOffset: 0,
});
return arrayToBase64(LinkPreview.encode(linkPreviewProto).finish());
}),
);
private readonly sendMessage = (
context: any,
conversationId: ConversationId,
userId: UserId,
clientId: DeviceId,
destinationUserId: UserId,
destinationClientId: DeviceId,
payload: string,
): number => {
const protoCalling = new Calling({content: payload});
const genericMessage = new GenericMessage({
[GENERIC_MESSAGE_TYPE.CALLING]: protoCalling,
messageId: createRandomUuid(),
});
const call = this.findCall(conversationId);
if (call?.blockMessages) {
return 0;
}
const options = this.targetMessageRecipients(payload, destinationUserId, destinationClientId);
const eventInfoEntity = new EventInfoEntity(genericMessage, conversationId, options);
this.conversationRepository.sendCallingMessage(eventInfoEntity, conversationId).catch(() => {
if (call) {
// we flag the call in order to prevent sending further messages
call.blockMessages = true;
}
assetEntity.previews().map(linkPreview => {
linkPreview.obfuscate();
const protoArticle = new Article({permanentUrl: linkPreview.url, title: linkPreview.title}); // deprecated format
const linkPreviewProto = new LinkPreview({
article: protoArticle,
permanentUrl: linkPreview.url,
title: linkPreview.title,
url: linkPreview.url,
urlOffset: 0,
});
return arrayToBase64(LinkPreview.encode(linkPreviewProto).finish());
}),
);
async function createEncodedCiphertext(
preKey,
text = 'Hello, World!',
receivingIdentity = TestFactory.cryptography_repository.cryptobox.identity,
) {
const bobEngine = new MemoryEngine();
await bobEngine.init('bob');
const sender = new Cryptobox(bobEngine, 1);
await sender.create();
const genericMessage = new GenericMessage({
[GENERIC_MESSAGE_TYPE.TEXT]: new Text({content: text}),
messageId: createRandomUuid(),
});
const sessionId = `from-${sender.identity.public_key.fingerprint()}-to-${preKey.key_pair.public_key.fingerprint()}`;
const preKeyBundle = Proteus.keys.PreKeyBundle.new(receivingIdentity.public_key, preKey);
const cipherText = await sender.encrypt(
sessionId,
GenericMessage.encode(genericMessage).finish(),
preKeyBundle.serialise(),
);
return arrayToBase64(cipherText);
}
it('can map a ping wrapped inside an external message', async () => {
let external_message = undefined;
const genericMessage = new GenericMessage({
knock: new Knock({hotKnock: false}),
messageId: createRandomUuid(),
});
const encryptedAsset = await encryptAesAsset(GenericMessage.encode(genericMessage).finish());
const keyBytes = new Uint8Array(encryptedAsset.keyBytes);
const sha256 = new Uint8Array(encryptedAsset.sha256);
event.data.data = await arrayToBase64(encryptedAsset.cipherText);
external_message = new GenericMessage({
external: new External({otrKey: keyBytes, sha256}),
messageId: createRandomUuid(),
});
const event_json = await mapper.mapGenericMessage(external_message, event);
expect(isObject(event_json)).toBeTruthy();
expect(event_json.type).toBe(ClientEvent.CONVERSATION.KNOCK);
expect(event_json.conversation).toBe(event.conversation);
expect(event_json.from).toBe(event.from);
expect(event_json.time).toBe(event.time);
expect(event_json.id).toBe(genericMessage.messageId);
});
const preKeys = await TestFactory.cryptography_repository.createCryptobox(database);
const alice = TestFactory.cryptography_repository.cryptobox.identity;
expect(alice).toBeDefined();
const aliceBundle = Proteus.keys.PreKeyBundle.new(alice.public_key, preKeys[0]);
const bobEngine = new MemoryEngine();
await bobEngine.init('bob');
const bob = new Cryptobox(bobEngine, 1);
await bob.create();
const plainText = 'Hello, Alice!';
const genericMessage = new GenericMessage({
[GENERIC_MESSAGE_TYPE.TEXT]: new Text({content: plainText}),
messageId: createRandomUuid(),
});
const cipherText = await bob.encrypt(
'session-with-alice',
GenericMessage.encode(genericMessage).finish(),
aliceBundle.serialise(),
);
const encodedCipherText = await arrayToBase64(cipherText);
const mockedEvent = {
data: {
text: encodedCipherText,
},
from: createRandomUuid(),