Class: Bridge

Bridge

new Bridge(opts)

Parameters:
Name Type Description
opts Object Options to pass to the bridge
Properties
Name Type Attributes Description
registration AppServiceRegistration | string Application service registration object or path to the registration file.
homeserverUrl string The base HS url
domain string The domain part for user_ids and room aliases e.g. "bar" in "@foo:bar".
controller Object The controller logic for the bridge.
Properties
Name Type Attributes Description
onEvent Bridge~onEvent Function. Called when an event has been received from the HS.
onUserQuery Bridge~onUserQuery <optional>
Function. If supplied, the bridge will invoke this function when queried via onUserQuery. If not supplied, no users will be provisioned on user queries. Provisioned users will automatically be stored in the associated userStore.
onAliasQuery Bridge~onAliasQuery <optional>
Function. If supplied, the bridge will invoke this function when queried via onAliasQuery. If not supplied, no rooms will be provisioned on alias queries. Provisioned rooms will automatically be stored in the associated roomStore.
onLog Bridge~onLog <optional>
Function. Invoked when logging. Defaults to a function which logs to the console.
roomStore RoomBridgeStore | string <optional>
The room store instance to use, or the path to the room .db file to load. A database will be created if this is not specified.
userStore UserBridgeStore | string <optional>
The user store instance to use, or the path to the user .db file to load. A database will be created if this is not specified.
suppressEcho boolean <optional>
True to stop receiving onEvent callbacks for events which were sent by a bridge user. Default: true.
clientFactory ClientFactory <optional>
The client factory instance to use. If not supplied, one will be created.
intentOptions Object <optional>
Options to supply to created Intent instances.
Properties
Name Type Attributes Description
bot Object <optional>
Options to supply to the bot intent.
clients Object <optional>
Options to supply to the client intents.
queue Object <optional>
Options for the onEvent queue. When the bridge receives an incoming transaction, it needs to asyncly query the data store for contextual info before calling onEvent. A queue is used to keep the onEvent calls consistent with the arrival order from the incoming transactions.
Properties
Name Type Attributes Description
type string <optional>
The type of queue to use when feeding through to Bridge~onEvent. One of: "none", single", "per_room". If "none", events are fed through as soon as contextual info is obtained, which may result in out of order events but stops HOL blocking. If "single", onEvent calls will be in order but may be slower due to HOL blocking. If "per_room", a queue per room ID is made which reduces the impact of HOL blocking to be scoped to a room. Default: "single".
perRequest boolean <optional>
True to only feed through the next event after the request object in the previous call succeeds or fails. It is vital that you consistently resolve/reject the request if this is 'true', else you will not get any further events from this queue. To aid debugging this, consider setting a delayed listener on the request factory. If false, the mere invockation of onEvent is enough to trigger the next event in the queue. You probably want to set this to 'true' if your Bridge~onEvent is performing async operations where ordering matters (e.g. messages). Default: false.
disableContext boolean <optional>
True to disable Bridge~BridgeContext parameters in Bridge~onEvent. Disabling the context makes the bridge do fewer database lookups, but prevents there from being a context parameter. Default: false.
Source:

Methods

getBot() → {AppServiceBot}

Get the AS bot instance.
Source:
Returns:
Type
AppServiceBot

getClientFactory() → {ClientFactory}

Retrieve the matrix client factory used when sending matrix requests.
Source:
Returns:
Type
ClientFactory

getIntent(userIdnullable, requestopt) → {Intent}

Retrieve an Intent instance for the specified user ID. If no ID is given, an instance for the bot itself is returned.
Parameters:
Name Type Attributes Description
userId string <nullable>
The user ID to get an Intent for.
request Request <optional>
Optional. The request instance to tie the MatrixClient instance to. Useful for logging contextual request IDs.
Source:
Returns:
The intent instance
Type
Intent

getIntentFromLocalpart(localpartnullable, requestopt) → {Intent}

Retrieve an Intent instance for the specified user ID localpart. This must be the complete user localpart.
Parameters:
Name Type Attributes Description
localpart string <nullable>
The user ID localpart to get an Intent for.
request Request <optional>
Optional. The request instance to tie the MatrixClient instance to. Useful for logging contextual request IDs.
Source:
Returns:
The intent instance
Type
Intent

getRequestFactory() → {RequestFactory}

Retrieve the request factory used to create incoming requests.
Source:
Returns:
Type
RequestFactory

getRoomStore() → (nullable) {RoomBridgeStore}

Retrieve a connected room store instance.
Source:
Returns:
The connected instance ready for querying.
Type
RoomBridgeStore

getUserStore() → (nullable) {UserBridgeStore}

