lazy-verification

This commit is contained in:
Michael Peters 2021-12-02 19:30:31 -06:00
parent 1592a38c7c
commit 74b02bfff6
5 changed files with 18 additions and 9 deletions

View File

@ -79,7 +79,7 @@ export class AutoVerifierWithArg<T, K> {
} }
} }
async fetchAndVerifyIfNeded(query: K): Promise<T | null> { async fetchAndVerifyIfNeded(query: K, lazyVerify: boolean = false): Promise<T | null> {
let token = this.tokenizer(query); let token = this.tokenizer(query);
this.tokenQueries.set(token, query); this.tokenQueries.set(token, query);
let autoVerifier = this.tokenAutoVerifiers.get(token); let autoVerifier = this.tokenAutoVerifiers.get(token);
@ -91,6 +91,6 @@ export class AutoVerifierWithArg<T, K> {
); );
this.tokenAutoVerifiers.set(token, autoVerifier); this.tokenAutoVerifiers.set(token, autoVerifier);
} }
return await autoVerifier.fetchAndVerifyIfNeeded(); return await autoVerifier.fetchAndVerifyIfNeeded(lazyVerify);
} }
} }

View File

@ -148,7 +148,8 @@ export class AutoVerifier<T> {
// Fetches the result of the primary fetchable // Fetches the result of the primary fetchable
// If the primary fetchable returns null but has not been verified yet, this will return the result of the trusted fetchable // If the primary fetchable returns null but has not been verified yet, this will return the result of the trusted fetchable
// If the trusted fetchable has not been used to verify the primary fetchable yet, this queries the trusted fetchable and calls verify // If the trusted fetchable has not been used to verify the primary fetchable yet, this queries the trusted fetchable and calls verify
async fetchAndVerifyIfNeeded(): Promise<T | null> { // @param lazyVerify: set to true to only verify if primaryResult returns null
async fetchAndVerifyIfNeeded(lazyVerify: boolean = false): Promise<T | null> {
return await new Promise<T | null>(async (resolve: (result: T | null) => void, reject: (error: Error) => void) => { return await new Promise<T | null>(async (resolve: (result: T | null) => void, reject: (error: Error) => void) => {
let resolved = false; let resolved = false;
try { try {
@ -158,7 +159,7 @@ export class AutoVerifier<T> {
let origPrimaryPromise = this.primaryPromise; let origPrimaryPromise = this.primaryPromise;
// pre-fetch the trusted result while we fetch the primary result // pre-fetch the trusted result while we fetch the primary result
if (this.trustedStatus === 'none') { if (!lazyVerify && this.trustedStatus === 'none') {
this.trustedStatus = 'fetching'; this.trustedStatus = 'fetching';
this.trustedPromise = this.trustedFunc(); this.trustedPromise = this.trustedFunc();
} }
@ -171,7 +172,7 @@ export class AutoVerifier<T> {
if (primaryResult) { if (primaryResult) {
resolve(primaryResult); resolve(primaryResult);
resolved = true; resolved = true;
if (this.trustedStatus === 'verified') { if (lazyVerify || this.trustedStatus === 'verified') {
return; return;
} }
} }

View File

@ -168,9 +168,9 @@ export class Message implements WithEquals<Message> {
let channel = this.channel instanceof Channel ? this.channel.name : `ch#${this.channel.id}`; let channel = this.channel instanceof Channel ? this.channel.name : `ch#${this.channel.id}`;
let member = this.member instanceof Member ? this.member.displayName : `m#${this.member.id}`; let member = this.member instanceof Member ? this.member.displayName : `m#${this.member.id}`;
if (this.resourceName) { if (this.resourceName) {
return `m#${this.id} ${channel} @${formatDate(this.sent)} ${member}: ${this.text ? this.text + ' / ' : ''}${this.resourceName}`; return `msg#${this.id} ${channel} @${formatDate(this.sent)} ${member}: ${this.text ? this.text + ' / ' : ''}${this.resourceName}`;
} else { } else {
return `m#${this.id} ${channel} @${formatDate(this.sent)} ${member}: ${this.text}`; return `msg#${this.id} ${channel} @${formatDate(this.sent)} ${member}: ${this.text}`;
} }
} }

View File

@ -183,7 +183,7 @@ export default class PairVerifierFetchable extends EventEmitter<Conflictable> im
return await this.fetchMessagesAfterVerifier.fetchAndVerifyIfNeded({ channelId, messageId, number }); return await this.fetchMessagesAfterVerifier.fetchAndVerifyIfNeded({ channelId, messageId, number });
} }
async fetchResource(resourceId: string): Promise<Resource | null> { async fetchResource(resourceId: string): Promise<Resource | null> {
return await this.fetchResourceVerifier.fetchAndVerifyIfNeded({ id: resourceId }); return await this.fetchResourceVerifier.fetchAndVerifyIfNeded({ id: resourceId }, true); // lazy verification
} }
async fetchTokens(): Promise<Token[] | null> { async fetchTokens(): Promise<Token[] | null> {
return await this.fetchTokensVerifier.fetchAndVerifyIfNeeded(); return await this.fetchTokensVerifier.fetchAndVerifyIfNeeded();

View File

@ -111,6 +111,9 @@ export default class CombinedGuild extends EventEmitter<Connectable & Conflictab
} }
this.ramGuild.handleMessagesChanged(messages); this.ramGuild.handleMessagesChanged(messages);
await this.personalDBGuild.handleMessagesChanged(messages); await this.personalDBGuild.handleMessagesChanged(messages);
let members = await this.grabRAMMembersMap();
let channels = await this.grabRAMChannelsMap();
for (let message of messages) message.fill(members, channels);
this.emit('update-messages', messages); this.emit('update-messages', messages);
}); });
@ -130,8 +133,13 @@ export default class CombinedGuild extends EventEmitter<Connectable & Conflictab
LOG.info(`g#${this.id} channels conflict`, { changes }); LOG.info(`g#${this.id} channels conflict`, { changes });
this.emit('conflict-channels', changesType, changes); this.emit('conflict-channels', changesType, changes);
}); });
ramDiskSocket.on('conflict-messages', (changesType: AutoVerifierChangesType, changes: Changes<Message>) => { ramDiskSocket.on('conflict-messages', async (changesType: AutoVerifierChangesType, changes: Changes<Message>) => {
LOG.info(`g#${this.id} messages conflict`, { changes }); LOG.info(`g#${this.id} messages conflict`, { changes });
let members = await this.grabRAMMembersMap();
let channels = await this.grabRAMChannelsMap();
for (let message of changes.added) message.fill(members, channels);
for (let dataPoint of changes.updated) dataPoint.newDataPoint.fill(members, channels);
for (let message of changes.deleted) message.fill(members, channels);
this.emit('conflict-messages', changesType, changes); this.emit('conflict-messages', changesType, changes);
}); });
ramDiskSocket.on('conflict-tokens', (changesType: AutoVerifierChangesType, changes: Changes<Token>) => { ramDiskSocket.on('conflict-tokens', (changesType: AutoVerifierChangesType, changes: Changes<Token>) => {