Class MatrixAgentPusherService

java.lang.Object
io.meeds.chat.service.MatrixAgentPusherService

@Service public class MatrixAgentPusherService extends Object
Registers, as the account itself, a notify-only Matrix pusher for every account of a kind an add-on declares (a digital worker), and keeps that account's push rules down to the mentions of the account, so that Synapse pushes to the platform the mentions of a worker that never opens a browser. The server defaults it leaves alone still notify an invitation (.m.rule.invite_for_me) and a room upgrade (.m.rule.tombstone); the push gateway drops both, since neither is a message.

The pusher's key is derived from the platform's Matrix JWT secret and the login, never stored, and only lets the push gateway recognise the account it is pushed for. Each account is reconciled by reconcile(String, String): the scheduled job does it for every such account, and the platform listeners queue a turn on one single thread, so a user save or a space join never waits on Synapse.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Duration
    How long the decision of the thread that queued a turn stands in for a re-read.
    static final String
    The legacy content rule matching the account's user name in a message body.
    static final Set<String>
    The server-default overrides and content rules that notify on a message not mentioning the account through m.mentions: the legacy name matches and the room mentions.
    static final int
    Most queued turns of accounts wanting a pusher; the scheduled job catches up the others.
    static final int
    Most extra pushers deleted in one turn; the rest go at the next one.
    static final int
    Most push rules changed in one turn; the rest go at the next one.
    static final Duration
    Request timeout of every Synapse call of a reconcile turn, the login included.
    static final Set<String>
    The rules never touched: the mention that triggers a worker, and room invitations.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MatrixAgentPusherService(MatrixService matrixService, MatrixHttpClient matrixHttpClient, io.meeds.social.core.identity.service.AccountKindService accountKindService, org.exoplatform.services.organization.OrganizationService organizationService)
    Creates the service.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    enqueueDesired(String login, String matrixId)
    Queues a turn for an account the calling thread found wanting a pusher, after the listener's own work: the turn then uses that decision instead of re-reading what the calling request may not have committed yet, for CARRIED_DECISION_VALIDITY.
    void
    What a platform listener calls once its own work is done: decides, on the calling thread, whether the account wants a pusher, and queues a turn carrying that decision and the account's Matrix id when it does.
    void
    Queues a turn for an account that left an account kind's group.
    Lists the accounts that want a pusher, over the groups of every account kind an add-on declares, in one transaction of its own.
    Gives the login a pushkey was derived for, after checking its MAC in constant time.
    Decides whether the push gateway accepts a worker pusher's key: its MAC matches, and the account it names still belongs to the group of a kind an add-on declares and still has a Matrix account.
    Derives the key of an account's pusher: b64url(login) "." b64url(HMAC-SHA256(K, label ":" login)), where K is derived from the platform's Matrix JWT secret.
    boolean
    Tells whether an account wants a pusher: it is enabled, has a Matrix account, and belongs to the group of an account kind an add-on declares.
    static boolean
    Tells whether a push rule belongs to the set this service manages on a worker's account: every server-default underride rule that notifies, the legacy name-match and room-mention overrides, and the legacy user-name content rule.
    void
    What the membership listener calls once a membership is deleted: queues a left-kind turn when the membership was in the group of an account kind and the account now belongs to no kind's group.
    void
    reconcile(String login, String gatewayUrl)
    Reconciles one account in a transaction of its own: what the scheduled job calls for each account.
    Resolves the push gateway url: the configured one, else the platform base url followed by MatrixConstants.PUSH_GATEWAY_PATH.
    void
    Runs the pending turn of an account, in a transaction of its own, on the turn thread.
    void
    Stops the turn thread with the context.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SYNAPSE_TIMEOUT

      public static final Duration SYNAPSE_TIMEOUT
      Request timeout of every Synapse call of a reconcile turn, the login included.
    • CARRIED_DECISION_VALIDITY

      public static final Duration CARRIED_DECISION_VALIDITY
      How long the decision of the thread that queued a turn stands in for a re-read.
    • MAX_PUSHER_DELETES_PER_TURN

      public static final int MAX_PUSHER_DELETES_PER_TURN
      Most extra pushers deleted in one turn; the rest go at the next one.
      See Also:
    • MAX_RULE_UPDATES_PER_TURN

      public static final int MAX_RULE_UPDATES_PER_TURN
      Most push rules changed in one turn; the rest go at the next one.
      See Also:
    • MAX_PENDING_DESIRED_TURNS

      public static final int MAX_PENDING_DESIRED_TURNS
      Most queued turns of accounts wanting a pusher; the scheduled job catches up the others.
      See Also:
    • MANAGED_OVERRIDE_RULES

      public static final Set<String> MANAGED_OVERRIDE_RULES
      The server-default overrides and content rules that notify on a message not mentioning the account through m.mentions: the legacy name matches and the room mentions.
    • MANAGED_CONTENT_RULE

      public static final String MANAGED_CONTENT_RULE
      The legacy content rule matching the account's user name in a message body.
      See Also:
    • UNMANAGED_RULES

      public static final Set<String> UNMANAGED_RULES
      The rules never touched: the mention that triggers a worker, and room invitations.
  • Constructor Details

    • MatrixAgentPusherService

      @Autowired public MatrixAgentPusherService(MatrixService matrixService, MatrixHttpClient matrixHttpClient, io.meeds.social.core.identity.service.AccountKindService accountKindService, org.exoplatform.services.organization.OrganizationService organizationService)
      Creates the service.
      Parameters:
      matrixService - the Matrix service, which mints the accounts' tokens
      matrixHttpClient - the Synapse client
      accountKindService - the account kinds the add-ons declare
      organizationService - the organization service
  • Method Details

    • shutdown

      @PreDestroy public void shutdown()
      Stops the turn thread with the context.
    • isDesired

      public boolean isDesired(String login) throws Exception
      Tells whether an account wants a pusher: it is enabled, has a Matrix account, and belongs to the group of an account kind an add-on declares. The chat add-on never names a kind.
      Parameters:
      login - the platform login
      Returns:
      true when the account wants a pusher
      Throws:
      Exception - when the user or its memberships cannot be read
    • getDesiredLogins

      public Set<String> getDesiredLogins()
      Lists the accounts that want a pusher, over the groups of every account kind an add-on declares, in one transaction of its own.
      Returns:
      the logins, never null
    • reconcile

      public void reconcile(String login, String gatewayUrl)
      Reconciles one account in a transaction of its own: what the scheduled job calls for each account.
      Parameters:
      login - the platform login
      gatewayUrl - the push gateway url, null when the configured one is invalid: nothing is then registered and existing pushers stay
    • enqueueIfDesired

      public void enqueueIfDesired(String login)
      What a platform listener calls once its own work is done: decides, on the calling thread, whether the account wants a pusher, and queues a turn carrying that decision and the account's Matrix id when it does. Nothing is queued for an account of no declared kind, and no Synapse call is made on the calling thread.
      Parameters:
      login - the platform login
    • enqueueDesired

      public void enqueueDesired(String login, String matrixId)
      Queues a turn for an account the calling thread found wanting a pusher, after the listener's own work: the turn then uses that decision instead of re-reading what the calling request may not have committed yet, for CARRIED_DECISION_VALIDITY. A pending turn of the same account is replaced, unless it is a left-kind turn. Over MAX_PENDING_DESIRED_TURNS pending turns, the turn is dropped and left to the scheduled job.
      Parameters:
      login - the platform login
      matrixId - the account's Matrix id as the calling thread read it
    • enqueueLeftKind

      public void enqueueLeftKind(String login)
      Queues a turn for an account that left an account kind's group. It replaces any pending turn of that account and is never dropped; it re-reads whether the account still wants a pusher and, when not, re-enables its managed push rules.
      Parameters:
      login - the platform login
    • onMembershipDeleted

      public void onMembershipDeleted(String login, String groupId)
      What the membership listener calls once a membership is deleted: queues a left-kind turn when the membership was in the group of an account kind and the account now belongs to no kind's group. Nothing is queued while chat is disabled, for another group, or when the memberships cannot be read, which leaves the account's push rules as they are.
      Parameters:
      login - the platform login of the deleted membership
      groupId - the group of the deleted membership
    • runPendingTurn

      public void runPendingTurn(String login)
      Runs the pending turn of an account, in a transaction of its own, on the turn thread.
      Parameters:
      login - the platform login
    • resolveGatewayUrl

      public String resolveGatewayUrl()
      Resolves the push gateway url: the configured one, else the platform base url followed by MatrixConstants.PUSH_GATEWAY_PATH. It must be an absolute http or https url whose raw path, as Synapse compares it, is exactly that one, the only path Synapse accepts.
      Returns:
      the url, null when it is invalid
    • getPushkey

      public String getPushkey(String login)
      Derives the key of an account's pusher: b64url(login) "." b64url(HMAC-SHA256(K, label ":" login)), where K is derived from the platform's Matrix JWT secret. Rotating that secret invalidates every worker pusher; the next turn registers new ones.
      Parameters:
      login - the platform login
      Returns:
      the pushkey
    • getLoginOfPushkey

      public String getLoginOfPushkey(String pushkey)
      Gives the login a pushkey was derived for, after checking its MAC in constant time.
      Parameters:
      pushkey - the pushkey Synapse pushed with
      Returns:
      the login, null when the key is malformed or its MAC does not match
    • getPushableLogin

      public String getPushableLogin(String pushkey)
      Decides whether the push gateway accepts a worker pusher's key: its MAC matches, and the account it names still belongs to the group of a kind an add-on declares and still has a Matrix account. The kind is computed from the group memberships, as isDesired(String) does when the pusher is registered, and not read from the kind shown on the profile: the gateway is unauthenticated, so the key is accepted on the same rule that registered it, and a failure to read the memberships is thrown rather than answered as an account of no kind. The enabled flag is not read: a disabled account's pushers are deleted by its Synapse deactivation.
      Parameters:
      pushkey - the pushkey Synapse pushed with
      Returns:
      the login, null when the key must be rejected
      Throws:
      IllegalStateException - when the account's memberships cannot be read
    • isManagedRule

      public static boolean isManagedRule(MatrixPushRule rule)
      Tells whether a push rule belongs to the set this service manages on a worker's account: every server-default underride rule that notifies, the legacy name-match and room-mention overrides, and the legacy user-name content rule. The trigger, .m.rule.is_user_mention, the invitation rule and any rule the account defined are never managed.
      Parameters:
      rule - the rule
      Returns:
      true when the rule is managed