Retrieve a connected user store instance.
Source:
Returns:
The connected instance ready for querying.
Type
UserBridgeStore

loadDatabases() → {Promise}

Load the user and room databases. Access them via getUserStore() and getRoomStore().
Source:
Returns:
Resolved/rejected when the user/room databases have been loaded.
Type
Promise

provisionUser(matrixUser, provisionedUser) → {Promise}

Provision a user on the homeserver.
Parameters:
Name Type Description
matrixUser MatrixUser The virtual user to be provisioned.
provisionedUser Bridge~ProvisionedUser Provisioning information.
Source:
Returns:
Resolved when provisioned.
Type
Promise

run(port, config, appServiceInstanceopt)

Run the bridge (start listening)
Parameters:
Name Type Attributes Description
port Number The port to listen on.
config Object Configuration options
appServiceInstance AppService <optional>
The AppService instance to attach to. If not provided, one will be created.
Source:

Type Definitions

BridgeContext

Type:
  • Object
Properties:
Name Type Description
senders Object Data models on senders of this event
Properties
Name Type Attributes Description
matrix MatrixUser The sender of this event
remote RemoteUser <nullable>
The first linked remote sender: remotes[0]
remotes Array.<RemoteUser> The linked remote senders
targets Object Data models on targets (e.g. state_key in m.room.member) of this event.
Properties
Name Type Attributes Description
matrix MatrixUser <nullable>
The target of this event if applicable.
remote RemoteUser <nullable>
The first linked remote target: remotes[0]
remotes Array.<RemoteUser> The linked remote targets
rooms Object Data models on rooms concerning this event.
Properties
Name Type Attributes Description
matrix MatrixRoom The room for this event.
remote RemoteRoom <nullable>
The first linked remote room: remotes[0]
remotes Array.<RemoteRoom> The linked remote rooms for this event
Source:

onAliasQuery(alias, aliasLocalpart) → {Bridge~ProvisionedRoom|Promise.<Bridge~ProvisionedRoom, Error>}

Invoked when the bridge receives an alias query from the homeserver. Supports both sync return values and async return values via promises.
Parameters:
Name Type Description
alias string The alias queried.
aliasLocalpart string The parsed localpart of the alias.
Source:
Returns:
Reject the promise / return null to not provision the room. Resolve the promise / return a Bridge~ProvisionedRoom object to provision the room.
Type
Bridge~ProvisionedRoom | Promise.<Bridge~ProvisionedRoom, Error>
Example
new Bridge({
  controller: {
    onAliasQuery: function(alias, aliasLocalpart) {
      return {
        creationOpts: {
          room_alias_name: aliasLocalpart, // IMPORTANT: must be set to make the link
          name: aliasLocalpart,
          topic: "Auto-generated bridged room"
        }
      };
    }
  }
});

onEvent(request, context)

Invoked when the bridge receives an event from the homeserver.
Parameters:
Name Type Description
request Request The request to resolve or reject depending on the outcome of this request. The 'data' attached to this Request is the raw event JSON received (accessed via request.getData())
context Bridge~BridgeContext Context for this event, including instantiated client instances.
Source:

onLog(line, isError)

Invoked when the bridge is attempting to log something.
Parameters:
Name Type Description
line string The text to be logged.
isError boolean True if this line should be treated as an error msg.
Source:

onUserQuery(matrixUser) → {Bridge~ProvisionedUser|Promise.<Bridge~ProvisionedUser, Error>}

Invoked when the bridge receives a user query from the homeserver. Supports both sync return values and async return values via promises.
Parameters:
Name Type Description
matrixUser MatrixUser The matrix user queried. Use getId() to get the user ID.
Source:
Returns:
Reject the promise / return null to not provision the user. Resolve the promise / return a Bridge~ProvisionedUser object to provision the user.
Type
Bridge~ProvisionedUser | Promise.<Bridge~ProvisionedUser, Error>
Example
new Bridge({
  controller: {
    onUserQuery: function(matrixUser) {
      var remoteUser = new RemoteUser("some_remote_id");
      return {
        name: matrixUser.localpart + " (Bridged)",
        url: "http://someurl.com/pic.jpg",
        user: remoteUser
      };
    }
  }
});

ProvisionedRoom

Type:
  • Object
Properties:
Name Type Attributes Description
creationOpts Object Room creation options to use when creating the room. Required.
remote RemoteRoom <optional>
The remote room to link to the provisioned room.
Source:

ProvisionedUser

Type:
  • Object
Properties:
Name Type Attributes Description
name string <optional>
The display name to set for the provisioned user.
url string <optional>
The avatar URL to set for the provisioned user.
remote RemoteUser <optional>
The remote user to link to the provisioned user.
Source: