Get latest 100 items on profiles - does not render yet

This commit is contained in:
SondreB 2023-01-08 22:23:39 +01:00
parent b01dd21639
commit 52d42a5f02
No known key found for this signature in database
GPG Key ID: D6CC44C75005FDBF
6 changed files with 98 additions and 60 deletions

View File

@ -196,4 +196,4 @@ header {
.search-result-option span {
margin-right: 0.4em;
}
}

View File

@ -30,7 +30,7 @@ export class CacheService {
tap({
next: (val) => {
// on next 11, etc.
console.log('on next', val);
// console.log('on next', val);
this.set(key, val, maxAge);
},
error: (error) => {
@ -38,7 +38,7 @@ export class CacheService {
this.inFlightObservables.delete(key);
throwError(() => error);
},
complete: () => console.log('on complete'),
complete: () => {},
})
);
} else {

View File

@ -30,6 +30,10 @@ export class DataService {
// console.log('PROFILE REQUESTED:', pubkey);
// await this.downloadProfile(pubkey);
// });
this.connected$.subscribe((status) => {
console.log('DataService: Yes we have connection!', status);
});
}
async initialize() {
@ -87,7 +91,11 @@ export class DataService {
}
// Observable that can be merged with to avoid performing calls unless we have connected to relays.
connected$ = this.appState.connected$.pipe(map((status) => status === true));
connected$ = this.appState.connected$.pipe(map((status) => status === true)).pipe(
tap((status) => {
console.log('STATUS:', status);
})
);
/** Creates an observable that will attempt to get newest profile entry across all relays and perform multiple callbacks if newer is found. */
downloadNewestProfiles(pubkeys: string[], requestTimeout = 10000) {
@ -106,38 +114,50 @@ export class DataService {
/** Creates an observable that will attempt to get newest profile events across all relays and perform multiple callbacks if newer is found. */
downloadNewestEvents(pubkeys: string[], kinds: number[], requestTimeout = 10000) {
return this.downloadNewestEventsByQuery([{ kinds: kinds, authors: pubkeys }]);
}
/** Creates an observable that will attempt to get newest profile events across all relays and perform multiple callbacks if newer is found. */
downloadNewestEventsByQuery(query: any, requestTimeout = 10000) {
// TODO: Tune the timeout. There is no point waiting for too long if the relay is overwhelmed with requests as we will simply build up massive backpressure in the client.
const query = [{ kinds: kinds, authors: pubkeys }];
const totalEvents: NostrEventDocument[] = [];
// TODO: Figure out if we end up having memory leak with this totalEvents array.
return this.connected$
.pipe(take(1))
.pipe(mergeMap(() => this.relayService.connectedRelays())) // TODO: Time this, it appears to take a lot of time??
.pipe(mergeMap((relay) => this.downloadFromRelay(query, relay)))
.pipe(
filter((data) => {
// This logic is to ensure we don't care about receiving the same data more than once, unless the data is newer.
const existingEventIndex = totalEvents.findIndex((e) => e.id === data.id);
if (existingEventIndex > -1) {
const existingEvent = totalEvents[existingEventIndex];
return (
this.connected$
// .pipe(take(1))
.pipe(mergeMap(() => this.relayService.connectedRelays())) // TODO: Time this, it appears to take a lot of time??
.pipe(
tap(() => {
console.log('tapping...');
})
)
.pipe(mergeMap((relay) => this.downloadFromRelay(query, relay)))
.pipe(
filter((data) => {
// This logic is to ensure we don't care about receiving the same data more than once, unless the data is newer.
const existingEventIndex = totalEvents.findIndex((e) => e.id === data.id);
if (existingEventIndex > -1) {
const existingEvent = totalEvents[existingEventIndex];
// Verify if newer, then replace
if (existingEvent.created_at < data.created_at) {
totalEvents[existingEventIndex] = data;
// Verify if newer, then replace
if (existingEvent.created_at < data.created_at) {
totalEvents[existingEventIndex] = data;
return true;
}
} else {
totalEvents.push(data);
return true;
}
} else {
totalEvents.push(data);
return true;
}
return false;
})
)
.pipe(
timeout(requestTimeout),
catchError((error) => of(`The query timed out before it could complete: ${JSON.stringify(query)}.`))
);
return false;
})
)
);
// .pipe(
// timeout(requestTimeout),
// catchError((error) => of(`The query timed out before it could complete: ${JSON.stringify(query)}.`))
// );
}
subscribeLatestEvents(kinds: number[], pubkeys: string[], limit: number) {

View File

@ -201,7 +201,7 @@ export class ProfileService {
// .pipe(shareReplay()) // TODO: Investigate if this helps us get reply from the same observable if subscribed twice.
.pipe(
tap((profile) => {
console.log('TAPPING ON PROFILE GET:', profile);
// console.log('TAPPING ON PROFILE GET:', profile);
// this.table.put(profile.pubkey);
})
)

View File

@ -388,17 +388,13 @@ export class RelayService {
for (var i = 0; i < items.length; i++) {
const entry = items[i];
const existingConnection = this.relays.find((r) => r.url == entry.url);
console.log('FOUND EXISTING CONNECTION:', existingConnection);
// If we are already connected, skip opening connection again.
if (existingConnection && existingConnection.status == 1) {
continue;
}
console.log('Opening connection to:', entry);
this.openConnection(entry);
}
}

View File

@ -13,6 +13,7 @@ import { NavigationService } from '../services/navigation.service';
import { CircleService } from '../services/circle.service';
import { MatTabChangeEvent } from '@angular/material/tabs';
import { map, Observable, of, Subscription } from 'rxjs';
import { DataService } from '../services/data.service';
@Component({
selector: 'app-user',
@ -55,6 +56,10 @@ export class UserComponent {
subscriptions: Subscription[] = [];
profileSubscription?: Subscription;
feedSubscription?: Subscription;
constructor(
public navigation: NavigationService,
public appState: ApplicationState,
@ -62,19 +67,32 @@ export class UserComponent {
private cd: ChangeDetectorRef,
public options: OptionsService,
public profiles: ProfileService,
private dataService: DataService,
private validator: DataValidation,
private circleService: CircleService,
private utilities: Utilities,
private router: Router
) {
// this.appState.title = 'Blockcore Notes';
) {}
// this.subscriptions.push(
// this.router.events.pipe(filter((event) => event instanceof NavigationEnd)).subscribe((event) => {
// console.log('ROUTING EVENT:', event);
// console.log('ROUTING EVENT:', this.router);
// })
// );
async follow() {
this.profile!.status = ProfileStatus.Follow;
await this.profiles.follow(this.pubkey!);
// this.feedService.downloadRecent([this.pubkey!]);
}
tabIndex?: number;
onTabChanged(event: MatTabChangeEvent) {
this.router.navigate([], { queryParams: { t: event.index }, replaceUrl: true });
}
ngOnInit() {
// setInterval(() => {
// console.log('Closed:', this.feedSubscription?.closed);
// }, 50);
this.appState.showBackButton = true;
this.appState.actions = [];
this.subscriptions.push(
this.activatedRoute.queryParams.subscribe(async (params) => {
@ -87,13 +105,22 @@ export class UserComponent {
this.activatedRoute.paramMap.subscribe(async (params) => {
const pubkey: any = params.get('id');
// Whenever the user changes, unsubsribe the profile observable (which normally should be completed, but for safety).
if (this.profileSubscription) {
this.profileSubscription.unsubscribe();
}
if (this.feedSubscription) {
this.feedSubscription.unsubscribe();
}
if (!pubkey) {
return;
}
this.pubkey = pubkey;
this.profiles.getProfile(pubkey).subscribe(async (profile) => {
this.profileSubscription = this.profiles.getProfile(pubkey).subscribe(async (profile) => {
this.profile = profile;
if (!this.profile) {
@ -121,25 +148,12 @@ export class UserComponent {
// If the user has name in their profile, show that and not pubkey.
this.appState.title = `@${this.profile.name}`;
});
this.feedSubscription = this.dataService.downloadNewestEventsByQuery([{ kinds: [1], authors: [this.pubkey], limit: 100 }]).subscribe((event) => {
console.log('EVENT:', event);
});
})
);
}
async follow() {
this.profile!.status = ProfileStatus.Follow;
await this.profiles.follow(this.pubkey!);
// this.feedService.downloadRecent([this.pubkey!]);
}
tabIndex?: number;
onTabChanged(event: MatTabChangeEvent) {
this.router.navigate([], { queryParams: { t: event.index }, replaceUrl: true });
}
ngOnInit() {
this.appState.showBackButton = true;
this.appState.actions = [];
// if (this.pubkey) {
// console.log('PIPING EVENTS...');
@ -177,5 +191,13 @@ export class UserComponent {
ngOnDestroy() {
this.utilities.unsubscribe(this.subscriptions);
if (this.profileSubscription) {
this.profileSubscription.unsubscribe();
}
if (this.feedSubscription) {
this.feedSubscription.unsubscribe();
}
}
}