feat: moderator clear all reactions behaviour now uses the cache

pull/2454/head
William Grant 3 years ago
parent 5ebd1775c0
commit dde61bb35b

@ -3,9 +3,8 @@ import React, { ReactElement, useEffect, useState } from 'react';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import styled from 'styled-components'; import styled from 'styled-components';
import { Data } from '../../data/data'; import { Data } from '../../data/data';
import { useMessageReactsPropsById } from '../../hooks/useParamSelector'; import { useMessageReactsPropsById, useWeAreModerator } from '../../hooks/useParamSelector';
import { isUsAnySogsFromCache } from '../../session/apis/open_group_api/sogsv3/knownBlindedkeys'; import { isUsAnySogsFromCache } from '../../session/apis/open_group_api/sogsv3/knownBlindedkeys';
import { getConversationController } from '../../session/conversations';
import { UserUtils } from '../../session/utils'; import { UserUtils } from '../../session/utils';
import { import {
updateReactClearAllModal, updateReactClearAllModal,
@ -285,9 +284,7 @@ export const ReactListModal = (props: Props): ReactElement => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const { convoId, isPublic } = msgProps; const { convoId, isPublic } = msgProps;
const weAreModerator = useWeAreModerator(convoId);
const convo = getConversationController().get(convoId);
const weAreModerator = convo.getConversationModelProps().weAreModerator;
const handleSelectedReaction = (emoji: string): boolean => { const handleSelectedReaction = (emoji: string): boolean => {
return currentReact === emoji; return currentReact === emoji;

@ -1,4 +1,5 @@
import AbortController from 'abort-controller'; import AbortController from 'abort-controller';
import { OpenGroupReactionResponse } from '../../../../types/Reaction';
import { OpenGroupRequestCommonType } from '../opengroupV2/ApiUtil'; import { OpenGroupRequestCommonType } from '../opengroupV2/ApiUtil';
import { import {
batchFirstSubIsSuccess, batchFirstSubIsSuccess,
@ -6,6 +7,12 @@ import {
OpenGroupBatchRow, OpenGroupBatchRow,
sogsBatchSend, sogsBatchSend,
} from './sogsV3BatchPoll'; } from './sogsV3BatchPoll';
import {
addToMutationCache,
ChangeType,
SogsV3Mutation,
updateMutationCache,
} from './sogsV3MutationCache';
import { hasReactionSupport } from './sogsV3SendReaction'; import { hasReactionSupport } from './sogsV3SendReaction';
/** /**
@ -23,6 +30,20 @@ export const clearSogsReactionByServerId = async (
return false; return false;
} }
const cacheEntry: SogsV3Mutation = {
server: roomInfos.serverUrl,
room: roomInfos.roomId,
changeType: ChangeType.REACTIONS,
seqno: null,
metadata: {
messageId: serverId,
emoji: reaction,
action: 'CLEAR',
},
};
addToMutationCache(cacheEntry);
const options: Array<OpenGroupBatchRow> = [ const options: Array<OpenGroupBatchRow> = [
{ {
type: 'deleteReaction', type: 'deleteReaction',
@ -37,8 +58,22 @@ export const clearSogsReactionByServerId = async (
'batch' 'batch'
); );
if (!result) {
throw new Error('Could not deleteReaction, res is invalid');
}
const rawMessage = (result.body && (result.body[0].body as OpenGroupReactionResponse)) || null;
if (!rawMessage) {
throw new Error('deleteReaction parsing failed');
}
try { try {
return batchGlobalIsSuccess(result) && batchFirstSubIsSuccess(result); if (batchGlobalIsSuccess(result) && batchFirstSubIsSuccess(result)) {
updateMutationCache(cacheEntry, rawMessage.seqno);
return true;
} else {
return false;
}
} catch (e) { } catch (e) {
window?.log?.error("clearSogsReactionByServerId Can't decode JSON body"); window?.log?.error("clearSogsReactionByServerId Can't decode JSON body");
} }

@ -11,7 +11,7 @@ export enum ChangeType {
REACTIONS = 0, REACTIONS = 0,
} }
type ReactionAction = 'ADD' | 'REMOVE'; type ReactionAction = 'ADD' | 'REMOVE' | 'CLEAR';
type ReactionChange = { type ReactionChange = {
messageId: number; // will be serverId of the reacted message messageId: number; // will be serverId of the reacted message
@ -73,16 +73,19 @@ export async function processMessagesUsingCache(
message: OpenGroupMessageV4 message: OpenGroupMessageV4
) { ) {
const updatedReactions = message.reactions; const updatedReactions = message.reactions;
const matches: Array<SogsV3Mutation> = filter(sogsMutationCache, { server, room }); const roomMatches: Array<SogsV3Mutation> = filter(sogsMutationCache, { server, room });
if (matches?.length) { if (roomMatches?.length) {
for (const match of matches) { for (const roomMatch of roomMatches) {
if (message.seqno && match.seqno && match.seqno <= message.seqno) { if (message.seqno && roomMatch.seqno && roomMatch.seqno <= message.seqno) {
const removedEntry = remove(sogsMutationCache, match); const removedEntry = remove(sogsMutationCache, roomMatch);
window.log.info('SOGS Mutation Cache: Entry ignored and removed!', removedEntry); window.log.info('SOGS Mutation Cache: Entry ignored and removed!', removedEntry);
} else if (!message.seqno || (message.seqno && match.seqno && match.seqno > message.seqno)) { } else if (
!message.seqno ||
(message.seqno && roomMatch.seqno && roomMatch.seqno > message.seqno)
) {
for (const reaction of Object.keys(message.reactions)) { for (const reaction of Object.keys(message.reactions)) {
const _matches = filter(sogsMutationCache, { const reactionMatches = filter(sogsMutationCache, {
server, server,
room, room,
changeType: ChangeType.REACTIONS, changeType: ChangeType.REACTIONS,
@ -91,9 +94,9 @@ export async function processMessagesUsingCache(
emoji: reaction, emoji: reaction,
}, },
}); });
if (_matches?.length) { if (reactionMatches?.length) {
for (const match of _matches) { for (const reactionMatch of reactionMatches) {
switch (match.metadata.action) { switch (reactionMatch.metadata.action) {
case 'ADD': case 'ADD':
updatedReactions[reaction].you = true; updatedReactions[reaction].you = true;
updatedReactions[reaction].count += 1; updatedReactions[reaction].count += 1;
@ -113,13 +116,12 @@ export async function processMessagesUsingCache(
default: default:
window.log.warn( window.log.warn(
'SOGS Mutation Cache: Unsupported metadata action in OpenGroupMessageV4', 'SOGS Mutation Cache: Unsupported metadata action in OpenGroupMessageV4',
match reactionMatch
); );
break;
} }
} }
const removedMatches = remove(sogsMutationCache, ...matches); const removedMatches = remove(sogsMutationCache, ...roomMatches);
window.log.info( window.log.info(
'SOGS Mutation Cache: Removed processed entries from cache!', 'SOGS Mutation Cache: Removed processed entries from cache!',
removedMatches removedMatches

Loading…
Cancel
Save