Skip to content

Commit c742764

Browse files
Mohit TejaniMohit Tejani
authored andcommitted
updated nextOffset calculation for hereNow to consider currently fetched users count
1 parent 7fbb679 commit c742764

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

pubnub/lib/src/dx/_endpoints/presence.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,21 +356,26 @@ class HereNowResult extends Result {
356356
var channelsOccupancies = (payload['channels'] as Map<String, dynamic>)
357357
.map((key, value) => MapEntry(key,
358358
ChannelOccupancy.fromJson(key, value as Map<String, dynamic>)));
359-
var maxOccupancy = channelsOccupancies.values
360-
.map((channel) => channel.count)
361-
.reduce((a, b) => a > b ? a : b);
362-
if ((offset! + limit!) < maxOccupancy) {
363-
nextOffset = offset + limit;
359+
var maxOccupancyChannel = channelsOccupancies.values
360+
.reduce((a, b) => a.count > b.count ? a : b);
361+
362+
if ((offset! + maxOccupancyChannel.uuids.length) <
363+
maxOccupancyChannel.count) {
364+
nextOffset = offset + maxOccupancyChannel.uuids.length;
364365
}
365366
return HereNowResult._(channelsOccupancies,
366367
payload['total_occupancy'] as int, payload['total_channels'] as int,
367368
nextOffset: nextOffset);
368369
} else {
369-
if (result.otherKeys['occupancy'] as int > (limit! + offset!)) {
370-
nextOffset = limit + offset;
370+
var channelOccupancy =
371+
ChannelOccupancy.fromJson(channelName!, result.otherKeys);
372+
var fetchedOccupanciesCount = channelOccupancy.uuids.length;
373+
if (result.otherKeys['occupancy'] as int >
374+
(fetchedOccupanciesCount + offset!)) {
375+
nextOffset = fetchedOccupanciesCount + offset;
371376
}
372377
return HereNowResult._({
373-
channelName: ChannelOccupancy.fromJson(channelName!, result.otherKeys)
378+
channelName: channelOccupancy
374379
}, result.otherKeys['occupancy'] as int, 1, nextOffset: nextOffset);
375380
}
376381
}

pubnub/test/integration/channel_groups/channel_groups_test.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,22 @@ void main() {
256256

257257
// 2. Subscribe to Group - Create subscription to channel group
258258
var subscription = pubnub!.subscribe(channelGroups: {groupName});
259-
259+
260260
// Wait for subscription to actually start listening
261261
await subscription.whenStarts;
262-
await Future.delayed(Duration(seconds: 2)); // Additional buffer for subscription to be fully ready
262+
await Future.delayed(Duration(
263+
seconds: 2)); // Additional buffer for subscription to be fully ready
263264

264265
// 3. Setup Message Listener - Set up listener BEFORE publishing
265266
var messageReceived = false;
266267
var messageCompleter = Completer<void>();
267268
var testMessage = 'Hello from channel group integration test!';
268269

269270
var testChannel = 'test_ch1_$randomSuffix';
270-
271+
271272
subscription.messages.listen((envelope) {
272-
if (envelope.payload == testMessage && envelope.channel == testChannel) {
273+
if (envelope.payload == testMessage &&
274+
envelope.channel == testChannel) {
273275
messageReceived = true;
274276
if (!messageCompleter.isCompleted) {
275277
messageCompleter.complete();
@@ -286,18 +288,24 @@ void main() {
286288
} catch (e) {
287289
// Timeout occurred
288290
}
289-
291+
290292
expect(messageReceived, isTrue,
291293
reason:
292294
'Message should be received through channel group subscription');
293295

294296
// 5. Group Modification - Add/remove channels and verify subscription updates
295-
await pubnub!.channelGroups.addChannels(groupName, {'test_ch3_$randomSuffix'});
297+
await pubnub!.channelGroups
298+
.addChannels(groupName, {'test_ch3_$randomSuffix'});
296299
await waitForConsistency();
297300

298301
var listResult = await pubnub!.channelGroups.listChannels(groupName);
299-
expect(listResult.channels,
300-
containsAll({'test_ch1_$randomSuffix', 'test_ch2_$randomSuffix', 'test_ch3_$randomSuffix'}));
302+
expect(
303+
listResult.channels,
304+
containsAll({
305+
'test_ch1_$randomSuffix',
306+
'test_ch2_$randomSuffix',
307+
'test_ch3_$randomSuffix'
308+
}));
301309

302310
await subscription.cancel();
303311
}, timeout: Timeout(Duration(seconds: 45)));

pubnub/test/unit/dx/fixtures/presence.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ const _hereNowMixedStateResponse = '''
170170
}
171171
''';
172172

173-
const _hereNowWithOutNextOffsetResponse = '''
173+
const _hereNowWithNextOffsetNeededResponse = '''
174174
{
175175
"message": "OK",
176176
"payload": {

pubnub/test/unit/dx/presence_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ void main() {
224224
method: 'GET',
225225
path:
226226
'/v2/presence/sub_key/demo/channel/test,my_channel?uuid=test&disable_uuids=0&limit=1000',
227-
).then(status: 200, body: _hereNowWithOutNextOffsetResponse);
227+
).then(status: 200, body: _hereNowWithNextOffsetNeededResponse);
228228

229229
final hereNowResponse = await pubnub!.hereNow(
230230
channels: {'test', 'my_channel'},
231231
);
232-
expect(hereNowResponse.nextOffset, equals(null));
232+
expect(hereNowResponse.nextOffset, equals(3));
233233
});
234234
});
235235

0 commit comments

Comments
 (0)