Skip to content

Commit 458f305

Browse files
Mohit TejaniMohit Tejani
authored andcommitted
revrted nextOffset calculation of hereNow
1 parent c742764 commit 458f305

File tree

4 files changed

+6
-307
lines changed

4 files changed

+6
-307
lines changed

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -340,43 +340,23 @@ class HereNowResult extends Result {
340340
final int totalOccupancy;
341341
final int totalChannels;
342342

343-
int? nextOffset;
344-
345-
HereNowResult._(this.channels, this.totalOccupancy, this.totalChannels,
346-
{this.nextOffset});
343+
HereNowResult._(this.channels, this.totalOccupancy, this.totalChannels);
347344

348345
factory HereNowResult.fromJson(Map<String, dynamic> object,
349-
{String? channelName,
350-
int? limit = MAXIMUM_COUNT,
351-
int? offset = DEFAULT_OFFSET}) {
352-
int? nextOffset;
346+
{String? channelName}) {
353347
var result = DefaultResult.fromJson(object);
354348
if (result.otherKeys.containsKey('payload')) {
355349
var payload = result.otherKeys['payload'] as Map<String, dynamic>;
356350
var channelsOccupancies = (payload['channels'] as Map<String, dynamic>)
357351
.map((key, value) => MapEntry(key,
358352
ChannelOccupancy.fromJson(key, value as Map<String, dynamic>)));
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;
365-
}
366353
return HereNowResult._(channelsOccupancies,
367-
payload['total_occupancy'] as int, payload['total_channels'] as int,
368-
nextOffset: nextOffset);
354+
payload['total_occupancy'] as int, payload['total_channels'] as int);
369355
} else {
370356
var channelOccupancy =
371357
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;
376-
}
377-
return HereNowResult._({
378-
channelName: channelOccupancy
379-
}, result.otherKeys['occupancy'] as int, 1, nextOffset: nextOffset);
358+
return HereNowResult._({channelName: channelOccupancy},
359+
result.otherKeys['occupancy'] as int, 1);
380360
}
381361
}
382362
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ mixin PresenceDx on Core {
5757
core: this,
5858
params: params,
5959
serialize: (object, [_]) => HereNowResult.fromJson(object,
60-
channelName: (channels.length == 1) ? channels.first : null,
61-
limit: limit,
62-
offset: offset));
60+
channelName: (channels.length == 1) ? channels.first : null));
6361
}
6462

