Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: notification metadata #110

Merged
merged 7 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions packages/blockchain-sdk-solana/examples/create-dapp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { BlockchainType, Dialect } from '../../sdk';
import { NodeDialectSolanaWalletAdapter, Solana, SolanaSdkFactory } from '..';
import { Keypair } from '@solana/web3.js';
import bs58 from 'bs58';

async function main() {
const monitor = Dialect.sdk<Solana>(
{
// environment: 'development',
environment: 'development',
},
SolanaSdkFactory.create({
wallet: new NodeDialectSolanaWalletAdapter(
Keypair.fromSecretKey(
new Uint8Array([
56, 111, 31, 152, 56, 138, 122, 34, 150, 99, 14, 65, 41, 148, 28,
58, 223, 101, 10, 31, 12, 146, 175, 132, 212, 6, 19, 222, 92, 177,
36, 123, 65, 246, 202, 251, 197, 105, 179, 188, 65, 163, 200, 12,
235, 148, 133, 28, 213, 61, 233, 180, 235, 110, 52, 2, 94, 136, 193,
46, 225, 253, 48, 144,
]),
),
),
}),
);
const uint8Array = new Uint8Array([
248, 80, 183, 221, 66, 144, 97, 254, 75, 101, 186, 117, 33, 130, 91, 117,
214, 195, 11, 209, 227, 230, 64, 138, 128, 91, 90, 248, 47, 46, 201, 100, 4,
1, 253, 87, 233, 66, 232, 136, 133, 117, 185, 11, 43, 114, 187, 61, 35, 102,
45, 78, 9, 6, 60, 136, 233, 210, 41, 32, 18, 228, 241, 182,
]);

const s = bs58.encode(uint8Array);
console.log(s);
return;
const monitor2 = Dialect.sdk<Solana>(
{
// environment: 'development',
environment: 'development',
},
SolanaSdkFactory.create({
wallet: new NodeDialectSolanaWalletAdapter(
Keypair.fromSecretKey(uint8Array),
),
}),
);

const monitorDapp = await monitor.dapps.find();
if (!monitorDapp) {
const created = await monitor.dapps.create({
name: 'MONITOR 1',
blockchainType: BlockchainType.SOLANA,
});
}

const monitorDapp2 = await monitor2.dapps.find();
if (!monitorDapp2) {
const created = await monitor2.dapps.create({
name: 'MONITOR 2',
blockchainType: BlockchainType.SOLANA,
});
}

const promise = await monitor2.threads.find({
otherMembers: ['6tBUD4bQzNehG3hQVtVFaGxre2P8rQoH99pubRtgSbSb'],
});

console.log(promise?.lastMessage?.metadata);
await monitorDapp2!.messages.send({
title: 'Hello topic',
message: 'Hello from monitor 2 topic',
actions: [
{ label: 'so', url: 'https://so.com' },
{ label: 'sat', url: 'https://sat.com' },
],
notificationTypeId: '9b153418-f804-498d-9ea3-08f62359032a',
});
}

main().catch(console.error);
8 changes: 5 additions & 3 deletions packages/blockchain-sdk-solana/examples/e2e-dapp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
NodeDialectSolanaWalletAdapter,
Solana,
SolanaSdkFactory,
} from '../src';
} from '@dialectlabs/blockchain-sdk-solana';

