From deda3b433d5759311fb8232f1d9b6db23a9d4372 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 9 Jan 2020 13:04:04 +0100 Subject: [PATCH] Fix various errors Found using mypy! --- fbchat/_client.py | 10 +--------- fbchat/_group.py | 8 ++++---- fbchat/_plan.py | 7 +++++++ fbchat/_thread.py | 25 ++++++++++++++++--------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 34620eb..2003083 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -2,7 +2,6 @@ import datetime import time import json import requests -from collections import OrderedDict from ._core import log from . import _util, _graphql, _session @@ -25,14 +24,7 @@ from ._quick_reply import ( QuickReplyEmail, ) from ._poll import Poll, PollOption -from ._plan import Plan - - -ACONTEXT = { - "action_history": [ - {"surface": "messenger_chat_tab", "mechanism": "messenger_composer"} - ] -} +from ._plan import ACONTEXT, Plan class Client: diff --git a/fbchat/_group.py b/fbchat/_group.py index ddaadc9..d7e6b71 100644 --- a/fbchat/_group.py +++ b/fbchat/_group.py @@ -1,6 +1,6 @@ import attr from ._core import attrs_default, Image -from . import _util, _session, _plan, _thread, _user +from . import _util, _session, _graphql, _plan, _thread, _user from typing import Sequence, Iterable @@ -69,10 +69,10 @@ class Group(_thread.ThreadABC): user_id: User ID to remove """ data = {"uid": user_id, "tid": self.id} - j = self._payload_post("/chat/remove_participants/", data) + j = self.session._payload_post("/chat/remove_participants/", data) def _admin_status(self, user_ids: Iterable[str], status: bool): - data = {"add": admin, "thread_fbid": self.id} + data = {"add": status, "thread_fbid": self.id} for i, user_id in enumerate(user_ids): data["admin_ids[{}]".format(i)] = str(user_id) @@ -119,7 +119,7 @@ class Group(_thread.ThreadABC): Args: require_admin_approval: True or False """ - data = {"set_mode": int(require_admin_approval), "thread_fbid": thread_id} + data = {"set_mode": int(require_admin_approval), "thread_fbid": self.id} j = self.session._payload_post("/messaging/set_approval_mode/?dpr=1", data) def _users_approval(self, user_ids: Iterable[str], approve: bool): diff --git a/fbchat/_plan.py b/fbchat/_plan.py index 118e761..b0ab61f 100644 --- a/fbchat/_plan.py +++ b/fbchat/_plan.py @@ -10,6 +10,13 @@ class GuestStatus(Enum): DECLINED = 3 +ACONTEXT = { + "action_history": [ + {"surface": "messenger_chat_tab", "mechanism": "messenger_composer"} + ] +} + + @attrs_default class Plan: """Represents a plan.""" diff --git a/fbchat/_thread.py b/fbchat/_thread.py index 8609766..d8911d8 100644 --- a/fbchat/_thread.py +++ b/fbchat/_thread.py @@ -1,8 +1,9 @@ import abc import attr +import collections import datetime from ._core import attrs_default, Enum, Image -from . import _util, _exception, _session +from . import _util, _exception, _session, _graphql, _attachment, _file, _plan from typing import MutableMapping, Any, Iterable, Tuple @@ -231,6 +232,8 @@ class ThreadABC(metaclass=abc.ABCMeta): Returns: list: `Message` objects """ + from . import _message + # TODO: Return proper searchable iterator params = { "id": self.id, @@ -244,12 +247,14 @@ class ThreadABC(metaclass=abc.ABCMeta): ) if j.get("message_thread") is None: - raise FBchatException("Could not fetch thread {}: {}".format(self.id, j)) + raise _exception.FBchatException( + "Could not fetch thread {}: {}".format(self.id, j) + ) read_receipts = j["message_thread"]["read_receipts"]["nodes"] messages = [ - Message._from_graphql(self.session, message, read_receipts) + _message.Message._from_graphql(self.session, message, read_receipts) for message in j["message_thread"]["messages"]["nodes"] ] messages.reverse() @@ -279,11 +284,11 @@ class ThreadABC(metaclass=abc.ABCMeta): break if i["node"].get("__typename") == "MessageImage": - yield ImageAttachment._from_list(i) + yield _file.ImageAttachment._from_list(i) elif i["node"].get("__typename") == "MessageVideo": - yield VideoAttachment._from_list(i) + yield _file.VideoAttachment._from_list(i) else: - yield Attachment(id=i["node"].get("legacy_attachment_id")) + yield _attachment.Attachment(id=i["node"].get("legacy_attachment_id")) del j[self.id]["message_shared_media"]["edges"][0] def set_nickname(self, user_id: str, nickname: str): @@ -385,8 +390,8 @@ class ThreadABC(metaclass=abc.ABCMeta): "title": name, "thread_id": self.id, "location_id": location_id or "", - "location_name": location or "", - "acontext": ACONTEXT, + "location_name": location_name or "", + "acontext": _plan.ACONTEXT, } j = self.session._payload_post("/ajax/eventreminder/create", data) if "error" in j: @@ -404,7 +409,9 @@ class ThreadABC(metaclass=abc.ABCMeta): # the POST parameters is badly implemented, and deals with ordering the options # wrongly. If you can find a way to fix this for the endpoint, or if you find # another endpoint, please do suggest it ;) - data = OrderedDict([("question_text", question), ("target_id", self.id)]) + data = collections.OrderedDict( + [("question_text", question), ("target_id", self.id)] + ) for i, (text, vote) in enumerate(options): data["option_text_array[{}]".format(i)] = text