6563
/// Announce in [channels] and [channelGroups] that a device linked to the UUID in the keyset left.

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

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -169,171 +169,3 @@ const _hereNowMixedStateResponse = '''
169169
]
170170
}
171171
''';
172-
173-
const _hereNowWithNextOffsetNeededResponse = '''
174-
{
175-
"message": "OK",
176-
"payload": {
177-
"channels": {
178-
"my_channel": {
179-
"occupancy": 3,
180-
"uuids": [
181-
"0a9e2e6a-1a36-4299-aa18-4d349e0019e5",
182-
"24a51c02-f0bd-4f6a-b2cb-42dcf6ac6138"
183-
]
184-
},
185-
"test": {
186-
"occupancy": 4,
187-
"uuids": [
188-
"93ce8247-223e-410d-a612-8e21c7ecb54a",
189-
"7e4c3056-1ea8-437e-bf68-253926a99309",
190-
"32f3fafb-3406-4ed0-aed5-1cdf138d9706"
191-
]
192-
}
193-
},
194-
"total_channels": 2,
195-
"total_occupancy": 7
196-
},
197-
"service": "Presence",
198-
"status": 200
199-
}
200-
''';
201-
202-
const _hereNowWithOutNextOffsetLastPage = '''
203-
{
204-
"message": "OK",
205-
"payload": {
206-
"channels": {
207-
"my_channel": {
208-
"occupancy": 3,
209-
"uuids": [
210-
"0a9e2e6a-1a36-4299-aa18-4d349e0019e5",
211-
"24a51c02-f0bd-4f6a-b2cb-42dcf6ac6138"
212-
]
213-
},
214-
"test": {
215-
"occupancy": 4,
216-
"uuids": [
217-
"93ce8247-223e-410d-a612-8e21c7ecb54a",
218-
"7e4c3056-1ea8-437e-bf68-253926a99309",
219-
"32f3fafb-3406-4ed0-aed5-1cdf138d9706"
220-
]
221-
}
222-
},
223-
"total_channels": 2,
224-
"total_occupancy": 7
225-
},
226-
"service": "Presence",
227-
"status": 200
228-
}
229-
''';
230-
231-
const _hereNowWithOffsetRequired = '''
232-
{
233-
"message": "OK",
234-
"payload": {
235-
"channels": {
236-
"my_channel": {
237-
"occupancy": 3,
238-
"uuids": [
239-
"0a9e2e6a-1a36-4299-aa18-4d349e0019e5",
240-
"24a51c02-f0bd-4f6a-b2cb-42dcf6ac6138"
241-
]
242-
},
243-
"test": {
244-
"occupancy": 4,
245-
"uuids": [
246-
"93ce8247-223e-410d-a612-8e21c7ecb54a",
247-
"7e4c3056-1ea8-437e-bf68-253926a99309"
248-
]
249-
}
250-
},
251-
"total_channels": 2,
252-
"total_occupancy": 7
253-
},
254-
"service": "Presence",
255-
"status": 200
256-
}
257-
''';
258-
259-
const _hereNowWithOffsetRequiredUnevenCount = '''
260-
{
261-
"message": "OK",
262-
"payload": {
263-
"channels": {
264-
"my_channel": {
265-
"occupancy": 3,
266-
"uuids": [
267-
"0a9e2e6a-1a36-4299-aa18-4d349e0019e5",
268-
"24a51c02-f0bd-4f6a-b2cb-42dcf6ac6138"
269-
]
270-
},
271-
"test": {
272-
"occupancy": 4,
273-
"uuids": [
274-
"93ce8247-223e-410d-a612-8e21c7ecb54a",
275-
"7e4c3056-1ea8-437e-bf68-253926a99309"
276-
]
277-
}
278-
},
279-
"total_channels": 2,
280-
"total_occupancy": 7
281-
},
282-
"service": "Presence",
283-
"status": 200
284-
}
285-
''';
286-
287-
const _hereNowSingleChannelResponse4Occupancies = '''
288-
{
289-
"message": "OK",
290-
"occupancy": 4,
291-
"service": "Presence",
292-
"status": 200,
293-
"uuids": [
294-
"93ce8247-223e-410d-a612-8e21c7ecb54a",
295-
"7e4c3056-1ea8-437e-bf68-253926a99309",
296-
"32f3fafb-3406-4ed0-aed5-1cdf138d9706",
297-
"5125atqy-3406-4ed0-aed5-1cdf138d9706"
298-
]
299-
}
300-
''';
301-
302-
const _hereNowSingleChannelResponse4OccupanciesLimit2 = '''
303-
{
304-
"message": "OK",
305-
"occupancy": 4,
306-
"service": "Presence",
307-
"status": 200,
308-
"uuids": [
309-
"93ce8247-223e-410d-a612-8e21c7ecb54a",
310-
"7e4c3056-1ea8-437e-bf68-253926a99309"
311-
]
312-
}
313-
''';
314-
315-
const _hereNowSingleChannelResponse4OccupanciesLimit2Offset1 = '''
316-
{
317-
"message": "OK",
318-
"occupancy": 4,
319-
"service": "Presence",
320-
"status": 200,
321-
"uuids": [
322-
"7e4c3056-1ea8-437e-bf68-253926a99309",
323-
"32f3fafb-3406-4ed0-aed5-1cdf138d9706"
324-
]
325-
}
326-
''';
327-
328-
const _hereNowSingleChannelsResponseMatchesLimit = '''
329-
{
330-
"message": "OK",
331-
"occupancy": 2,
332-
"service": "Presence",
333-
"status": 200,
334-
"uuids": [
335-
"7e4c3056-1ea8-437e-bf68-253926a99309",
336-
"32f3fafb-3406-4ed0-aed5-1cdf138d9706"
337-
]
338-
}
339-
''';

