fix: add toast on rate limit hit for reactions

This commit is contained in:
Audric Ackermann 2022-10-07 12:22:15 +11:00
parent bffc9eddd5
commit 135b9cf34d
5 changed files with 15 additions and 6 deletions

View file

@ -484,6 +484,7 @@
"clearAllReactions": "Are you sure you want to clear all $emoji$ ?",
"expandedReactionsText": "Show Less",
"reactionNotification": "Reacts to a message with $emoji$",
"rateLimitReactMessage": "Slow down! You've sent too many emoji reacts. Try again soon",
"otherSingular": "$number$ other",
"otherPlural": "$number$ others",
"reactionPopup": "reacted with",

View file

@ -5,7 +5,7 @@ import { Action, OpenGroupReactionResponse, Reaction } from '../../../../types/R
import { getEmojiDataFromNative } from '../../../../util/emoji';
import { Reactions } from '../../../../util/reactions';
import { OnionSending } from '../../../onions/onionSend';
import { UserUtils } from '../../../utils';
import { ToastUtils, UserUtils } from '../../../utils';
import { OpenGroupPollingUtils } from '../opengroupV2/OpenGroupPollingUtils';
import { getUsBlindedInThatServer } from './knownBlindedkeys';
import { batchGlobalIsSuccess, parseBatchGlobalStatusCode } from './sogsV3BatchPoll';
@ -58,6 +58,8 @@ export const sendSogsReactionOnionV4 = async (
}
if (Reactions.hitRateLimit()) {
ToastUtils.pushRateLimitHitReactions();
return false;
}

View file

@ -277,3 +277,7 @@ export function pushNoMediaUntilApproved() {
export function pushMustBeApproved() {
pushToastError('mustBeApproved', window.i18n('mustBeApproved'));
}
export function pushRateLimitHitReactions() {
pushToastInfo('reactRateLimit', '', window?.i18n?.('rateLimitReactMessage')); // because otherwise test fails
}

View file

@ -87,6 +87,7 @@ export type LocalizerKeys =
| 'enterNewPassword'
| 'expandedReactionsText'
| 'openMessageRequestInbox'
| 'rateLimitReactMessage'
| 'enterPassword'
| 'enterSessionIDOfRecipient'
| 'join'

View file

@ -6,7 +6,7 @@ import {
getUsBlindedInThatServer,
isUsAnySogsFromCache,
} from '../session/apis/open_group_api/sogsv3/knownBlindedkeys';
import { UserUtils } from '../session/utils';
import { ToastUtils, UserUtils } from '../session/utils';
import { Action, OpenGroupReactionList, ReactionList, RecentReactions } from '../types/Reaction';
import { getRecentReactions, saveRecentReations } from '../util/storage';
@ -17,14 +17,14 @@ const rateTimeLimit = 60 * 1000;
const latestReactionTimestamps: Array<number> = [];
function hitRateLimit(): boolean {
const timestamp = Date.now();
latestReactionTimestamps.push(timestamp);
const now = Date.now();
latestReactionTimestamps.push(now);
if (latestReactionTimestamps.length > rateCountLimit) {
const firstTimestamp = latestReactionTimestamps[0];
if (timestamp - firstTimestamp < rateTimeLimit) {
if (now - firstTimestamp < rateTimeLimit) {
latestReactionTimestamps.pop();
window.log.warn('Only 20 reactions are allowed per minute');
window.log.warn(`Only ${rateCountLimit} reactions are allowed per minute`);
return true;
} else {
latestReactionTimestamps.shift();
@ -86,6 +86,7 @@ const sendMessageReaction = async (messageId: string, emoji: string) => {
}
if (hitRateLimit()) {
ToastUtils.pushRateLimitHitReactions();
return;
}