Skip to content

Commit ad22482

Browse files
committed
fix: add toast on rate limit hit for reactions
1 parent b6305b6 commit ad22482

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

_locales/en/messages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@
484484
"clearAllReactions": "Are you sure you want to clear all $emoji$ ?",
485485
"expandedReactionsText": "Show Less",
486486
"reactionNotification": "Reacts to a message with $emoji$",
487+
"rateLimitReactMessage": "Slow down! You've sent too many emoji reacts. Try again soon",
487488
"otherSingular": "$number$ other",
488489
"otherPlural": "$number$ others",
489490
"reactionPopup": "reacted with",

ts/session/apis/open_group_api/sogsv3/sogsV3SendReaction.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Action, OpenGroupReactionResponse, Reaction } from '../../../../types/R
55
import { getEmojiDataFromNative } from '../../../../util/emoji';
66
import { Reactions } from '../../../../util/reactions';
77
import { OnionSending } from '../../../onions/onionSend';
8-
import { UserUtils } from '../../../utils';
8+
import { ToastUtils, UserUtils } from '../../../utils';
99
import { OpenGroupPollingUtils } from '../opengroupV2/OpenGroupPollingUtils';
1010
import { getUsBlindedInThatServer } from './knownBlindedkeys';
1111
import { batchGlobalIsSuccess, parseBatchGlobalStatusCode } from './sogsV3BatchPoll';
@@ -58,6 +58,8 @@ export const sendSogsReactionOnionV4 = async (
5858
}
5959

6060
if (Reactions.hitRateLimit()) {
61+
ToastUtils.pushRateLimitHitReactions();
62+
6163
return false;
6264
}
6365

ts/session/utils/Toast.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,7 @@ export function pushNoMediaUntilApproved() {
277277
export function pushMustBeApproved() {
278278
pushToastError('mustBeApproved', window.i18n('mustBeApproved'));
279279
}
280+
281+
export function pushRateLimitHitReactions() {
282+
pushToastInfo('reactRateLimit', '', window?.i18n?.('rateLimitReactMessage')); // because otherwise test fails
283+
}

ts/types/LocalizerKeys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export type LocalizerKeys =
8787
| 'enterNewPassword'
8888
| 'expandedReactionsText'
8989
| 'openMessageRequestInbox'
90+
| 'rateLimitReactMessage'
9091
| 'enterPassword'
9192
| 'enterSessionIDOfRecipient'
9293
| 'join'

ts/util/reactions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getUsBlindedInThatServer,
77
isUsAnySogsFromCache,
88
} from '../session/apis/open_group_api/sogsv3/knownBlindedkeys';
9-
import { UserUtils } from '../session/utils';
9+
import { ToastUtils, UserUtils } from '../session/utils';
1010

1111
import { Action, OpenGroupReactionList, ReactionList, RecentReactions } from '../types/Reaction';
1212
import { getRecentReactions, saveRecentReations } from '../util/storage';
@@ -17,14 +17,14 @@ const rateTimeLimit = 60 * 1000;
1717
const latestReactionTimestamps: Array<number> = [];
1818

1919
function hitRateLimit(): boolean {
20-
const timestamp = Date.now();
21-
latestReactionTimestamps.push(timestamp);
20+
const now = Date.now();
21+
latestReactionTimestamps.push(now);
2222

2323
if (latestReactionTimestamps.length > rateCountLimit) {
2424
const firstTimestamp = latestReactionTimestamps[0];
25-
if (timestamp - firstTimestamp < rateTimeLimit) {
25+
if (now - firstTimestamp < rateTimeLimit) {
2626
latestReactionTimestamps.pop();
27-
window.log.warn('Only 20 reactions are allowed per minute');
27+
window.log.warn(`Only ${rateCountLimit} reactions are allowed per minute`);
2828
return true;
2929
} else {
3030
latestReactionTimestamps.shift();
@@ -86,6 +86,7 @@ const sendMessageReaction = async (messageId: string, emoji: string) => {
8686
}
8787

8888
if (hitRateLimit()) {
89+
ToastUtils.pushRateLimitHitReactions();
8990
return;
9091
}
9192

0 commit comments

Comments
 (0)