feat: created a cache for opengroups reactions, we can add and update them

This commit is contained in:
William Grant 2022-08-26 16:51:48 +10:00
parent 2a94f54039
commit e409c7ca82
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,68 @@
/**
* This is strictly use to resolve conflicts between local state and the opengroup poll updates
* Currently only supports message reactions 26/08/2022
*/
import { findIndex } from 'lodash';
export enum ChangeType {
REACTIONS = 0,
}
type ReactionAction = 'ADD' | 'REMOVE';
type ReactionChange = {
messageId: number; // will be serverId of the reacted message
emoji: string;
action: ReactionAction;
};
export type SogsV3Mutation = {
seqno: number | null; // null until mutating API request returns
server: string; // server address
room: string; // room name
changeType: ChangeType;
metadata: ReactionChange; // For now we only support message reactions
};
// we don't want to export this, we want to export functions that manipulate it
const sogsMutationCache: Array<SogsV3Mutation> = [];
function verifyEntry(entry: SogsV3Mutation): boolean {
return Boolean(
!entry.server ||
!entry.room ||
entry.seqno !== null ||
entry.metadata.messageId ||
entry.metadata.emoji ||
entry.metadata.action === 'ADD' ||
entry.metadata.action === 'REMOVE'
);
}
export function addToMutationCache(entry: SogsV3Mutation) {
if (!verifyEntry(entry)) {
window.log.error('SOGS Mutation Cache: Entry verification failed!');
} else {
sogsMutationCache.push(entry);
window.log.info('SOGS Mutation Cache: Entry added!', entry);
}
}
export function updateMutationCache(entry: SogsV3Mutation) {
if (!verifyEntry(entry)) {
window.log.error('SOGS Mutation Cache: Entry verification failed!');
} else {
const entryIndex = findIndex(sogsMutationCache, entry);
if (entryIndex >= 0) {
sogsMutationCache[entryIndex] = entry;
window.log.info('SOGS Mutation Cache: Entry updated!', entry);
} else {
window.log.error('SOGS Mutation Cache: Updated failed! Cannot find entry');
}
}
}
export function removeFromMutationCache() {
// TODO
}

View File

@ -6,6 +6,12 @@ import { hitRateLimit } from '../../../../util/reactions';
import { OnionSending } from '../../../onions/onionSend';
import { OpenGroupPollingUtils } from '../opengroupV2/OpenGroupPollingUtils';
import { batchGlobalIsSuccess, parseBatchGlobalStatusCode } from './sogsV3BatchPoll';
import {
addToMutationCache,
ChangeType,
SogsV3Mutation,
updateMutationCache,
} from './sogsV3MutationCache';
export const hasReactionSupport = async (serverId: number): Promise<boolean> => {
const found = await Data.getMessageByServerId(serverId);
@ -57,6 +63,20 @@ export const sendSogsReactionOnionV4 = async (
const method = reaction.action === Action.REACT ? 'PUT' : 'DELETE';
const serverPubkey = allValidRoomInfos[0].serverPublicKey;
const cacheEntry: SogsV3Mutation = {
server: serverUrl,
room: room,
changeType: ChangeType.REACTIONS,
seqno: null,
metadata: {
messageId: reaction.id,
emoji,
action: reaction.action === Action.REACT ? 'ADD' : 'REMOVE',
},
};
addToMutationCache(cacheEntry);
// reaction endpoint requires an empty dict {}
const stringifiedBody = null;
const result = await OnionSending.sendJsonViaOnionV4ToSogs({
@ -93,5 +113,11 @@ export const sendSogsReactionOnionV4 = async (
`reaction on ${serverUrl}/${room}`
);
const success = Boolean(reaction.action === Action.REACT ? rawMessage.added : rawMessage.removed);
if (success && rawMessage.seqno) {
cacheEntry.seqno = rawMessage.seqno;
updateMutationCache(cacheEntry);
}
return success;
};

View File

@ -145,4 +145,5 @@ export type OpenGroupReactionList = Record<string, OpenGroupReaction>;
export interface OpenGroupReactionResponse {
added?: boolean;
removed?: boolean;
seqno: number;
}