unverify both on disconnect, neither on connect.

This commit is contained in:
Michael Peters 2022-10-25 23:22:34 -07:00
parent 6e95ec4cec
commit 72c34f7e63
5 changed files with 20 additions and 21 deletions

View File

@ -27,7 +27,7 @@ export class AutoVerifierWithArg<T, K> {
private primaryFunc: (query: K) => Promise<T | null>, private primaryFunc: (query: K) => Promise<T | null>,
private trustedFunc: (query: K) => Promise<T | null>, private trustedFunc: (query: K) => Promise<T | null>,
private verifyFunc: (query: K, primaryResult: T | null, trustedResult: T | null) => Promise<boolean>, private verifyFunc: (query: K, primaryResult: T | null, trustedResult: T | null) => Promise<boolean>,
private name: string | null = null, // for debugging purposes private name: string | null = null, // for debugging purposes
) {} ) {}
static createStandardPartialMessageListAutoVerifier<T extends WithEquals<T> & { id: string }>( static createStandardPartialMessageListAutoVerifier<T extends WithEquals<T> & { id: string }>(
@ -38,7 +38,7 @@ export class AutoVerifierWithArg<T, K> {
changesType: AutoVerifierChangesType, changesType: AutoVerifierChangesType,
changes: Changes<T>, changes: Changes<T>,
) => Promise<boolean>, ) => Promise<boolean>,
name: string | null = null, name: string | null = null,
) { ) {
return new AutoVerifierWithArg<T[], PartialMessageListQuery>( return new AutoVerifierWithArg<T[], PartialMessageListQuery>(
query => `ch#${query.channelId} mo#${query.messageOrderId}->${query.number}`, query => `ch#${query.channelId} mo#${query.messageOrderId}->${query.number}`,
@ -46,20 +46,20 @@ export class AutoVerifierWithArg<T, K> {
query => trustedFunc(query), query => trustedFunc(query),
async (query: PartialMessageListQuery, primaryResult: T[] | null, trustedResult: T[] | null) => { async (query: PartialMessageListQuery, primaryResult: T[] | null, trustedResult: T[] | null) => {
// lOG.debug('messages verify: ', { // lOG.debug('messages verify: ', {
// query, // query,
// // primaryResult: primaryResult?.map((e: any) => e.sent).sort(), // // primaryResult: primaryResult?.map((e: any) => e.sent).sort(),
// // trustedResult: trustedResult?.map((e: any) => e.sent).sort(), // // trustedResult: trustedResult?.map((e: any) => e.sent).sort(),
// zipped: primaryResult && trustedResult && primaryResult.length === trustedResult.length && Q.zip( // zipped: primaryResult && trustedResult && primaryResult.length === trustedResult.length && Q.zip(
// primaryResult?.sort((a: any, b: any) => a.sent.getTime() - b.sent.getTime()).map((e: any) => e.text), // primaryResult?.sort((a: any, b: any) => a.sent.getTime() - b.sent.getTime()).map((e: any) => e.text),
// trustedResult?.sort((a: any, b: any) => a.sent.getTime() - b.sent.getTime()).map((e: any) => e.text) // trustedResult?.sort((a: any, b: any) => a.sent.getTime() - b.sent.getTime()).map((e: any) => e.text)
// ) // )
// }); // });
const changes = AutoVerifier.getChanges<T>(primaryResult, trustedResult); const changes = AutoVerifier.getChanges<T>(primaryResult, trustedResult);
//lOG.debug('changes:', { changes }); //lOG.debug('changes:', { changes });
const changesType = AutoVerifier.getListChangesType<T>(primaryResult, trustedResult, changes); const changesType = AutoVerifier.getListChangesType<T>(primaryResult, trustedResult, changes);
return await changesFunc(query, changesType, changes); return await changesFunc(query, changesType, changes);
}, },
name, name,
); );
} }
@ -72,7 +72,7 @@ export class AutoVerifierWithArg<T, K> {
primaryResult: T | null, primaryResult: T | null,
trustedResult: T | null, trustedResult: T | null,
) => Promise<boolean>, ) => Promise<boolean>,
name: string | null = null, name: string | null = null,
) { ) {
return new AutoVerifierWithArg<T, IDQuery>( return new AutoVerifierWithArg<T, IDQuery>(
query => `id#${query.id}`, query => `id#${query.id}`,
@ -82,7 +82,7 @@ export class AutoVerifierWithArg<T, K> {
const changesType = AutoVerifier.getSingleChangesType<T>(primaryResult, trustedResult); const changesType = AutoVerifier.getSingleChangesType<T>(primaryResult, trustedResult);
return await changesFunc(query, changesType, primaryResult, trustedResult); return await changesFunc(query, changesType, primaryResult, trustedResult);
}, },
name, name,
); );
} }
@ -122,7 +122,7 @@ export class AutoVerifierWithArg<T, K> {
async () => await this.trustedFunc(query), async () => await this.trustedFunc(query),
async (primaryResult: T | null, trustedResult: T | null) => async (primaryResult: T | null, trustedResult: T | null) =>
await this.verifyFunc(query, primaryResult, trustedResult), await this.verifyFunc(query, primaryResult, trustedResult),
`${this.name}::${token.slice(0, 8)}` `${this.name}::${token.slice(0, 8)}`
); );
this.tokenAutoVerifiers.set(token, autoVerifier); this.tokenAutoVerifiers.set(token, autoVerifier);
} }