// Initialize an SDK for interacting with Dialect services. All SDKs have a wallet tied
// to them for authentication purposes. In this case, we are initializing an sdk for the
Expand Down Expand Up @@ -66,7 +66,7 @@ async function getOrRegisterDapp() {
let dapp = await sdk.dapps.find();
if (!dapp) {
console.log(`Dapp not found, creating it...`);
dapp = await sdk.dapps.create({
const createdDapp = await sdk.dapps.create({
name: 'Example dapp',
description: 'Example dapp description.',
blockchainType: BlockchainType.SOLANA,
Expand All @@ -76,6 +76,8 @@ async function getOrRegisterDapp() {
dapp!.description
}; messaging address: ${dapp!.address})`,
);

return createdDapp;
}
return dapp;
return dapp.messages.send({});
}
59 changes: 59 additions & 0 deletions packages/blockchain-sdk-solana/examples/send-notif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { BlockchainType, Dialect, DialectSdk } from '../../sdk';
import { NodeDialectSolanaWalletAdapter, Solana, SolanaSdkFactory } from '..';
import { Keypair } from '@solana/web3.js';

async function getOrCreateDapp(monitor: DialectSdk<Solana>) {
const monitorDapp = await monitor.dapps.find();
if (!monitorDapp) {
const created = await monitor.dapps.create({
name: 'MONITOR 1',
blockchainType: BlockchainType.SOLANA,
});
return created;
}
return monitorDapp;
}

async function main() {
const sdk = Dialect.sdk<Solana>(
{
// environment: 'development',
environment: 'development',
},
SolanaSdkFactory.create({
wallet: new NodeDialectSolanaWalletAdapter(
Keypair.fromSecretKey(
new Uint8Array([
248, 80, 183, 221, 66, 144, 97, 254, 75, 101, 186, 117, 33, 130, 91,
117, 214, 195, 11, 209, 227, 230, 64, 138, 128, 91, 90, 248, 47, 46,
201, 100, 4, 1, 253, 87, 233, 66, 232, 136, 133, 117, 185, 11, 43,
114, 187, 61, 35, 102, 45, 78, 9, 6, 60, 136, 233, 210, 41, 32, 18,
228, 241, 182,
]),
),
),
}),
);
const dapp = await getOrCreateDapp(sdk);

await dapp.messages.send({
title: 'Hello topic 1',
message: 'Hello from monitor 1 topic',
actions: [
{ label: 'so', url: 'https://so.com' },
{ label: 'sat', url: 'https://sat.com' },
],
notificationTypeId: '9b153418-f804-498d-9ea3-08f62359032a',
});
await dapp.messages.send({
title: 'Hello topic 2',
message: 'Hello from monitor 2 topic',
actions: [
{ label: 'le', url: 'https://le.com' },
{ label: 'zhat', url: 'https://zhat.com' },
],
notificationTypeId: 'dd20b141-ca09-4fde-9b36-19ac2560edca',
});
}

main().catch(console.error);
12 changes: 12 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# CHANGELOG

## [1.7.2] - 2024-03-12

- feature: fix version number

## [1.7.1] - 2024-03-12

- feature: hide tags in notification model

## [1.7.0] - 2024-03-08

- feature: add notification metadata

## [1.5.2] - 2023-01-17

- fix: stale token after expiration
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dialectlabs/sdk",
"version": "1.6.1",
"version": "1.7.2",
"repository": "[email protected]:dialectlabs/sdk.git",
"author": "dialectlabs",
"license": "Apache-2.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/src/dapp/dapp.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,18 @@ export interface FindDappQuery {
blockchainType?: BlockchainType;
}

export interface DappMessageAction {
label: string;
url: string;
}

export interface SendDappMessageCommandBase {
message: string;
title?: string;
notificationTypeId?: string;
addressTypes?: AddressType[];
// tags?: string[];
actions?: DappMessageAction[];
}

export type BroadcastDappMessageCommand = SendDappMessageCommandBase;
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk/src/dialect-cloud-api/data-service-dapps-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
withReThrowingDataServiceError,
} from './data-service-api';
import axios from 'axios';
import type { BlockchainType } from '../dapp/dapp.interface';
import type { BlockchainType, DappMessageAction } from '../dapp/dapp.interface';

export interface DataServiceDappsApi {
create(command: Omit<CreateDappCommandDto, 'publicKey'>): Promise<DappDto>;
Expand Down Expand Up @@ -183,11 +183,18 @@ export enum AddressTypeDto {
Wallet = 'WALLET',
}

export class DappMessageActionDto {
label!: string;
url!: string;
}

class SendDappMessageCommand {
title?: string;
message!: string;
notificationTypeId?: string;
addressTypes?: AddressTypeDto[];
// tags?: string[];
actions?: DappMessageActionDto[];
}

export class UnicastDappMessageCommandDto extends SendDappMessageCommand {
Expand Down
13 changes: 12 additions & 1 deletion packages/sdk/src/dialect-cloud-api/data-service-dialects-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
withReThrowingDataServiceError,
} from './data-service-api';
import axios from 'axios';

export interface DataServiceDialectsApi {
create(command: CreateDialectCommand): Promise<DialectAccountDto>;

Expand Down Expand Up @@ -256,11 +255,23 @@ export enum MemberScopeDto {
WRITE = 'WRITE',
}

export class MessageActionDto {
label!: string;
url!: string;
}
export class MessageMetadataDto {
title?: string;
tags?: string[];
actions?: MessageActionDto[];
notificationTypeId?: string;
}

export interface MessageDto {
readonly owner: string;
readonly text: number[];
readonly timestamp: number;
readonly deduplicationId?: string;
readonly metadata?: MessageMetadataDto;
}

export interface MessagesDto {
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/internal/dapp/data-service-dapp-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export class DataServiceDappMessages implements DappMessages {
addressTypes: command?.addressTypes?.map((addr) =>
toAddressTypeDto(addr),
),
// tags: command.tags,
actions: command.actions,
}),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class DataServiceMessaging implements Messaging {
? thisThreadMember
: otherMembersPks[lastMessage.owner]!,
deduplicationId: lastMessage.deduplicationId,
metadata: lastMessage.metadata,
};
}

Expand Down Expand Up @@ -311,14 +312,15 @@ export class DataServiceThread implements Thread {
const { messages } = await withErrorParsing(
this.dataServiceDialectsApi.getMessages(this.address.toString()),
);
const threadMessages = messages.map((it) => ({
const threadMessages: ThreadMessage[] = messages.map((it) => ({
author:
it.owner === this.me.address.toString()
? this.me
: this.otherMembersPks[it.owner]!,
timestamp: new Date(it.timestamp),
text: this.textSerde.deserialize(new Uint8Array(it.text)),
deduplicationId: it.deduplicationId,
metadata: it.metadata,
}));
this.lastMessage = threadMessages[0] ?? null;
return threadMessages;
Expand Down
14 changes: 14 additions & 0 deletions packages/sdk/src/messaging/messaging.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,24 @@ export interface SendMessageCommand {
deduplicationId?: string;
}

export interface MessageAction {
label: string;
url: string;
}

export interface MessageMetadata {
title?: string;
// tags?: string[];
actions?: MessageAction[];
notificationTypeId?: string;
notificationTypeHumanReadableId?: string;
}

export interface ThreadMessage {
text: string;
timestamp: Date;
author: ThreadMember;
metadata?: MessageMetadata;
deduplicationId?: string;
}

Expand Down
44 changes: 23 additions & 21 deletions packages/sdk/tests/dialect-cloud-api/data-service-dapps-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {

describe('Data service dapps api (e2e)', () => {
const baseUrl = 'http://localhost:8080';
// const baseUrl = 'https://dev.dialectapi.to';

let dappsApi: DataServiceDappsApi;
let dappAccountAddress: AccountAddress;
Expand All @@ -43,27 +44,28 @@ describe('Data service dapps api (e2e)', () => {

test('can create dapp and find all dappAddresses', async () => {
// when
const command: CreateDappCommand = {
name: 'Test dapp',
description: 'Test description',
avatarUrl: 'https://www.dialect.to/favicon-32x32.png',
blockchainType: BlockchainType.SOLANA,
};
const created = await dappsApi.create(command);
const addresses = await dappsApi.findAllDappAddresses();
// then
const dappDtoExpected: DappDto = {
id: expect.any(String),
publicKey: dappAccountAddress.toString(),
name: command.name,
description: command.description,
avatarUrl: command.avatarUrl,
verified: false,
telegramBotUserName: expect.any(String),
blockchainType: BlockchainType.SOLANA,
};
expect(created).toMatchObject(dappDtoExpected);
expect(addresses).toMatchObject([]);
await dappsApi.find();
// const command: CreateDappCommand = {
// name: 'Test dapp',
// description: 'Test description',
// avatarUrl: 'https://www.dialect.to/favicon-32x32.png',
// blockchainType: BlockchainType.SOLANA,
// };
// const created = await dappsApi.create(command);
// const addresses = await dappsApi.findAllDappAddresses();
// // then
// const dappDtoExpected: DappDto = {
// id: expect.any(String),
// publicKey: dappAccountAddress.toString(),
// name: command.name,
// description: command.description,
// avatarUrl: command.avatarUrl,
// verified: false,
// telegramBotUserName: expect.any(String),
// blockchainType: BlockchainType.SOLANA,
// };
// expect(created).toMatchObject(dappDtoExpected);
// expect(addresses).toMatchObject([]);
});

test('can find dapp', async () => {
Expand Down
Loading