Skip to content

Commit

Permalink
gitter: Fix occupants for GitterRoom
Browse files Browse the repository at this point in the history
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 errbotio#38
  • Loading branch information
nvzard committed Mar 12, 2019
1 parent 14a428b commit 6d7fe49
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions gitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,13 @@ class GitterRoom(Room):
def invite(self, *args) -> None:
pass

def __init__(self, backend, idd, uri, name):
def __init__(self, backend, idd, uri, name, user_count):
self._backend = backend
self._name = name
self._uri = uri
self._idd = idd
self._joined = False
self._user_count = user_count

def join(self, username=None, password=None):
log.debug("Joining room %s (%s)" % (self._uri, self._idd))
Expand All @@ -185,6 +186,10 @@ def name(self):
def joined(self):
return self._joined

@property
def user_count(self):
return self._user_count

exists = True # TODO

def destroy(self):
Expand All @@ -205,12 +210,22 @@ def topic(self):
else:
raise RoomNotFoundError("Cannot find the room '{}'".format(self.uri))

def _update_user_count(self):
json_rooms = self._backend.readAPIRequest('rooms')
for json_room in json_rooms:
if json_room['id'] is self.idd:
self._user_count = 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))
self._update_user_count()
for skip in range(0, self.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):
Expand Down Expand Up @@ -369,7 +384,7 @@ def rooms(self):
if not json_room['oneToOne']:
log.debug("found room %s (%s)" % (json_room['name'], json_room['uri']))
rooms.append(GitterRoom(self, json_room['id'], json_room['uri'],
json_room['name']))
json_room['name'], json_room['userCount']))
return rooms

def contacts(self):
Expand Down

0 comments on commit 6d7fe49

Please sign in to comment.