Skip to content

Commit

Permalink
protocol: extract out enable/disable_protocol methods
Browse files Browse the repository at this point in the history
for #880
  • Loading branch information
snarfed committed Apr 19, 2024
1 parent 64a196a commit 3c55d7c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
28 changes: 27 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from oauth_dropins.webutil.util import json_dumps, json_loads

import common
from common import add, base64_to_long, DOMAIN_RE, long_to_base64, unwrap
from common import add, base64_to_long, DOMAIN_RE, long_to_base64, remove, unwrap
import ids

# maps string label to Protocol subclass. populated by ProtocolUserMeta.
Expand Down Expand Up @@ -348,6 +348,32 @@ def status(self):

return None

@ndb.transactional()
def enable_protocol(self, to_proto):
"""Adds ``to_proto` to :attr:`enabled_protocols`.
Args:
to_proto (:class:`protocol.Protocol` subclass)
"""
user = self.key.get()
add(user.enabled_protocols, to_proto.LABEL)
user.put()

add(self.enabled_protocols, to_proto.LABEL)

@ndb.transactional()
def disable_protocol(self, to_proto):
"""Removes ``to_proto` from :attr:`enabled_protocols`.
Args:
to_proto (:class:`protocol.Protocol` subclass)
"""
user = self.key.get()
remove(user.enabled_protocols, to_proto.LABEL)
user.put()

remove(self.enabled_protocols, to_proto.LABEL)

def handle_as(self, to_proto):
"""Returns this user's handle in a different protocol.
Expand Down
18 changes: 3 additions & 15 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
DOMAINS,
error,
PROTOCOL_DOMAINS,
remove,
subdomain_wrap,
)
from flask_app import app
Expand Down Expand Up @@ -802,13 +801,7 @@ def receive(from_cls, obj, authed_as=None, internal=False):
logger.info("Ignoring block, target isn't one of our protocol domains")
return 'OK', 200

@ndb.transactional()
def disable_protocol():
user = from_user.key.get()
remove(user.enabled_protocols, proto.LABEL)
user.put()

disable_protocol()
from_user.disable_protocol(proto)
return 'OK', 200

# fetch actor if necessary
Expand Down Expand Up @@ -837,13 +830,8 @@ def disable_protocol():
proto = Protocol.for_bridgy_subdomain(inner_obj_id)
if proto:
# follow of one of our protocol users; enable that protocol
@ndb.transactional()
def enable_protocol():
user = from_user.key.get()
add(user.enabled_protocols, proto.LABEL)
user.put()

enable_protocol()
from_user.enable_protocol(proto)
# TODO: accept
return 'OK', 200

from_cls.handle_follow(obj)
Expand Down

0 comments on commit 3c55d7c

Please sign in to comment.