From 0d5c0959db11f422b9be1ca68dbc69b0f4d704e8 Mon Sep 17 00:00:00 2001 From: Nitanshu Vashistha Date: Sun, 28 Oct 2018 23:56:21 +0530 Subject: [PATCH] gitter: Fix occupants for GitterRoom The limit parameter was not used which defaults to 30 and returns maximum 100 users at a time. Hence, it fails to return all the members in the room. In order to tackle this, we need to iterate by skipping 100 members with every API request. Also users count needs to be updated everytime list of occupants is requested as new users may join a room in realtime. Closes https://github.com/errbotio/err-backend-gitter/issues/38 --- gitter.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gitter.py b/gitter.py index cec08ad..cb43c22 100644 --- a/gitter.py +++ b/gitter.py @@ -205,12 +205,22 @@ def topic(self): else: raise RoomNotFoundError("Cannot find the room '{}'".format(self.uri)) + def _user_count(self): + json_rooms = self._backend.readAPIRequest('rooms') + for json_room in json_rooms: + if json_room['id'] is self.idd: + return json_room['userCount'] + @property def occupants(self): occupants = [] - json_users = self._backend.readAPIRequest('rooms/%s/users' % self._idd) - for json_user in json_users: - occupants.append(GitterRoomOccupant.build_from_json(self, json_user)) + user_count = self._user_count() + for skip in range(0, user_count, 100): + # 100 is the maximum number of users Gitter API can return. + json_users = self._backend.readAPIRequest( + 'rooms/%s/users?skip={}?limit={}'.format(self.idd, skip, 100)) + for json_user in json_users: + occupants.append(GitterRoomOccupant.build_from_json(self, json_user)) return occupants def __eq_(self, other):