pubnub/test/unit/dx/presence_test.dart

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -218,117 +218,6 @@ void main() {
218218
throwsA(TypeMatcher<PubNubException>()),
219219
);
220220
});
221-
222-
test('hereNow without offset parameter', () async {
223-
when(
224-
method: 'GET',
225-
path:
226-
'/v2/presence/sub_key/demo/channel/test,my_channel?uuid=test&disable_uuids=0&limit=1000',
227-
).then(status: 200, body: _hereNowWithNextOffsetNeededResponse);
228-
229-
final hereNowResponse = await pubnub!.hereNow(
230-
channels: {'test', 'my_channel'},
231-
);
232-
expect(hereNowResponse.nextOffset, equals(3));
233-
});
234-
});
235-
236-
test('hereNow with limit and offset parameter', () async {
237-
when(
238-
method: 'GET',
239-
path:
240-
'/v2/presence/sub_key/demo/channel/test,my_channel?uuid=test&disable_uuids=0&limit=3&offset=1',
241-
).then(status: 200, body: _hereNowWithOutNextOffsetLastPage);
242-
243-
final hereNowResponse = await pubnub!.hereNow(
244-
channels: {'test', 'my_channel'},
245-
limit: 3,
246-
offset: 1,
247-
);
248-
expect(hereNowResponse.nextOffset, equals(null));
249-
});
250-
251-
test('hereNow with less limit value parameter', () async {
252-
when(
253-
method: 'GET',
254-
path:
255-
'/v2/presence/sub_key/demo/channel/test,my_channel?uuid=test&disable_uuids=0&limit=2',
256-
).then(status: 200, body: _hereNowWithOffsetRequired);
257-
258-
final hereNowResponse =
259-
await pubnub!.hereNow(channels: {'test', 'my_channel'}, limit: 2);
260-
expect(hereNowResponse.nextOffset, equals(2));
261-
});
262-
263-
test(
264-
'hereNow with multichannel less limit value parameter, uneven presence',
265-
() async {
266-
when(
267-
method: 'GET',
268-
path:
269-
'/v2/presence/sub_key/demo/channel/test,my_channel?uuid=test&disable_uuids=0&limit=2&offset=1',
270-
).then(status: 200, body: _hereNowWithOffsetRequiredUnevenCount);
271-
272-
final hereNowResponse = await pubnub!
273-
.hereNow(channels: {'test', 'my_channel'}, limit: 2, offset: 1);
274-
expect(hereNowResponse.nextOffset, equals(3));
275-
});
276-
277-
test('hereNow with single channel default limit offset', () async {
278-
when(
279-
method: 'GET',
280-
path:
281-
'/v2/presence/sub_key/demo/channel/test?uuid=test&disable_uuids=0&limit=1000',
282-
).then(status: 200, body: _hereNowSingleChannelResponse4Occupancies);
283-
284-
final hereNowResponse = await pubnub!.hereNow(channels: {'test'});
285-
expect(hereNowResponse.nextOffset, equals(null));
286-
expect(hereNowResponse.totalOccupancy, equals(4));
287-
});
288-
289-
test('hereNow with single channel, nextOffset in the response', () async {
290-
when(
291-
method: 'GET',
292-
path:
293-
'/v2/presence/sub_key/demo/channel/test?uuid=test&disable_uuids=0&limit=2',
294-
).then(
295-
status: 200, body: _hereNowSingleChannelResponse4OccupanciesLimit2);
296-
297-
final hereNowResponse =
298-
await pubnub!.hereNow(channels: {'test'}, limit: 2);
299-
expect(hereNowResponse.nextOffset, equals(2));
300-
expect(hereNowResponse.totalOccupancy, equals(4));
301-
});
302-
303-
test('hereNow with single channel with offset, nextOffset in the response',
304-
() async {
305-
when(
306-
method: 'GET',
307-
path:
308-
'/v2/presence/sub_key/demo/channel/test?uuid=test&disable_uuids=0&limit=2&offset=1',
309-
).then(
310-
status: 200,
311-
body: _hereNowSingleChannelResponse4OccupanciesLimit2Offset1);
312-
313-
final hereNowResponse =
314-
await pubnub!.hereNow(channels: {'test'}, limit: 2, offset: 1);
315-
expect(hereNowResponse.nextOffset, equals(3));
316-
expect(hereNowResponse.totalOccupancy, equals(4));
317-
});
318-
319-
test(
320-
'hereNow with single channel in the response exact users count matches with limit',
321-
() async {
322-
when(
323-
method: 'GET',
324-
path:
325-
'/v2/presence/sub_key/demo/channel/test?uuid=test&disable_uuids=0&limit=2',
326-
).then(status: 200, body: _hereNowSingleChannelsResponseMatchesLimit);
327-
328-
final hereNowResponse =
329-
await pubnub!.hereNow(channels: {'test'}, limit: 2);
330-
expect(hereNowResponse.nextOffset, equals(null));
331-
expect(hereNowResponse.totalOccupancy, equals(2));
332221
});
333222

334223
group('HereNowParams', () {

0 commit comments

Comments
 (0)