Something went wrong. Try again.
🌉 A bridge between decentralized social networks fed.brid.gy
Something went wrong. Try again.
bridgy-fed ui.py
2.9 kB · 93 lines
Python
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394"""Stub protocol class for internal objects, ie objects that we create ourselves.
"UI" isn't an ideal name. It now includes internal objects broadly, eg responsesthat users create via the Mastodon API, not just objects created via our own UI(s).
Examples of where we currently create these:
* Responses generated in pages.respond_** Interactions with unbridged users and objects via our Mastodon API
(follow.py's follows and dms.py's DMs aren't internal objects. Their ids are intheir author's id space, eg ``https://user.com/#follow-...``, so their``source_protocol`` is their author's protocol.)
Ids are ``ui:...``, opaque and protocol independent. Nothing should parse them.:class:`Object`s' ``source_protocol`` is ``ui``.
:attr:`Object.users` must be populated with the *author's* key. :meth:`models.Object._pre_put_hook` asserts it.
Get the author's protocol via :meth:`models.Object.owner_protocol`, which uses``users`` if available, then falls back to ``author``/``actor`` ids.
:meth:`ids.translate_object_id` etc wrap these ids on their *author's* subdomain,eg ``https://bsky.brid.gy/convert/ap/ui:...``, which we serve from:func:`convert.convert`. This has to match the author's actor id's domain, orMastodon and other fedi servers reject their ``Create`` activities."""import logging
from google.cloud import ndb
import idsimport modelsimport protocol
logger = logging.getLogger(__name__)
class UIProtocol(models.User, protocol.Protocol): LABEL = 'ui' # no ui.brid.gy; make subdomain_wrap falls back to fed.brid.gy ABBREV = None SUPPORTED_AS1_TYPES = ('comment', 'block', 'delete', 'follow', 'like', 'note', 'post', 'share', 'stop-following', 'undo', 'update')
def _pre_put_hook(self): raise NotImplementedError()
@classmethod def owns_id(self, id): return id.startswith('ui:')
@classmethod def id_type(cls, id): """We never have users, so all of our ids are object ids.""" if cls.owns_id(id): return ids.IdType.OBJECT
@ndb.ComputedProperty def handle(self): return None
@classmethod def handle_to_id(cls, handle): return None
@classmethod def create_for(cls, user): raise ValueError()
@classmethod def send(to_cls, *args, **kwargs): return False
@classmethod def fetch(cls, obj, **kwargs): return False
@classmethod def load(cls, id, remote=None, **kwargs): """We're the origin for internal objects, so they're never fetchable.""" if remote: logger.info(f'Overriding remote=True to remote=False for UIProtocol.load of{id} because ui: objects are internal only')
return super().load(id, remote=False, **kwargs)
@classmethod def target_for(cls, obj, **kwargs): return None
@classmethod def bot_user_id(cls): return None