Class MatrixAgentPusherService
java.lang.Object
io.meeds.chat.service.MatrixAgentPusherService
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
FieldsModifier and TypeFieldDescriptionstatic final DurationHow long the decision of the thread that queued a turn stands in for a re-read.static final StringThe legacy content rule matching the account's user name in a message body.The server-default overrides and content rules that notify on a message not mentioning the account throughm.mentions: the legacy name matches and the room mentions.static final intMost queued turns of accounts wanting a pusher; the scheduled job catches up the others.static final intMost extra pushers deleted in one turn; the rest go at the next one.static final intMost push rules changed in one turn; the rest go at the next one.static final DurationRequest timeout of every Synapse call of a reconcile turn, the login included.The rules never touched: the mention that triggers a worker, and room invitations. -
Constructor Summary
ConstructorsConstructorDescriptionMatrixAgentPusherService(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 TypeMethodDescriptionvoidenqueueDesired(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, forCARRIED_DECISION_VALIDITY.voidenqueueIfDesired(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.voidenqueueLeftKind(String login) 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.getLoginOfPushkey(String pushkey) Gives the login a pushkey was derived for, after checking its MAC in constant time.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.getPushkey(String login) Derives the key of an account's pusher:b64url(login) "." b64url(HMAC-SHA256(K, label ":" login)), whereKis derived from the platform's Matrix JWT secret.booleanTells 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 booleanisManagedRule(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.voidonMembershipDeleted(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.voidReconciles 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 byMatrixConstants.PUSH_GATEWAY_PATH.voidrunPendingTurn(String login) Runs the pending turn of an account, in a transaction of its own, on the turn thread.voidshutdown()Stops the turn thread with the context.
-
Field Details
-
SYNAPSE_TIMEOUT
Request timeout of every Synapse call of a reconcile turn, the login included. -
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_TURNMost 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_TURNMost 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_TURNSMost queued turns of accounts wanting a pusher; the scheduled job catches up the others.- See Also:
-
MANAGED_OVERRIDE_RULES
-
MANAGED_CONTENT_RULE
The legacy content rule matching the account's user name in a message body.- See Also:
-
UNMANAGED_RULES
-
-
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' tokensmatrixHttpClient- the Synapse clientaccountKindService- the account kinds the add-ons declareorganizationService- the organization service
-
-
Method Details
-
shutdown
@PreDestroy public void shutdown()Stops the turn thread with the context. -
isDesired
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
-
reconcile
Reconciles one account in a transaction of its own: what the scheduled job calls for each account.- Parameters:
login- the platform logingatewayUrl- the push gateway url, null when the configured one is invalid: nothing is then registered and existing pushers stay
-
enqueueIfDesired
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
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, forCARRIED_DECISION_VALIDITY. A pending turn of the same account is replaced, unless it is a left-kind turn. OverMAX_PENDING_DESIRED_TURNSpending turns, the turn is dropped and left to the scheduled job.- Parameters:
login- the platform loginmatrixId- the account's Matrix id as the calling thread read it
-
enqueueLeftKind
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
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 membershipgroupId- the group of the deleted membership
-
runPendingTurn
Runs the pending turn of an account, in a transaction of its own, on the turn thread.- Parameters:
login- the platform login
-
resolveGatewayUrl
Resolves the push gateway url: the configured one, else the platform base url followed byMatrixConstants.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
Derives the key of an account's pusher:b64url(login) "." b64url(HMAC-SHA256(K, label ":" login)), whereKis 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
-
getPushableLogin
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, asisDesired(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
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
-