Пример #1
0
def extract_status_change(
        chat_member_update: ChatMemberUpdated,
) -> Optional[Tuple[bool, bool]]:
    status_change = chat_member_update.difference().get("status")
    old_is_member, new_is_member = chat_member_update.difference().get(
        "is_member", (None, None)
    )

    if status_change is None:
        return None

    old_status, new_status = status_change
    was_member = (
            old_status
            in [
                ChatMember.MEMBER,
                ChatMember.CREATOR,
                ChatMember.ADMINISTRATOR,
            ]
            or (old_status == ChatMember.RESTRICTED and old_is_member is True)
    )
    is_member = (
            new_status
            in [
                ChatMember.MEMBER,
                ChatMember.CREATOR,
                ChatMember.ADMINISTRATOR,
            ]
            or (new_status == ChatMember.RESTRICTED and new_is_member is True)
    )

    return was_member, is_member
Пример #2
0
def extract_status_change(
    chat_member_update: ChatMemberUpdated,
) -> Optional[Tuple[bool, bool]]:
    """Takes a ChatMemberUpdated instance and extracts whether the 'old_chat_member' was a member
    of the chat and whether the 'new_chat_member' is a member of the chat. Returns None, if
    the status didn't change.
    """
    status_change = chat_member_update.difference().get("status")
    old_is_member, new_is_member = chat_member_update.difference().get("is_member", (None, None))

    if status_change is None:
        return None

    old_status, new_status = status_change
    was_member = (
        old_status
        in [
            ChatMember.MEMBER,
            ChatMember.CREATOR,
            ChatMember.ADMINISTRATOR,
        ]
        or (old_status == ChatMember.RESTRICTED and old_is_member is True)
    )
    is_member = (
        new_status
        in [
            ChatMember.MEMBER,
            ChatMember.CREATOR,
            ChatMember.ADMINISTRATOR,
        ]
        or (new_status == ChatMember.RESTRICTED and new_is_member is True)
    )

    return was_member, is_member
Пример #3
0
def tlg_extract_members_status_change(
    chat_member_update: ChatMemberUpdated, ) -> Optional[Tuple[bool, bool]]:
    '''Takes a ChatMemberUpdated instance and extracts whether the
    "old_chat_member" was a member of the chat and whether the
    "new_chat_member" is a member of the chat. Returns None, if the status
    didn't change.'''
    status_change = chat_member_update.difference().get("status")
    old_is_member, new_is_member = chat_member_update.difference().get( \
            "is_member", (None, None))
    if status_change is None:
        if (old_is_member is None) or (new_is_member is None):
            return None
        was_member = old_is_member
        is_member = new_is_member
        return was_member, is_member
    old_status, new_status = status_change
    was_member = old_status in [
        ChatMember.MEMBER,
        ChatMember.CREATOR,
        ChatMember.ADMINISTRATOR,
    ] or (old_status == ChatMember.RESTRICTED and old_is_member is True)
    is_member = new_status in [
        ChatMember.MEMBER,
        ChatMember.CREATOR,
        ChatMember.ADMINISTRATOR,
    ] or (new_status == ChatMember.RESTRICTED and new_is_member is True)
    return was_member, is_member
    def test_difference_required(self, user, chat):
        old_chat_member = ChatMember(user, "old_status")
        new_chat_member = ChatMember(user, "new_status")
        chat_member_updated = ChatMemberUpdated(
            chat, user, datetime.datetime.utcnow(), old_chat_member, new_chat_member
        )
        assert chat_member_updated.difference() == {"status": ("old_status", "new_status")}

        # We deliberately change an optional argument here to make sure that comparison doesn't
        # just happens by id/required args
        new_user = User(1, "First name", False, last_name="last name")
        new_chat_member.user = new_user
        assert chat_member_updated.difference() == {
            "status": ("old_status", "new_status"),
            "user": (user, new_user),
        }
    def test_difference_required(self, user, chat):
        old_chat_member = ChatMember(user, 'old_status')
        new_chat_member = ChatMember(user, 'new_status')
        chat_member_updated = ChatMemberUpdated(
            chat, user, datetime.datetime.utcnow(), old_chat_member, new_chat_member
        )
        assert chat_member_updated.difference() == {'status': ('old_status', 'new_status')}

        # We deliberately change an optional argument here to make sure that comparision doesn't
        # just happens by id/required args
        new_user = User(1, 'First name', False, last_name='last name')
        new_chat_member.user = new_user
        assert chat_member_updated.difference() == {
            'status': ('old_status', 'new_status'),
            'user': (user, new_user),
        }
 def test_difference_optionals(self, optional_attribute, user, chat):
     # we use datetimes here, because we need that for `until_date` and it doesn't matter for
     # the other attributes
     old_value = datetime.datetime(2020, 1, 1)
     new_value = datetime.datetime(2021, 1, 1)
     old_chat_member = ChatMember(user, "status", **{optional_attribute: old_value})
     new_chat_member = ChatMember(user, "status", **{optional_attribute: new_value})
     chat_member_updated = ChatMemberUpdated(
         chat, user, datetime.datetime.utcnow(), old_chat_member, new_chat_member
     )
     assert chat_member_updated.difference() == {optional_attribute: (old_value, new_value)}
 def test_difference_different_classes(self, user, chat):
     old_chat_member = ChatMemberOwner(user=user, is_anonymous=False)
     new_chat_member = ChatMemberBanned(user=user, until_date=datetime.datetime(2021, 1, 1))
     chat_member_updated = ChatMemberUpdated(
         chat=chat,
         from_user=user,
         date=datetime.datetime.utcnow(),
         old_chat_member=old_chat_member,
         new_chat_member=new_chat_member,
     )
     diff = chat_member_updated.difference()
     assert diff.pop("is_anonymous") == (False, None)
     assert diff.pop("until_date") == (None, datetime.datetime(2021, 1, 1))
     assert diff.pop("status") == (ChatMember.OWNER, ChatMember.BANNED)
     assert diff == {}