Skip to content

Commit

Permalink
Merge branch 'develop' into removeWinston
Browse files Browse the repository at this point in the history
  • Loading branch information
malteish committed Sep 2, 2024
2 parents 469415f + 7a81827 commit 9e2191d
Show file tree
Hide file tree
Showing 40 changed files with 799 additions and 631 deletions.
6 changes: 3 additions & 3 deletions packages/delivery-service/src/delivery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ describe('Delivery', () => {

const { status } = await request(app)
.post(
'/messages/0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870/syncAcknowledgments',
'/messages/0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870/syncAcknowledgements',
)
.set({
authorization: `Bearer ${token}`,
})

.send({
acknowledgments: [
acknowledgements: [
{
contactAddress:
'0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870',
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('Delivery', () => {

const { status } = await request(app)
.post(
'/messages/0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870/syncAcknowledgments',
'/messages/0x99C19AB10b9EC8aC6fcda9586E81f6B73a298870/syncAcknowledgements',
)
.set({
authorization: `Bearer ${token}`,
Expand Down
37 changes: 27 additions & 10 deletions packages/delivery-service/src/delivery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Acknowledgment,
Acknowledgement,
getConversationId,
schema,
} from '@dm3-org/dm3-lib-delivery';
Expand All @@ -22,12 +22,12 @@ const syncAcknowledgementParamsSchema = {
const syncAcknowledgementBodySchema = {
type: 'object',
properties: {
acknowledgments: {
acknowledgements: {
type: 'array',
items: schema.Acknowledgment,
items: schema.Acknowledgement,
},
},
required: ['acknowledgments'],
required: ['acknowledgements'],
additionalProperties: false,
};

Expand All @@ -51,11 +51,12 @@ export default (
);
});
//Returns all incoming messages for a specific contact name
//TODO deprecated. remove in the future
router.get(
'/messages/:ensName/contact/:contactEnsName',
async (req: express.Request, res, next) => {
try {
//retive the address for the contact name since it is used as a key in the db
//retrieve the address for the contact name since it is used as a key in the db
const receiverAddress = await web3Provider.resolveName(
req.params.ensName,
);
Expand Down Expand Up @@ -111,10 +112,26 @@ export default (
//@ts-ignore
async (req: express.Request, res, next) => {
try {
console.debug('get incoming messages for ', req.params.ensName);
//retrieve the address for the contact name since it is used as a key in the db
const receiverAddress = await web3Provider.resolveName(
req.params.ensName,
);
//If the address is not found we return a 404. This should normally not happen since the receiver always is known to the delivery service
if (!receiverAddress) {
console.error(
'receiver address not found for name ',
req.params.ensName,
);
return res.status(404).send({
error:
'receiver address not found for name ' +
req.params.ensName,
});
}
console.debug('get incoming messages for ', receiverAddress);
//TODO use address
const incomingMessages = await db.getIncomingMessages(
req.params.ensName,
receiverAddress,
//Fetch the last 10 messages per conversation
//If we decide to add pagination for that endpoint we can pass this value as a param
1000,
Expand All @@ -126,7 +143,7 @@ export default (
},
);
router.post(
'/messages/:ensName/syncAcknowledgments/',
'/messages/:ensName/syncAcknowledgements/',
async (req, res, next) => {
const hasValidParams = validateSchema(
syncAcknowledgementParamsSchema,
Expand Down Expand Up @@ -161,8 +178,8 @@ export default (
}

await Promise.all(
req.body.acknowledgments.map(
async (ack: Acknowledgment) => {
req.body.acknowledgements.map(
async (ack: Acknowledgement) => {
const contactEnsName = await db.getIdEnsName(
ack.contactAddress,
);
Expand Down
13 changes: 10 additions & 3 deletions packages/delivery-service/src/message/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,11 @@ describe('MessageProcessor', () => {
await messageProcessor.processEnvelop(incomingEnvelop);

//createMessageCall
const [_, actualEnvelop] = createMessageMock.mock.calls[0];
const [receiverAddress, _, actualEnvelop] =
createMessageMock.mock.calls[0];

expect(createMessageMock).toBeCalled();
expect(receiverAddress).toBe(receiver.address);
expect(actualEnvelop['message']).toBe(incomingEnvelop.message);

const actualPostmark = await decryptAsymmetric(
Expand Down Expand Up @@ -666,10 +668,13 @@ describe('MessageProcessor', () => {
await messageProcessor.processEnvelop(incomingEnvelop2);

//createMessageCall
const [_, actualEnvelop] = createMessageMock.mock.calls[0];
const [receiverAddress, _, actualEnvelop] =
createMessageMock.mock.calls[0];

expect(createMessageMock).toBeCalled();
expect(createMessageMock).toBeCalledTimes(2);
expect(receiverAddress).toBe(receiver.address);

expect(actualEnvelop['message']).toBe(incomingEnvelop1.message);

const actualPostmark = await decryptAsymmetric(
Expand Down Expand Up @@ -752,9 +757,11 @@ describe('MessageProcessor', () => {
await messageProcessor.processEnvelop(incomingEnvelop);

//createMessageCall
const [_, actualEnvelop] = createMessageMock.mock.calls[0];
const [receiverAddress, _, actualEnvelop] =
createMessageMock.mock.calls[0];

expect(createMessageMock).toBeCalled();
expect(receiverAddress).toBe(receiver.address);
expect(actualEnvelop['message']).toBe(incomingEnvelop.message);

const actualPostmark = await decryptAsymmetric(
Expand Down
6 changes: 5 additions & 1 deletion packages/delivery-service/src/message/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ export class MessageProcessor {
),
};
console.debug('storeNewMessage', conversationId);
await this.db.createMessage(conversationId, envelopWithPostmark);
await this.db.createMessage(
receiverAddress,
conversationId,
envelopWithPostmark,
);

//If there is currently a webSocket connection open to the receiver, the message will be directly send.
if (await this.webSocketManager.isConnected(receiverAddress)) {
Expand Down
1 change: 1 addition & 0 deletions packages/delivery-service/src/persistence/getDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface IDatabase extends IAccountDatabase {
limit: number,
) => Promise<EncryptionEnvelop[]>;
createMessage: (
receiverAddress: string,
conversationId: string,
envelop: EncryptionEnvelop,
) => Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Create Message', () => {

expect(priorCreateMessages.length).toBe(0);

await db.createMessage(conversionId, envelop);
await db.createMessage(RECEIVER_ADDRESS, conversionId, envelop);

const afterCreateMessages = await db.getMessages(conversionId, 0, 50);

Expand All @@ -65,8 +65,12 @@ describe('Create Message', () => {
const firstMessageConversionId = SENDER_ADDRESS + RECEIVER_ADDRESS;
const secondMessageConversionId = RECEIVER_ADDRESS + RECEIVER_ADDRESS;

await db.createMessage(firstMessageConversionId, envelop);
await db.createMessage(secondMessageConversionId, {
await db.createMessage(
RECEIVER_ADDRESS,
firstMessageConversionId,
envelop,
);
await db.createMessage(RECEIVER_ADDRESS, secondMessageConversionId, {
...envelop,
message: 'foo',
metadata: {
Expand All @@ -90,7 +94,7 @@ describe('Create Message', () => {
it('Rejcts message with an invalid schema', async () => {
const invalidMessage = {} as EncryptionEnvelop;
try {
await db.createMessage('foo', invalidMessage);
await db.createMessage(RECEIVER_ADDRESS, 'foo', invalidMessage);
fail();
} catch (e) {
expect(e).toStrictEqual(Error('Invalid message'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { EncryptionEnvelop, schema } from '@dm3-org/dm3-lib-messaging';
import { stringify, validateSchema } from '@dm3-org/dm3-lib-shared';
import { Redis, RedisPrefix } from '../getDatabase';
import {
schema,
DeliveryInformation,
EncryptionEnvelop,
} from '@dm3-org/dm3-lib-messaging';
import { validateSchema, stringify } from '@dm3-org/dm3-lib-shared';
export function createMessage(redis: Redis) {
return async (
receiverAddress: string,
conversationId: string,
envelop: EncryptionEnvelop,
createdAt: number = new Date().getTime(),
Expand All @@ -25,31 +22,12 @@ export function createMessage(redis: Redis) {
value: stringify(envelop),
});

/**
* add a redis set key = envelop.metadata.deliveryInformation.to and value = conversationId
*/
/**
* We can assume that the deliveryInformation is always encrypted because the
* DS must've encrypted it before persisting the message to the database.
*
*
* In the future we have to refactor the DeliveryInformation Type
* to we can ensure that on compile time. https://github.com/corpus-io/dm3/issues/479
*/
const encryptedDeliverInformation = envelop.metadata
.deliveryInformation as DeliveryInformation;
console.debug('store incoming conversation for', receiverAddress);

console.debug(
'store incoming conversation for',
encryptedDeliverInformation.to,
);

await redis.zAdd(
RedisPrefix.IncomingConversations + encryptedDeliverInformation.to,
{
score: createdAt,
value: conversationId,
},
);
//We've to keep track of every incoming conversations for the address
await redis.zAdd(RedisPrefix.IncomingConversations + receiverAddress, {
score: createdAt,
value: conversationId,
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('Delete Expired messages', () => {

it('Delete all messages createdAt before a given time', async () => {
await db.createMessage(
'',
'foox',
{
message: 'hello',
Expand All @@ -31,6 +32,7 @@ describe('Delete Expired messages', () => {
1,
);
await db.createMessage(
'',
'foox',
{
message: 'world',
Expand All @@ -44,7 +46,9 @@ describe('Delete Expired messages', () => {
},
3,
);

await db.createMessage(
'',
'foox',
{
message: 'dm3',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('Sync Acknowledge', () => {

expect(priorCreateMessages.length).toBe(0);

await db.createMessage(conversionId, envelop1);
await db.createMessage(conversionId, envelop2);
await db.createMessage(RECEIVER_ADDRESS, conversionId, envelop1);
await db.createMessage(RECEIVER_ADDRESS, conversionId, envelop2);

const afterCreateMessages = await db.getIncomingMessages(
RECEIVER_ADDRESS,
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Sync Acknowledge', () => {

expect(priorCreateMessages.length).toBe(0);

await db.createMessage(conversionId, envelop1);
await db.createMessage(RECEIVER_ADDRESS, conversionId, envelop1);

const afterCreateMessages = await db.getIncomingMessages(
RECEIVER_ADDRESS,
Expand Down
1 change: 0 additions & 1 deletion packages/lib/delivery-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './auth-http';
export * from './profile-http';
export * from './messaging-http';
export * from './messaging-ws';
export * from './ds-properties';
export * from './notification-http';
Loading

0 comments on commit 9e2191d

Please sign in to comment.