superduper.session
==================

.. py:module:: superduper.session

.. autoapi-nested-parse::

   User actions



Classes
-------

.. autoapisummary::

   superduper.session.Session


Module Contents
---------------

.. py:class:: Session(user)



   The session of a registered :term:`User`.

   Represents a gateway user logged in to the legacy network and performing actions.

   Will be instantiated automatically on slidge startup for each registered user,
   or upon registration for new (validated) users.

   Must be subclassed for a functional :term:`Legacy Module`.


   .. py:method:: login()
      :async:


      Logs in the gateway user to the legacy network.

      Triggered when the gateway start and on user registration.
      It is recommended that this function returns once the user is logged in,
      so if you need to await forever (for instance to listen to incoming events),
      it's a good idea to wrap your listener in an asyncio.Task.

      :return: Optionally, a text to use as the gateway status, e.g., "Connected as 'dude@legacy.network'"



   .. py:method:: on_text(chat, text, *, reply_to_msg_id = None, reply_to_fallback_text = None, reply_to = None, thread = None, link_previews = (), mentions = None)
      :async:


      Triggered when the user sends a text message from XMPP to a bridged entity, e.g.
      to ``translated_user_name@slidge.example.com``, or ``translated_group_name@slidge.example.com``

      Override this and implement sending a message to the legacy network in this method.

      :param text: Content of the message
      :param chat: Recipient of the message. :class:`.LegacyContact` instance for 1:1 chat,
          :class:`.MUC` instance for groups.
      :param reply_to_msg_id: A legacy message ID if the message references (quotes)
          another message (:xep:`0461`)
      :param reply_to_fallback_text: Content of the quoted text. Not necessarily set
          by XMPP clients
      :param reply_to: Author of the quoted message. :class:`LegacyContact` instance for
          1:1 chat, :class:`LegacyParticipant` instance for groups.
          If `None`, should be interpreted as a self-reply if reply_to_msg_id is not None.
      :param link_previews: A list of sender-generated link previews.
          At the time of writing, only `Cheogram <https://wiki.soprani.ca/CheogramApp/LinkPreviews>`_
          supports it.
      :param mentions: (only for groups) A list of Contacts mentioned by their
          nicknames.
      :param thread:

      :return: An ID of some sort that can be used later to ack and mark the message
          as read by the user



   .. py:attribute:: xmpp
      :type:  slidge.core.gateway.BaseGateway

      The gateway instance singleton. Use it for low-level XMPP calls or custom methods that are not
      session-specific.



   .. py:attribute:: MESSAGE_IDS_ARE_THREAD_IDS
      :value: False


      Set this to True if the legacy service uses message IDs as thread IDs,
      eg Mattermost, where you can only 'create a thread' by replying to the message,
      in which case the message ID is also a thread ID (and all messages are potential
      threads).



   .. py:attribute:: SPECIAL_MSG_ID_PREFIX
      :type:  Optional[str]
      :value: None


      If you set this, XMPP message IDs starting with this won't be converted to legacy ID,
      but passed as is to :meth:`.on_react`, and usual checks for emoji restriction won't be
      applied.
      This can be used to implement voting in polls in a hacky way.



   .. py:method:: logout()
      :abstractmethod:

      :async:


      Logs out the gateway user from the legacy network.

      Called on gateway shutdown.



   .. py:method:: on_file(chat, url, *, http_response, reply_to_msg_id = None, reply_to_fallback_text = None, reply_to = None, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user sends a file using HTTP Upload (:xep:`0363`)

      :param url: URL of the file
      :param chat: See :meth:`.BaseSession.on_text`
      :param http_response: The HTTP GET response object on the URL
      :param reply_to_msg_id: See :meth:`.BaseSession.on_text`
      :param reply_to_fallback_text: See :meth:`.BaseSession.on_text`
      :param reply_to: See :meth:`.BaseSession.on_text`
      :param thread:

      :return: An ID of some sort that can be used later to ack and mark the message
          as read by the user



   .. py:method:: on_sticker(chat, sticker, *, reply_to_msg_id = None, reply_to_fallback_text = None, reply_to = None, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user sends a file using HTTP Upload (:xep:`0363`)

      :param chat: See :meth:`.BaseSession.on_text`
      :param sticker: The sticker sent by the user.
      :param reply_to_msg_id: See :meth:`.BaseSession.on_text`
      :param reply_to_fallback_text: See :meth:`.BaseSession.on_text`
      :param reply_to: See :meth:`.BaseSession.on_text`
      :param thread:

      :return: An ID of some sort that can be used later to ack and mark the message
          as read by the user



   .. py:method:: on_active(chat, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user sends an 'active' chat state (:xep:`0085`)

      :param chat: See :meth:`.BaseSession.on_text`
      :param thread:



   .. py:method:: on_inactive(chat, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user sends an 'inactive' chat state (:xep:`0085`)

      :param chat: See :meth:`.BaseSession.on_text`
      :param thread:



   .. py:method:: on_composing(chat, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user starts typing in a legacy chat (:xep:`0085`)

      :param chat: See :meth:`.BaseSession.on_text`
      :param thread:



   .. py:method:: on_paused(chat, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user pauses typing in a legacy chat (:xep:`0085`)

      :param chat: See :meth:`.BaseSession.on_text`
      :param thread:



   .. py:method:: on_gone(chat, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user is "gone" in a legacy chat (:xep:`0085`)

      :param chat: See :meth:`.BaseSession.on_text`
      :param thread:



   .. py:method:: on_displayed(chat, legacy_msg_id, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user reads a message in a legacy chat. (:xep:`0333`)

      This is only possible if a valid ``legacy_msg_id`` was passed when
      transmitting a message from a legacy chat to the user, eg in
      :meth:`slidge.contact.LegacyContact.send_text`
      or
      :meth:`slidge.group.LegacyParticipant.send_text`.

      :param chat: See :meth:`.BaseSession.on_text`
      :param legacy_msg_id: Identifier of the message/
      :param thread:



   .. py:method:: on_correct(chat, text, legacy_msg_id, *, thread = None, link_previews = (), mentions = None)
      :abstractmethod:

      :async:


      Triggered when the user corrects a message using :xep:`0308`

      This is only possible if a valid ``legacy_msg_id`` was returned by
      :meth:`.on_text`.

      :param chat: See :meth:`.BaseSession.on_text`
      :param text: The new text
      :param legacy_msg_id: Identifier of the edited message
      :param thread:
      :param link_previews: A list of sender-generated link previews.
          At the time of writing, only `Cheogram <https://wiki.soprani.ca/CheogramApp/LinkPreviews>`_
          supports it.
      :param mentions: (only for groups) A list of Contacts mentioned by their
          nicknames.



   .. py:method:: on_react(chat, legacy_msg_id, emojis, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user sends message reactions (:xep:`0444`).

      :param chat: See :meth:`.BaseSession.on_text`
      :param thread:
      :param legacy_msg_id: ID of the message the user reacts to
      :param emojis: Unicode characters representing reactions to the message ``legacy_msg_id``.
          An empty string means "no reaction", ie, remove all reactions if any were present before



   .. py:method:: on_retract(chat, legacy_msg_id, thread = None)
      :abstractmethod:

      :async:


      Triggered when the user retracts (:xep:`0424`) a message.

      :param chat: See :meth:`.BaseSession.on_text`
      :param thread:
      :param legacy_msg_id: Legacy ID of the retracted message



   .. py:method:: on_presence(resource, show, status, resources, merged_resource)
      :abstractmethod:

      :async:


      Called when the gateway component receives a presence, ie, when
      one of the user's clients goes online of offline, or changes its
      status.

      :param resource: The XMPP client identifier, arbitrary string.
      :param show: The presence ``<show>``, if available. If the resource is
          just 'available' without any ``<show>`` element, this is an empty
          str.
      :param status: A status message, like a deeply profound quote, eg,
          "Roses are red, violets are blue, [INSERT JOKE]".
      :param resources: A summary of all the resources for this user.
      :param merged_resource: A global presence for the user account,
          following rules described in :meth:`merge_resources`



   .. py:method:: on_search(form_values)
      :abstractmethod:

      :async:


      Triggered when the user uses Jabber Search (:xep:`0055`) on the component

      Form values is a dict in which keys are defined in :attr:`.BaseGateway.SEARCH_FIELDS`

      :param form_values: search query, defined for a specific plugin by overriding
          in :attr:`.BaseGateway.SEARCH_FIELDS`
      :return:



   .. py:method:: on_avatar(bytes_, hash_, type_, width, height)
      :abstractmethod:

      :async:


      Triggered when the user uses modifies their avatar via :xep:`0084`.

      :param bytes_: The data of the avatar. According to the spec, this
          should always be a PNG, but some implementations do not respect
          that. If `None` it means the user has unpublished their avatar.
      :param hash_: The SHA1 hash of the avatar data. This is an identifier of
          the avatar.
      :param type_: The MIME type of the avatar.
      :param width: The width of the avatar image.
      :param height: The height of the avatar image.



   .. py:method:: on_moderate(muc, legacy_msg_id, reason)
      :abstractmethod:

      :async:


      Triggered when the user attempts to retract a message that was sent in
      a MUC using :xep:`0425`.

      If retraction is not possible, this should raise the appropriate
      XMPPError with a human-readable message.

      NB: the legacy module is responsible for calling
      :method:`LegacyParticipant.moderate` when this is successful, because
      slidge will acknowledge the moderation IQ, but will not send the
      moderation message from the MUC automatically.

      :param muc: The MUC in which the message was sent
      :param legacy_msg_id: The legacy ID of the message to be retracted
      :param reason: Optionally, a reason for the moderation, given by the
          user-moderator.



   .. py:method:: on_create_group(name, contacts)
      :abstractmethod:

      :async:


      Triggered when the user request the creation of a group via the
      dedicated :term:`Command`.

      :param name: Name of the group
      :param contacts: list of contacts that should be members of the group



   .. py:method:: on_invitation(contact, muc, reason)
      :async:


      Triggered when the user invites a :term:`Contact` to a legacy MUC via
      :xep:`0249`.

      The default implementation calls :meth:`LegacyMUC.on_set_affiliation`
      with the 'member' affiliation. Override if you want to customize this
      behaviour.

      :param contact: The invitee
      :param muc: The group
      :param reason: Optionally, a reason



   .. py:method:: on_leave_group(muc_legacy_id)
      :abstractmethod:

      :async:


      Triggered when the user leaves a group via the dedicated slidge command
      or the :xep:`0077` ``<remove />`` mechanism.

      This should be interpreted as definitely leaving the group.

      :param muc_legacy_id: The legacy ID of the group to leave



   .. py:method:: on_preferences(previous, new)
      :abstractmethod:

      :async:


      This is called when the user updates their preferences.

      Override this if you need set custom preferences field and need to trigger
      something when a preference has changed.



   .. py:method:: legacy_to_xmpp_msg_id(legacy_msg_id)
      :staticmethod:


      Convert a legacy msg ID to a valid XMPP msg ID.
      Needed for read marks, retractions and message corrections.

      The default implementation just converts the legacy ID to a :class:`str`,
      but this should be overridden in case some characters needs to be escaped,
      or to add some additional,
      :term:`legacy network <Legacy Network`>-specific logic.

      :param legacy_msg_id:
      :return: A string that is usable as an XMPP stanza ID



   .. py:method:: xmpp_to_legacy_msg_id(i)
      :staticmethod:


      Convert a legacy XMPP ID to a valid XMPP msg ID.
      Needed for read marks and message corrections.

      The default implementation just converts the legacy ID to a :class:`str`,
      but this should be overridden in case some characters needs to be escaped,
      or to add some additional,
      :term:`legacy network <Legacy Network`>-specific logic.

      The default implementation is an identity function.

      :param i: The XMPP stanza ID
      :return: An ID that can be used to identify a message on the legacy network



   .. py:method:: send_gateway_status(status = None, show=Optional[PresenceShows], **kwargs)

      Send a presence from the gateway to the user.

      Can be used to indicate the user session status, ie "SMS code required", "connected", …

      :param status: A status message
      :param show: Presence stanza 'show' element. I suggest using "dnd" to show
          that the gateway is not fully functional



   .. py:method:: send_gateway_message(text, **msg_kwargs)

      Send a message from the gateway component to the user.

      Can be used to indicate the user session status, ie "SMS code required", "connected", …

      :param text: A text



   .. py:method:: send_gateway_invite(muc, reason = None, password = None)

      Send an invitation to join a MUC, emanating from the gateway component.

      :param muc:
      :param reason:
      :param password:



   .. py:method:: input(text, **msg_kwargs)
      :async:


      Request user input via direct messages from the gateway component.

      Wraps call to :meth:`.BaseSession.input`

      :param text: The prompt to send to the user
      :param msg_kwargs: Extra attributes
      :return:



   .. py:method:: send_qr(text)
      :async:


      Sends a QR code generated from 'text' via HTTP Upload and send the URL to
      ``self.user``

      :param text: Text to encode as a QR code



