Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
vivganes committed Nov 3, 2023
1 parent a508c27 commit c775aa3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/app/model/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export interface Community{
displayName?:string,
id?:string,
description?:string,
rules?:string;
rules?:string,
image?:string,
creatorProfile?:NDKUserProfile,
moderatorHexKeys?:string[],
followersHexKeys?:string[]
followersHexKeys?:string[],
created_at?: number
}
16 changes: 16 additions & 0 deletions src/app/service/community-page.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { CommunityPageService } from './community-page.service';

describe('CommunityPageService', () => {
let service: CommunityPageService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(CommunityPageService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
76 changes: 76 additions & 0 deletions src/app/service/community-page.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Injectable } from '@angular/core';
import { ObjectCacheService } from './object-cache.service';
import { Community } from '../model/community';

@Injectable({
providedIn: 'root',
})
export class CommunityPageService {
constructor(private objectCache: ObjectCacheService) {}

fastForward(lastRow: Community, idProp: string) {
let fastForwardComplete = false;
return (item: Community) => {
if (fastForwardComplete) return true;
//@ts-ignore
if (item[idProp] === lastRow[idProp]) {
fastForwardComplete = true;
}
return false;
};
}

async getPageWithNumber(pageNumber:number) {
const PAGE_SIZE = 10;

// A helper function we will use below.
// It will prevent the same results to be returned again for next page.

// Criterion filter in plain JS:
const criterionFunction = (community: Community) => community.id !== undefined; // Just an example...

//
// Query First Page
//
let page = await this.objectCache.communities
.orderBy('created_at') // Utilize index for sorting
.filter(criterionFunction)
.limit(PAGE_SIZE)
.toArray();
if(pageNumber === 0){
return page;
}

//
// Page 2
//
// "page" variable is an array of results from last request:
if (page.length < PAGE_SIZE) return; // Done
let lastEntry = page[page.length - 1];
page = await this.objectCache.communities
// Use index to fast forward as much as possible
// This line is what makes the paging optimized
.where('created_at')
.belowOrEqual(lastEntry.created_at) // makes it sorted by lastName

// Use helper function to fast forward to the exact last result:
.filter(this.fastForward(lastEntry, 'id'))
.limit(PAGE_SIZE)
.toArray();
if (pageNumber === 1){
return page;
}
//
// Page N
//
if (page.length < PAGE_SIZE) return; // Done
lastEntry = page[page.length - 1];
page = await this.objectCache.communities
.where('created_at')
.belowOrEqual(lastEntry.created_at)
.filter(this.fastForward(lastEntry, 'id'))
.limit(PAGE_SIZE)
.toArray();
return page;
}
}
38 changes: 22 additions & 16 deletions src/app/service/ndkprovider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,26 +390,31 @@ export class NdkproviderService {
await newNDK.assertSigner();
}
try {
await newNDK.connect(1000).catch(e => console.log(e));
await newNDK.connect(1000)
.then(()=>{
this.isTryingZapddit = false;
this.refreshAppData().then(() => {
this.loggingIn = false;
//once all setup is done, then only set loggedIn=true to start rendering
this.loggedIn = true;
this.loginCompleteEmitter.emit(true);

this.fetchFollowersFromCache();
this.fetchMutedUsersFromCache();

this.checkIfNIP05Verified(this.currentUserProfile?.nip05, this.currentUser?.pubkey);

});


})
.catch(e => console.log(e));
this.ndk = newNDK;
} catch (e) {
console.log('Error in connecting NDK ' + e);
}
}
this.isTryingZapddit = false;
await this.refreshAppData();

this.loggingIn = false;
//once all setup is done, then only set loggedIn=true to start rendering
this.loggedIn = true;
this.loginCompleteEmitter.emit(true);

this.fetchFollowersFromCache();
this.fetchMutedUsersFromCache();


await this.checkIfNIP05Verified(this.currentUserProfile?.nip05, this.currentUser?.pubkey);


}

isLoggedIn(): boolean {
Expand Down Expand Up @@ -818,7 +823,8 @@ export class NdkproviderService {
rules: rules,
image: image,
creatorHexKey: creatorHexKey,
moderatorHexKeys: moderatorHexKeys
moderatorHexKeys: moderatorHexKeys,
created_at: communityEvent.created_at
});
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/service/object-cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { Community } from '../model/community';

const DATASTORE = {
users: "hexPubKey, name, displayName, nip05, npub",
communities: "id,name,displayName,creatorHexKey,description"
communities: "id,name,displayName,creatorHexKey,description,created_at"
};

const VERSION = 1;


@Injectable({
providedIn: 'root'
Expand All @@ -26,7 +28,7 @@ export class ObjectCacheService extends Dexie {
this.version(1).stores({
users: DATASTORE.users
});
this.version(2).stores(DATASTORE);
this.version(Math.round(this.verno + 2)).stores(DATASTORE);
}


Expand Down

0 comments on commit c775aa3

Please sign in to comment.