View File

@ -158,7 +158,6 @@ export class AutoVerifier<T> {
// you CAN safely call this while another fetch is going on! How convenient // you CAN safely call this while another fetch is going on! How convenient
unverify(): void { unverify(): void {
// lOG.debug(`v#${this.verifierId}: unverifying`);
if (this.primaryPromise) { if (this.primaryPromise) {
this.primaryPromise.catch(e => { this.primaryPromise.catch(e => {
console.warn('caught unverified primary promise', e); console.warn('caught unverified primary promise', e);

View File

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

View File

@ -67,11 +67,11 @@ export default class CombinedGuild
// connect/Disconnect // connect/Disconnect
this.socketGuild.on('connect', () => { this.socketGuild.on('connect', () => {
LOG.info(`g#${this.id} connected`); LOG.info(`g#${this.id} connected`);
ramDiskSocket.unverify(); // make sure to query the disk-socket AutoVerifier again
this.emit('connect'); this.emit('connect');
}); });
this.socketGuild.on('disconnect', async () => { this.socketGuild.on('disconnect', async () => {
LOG.info(`g#${this.id} disconnected`); LOG.info(`g#${this.id} disconnected`);
ramDiskSocket.unverify();
diskSocket.unverify(); diskSocket.unverify();
await personalDB.clearAllMembersStatus(this.id); await personalDB.clearAllMembersStatus(this.id);
this.emit('disconnect'); this.emit('disconnect');

View File

@ -558,7 +558,7 @@ function bindActionEvents(io: socketio.Server, client: socketio.Socket, identity
const message = await DB.insertMessage(identity.guildId, channelId, identity.memberId, text); const message = await DB.insertMessage(identity.guildId, channelId, identity.memberId, text);
LOG.info( LOG.info(
`m#${message.id} ch#${message.channel_id} s@${formatDate(message.sent_dtg)} u#${message.member_id}: ${ `g#${identity.guildId.slice(0, 4)} m#${message.id.slice(0, 4)} ch#${message.channel_id.slice(0, 4)} s@${formatDate(message.sent_dtg)} u#${message.member_id.slice(0, 4)}: ${
message.text message.text
}`, }`,
); );
@ -596,7 +596,7 @@ function bindActionEvents(io: socketio.Server, client: socketio.Socket, identity
resourceName = resourceName.replace(/[^A-Za-z0-9 .]/g, '_'); // only alphanumerics for file names resourceName = resourceName.replace(/[^A-Za-z0-9 .]/g, '_'); // only alphanumerics for file names
LOG.info( LOG.info(
`u#${identity.memberId?.slice(0, 4)}: resource message with resource of size: ${resource.length} bytes`, `u#${identity.memberId.slice(0, 4)}: resource message with resource of size: ${resource.length} bytes`,
); );
// try to get the dimensions of the resource if it is an image so that we can scale it down for the preview // try to get the dimensions of the resource if it is an image so that we can scale it down for the preview