Skip to content

Commit e197c81

Browse files
sravan-sSravan S
andauthored
fix: Add rate limit to markAsDelivered call (#143)
Co-authored-by: Sravan S <sravan@sendbird.com> fixes: https://sendbird.atlassian.net/browse/UIKIT-1718
1 parent 12dcd05 commit e197c81

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/smart-components/ChannelList/index.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { uuidv4 } from '../../utils/uuid';
2727
import './index.scss';
2828

2929
const noop = () => { };
30+
const DELIVERY_RECIPT = 'delivery_receipt';
3031

3132
function ChannelList(props) {
3233
const {
@@ -232,9 +233,20 @@ function ChannelList(props) {
232233
type: channelListActions.FETCH_CHANNELS_SUCCESS,
233234
payload: channelList,
234235
});
235-
if (channelList && typeof channelList.forEach === 'function') {
236+
237+
const canSetMarkAsDelivered = sdk?.appInfo?.premiumFeatureList
238+
?.find((feature) => (feature === DELIVERY_RECIPT));
239+
240+
if (canSetMarkAsDelivered) {
236241
logger.info('ChannelList: Marking all channels as read');
237-
channelList.forEach((c) => c.markAsDelivered());
242+
// eslint-disable-next-line no-unused-expressions
243+
channelList?.forEach((channel, idx) => {
244+
// Plan-based rate limits - minimum limit is 5 requests per second
245+
setTimeout(() => {
246+
// eslint-disable-next-line no-unused-expressions
247+
channel?.markAsDelivered();
248+
}, 500 * idx);
249+
});
238250
}
239251
});
240252
}

src/smart-components/ChannelList/utils.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as channelActions from './dux/actionTypes';
22
import * as topics from '../../lib/pubSub/topics';
33

4+
const DELIVERY_RECIPT = 'delivery_receipt';
45
const createEventHandler = ({
56
sdk,
67
sdkChannelHandlerId,
@@ -217,9 +218,18 @@ function setupChannelList({
217218
type: channelActions.INIT_CHANNELS_SUCCESS,
218219
payload: sorted,
219220
});
220-
if (channelList && typeof channelList.forEach === 'function') {
221-
logger.info('ChannelList - mark all channels as delivered');
222-
channelList.forEach((c) => c.markAsDelivered());
221+
const canSetMarkAsDelivered = sdk?.appInfo?.premiumFeatureList
222+
?.find((feature) => (feature === DELIVERY_RECIPT));
223+
if (canSetMarkAsDelivered) {
224+
logger.info('ChannelList: Marking all channels as read');
225+
// eslint-disable-next-line no-unused-expressions
226+
channelList?.forEach((channel, idx) => {
227+
// Plan-based rate limits - minimum limit is 5 requests per second
228+
setTimeout(() => {
229+
// eslint-disable-next-line no-unused-expressions
230+
channel?.markAsDelivered();
231+
}, 500 * idx);
232+
});
223233
}
224234
});
225235
} else {

0 commit comments

Comments
 (0)