Skip to content

Commit

Permalink
chore: add relays and logging on relay events
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 24, 2024
1 parent f050ead commit b3c259e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion apps/nostr/src/nostr.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import NDK, {
NDKEvent,
NDKPrivateKeySigner,
NDKRelay,
NDKUser,
} from '@nostr-dev-kit/ndk';
import { nip19 } from 'nostr-tools';
Expand All @@ -16,6 +17,9 @@ const explicitRelayUrls = [
'wss://relay.damus.io',
'wss://relay.nostr.bg',
'wss://relay.snort.social',
'wss://nostr.mom',
'wss://nos.lol',
'wss://relay.nostr.bg'
];

@Injectable()
Expand All @@ -37,6 +41,15 @@ export class NostrService {
signer,
});


this.ndk.pool.on('relay:connect', (relay: NDKRelay) => {
this.logger.log(`Connected to relay: ${relay.url}`);
});

this.ndk.pool.on('relay:disconnect', (relay: NDKRelay) => {
this.logger.warn(`Disconnected from relay: ${relay.url}`);
});

this.connectRelays()
.then(() => {
this.logger.log('NostrService connected');
Expand Down Expand Up @@ -107,7 +120,7 @@ export class NostrService {
async sendEncryptedDirectMessage({
message,
recipient,
retry,
retry = true,
}: SendEncryptedNostrDmDto): Promise<void> {
try {
if (!this.connected) {
Expand All @@ -116,6 +129,10 @@ export class NostrService {

const receiver = this.parseRecipient(recipient) || this.pubkey;

this.logger.log(
`${this.ndk.pool.connectedRelays().length} relays connected`,
);

const dm = new NDKEvent(this.ndk, {
kind: 4,
content: message,
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/dto/send-encrypted-nostr-dm.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class NostrRecipientDto implements NostrRecipient {
@IsOptional()
@IsString()
@Type(() => String)
@ApiProperty()
@ApiProperty({ example: '' })
pubkey: string;
}

export class SendEncryptedNostrDmDto implements NostrDirectMessageRequest {
@IsNotEmpty()
@IsString()
@Type(() => String)
@ApiProperty()
@ApiProperty({ example: 'hello bitsacco'})
message: string;

@IsDefined()
Expand Down

0 comments on commit b3c259e

Please sign in to comment.