diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts index 5579b7ba53ffbf53a6e6aa0ab28b3c6c0027518d..02e8c6d2489761a2aeb260320cff5548c0e6d423 100644 --- a/app/lib/dal/fileDAL.ts +++ b/app/lib/dal/fileDAL.ts @@ -933,11 +933,9 @@ export class FileDAL implements ServerDAO { } async searchJustIdentitiesByPubkey(pubkey: string): Promise<DBIdentity[]> { - const pendings = await this.idtyDAL.getFromPubkey(pubkey); - const writtens = await this.iindexDAL.searchByPubkey(pubkey); - const nonPendings = Underscore.filter(writtens, (w: IindexEntry) => { - return Underscore.where(pendings, { pubkey: w.pub }).length == 0; - }); + const pendings = await this.idtyDAL.findByPub(pubkey); + const writtenIdty = await this.iindexDAL.getOldFromPubkey(pubkey); + const nonPendings = writtenIdty && Underscore.where(pendings, { pubkey: writtenIdty.pub }).length === 0 ? [writtenIdty] : []; const found = pendings.concat( nonPendings.map((i: any) => { // Use the correct field diff --git a/app/lib/dal/indexDAL/abstract/IIndexDAO.ts b/app/lib/dal/indexDAL/abstract/IIndexDAO.ts index ecf64d441911bb4522d73e01fed374ca8e014f07..e5970f2149790b79e623767d9bc1a4b81ed55426 100644 --- a/app/lib/dal/indexDAL/abstract/IIndexDAO.ts +++ b/app/lib/dal/indexDAL/abstract/IIndexDAO.ts @@ -19,7 +19,7 @@ export interface IIndexDAO extends ReduceableDAO<IindexEntry> { searchThoseMatching(search: string): Promise<OldIindexEntry[]>; - searchByPubkey(search: string): Promise<OldIindexEntry[]>; + getOldFromPubkey(search: string): Promise<OldIindexEntry | null>; getFullFromUID(uid: string): Promise<FullIindexEntry>; diff --git a/app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts b/app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts index e7157b34ef9a67f2b707fb0b97e9511e1a029ec3..fd5010024ec958c2e6768c2ac5de27427f3c9ba1 100644 --- a/app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts +++ b/app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts @@ -281,13 +281,11 @@ export class LevelDBIindex extends LevelDBTable<IindexEntry[]> .concat(pubIdentities.filter((p) => p.pub)); } - async searchByPubkey(pub: string): Promise<OldIindexEntry[]> { + async getOldFromPubkey(pub: string): Promise<OldIindexEntry | null> { const identities = await this.findByPub(pub); if (!identities.length) { - return []; + return null; } - // TODO Why do we need to use reduce() on array ? This will merge items into one object - const mergedIdentities = OldTransformers.toOldIindexEntry(reduce(identities)) - return [mergedIdentities]; + return OldTransformers.toOldIindexEntry(reduce(identities)); } } diff --git a/app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts b/app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts index 2cf0aeb7fa693db83325a146971b06913dced8e2..806ee307a6d2fc7e38831e2b2b8a7027c4bbbefc 100644 --- a/app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts +++ b/app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts @@ -1,4 +1,4 @@ -import { FullIindexEntry, IindexEntry, Indexer } from "../../../indexer"; +import {FullIindexEntry, IindexEntry, Indexer, reduce} from "../../../indexer"; import { SQLiteDriver } from "../../drivers/SQLiteDriver"; import { MonitorExecutionTime } from "../../../debug/MonitorExecutionTime"; import { IIndexDAO } from "../abstract/IIndexDAO"; @@ -212,6 +212,15 @@ export class SqliteIIndex extends SqliteTable<IindexEntry> return (await this.getFromUID(uid)) as FullIindexEntry; } + @MonitorExecutionTime() + async getOldFromPubkey(pub: string): Promise<OldIindexEntry | null> { + const identities = await this.find("SELECT * FROM iindex WHERE pub = ? order by writtenOn ASC", [pub]); + if (!identities.length) { + return null; + } + return OldTransformers.toOldIindexEntry(reduce(identities)); + } + @MonitorExecutionTime() async getMembers(): Promise<{ pubkey: string; uid: string | null }[]> { const members = await this.find( @@ -263,14 +272,4 @@ export class SqliteIIndex extends SqliteTable<IindexEntry> ]) ).map(OldTransformers.toOldIindexEntry); } - - @MonitorExecutionTime() - async searchByPubkey(pub: string): Promise<OldIindexEntry[]> { - // TODO Why not need reduce() here ? As done in the LevelDB implementation - return ( - await this.find("SELECT * FROM iindex WHERE pub = ?", [ - pub - ]) - ).map(OldTransformers.toOldIindexEntry); - } } diff --git a/app/lib/dal/sqliteDAL/IdentityDAL.ts b/app/lib/dal/sqliteDAL/IdentityDAL.ts index 9716b13f77490630630d5c46b948ecc201b351d2..012a1646f2ef0d346e045a745fdee90fe541b656 100644 --- a/app/lib/dal/sqliteDAL/IdentityDAL.ts +++ b/app/lib/dal/sqliteDAL/IdentityDAL.ts @@ -328,8 +328,8 @@ export class IdentityDAL extends AbstractSQLite<DBIdentity> { }); } - getFromPubkey(pubkey: string) { - return this.sqlFind({pubkey}); + findByPub(pub: string) { + return this.sqlFind({pubkey: pub}); } async trimExpiredIdentities(medianTime: number) {