Merge pull request #2539 from Bilb/add-toast-react-rate-limit

fix: add toast on rate limit hit for reactions
pull/2525/head
Audric Ackermann 3 years ago committed by GitHub
commit e2c3ccef84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

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

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

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

Loading…
Cancel
Save