Skip to content

Commit

Permalink
mantendo a conexão ativa
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioconselheiro committed Nov 12, 2023
1 parent b32eac0 commit 2ed1269
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/app/shared/nostr-api/nostr-api.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { NostrEventFactory } from './nostr-event.factory';
import { NostrService } from './nostr.service';

@NgModule({
imports: [
CommonModule
],
providers: [
NostrService
NostrService,
NostrEventFactory
]
})
export class NostrApiModule { }
40 changes: 30 additions & 10 deletions src/app/shared/nostr-api/nostr.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { Injectable } from '@angular/core';
import { Event, Filter, SimplePool } from 'nostr-tools';
import { Event, Filter, SimplePool, validateEvent, verifySignature } from 'nostr-tools';
import { defaultRelays } from '../../default-relays.const';

@Injectable({
providedIn: 'root'
})
@Injectable()
export class NostrService {
private readonly relays = defaultRelays;

private static instance: NostrService | null = null;

pool = new SimplePool();

constructor() {
if (!NostrService.instance) {
NostrService.instance = this;
}

return NostrService.instance;
}

get<K extends number>(filters: Filter<K>[]): Promise<Array<Event<K>>> {
const pool = new SimplePool();
const events = new Array<Event<K>>();
const sub = pool.sub(
this.relays, filters
const sub = this.pool.sub(
defaultRelays, filters
);

sub.on('event', event => {
Expand All @@ -22,9 +30,21 @@ export class NostrService {
return new Promise(resolve => {
sub.on('eose', () => {
resolve(events);
sub.unsub();
pool.close(this.relays);
});
});
}

async publish<K extends number>(event: Event<K>): Promise<void> {
const ok = validateEvent(event);
const veryOk = verifySignature(event);

if (!ok || !veryOk) {
console.error(' :: event is not valid... aborting...');
return Promise.resolve();
}

await this.pool.publish(defaultRelays, event);

return Promise.resolve();
}
}
11 changes: 0 additions & 11 deletions src/app/shared/omegle/omegle.nostr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ import { Injectable } from '@angular/core';
import { NostrService } from '../nostr-api/nostr.service';
import { NostrEventKind } from '@domain/nostr-event-kind.enum';

/**
* Nip 38 e nip 4 vou usar
* O nip 38 indica um status em que o usuário está (online, offline, ocupado e variações)
* O 4 é chat direto criptografado
* 1. O nsec é gerado
* 2. Aplico um user status de interessado em bate papo
* 3. Filtra nos relays eventos de user status interessado em bate papo
* 4. Conecta os dois em chat criptografado e atualiza o user status pra ocupado
* Cada nova conversa por ser um novo nsec
*/

@Injectable()
export class OmegleNostr {

Expand Down

0 comments on commit 2ed1269

Please sign in to comment.