Skip to content

Commit 6076931

Browse files
committed
add Messages to conversation
1 parent 14438b9 commit 6076931

File tree

3 files changed

+197
-165
lines changed

3 files changed

+197
-165
lines changed

packages/js-sdk/src/Dm3Sdk.test.ts

Lines changed: 171 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { StorageAPI } from '@dm3-org/dm3-lib-storage';
22
import {
3+
getMockDeliveryServiceProfile,
4+
MockDeliveryServiceProfile,
35
MockedUserProfile,
6+
MockMessageFactory,
47
mockUserProfile,
58
} from '@dm3-org/dm3-lib-test-helper';
69
import axios from 'axios';
@@ -19,6 +22,8 @@ describe('Dm3Sdk', () => {
1922
//Axios mock to mock the http requests
2023
let axiosMock;
2124

25+
let deliveryService: MockDeliveryServiceProfile;
26+
2227
beforeEach(async () => {
2328
alice = await mockUserProfile(
2429
ethers.Wallet.createRandom(),
@@ -33,6 +38,11 @@ describe('Dm3Sdk', () => {
3338
'test.io',
3439
]);
3540

41+
deliveryService = await getMockDeliveryServiceProfile(
42+
ethers.Wallet.createRandom(),
43+
'http://localhost:3000',
44+
);
45+
3646
axiosMock = new MockAdapter(axios);
3747

3848
//Mock BackendConnector HttpRequests
@@ -63,126 +73,170 @@ describe('Dm3Sdk', () => {
6373
.reply(200);
6474
});
6575

66-
it('can add a conversation to the contact list', async () => {
67-
const mockTldResolver = {
68-
resolveTLDtoAlias: async () =>
69-
`${normalizeEnsName(bob.address)}.addr.test`,
70-
resolveAliasToTLD: async () => 'bob.eth',
71-
} as unknown as ITLDResolver;
72-
73-
const mockConfig: Dm3SdkConfig = {
74-
mainnetProvider: {} as ethers.providers.JsonRpcProvider,
75-
storageApi: {
76-
addConversation: async () => {},
77-
} as unknown as StorageAPI,
78-
nonce: '1',
79-
defaultDeliveryService: 'test.io',
80-
addressEnsSubdomain: '.addr.test',
81-
userEnsSubdomain: '.user.test',
82-
resolverBackendUrl: 'resolver.io',
83-
backendUrl: 'http://localhost:4060',
84-
_tld: mockTldResolver,
85-
};
86-
87-
const dm3 = await new Dm3Sdk(mockConfig).login({
88-
profileKeys: alice.profileKeys,
89-
profile: alice.signedUserProfile,
90-
accountAddress: alice.address,
76+
describe('conversations', () => {
77+
it('can add a conversation to the contact list', async () => {
78+
const mockTldResolver = {
79+
resolveTLDtoAlias: async () =>
80+
`${normalizeEnsName(bob.address)}.addr.test`,
81+
resolveAliasToTLD: async () => 'bob.eth',
82+
} as unknown as ITLDResolver;
83+
84+
const mockConfig: Dm3SdkConfig = {
85+
mainnetProvider: {} as ethers.providers.JsonRpcProvider,
86+
storageApi: {
87+
addConversation: async () => {},
88+
} as unknown as StorageAPI,
89+
nonce: '1',
90+
defaultDeliveryService: 'test.io',
91+
addressEnsSubdomain: '.addr.test',
92+
userEnsSubdomain: '.user.test',
93+
resolverBackendUrl: 'resolver.io',
94+
backendUrl: 'http://localhost:4060',
95+
_tld: mockTldResolver,
96+
};
97+
98+
const dm3 = await new Dm3Sdk(mockConfig).login({
99+
profileKeys: alice.profileKeys,
100+
profile: alice.signedUserProfile,
101+
accountAddress: alice.address,
102+
});
103+
104+
await dm3.conversations.addConversation('bob.eth');
105+
const c = dm3.conversations.list;
106+
expect(c.length).toBe(1);
107+
expect(c[0].contact.name).toBe('bob.eth');
91108
});
92-
93-
await dm3.conversations.addConversation('bob.eth');
94-
const c = dm3.conversations.list;
95-
expect(c.length).toBe(1);
96-
expect(c[0].contact.name).toBe('bob.eth');
97-
});
98-
it('can multiple conversations to the contact list', async () => {
99-
const mockTldResolver = {
100-
resolveTLDtoAlias: async (ensName: string) => {
101-
if (ensName === 'alice.eth') {
102-
return `${normalizeEnsName(alice.address)}.addr.test`;
103-
}
104-
if (ensName === 'bob.eth') {
105-
return `${normalizeEnsName(bob.address)}.addr.test`;
106-
}
107-
return `${normalizeEnsName(karl.address)}.addr.test`;
108-
},
109-
resolveAliasToTLD: async (ensName: string) => {
110-
if (
111-
normalizeEnsName(ensName) ===
112-
normalizeEnsName(alice.address) + '.addr.test'
113-
) {
114-
return 'alice.eth';
115-
}
116-
if (
117-
normalizeEnsName(ensName) ===
118-
normalizeEnsName(bob.address) + '.addr.test'
119-
) {
120-
return 'bob.eth';
121-
}
122-
return 'karl.eth';
123-
},
124-
} as unknown as ITLDResolver;
125-
126-
const mockConfig: Dm3SdkConfig = {
127-
mainnetProvider: {} as ethers.providers.JsonRpcProvider,
128-
storageApi: {
129-
addConversation: async () => {},
130-
} as unknown as StorageAPI,
131-
nonce: '1',
132-
defaultDeliveryService: 'test.io',
133-
addressEnsSubdomain: '.addr.test',
134-
userEnsSubdomain: '.user.test',
135-
resolverBackendUrl: 'resolver.io',
136-
backendUrl: 'http://localhost:4060',
137-
_tld: mockTldResolver,
138-
};
139-
140-
const dm3 = await new Dm3Sdk(mockConfig).login({
141-
profileKeys: alice.profileKeys,
142-
profile: alice.signedUserProfile,
143-
accountAddress: alice.address,
109+
it('can multiple conversations to the contact list', async () => {
110+
const mockTldResolver = {
111+
resolveTLDtoAlias: async (ensName: string) => {
112+
if (ensName === 'alice.eth') {
113+
return `${normalizeEnsName(alice.address)}.addr.test`;
114+
}
115+
if (ensName === 'bob.eth') {
116+
return `${normalizeEnsName(bob.address)}.addr.test`;
117+
}
118+
return `${normalizeEnsName(karl.address)}.addr.test`;
119+
},
120+
resolveAliasToTLD: async (ensName: string) => {
121+
if (
122+
normalizeEnsName(ensName) ===
123+
normalizeEnsName(alice.address) + '.addr.test'
124+
) {
125+
return 'alice.eth';
126+
}
127+
if (
128+
normalizeEnsName(ensName) ===
129+
normalizeEnsName(bob.address) + '.addr.test'
130+
) {
131+
return 'bob.eth';
132+
}
133+
return 'karl.eth';
134+
},
135+
} as unknown as ITLDResolver;
136+
137+
const mockConfig: Dm3SdkConfig = {
138+
mainnetProvider: {} as ethers.providers.JsonRpcProvider,
139+
storageApi: {
140+
addConversation: async () => {},
141+
} as unknown as StorageAPI,
142+
nonce: '1',
143+
defaultDeliveryService: 'test.io',
144+
addressEnsSubdomain: '.addr.test',
145+
userEnsSubdomain: '.user.test',
146+
resolverBackendUrl: 'resolver.io',
147+
backendUrl: 'http://localhost:4060',
148+
_tld: mockTldResolver,
149+
};
150+
151+
const dm3 = await new Dm3Sdk(mockConfig).login({
152+
profileKeys: alice.profileKeys,
153+
profile: alice.signedUserProfile,
154+
accountAddress: alice.address,
155+
});
156+
157+
await dm3.conversations.addConversation('bob.eth');
158+
await dm3.conversations.addConversation('karl.eth');
159+
const c = dm3.conversations.list;
160+
dm3;
161+
console.log(c);
162+
expect(c.length).toBe(2);
163+
expect(c[0].contact.name).toBe('bob.eth');
164+
expect(c[1].contact.name).toBe('karl.eth');
144165
});
145-
146-
await dm3.conversations.addConversation('bob.eth');
147-
await dm3.conversations.addConversation('karl.eth');
148-
const c = dm3.conversations.list;
149-
150-
console.log(c);
151-
expect(c.length).toBe(2);
152-
expect(c[0].contact.name).toBe('bob.eth');
153-
expect(c[1].contact.name).toBe('karl.eth');
154-
});
155-
it('dont add duplicate conversations', async () => {
156-
const mockTldResolver = {
157-
resolveTLDtoAlias: async () =>
158-
`${normalizeEnsName(bob.address)}.addr.test`,
159-
resolveAliasToTLD: async () => 'bob.eth',
160-
} as unknown as ITLDResolver;
161-
162-
const mockConfig: Dm3SdkConfig = {
163-
mainnetProvider: {} as ethers.providers.JsonRpcProvider,
164-
storageApi: {
165-
addConversation: async () => {},
166-
} as unknown as StorageAPI,
167-
nonce: '1',
168-
defaultDeliveryService: 'test.io',
169-
addressEnsSubdomain: '.addr.test',
170-
userEnsSubdomain: '.user.test',
171-
resolverBackendUrl: 'resolver.io',
172-
backendUrl: 'http://localhost:4060',
173-
_tld: mockTldResolver,
174-
};
175-
176-
const dm3 = await new Dm3Sdk(mockConfig).login({
177-
profileKeys: alice.profileKeys,
178-
profile: alice.signedUserProfile,
179-
accountAddress: alice.address,
166+
it('dont add duplicate conversations', async () => {
167+
const mockTldResolver = {
168+
resolveTLDtoAlias: async () =>
169+
`${normalizeEnsName(bob.address)}.addr.test`,
170+
resolveAliasToTLD: async () => 'bob.eth',
171+
} as unknown as ITLDResolver;
172+
173+
const mockConfig: Dm3SdkConfig = {
174+
mainnetProvider: {} as ethers.providers.JsonRpcProvider,
175+
storageApi: {
176+
addConversation: async () => {},
177+
} as unknown as StorageAPI,
178+
nonce: '1',
179+
defaultDeliveryService: 'test.io',
180+
addressEnsSubdomain: '.addr.test',
181+
userEnsSubdomain: '.user.test',
182+
resolverBackendUrl: 'resolver.io',
183+
backendUrl: 'http://localhost:4060',
184+
_tld: mockTldResolver,
185+
};
186+
187+
const dm3 = await new Dm3Sdk(mockConfig).login({
188+
profileKeys: alice.profileKeys,
189+
profile: alice.signedUserProfile,
190+
accountAddress: alice.address,
191+
});
192+
193+
await dm3.conversations.addConversation('bob.eth');
194+
await dm3.conversations.addConversation('bob.eth');
195+
const c = dm3.conversations.list;
196+
expect(c.length).toBe(1);
197+
expect(c[0].contact.name).toBe('bob.eth');
180198
});
199+
});
181200

182-
await dm3.conversations.addConversation('bob.eth');
183-
await dm3.conversations.addConversation('bob.eth');
184-
const c = dm3.conversations.list;
185-
expect(c.length).toBe(1);
186-
expect(c[0].contact.name).toBe('bob.eth');
201+
describe('messages', () => {
202+
it('can send a message', async () => {
203+
const mockTldResolver = {
204+
resolveTLDtoAlias: async () =>
205+
`${normalizeEnsName(bob.address)}.addr.test`,
206+
resolveAliasToTLD: async () => 'bob.eth',
207+
} as unknown as ITLDResolver;
208+
209+
const mockConfig: Dm3SdkConfig = {
210+
mainnetProvider: {} as ethers.providers.JsonRpcProvider,
211+
storageApi: {
212+
addConversation: async () => {},
213+
addMessage: async () => {},
214+
} as unknown as StorageAPI,
215+
nonce: '1',
216+
defaultDeliveryService: 'test.io',
217+
addressEnsSubdomain: '.addr.test',
218+
userEnsSubdomain: '.user.test',
219+
resolverBackendUrl: 'resolver.io',
220+
backendUrl: 'http://localhost:4060',
221+
_tld: mockTldResolver,
222+
};
223+
224+
const dm3 = await new Dm3Sdk(mockConfig).login({
225+
profileKeys: alice.profileKeys,
226+
profile: alice.signedUserProfile,
227+
accountAddress: alice.address,
228+
});
229+
230+
const msgFactory = MockMessageFactory(alice, bob, deliveryService);
231+
232+
const msg1 = await msgFactory.createMessage('Hi');
233+
234+
const c = await dm3.conversations.addConversation('bob.eth');
235+
expect(c?.messages.list().length).toBe(0);
236+
await c?.messages.addMessage('bob.eth', msg1);
237+
238+
expect(c?.messages.list().length).toBe(1);
239+
expect(c?.messages.list()[0].envelop.message.message).toBe('Hi');
240+
});
187241
});
188242
});

packages/js-sdk/src/conversation/Conversations.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ethers } from 'ethers';
88
import { ITLDResolver } from '../tld/nameService/ITLDResolver';
99
import { hydrateContract as hydrateContact } from './hydrate/hydrateContact';
1010
import { Contact, Conversation, getEmptyContact } from './types';
11+
import { Messages } from '../message/Messages';
1112

1213
export class Conversations {
1314
private readonly provider: ethers.providers.JsonRpcProvider;
@@ -88,7 +89,7 @@ export class Conversations {
8889

8990
const newConversation: Conversation = {
9091
//TODO change that once Message class has been implemented
91-
messages: undefined as any,
92+
messages: new Messages(this.storageApi, this),
9293
contact: newContact,
9394
};
9495
//Set the new contact to the list
@@ -102,7 +103,7 @@ export class Conversations {
102103
);
103104

104105
const hydratedConversation: Conversation = {
105-
messages: undefined as any,
106+
messages: new Messages(this.storageApi, this),
106107
contact: hydratedContact,
107108
};
108109
//find existing contact and replace it with the hydrated one
@@ -131,7 +132,7 @@ export class Conversations {
131132
this.addressEnsSubdomain,
132133
);
133134
const hydratedConversation: Conversation = {
134-
messages: undefined as any,
135+
messages: new Messages(this.storageApi, this),
135136
contact: hydratedContact,
136137
};
137138
this.list.push(hydratedConversation);

0 commit comments

Comments
 (0)