From 1f37277a8dc5dc140722266df42614de6b5b42c0 Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sat, 7 Oct 2017 04:16:07 +1100 Subject: [PATCH 1/9] started adding rooms --- fbchat/client.py | 7 +++++++ fbchat/graphql.py | 22 +++++++++++++++++++++- fbchat/models.py | 23 +++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/fbchat/client.py b/fbchat/client.py index 778849a..e23e24f 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -553,6 +553,7 @@ class Client(object): elif node['__typename'] == 'Group': # We don't handle Facebook "Groups" pass + # TODO Add Rooms else: log.warning('Unknown __typename: {} in {}'.format(repr(node['__typename']), node)) @@ -707,6 +708,9 @@ class Client(object): if entry.get('thread_type') == 'GROUP': _id = entry['thread_key']['thread_fbid'] rtn[_id] = graphql_to_group(entry) + elif entry.get('thread_type') == 'ROOM': + _id = entry['thread_key']['thread_fbid'] + rtn[_id] = graphql_to_room(entry) elif entry.get('thread_type') == 'ONE_TO_ONE': _id = entry['thread_key']['other_user_id'] if pages_and_users.get(_id) is None: @@ -791,6 +795,9 @@ class Client(object): entries.append(participants[k['other_user_fbid']]) elif k['thread_type'] == 2: entries.append(Group(k['thread_fbid'], participants=set([p.strip('fbid:') for p in k['participants']]), photo=k['image_src'], name=k['name'], message_count=k['message_count'])) + # TODO + elif k['thread_type'] == 4: + entries.append(Group(k['thread_fbid'], participants=set([p.strip('fbid:') for p in k['participants']]), photo=k['image_src'], name=k['name'], message_count=k['message_count'])) else: raise FBchatException('A thread had an unknown thread type: {}'.format(k)) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 857d19e..821cdb4 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -42,7 +42,7 @@ def get_customization_info(thread): 'emoji': info.get('emoji'), 'color': graphql_color_to_enum(info.get('outgoing_bubble_color')) } - if thread.get('thread_type') == 'GROUP' or thread.get('is_group_thread') or thread.get('thread_key', {}).get('thread_fbid'): + if thread.get('thread_type') in ('GROUP', 'ROOM') or thread.get('is_group_thread') or thread.get('thread_key', {}).get('thread_fbid'): rtn['nicknames'] = {} for k in info.get('participant_customizations', []): rtn['nicknames'][k['participant_id']] = k.get('nickname') @@ -118,6 +118,26 @@ def graphql_to_group(group): message_count=group.get('messages_count') ) +def graphql_to_room(room): + if room.get('image') is None: + room['image'] = {} + c_info = get_customization_info(room) + return Room( + room['thread_key']['thread_fbid'], + participants=set([node['messaging_actor']['id'] for node in room['all_participants']['nodes']]), + nicknames=c_info.get('nicknames'), + color=c_info.get('color'), + emoji=c_info.get('emoji'), + photo=room['image'].get('uri'), + name=room.get('name'), + message_count=room.get('messages_count'), + admins = set([node.get('id') for node in room.get('thread_admins')]), + approval_mode = bool(room.get('approval_mode')), + approval_requests = set([node.get('id') for node in room['thread_queue_metatdata'].get('approval_requests')), + join_link = room['joinable_mode'].get('link'), + privacy_mode = bool(room.get('privacy_mode')), + ) + def graphql_to_page(page): if page.get('profile_picture') is None: page['profile_picture'] = {} diff --git a/fbchat/models.py b/fbchat/models.py index c61ceb1..613629f 100644 --- a/fbchat/models.py +++ b/fbchat/models.py @@ -109,6 +109,28 @@ class Group(Thread): self.emoji = emoji +class Room(Group): + # Set containing user IDs of thread admins + admins = set + # True if users need approval to join + approval_mode = bool + # Set containing user IDs requesting to join + approval_requests = set + # Link for joining room + join_link = str + # True is room is not discoverable + privacy_mode = bool + + def __init__(self, uid, admins=set(), approval_mode=None, approval_requests=set(), join_link=None, privacy_mode=None, **kwargs): + """Represents a Facebook room. Inherits `Group`""" + super(Room, self).__init__(ThreadType.ROOM, uid, **kwargs) + self.admins = admins + self.approval_mode = approval_mode + self.approval_requests = approval_requests + self.join_link = join_link + self.privacy_mode = privacy_mode + + class Page(Thread): #: The page's custom url url = str @@ -192,6 +214,7 @@ class ThreadType(Enum): USER = 1 GROUP = 2 PAGE = 3 + ROOM = 4 class TypingStatus(Enum): """Used to specify whether the user is typing or has stopped typing""" From d52dac233e0894bb304c3d9062f5472e4dd42945 Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sat, 7 Oct 2017 15:03:59 +1100 Subject: [PATCH 2/9] made appropriate changes to default args of rooms --- fbchat/graphql.py | 14 ++------------ fbchat/models.py | 6 +++++- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 821cdb4..fa51870 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -119,21 +119,11 @@ def graphql_to_group(group): ) def graphql_to_room(room): - if room.get('image') is None: - room['image'] = {} - c_info = get_customization_info(room) return Room( - room['thread_key']['thread_fbid'], - participants=set([node['messaging_actor']['id'] for node in room['all_participants']['nodes']]), - nicknames=c_info.get('nicknames'), - color=c_info.get('color'), - emoji=c_info.get('emoji'), - photo=room['image'].get('uri'), - name=room.get('name'), - message_count=room.get('messages_count'), + **vars(graphql_to_group(room)), admins = set([node.get('id') for node in room.get('thread_admins')]), approval_mode = bool(room.get('approval_mode')), - approval_requests = set([node.get('id') for node in room['thread_queue_metatdata'].get('approval_requests')), + approval_requests = set(node.get('id') for node in room['thread_queue_metatdata'].get('approval_requests')), join_link = room['joinable_mode'].get('link'), privacy_mode = bool(room.get('privacy_mode')), ) diff --git a/fbchat/models.py b/fbchat/models.py index 613629f..d7024cb 100644 --- a/fbchat/models.py +++ b/fbchat/models.py @@ -121,11 +121,15 @@ class Room(Group): # True is room is not discoverable privacy_mode = bool - def __init__(self, uid, admins=set(), approval_mode=None, approval_requests=set(), join_link=None, privacy_mode=None, **kwargs): + def __init__(self, uid, admins=None, approval_mode=None, approval_requests=None, join_link=None, privacy_mode=None, **kwargs): """Represents a Facebook room. Inherits `Group`""" super(Room, self).__init__(ThreadType.ROOM, uid, **kwargs) + if admins is None: + admins = set() self.admins = admins self.approval_mode = approval_mode + if approval_requests is None: + approval_requests = set() self.approval_requests = approval_requests self.join_link = join_link self.privacy_mode = privacy_mode From 730bab5d4084d7448580a3e14243e8e2ea5576c0 Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sat, 7 Oct 2017 17:43:27 +1100 Subject: [PATCH 3/9] added rooms under thread_info --- fbchat/client.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index e23e24f..22e9e2a 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -513,6 +513,7 @@ class Client(object): return [graphql_to_page(node) for node in j[name]['pages']['nodes']] + # TODO intergrate Rooms def searchForGroups(self, name, limit=1): """ Find and get group thread by its name @@ -787,19 +788,29 @@ class Client(object): raise FBchatException('A participant had an unknown type {}: {}'.format(p['type'], p)) entries = [] - for k in j['payload']['threads']: - if k['thread_type'] == 1: - if k['other_user_fbid'] not in participants: - raise FBchatException('The thread {} was not in participants: {}'.format(k, j['payload'])) - participants[k['other_user_fbid']].message_count = k['message_count'] - entries.append(participants[k['other_user_fbid']]) - elif k['thread_type'] == 2: - entries.append(Group(k['thread_fbid'], participants=set([p.strip('fbid:') for p in k['participants']]), photo=k['image_src'], name=k['name'], message_count=k['message_count'])) - # TODO - elif k['thread_type'] == 4: - entries.append(Group(k['thread_fbid'], participants=set([p.strip('fbid:') for p in k['participants']]), photo=k['image_src'], name=k['name'], message_count=k['message_count'])) - else: - raise FBchatException('A thread had an unknown thread type: {}'.format(k)) + if 'threads' in j['payload']: + for k in j['payload']['threads']: + if k['thread_type'] == 1: + if k['other_user_fbid'] not in participants: + raise FBchatException('The thread {} was not in participants: {}'.format(k, j['payload'])) + participants[k['other_user_fbid']].message_count = k['message_count'] + entries.append(participants[k['other_user_fbid']]) + elif k['thread_type'] == 2: + entries.append(Group(k['thread_fbid'], participants=set([p.strip('fbid:') for p in k['participants']]), photo=k['image_src'], name=k['name'], message_count=k['message_count'])) + elif k['thread_type'] == 3: + entries.append(Room( + k['thread_fbid'], + participants = set(p.strip('fbid:') for p in k['participants']), + photo = k['image_src'], + name = k['name'], + message_count = k['message_count'], + admins = set(p.lstrip('fbid:') for p in k['admin_ids']), + approval_mode = k['approval_mode'], + approval_requests = set(p.lstrip('fbid:') for p in k['approval_queue_ids']), + join_link = k['joinable_mode']['link'], + )) + else: + raise FBchatException('A thread had an unknown thread type: {}'.format(k)) return entries From eae1db9c7d37aaed4b1d199de0502a8c66118a3f Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sat, 7 Oct 2017 18:09:24 +1100 Subject: [PATCH 4/9] removed list and rstrip --- fbchat/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fbchat/client.py b/fbchat/client.py index 22e9e2a..bc65baa 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -800,7 +800,7 @@ class Client(object): elif k['thread_type'] == 3: entries.append(Room( k['thread_fbid'], - participants = set(p.strip('fbid:') for p in k['participants']), + participants = set(p.lstrip('fbid:') for p in k['participants']), photo = k['image_src'], name = k['name'], message_count = k['message_count'], From 116b39cf6aca0bd6d536b1244ea2e473f52ead80 Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sun, 8 Oct 2017 03:07:34 +1100 Subject: [PATCH 5/9] fixed superclass init error --- fbchat/client.py | 2 +- fbchat/models.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index bc65baa..359f626 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -807,7 +807,7 @@ class Client(object): admins = set(p.lstrip('fbid:') for p in k['admin_ids']), approval_mode = k['approval_mode'], approval_requests = set(p.lstrip('fbid:') for p in k['approval_queue_ids']), - join_link = k['joinable_mode']['link'], + join_link = k['joinable_mode']['link'] )) else: raise FBchatException('A thread had an unknown thread type: {}'.format(k)) diff --git a/fbchat/models.py b/fbchat/models.py index d7024cb..efa05ce 100644 --- a/fbchat/models.py +++ b/fbchat/models.py @@ -123,7 +123,8 @@ class Room(Group): def __init__(self, uid, admins=None, approval_mode=None, approval_requests=None, join_link=None, privacy_mode=None, **kwargs): """Represents a Facebook room. Inherits `Group`""" - super(Room, self).__init__(ThreadType.ROOM, uid, **kwargs) + super(Room, self).__init__(uid, **kwargs) + self.type = ThreadType.ROOM if admins is None: admins = set() self.admins = admins From b1a2ff7d8446b559eb28b601a25b9fc4ae6a0415 Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sun, 15 Oct 2017 03:56:09 +1100 Subject: [PATCH 6/9] updated for python2.7 --- fbchat/graphql.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index fa51870..c5eb62f 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -119,8 +119,18 @@ def graphql_to_group(group): ) def graphql_to_room(room): - return Room( - **vars(graphql_to_group(room)), + if room.get('image') is None: + room['image'] = {} + c_info = get_customization_info(room) + return room( + room['thread_key']['thread_fbid'], + participants=set([node['messaging_actor']['id'] for node in room['all_participants']['nodes']]), + nicknames=c_info.get('nicknames'), + color=c_info.get('color'), + emoji=c_info.get('emoji'), + photo=room['image'].get('uri'), + name=room.get('name'), + message_count=room.get('messages_count'), admins = set([node.get('id') for node in room.get('thread_admins')]), approval_mode = bool(room.get('approval_mode')), approval_requests = set(node.get('id') for node in room['thread_queue_metatdata'].get('approval_requests')), From 6f29aa82cb34cee686c874cd55dd464eced050b3 Mon Sep 17 00:00:00 2001 From: ekohilas Date: Sun, 15 Oct 2017 15:15:56 +1100 Subject: [PATCH 7/9] fixed class mistype --- fbchat/graphql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index c5eb62f..0a88103 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -122,7 +122,7 @@ def graphql_to_room(room): if room.get('image') is None: room['image'] = {} c_info = get_customization_info(room) - return room( + return Room( room['thread_key']['thread_fbid'], participants=set([node['messaging_actor']['id'] for node in room['all_participants']['nodes']]), nicknames=c_info.get('nicknames'), From d1f457866bf15bf977c4e873ed346af8450a2741 Mon Sep 17 00:00:00 2001 From: ekohilas Date: Fri, 20 Oct 2017 03:08:27 +1100 Subject: [PATCH 8/9] fixed dict typo --- fbchat/graphql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 0a88103..f08033d 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -133,7 +133,7 @@ def graphql_to_room(room): message_count=room.get('messages_count'), admins = set([node.get('id') for node in room.get('thread_admins')]), approval_mode = bool(room.get('approval_mode')), - approval_requests = set(node.get('id') for node in room['thread_queue_metatdata'].get('approval_requests')), + approval_requests = set(node.get('id') for node in room['thread_queue_metadata'].get('approval_requests')), join_link = room['joinable_mode'].get('link'), privacy_mode = bool(room.get('privacy_mode')), ) From 4b3eb440cfb814658c8767e60f054e65c675b3ff Mon Sep 17 00:00:00 2001 From: ekohilas Date: Fri, 20 Oct 2017 03:17:26 +1100 Subject: [PATCH 9/9] fixed missing get --- fbchat/graphql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index f08033d..08150c6 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -133,7 +133,7 @@ def graphql_to_room(room): message_count=room.get('messages_count'), admins = set([node.get('id') for node in room.get('thread_admins')]), approval_mode = bool(room.get('approval_mode')), - approval_requests = set(node.get('id') for node in room['thread_queue_metadata'].get('approval_requests')), + approval_requests = set(node.get('id') for node in room['thread_queue_metadata'].get('approval_requests', {}).get('nodes')), join_link = room['joinable_mode'].get('link'), privacy_mode = bool(room.get('privacy_mode')), )