From 9617d8ab7189cfa0a6b8273be7ab5d4def9456a3 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Sat, 27 Apr 2024 20:34:37 -0700 Subject: [PATCH] move Protocol.is_enabled to User.is_enabled --- models.py | 41 +++++++++++++++++++++++++++++++++++++++++ protocol.py | 39 --------------------------------------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/models.py b/models.py index c4c10cd..e8b34e4 100644 --- a/models.py +++ b/models.py @@ -358,6 +358,47 @@ class User(StringIdModel, metaclass=ProtocolUserMeta): return None + def is_enabled(self, to_proto): + """Returns True if this user can be bridged to a given protocol. + + Reasons this might return False: + * We haven't turned on bridging these two protocols yet. + * The user is opted out. + * The user is on a domain that's opted out. + * The from protocol requires opt in, and the user hasn't opted in. + + Args: + to_proto (Protocol subclass) + + Returns: + bool: + """ + from protocol import Protocol + assert issubclass(to_proto, Protocol) + + if self.__class__ == to_proto: + return True + + from_label = self.LABEL + to_label = to_proto.LABEL + + # unit tests + if DEBUG and (from_label in ('fake', 'other') + or (to_label in ('fake', 'other') and from_label != 'eefake')): + return True + + if bot_protocol := Protocol.for_bridgy_subdomain(self.key.id()): + return to_proto != bot_protocol + + if self.status == 'opt-out': + return False + + if (to_label in self.enabled_protocols + or to_label in self.DEFAULT_ENABLED_PROTOCOLS): + return True + + return False + @ndb.transactional() def enable_protocol(self, to_proto): """Adds ``to_proto` to :attr:`enabled_protocols`. diff --git a/protocol.py b/protocol.py index 874700e..0009583 100644 --- a/protocol.py +++ b/protocol.py @@ -143,45 +143,6 @@ class Protocol: label = domain.removesuffix(common.SUPERDOMAIN) return PROTOCOLS.get(label) - def is_enabled(self, to_proto): - """Returns True if this user can be bridged to a given protocol. - - Reasons this might return False: - * We haven't turned on bridging these two protocols yet. - * The user is opted out. - * The user is on a domain that's opted out. - * The from protocol requires opt in, and the user hasn't opted in. - - Args: - to_proto (Protocol subclass) - - Returns: - bool: - """ - assert issubclass(to_proto, Protocol) - if self.__class__ == to_proto: - return True - - from_label = self.LABEL - to_label = to_proto.LABEL - - # unit tests - if DEBUG and (from_label in ('fake', 'other') - or (to_label in ('fake', 'other') and from_label != 'eefake')): - return True - - if bot_protocol := Protocol.for_bridgy_subdomain(self.key.id()): - return to_proto != bot_protocol - - if self.status == 'opt-out': - return False - - if (to_label in self.enabled_protocols - or to_label in self.DEFAULT_ENABLED_PROTOCOLS): - return True - - return False - @classmethod def owns_id(cls, id): """Returns whether this protocol owns the id, or None if it's unclear.