Skip to content

Commit 8e96907

Browse files
committed
sdk: 2.0.0-beta.5 - dedup dapp message sending in compat mode
1 parent ced9019 commit 8e96907

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dialectlabs/sdk",
3-
"version": "2.0.0-beta.4",
3+
"version": "2.0.0-beta.5",
44
"type": "module",
55
"repository": "[email protected]:dialectlabs/sdk.git",
66
"author": "dialectlabs",

packages/sdk/src/internal/dapp/data-service-dapp-messages.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
type SendDappMessageCommand,
99
type UnicastDappMessageCommand,
1010
} from '../../dapp/dapp.interface';
11-
import { toAddressTypeDto } from '../../address/addresses.interface';
11+
import {
12+
AddressType,
13+
toAddressTypeDto,
14+
} from '../../address/addresses.interface';
1215
import {
1316
DappMessageActionTypeDto,
1417
DappMessageLinksActionDto,
@@ -18,19 +21,42 @@ import {
1821
import { withErrorParsing } from '../../dialect-cloud-api/data-service-errors';
1922

2023
export class DataServiceDappMessages implements DappMessages {
21-
constructor(private readonly api: DataServiceDappsApi) {}
24+
constructor(
25+
private readonly api: DataServiceDappsApi,
26+
private readonly addressTypePredicate: (at: AddressType) => boolean = () =>
27+
true,
28+
) {}
2229

2330
async send(command: SendDappMessageCommand): Promise<void> {
2431
if (command.addressTypes?.length === 0) {
2532
return;
2633
}
34+
const addressTypesNormalized = command.addressTypes ?? [
35+
AddressType.Email,
36+
AddressType.Telegram,
37+
AddressType.Wallet,
38+
];
39+
40+
const addressTypesFiltered = addressTypesNormalized.filter(
41+
this.addressTypePredicate,
42+
);
43+
2744
if ('recipient' in command) {
28-
return this.unicast(command);
45+
return this.unicast({
46+
...command,
47+
addressTypes: addressTypesFiltered,
48+
});
2949
}
3050
if ('recipients' in command) {
31-
return this.multicast(command);
51+
return this.multicast({
52+
...command,
53+
addressTypes: addressTypesFiltered,
54+
});
3255
}
33-
return this.broadcast(command);
56+
return this.broadcast({
57+
...command,
58+
addressTypes: addressTypesFiltered,
59+
});
3460
}
3561

3662
private async unicast(command: UnicastDappMessageCommand) {

packages/sdk/src/internal/sdk/sdk-factory.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import type { DataServiceApi } from '../../dialect-cloud-api/data-service-api';
4646
import { DataServiceWalletsApiClientV1 } from '../../dialect-cloud-api/data-service-wallets-api.v1';
4747
import { AlertsV2DappMessages } from '../dapp/alerts-v2-dapp-messages';
4848
import { AppV2Api } from '../../dialect-cloud-api/v2/application-api';
49+
import { AddressType } from '../../address/addresses.interface';
4950

5051
export class InternalDialectSdk<ChainSdk extends BlockchainSdk>
5152
implements DialectSdk<ChainSdk>
@@ -234,21 +235,25 @@ export class DialectSdkFactory<ChainSdk extends BlockchainSdk> {
234235
): DappMessages {
235236
const dappMessages: DappMessages[] = [];
236237

237-
const dataServiceDappMessages = new DataServiceDappMessages(
238-
dataServiceApi.dapps,
239-
);
240-
const alertsV2DappMessages = new AlertsV2DappMessages(dappId, appV2Api);
241-
242238
switch (config.dialectCloud.apiVersion) {
243239
case 1:
244-
dappMessages.push(dataServiceDappMessages);
240+
dappMessages.push(new DataServiceDappMessages(dataServiceApi.dapps));
245241
break;
246242
case 2:
247-
dappMessages.push(alertsV2DappMessages);
243+
dappMessages.push(new AlertsV2DappMessages(dappId, appV2Api));
248244
break;
249245
case '2_compat':
250-
dappMessages.push(dataServiceDappMessages);
251-
dappMessages.push(alertsV2DappMessages);
246+
dappMessages.push(
247+
new DataServiceDappMessages(
248+
dataServiceApi.dapps,
249+
// The V2 API ensures compatibility with Telegram and the email server side,
250+
// so we avoid dispatching duplicate alerts. Therefore, we use only the V2 API
251+
// for the WALLET channel in compatibility mode. As a result, alerts to the
252+
// TELEGRAM and EMAIL channels will be sent to user only once.
253+
(at) => at === AddressType.Wallet,
254+
),
255+
);
256+
dappMessages.push(new AlertsV2DappMessages(dappId, appV2Api));
252257
break;
253258
default:
254259
throw new IllegalArgumentError(

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,19 @@
493493
dependencies:
494494
"@jridgewell/trace-mapping" "0.3.9"
495495

496+
"@dialectlabs/[email protected]":
497+
version "2.0.0-beta.4"
498+
resolved "https://registry.yarnpkg.com/@dialectlabs/sdk/-/sdk-2.0.0-beta.4.tgz#bc839428cf8d99250df05f5d6608c7c139c67e06"
499+
integrity sha512-CGZe46uSQq1AtB7n71tabHVABMM2D5FAGOc3GV3O4KIePTU1+3nhCCCNMpT8zpz7IOXzRQCZdGiJ738xKNuFSg==
500+
dependencies:
501+
"@stablelib/base64" "^1.0.1"
502+
axios "^0.27.2"
503+
bs58 "^5.0.0"
504+
ed2curve "^0.3.0"
505+
js-sha3 "^0.8.0"
506+
nanoid "^3.3.4"
507+
tweetnacl "^1.0.3"
508+
496509
"@esbuild/[email protected]":
497510
version "0.19.12"
498511
resolved "https://registry.yarnpkg.com/@esbuild/aix-ppc64/-/aix-ppc64-0.19.12.tgz#d1bc06aedb6936b3b6d313bf809a5a40387d2b7f"

0 commit comments

Comments
 (0)