From 8b6d9b16c6028b16dd3afd8bcaa8c2a6183444a9 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 9 Jan 2020 21:00:34 +0100 Subject: [PATCH] Remove ThreadColor Replaced with raw color values. In the future, we should probably investigate using "themes" --- docs/api.rst | 2 - docs/intro.rst | 4 +- examples/interract.py | 4 +- examples/keepbot.py | 2 +- fbchat/__init__.py | 2 +- fbchat/_client.py | 7 +- fbchat/_group.py | 6 +- fbchat/_thread.py | 111 ++++++++++++++++++------------ fbchat/_user.py | 10 +-- tests/test_group.py | 2 +- tests/test_thread.py | 23 +++---- tests/test_thread_interraction.py | 12 ++-- tests/test_user.py | 3 +- 13 files changed, 103 insertions(+), 85 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 942371d..61fc4f1 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -54,8 +54,6 @@ Miscellaneous .. autoclass:: ThreadLocation(Enum) :undoc-members: -.. autoclass:: ThreadColor(Enum) - :undoc-members: .. autoclass:: ActiveStatus() .. autoclass:: TypingStatus(Enum) :undoc-members: diff --git a/docs/intro.rst b/docs/intro.rst index 5dc9f90..46fa972 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -70,8 +70,8 @@ corresponds to the ID of a single user, and the ID of a group respectively:: Some functions don't require a thread type, so in these cases you just provide the thread ID:: thread = fbchat.Thread(session=session, id="") - thread.set_color(ThreadColor.BILOBA_FLOWER) - thread.set_color(ThreadColor.MESSENGER_BLUE) + thread.set_color("#a695c7") + thread.set_color("#67b868") .. _intro_message_ids: diff --git a/examples/interract.py b/examples/interract.py index 279c0cb..ec2471f 100644 --- a/examples/interract.py +++ b/examples/interract.py @@ -54,8 +54,8 @@ thread.set_nickname(fbchat.User(session=session, id=""), " str: + if not inp: + return DEFAULT_COLOR + # Strip the alpha value, and lower the string + return "#{}".format(inp[2:].lower()) + @staticmethod def _parse_customization_info(data: Any) -> MutableMapping[str, Any]: - if data is None or data.get("customization_info") is None: - return {} + if not data or not data.get("customization_info"): + return {"emoji": None, "color": DEFAULT_COLOR} info = data["customization_info"] rtn = { "emoji": info.get("emoji"), - "color": ThreadColor._from_graphql(info.get("outgoing_bubble_color")), + "color": ThreadABC._parse_color(info.get("outgoing_bubble_color")), } if ( data.get("thread_type") == "GROUP" diff --git a/fbchat/_user.py b/fbchat/_user.py index ef26967..0e51f7c 100644 --- a/fbchat/_user.py +++ b/fbchat/_user.py @@ -110,7 +110,7 @@ class UserData(User): nickname = attr.ib(None) #: The clients nickname, as seen by the user own_nickname = attr.ib(None) - #: A `ThreadColor`. The message color + #: The message color color = attr.ib(None) #: The default emoji emoji = attr.ib(None) @@ -136,8 +136,8 @@ class UserData(User): gender=GENDERS.get(data["gender"]), affinity=data.get("viewer_affinity"), nickname=c_info.get("nickname"), - color=c_info.get("color"), - emoji=c_info.get("emoji"), + color=c_info["color"], + emoji=c_info["emoji"], own_nickname=c_info.get("own_nickname"), photo=Image._from_uri(data["profile_picture"]), name=data["name"], @@ -186,8 +186,8 @@ class UserData(User): is_friend=user["is_viewer_friend"], gender=GENDERS.get(user["gender"]), nickname=c_info.get("nickname"), - color=c_info.get("color"), - emoji=c_info.get("emoji"), + color=c_info["color"], + emoji=c_info["emoji"], own_nickname=c_info.get("own_nickname"), photo=Image._from_uri(user["big_image_src"]), message_count=data["messages_count"], diff --git a/tests/test_group.py b/tests/test_group.py index 5fc7135..ebdfed9 100644 --- a/tests/test_group.py +++ b/tests/test_group.py @@ -35,7 +35,7 @@ def test_group_from_graphql(session): plan=None, participants={"1234", "2345", "3456"}, nicknames={}, - color=None, + color="#0084ff", emoji="😀", admins={"1234"}, approval_mode=False, diff --git a/tests/test_thread.py b/tests/test_thread.py index 6fc597c..344b42e 100644 --- a/tests/test_thread.py +++ b/tests/test_thread.py @@ -1,20 +1,19 @@ import pytest import fbchat -from fbchat import ThreadColor, ThreadABC, Thread +from fbchat import ThreadABC, Thread -def test_thread_color_from_graphql(): - assert None is ThreadColor._from_graphql(None) - assert ThreadColor.MESSENGER_BLUE is ThreadColor._from_graphql("") - assert ThreadColor.VIKING is ThreadColor._from_graphql("FF44BEC7") - assert ThreadColor._from_graphql("DEADBEEF") is getattr( - ThreadColor, "UNKNOWN_#ADBEEF" - ) +def test_parse_color(): + assert "#0084ff" == ThreadABC._parse_color(None) + assert "#0084ff" == ThreadABC._parse_color("") + assert "#44bec7" == ThreadABC._parse_color("FF44BEC7") + assert "#adbeef" == ThreadABC._parse_color("DEADBEEF") def test_thread_parse_customization_info_empty(): - assert {} == ThreadABC._parse_customization_info(None) - assert {} == ThreadABC._parse_customization_info({"customization_info": None}) + default = {"color": "#0084ff", "emoji": None} + assert default == ThreadABC._parse_customization_info(None) + assert default == ThreadABC._parse_customization_info({"customization_info": None}) def test_thread_parse_customization_info_group(): @@ -34,7 +33,7 @@ def test_thread_parse_customization_info_group(): } expected = { "emoji": "🎉", - "color": ThreadColor.BRILLIANT_ROSE, + "color": "#ff5ca1", "nicknames": {"123456789": "A", "987654321": "B"}, } assert expected == ThreadABC._parse_customization_info(data) @@ -55,7 +54,7 @@ def test_thread_parse_customization_info_user(): "thread_type": "ONE_TO_ONE", # ... Other irrelevant fields } - expected = {"emoji": None, "color": None, "own_nickname": "A", "nickname": "B"} + expected = {"emoji": None, "color": "#0084ff", "own_nickname": "A", "nickname": "B"} assert expected == ThreadABC._parse_customization_info(data) diff --git a/tests/test_thread_interraction.py b/tests/test_thread_interraction.py index df05fdb..5b05366 100644 --- a/tests/test_thread_interraction.py +++ b/tests/test_thread_interraction.py @@ -1,6 +1,6 @@ import pytest -from fbchat import Message, FBchatFacebookError, TypingStatus, ThreadColor +from fbchat import Message, FBchatFacebookError, TypingStatus from utils import random_hex, subset from os import path @@ -91,14 +91,10 @@ def test_change_image_remote(client1, group, catch_event): ) -@pytest.mark.parametrize( - "color", - [x for x in ThreadColor if x in [ThreadColor.MESSENGER_BLUE, ThreadColor.PUMPKIN]], -) -def test_change_color(client, catch_event, compare, color): +def test_change_color(client, catch_event, compare): with catch_event("on_color_change") as x: - client.change_thread_color(color) - assert compare(x, new_color=color) + client.change_thread_color("#44bec7") + assert compare(x, new_color="#44bec7") @pytest.mark.xfail(raises=FBchatFacebookError, reason="Should fail, but doesn't") diff --git a/tests/test_user.py b/tests/test_user.py index 3685c24..a26eedd 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -27,6 +27,7 @@ def test_user_from_graphql(session): is_friend=True, gender="female_singular", affinity=0.4560002, + color="#0084ff", ) == UserData._from_graphql(session, data) @@ -152,7 +153,7 @@ def test_user_from_thread_fetch(session): gender="female_singular", nickname="A", own_nickname="B", - color=None, + color="#0084ff", emoji=None, ) == UserData._from_thread_fetch(session, data)