@jeongsu816/matrix-bot-sdk 0.0.0-stage → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +50 -2
- package/lib/AdminApis.d.ts +52 -0
- package/lib/AdminApis.js +29 -0
- package/lib/AdminApis.js.map +1 -0
- package/lib/DMs.d.ts +48 -0
- package/lib/DMs.js +160 -0
- package/lib/DMs.js.map +1 -0
- package/lib/IFilter.d.ts +7 -0
- package/lib/IFilter.js +3 -0
- package/lib/IFilter.js.map +1 -0
- package/lib/MatrixAuth.d.ts +45 -0
- package/lib/MatrixAuth.js +125 -0
- package/lib/MatrixAuth.js.map +1 -0
- package/lib/MatrixClient.d.ts +834 -0
- package/lib/MatrixClient.js +2268 -0
- package/lib/MatrixClient.js.map +1 -0
- package/lib/PantalaimonClient.d.ts +42 -0
- package/lib/PantalaimonClient.js +44 -0
- package/lib/PantalaimonClient.js.map +1 -0
- package/lib/SynapseAdminApis.d.ts +350 -0
- package/lib/SynapseAdminApis.js +244 -0
- package/lib/SynapseAdminApis.js.map +1 -0
- package/lib/SynchronousMatrixClient.d.ts +82 -0
- package/lib/SynchronousMatrixClient.js +130 -0
- package/lib/SynchronousMatrixClient.js.map +1 -0
- package/lib/UnstableApis.d.ts +43 -0
- package/lib/UnstableApis.js +74 -0
- package/lib/UnstableApis.js.map +1 -0
- package/lib/appservice/Appservice.d.ts +328 -0
- package/lib/appservice/Appservice.js +803 -0
- package/lib/appservice/Appservice.js.map +1 -0
- package/lib/appservice/Intent.d.ts +113 -0
- package/lib/appservice/Intent.js +363 -0
- package/lib/appservice/Intent.js.map +1 -0
- package/lib/appservice/MatrixBridge.d.ts +85 -0
- package/lib/appservice/MatrixBridge.js +111 -0
- package/lib/appservice/MatrixBridge.js.map +1 -0
- package/lib/appservice/UnstableAppserviceApis.d.ts +41 -0
- package/lib/appservice/UnstableAppserviceApis.js +62 -0
- package/lib/appservice/UnstableAppserviceApis.js.map +1 -0
- package/lib/appservice/http_responses.d.ts +77 -0
- package/lib/appservice/http_responses.js +3 -0
- package/lib/appservice/http_responses.js.map +1 -0
- package/lib/b64.d.ts +42 -0
- package/lib/b64.js +68 -0
- package/lib/b64.js.map +1 -0
- package/lib/e2ee/CrossSigningBootstrap.d.ts +73 -0
- package/lib/e2ee/CrossSigningBootstrap.js +86 -0
- package/lib/e2ee/CrossSigningBootstrap.js.map +1 -0
- package/lib/e2ee/CryptoClient.d.ts +163 -0
- package/lib/e2ee/CryptoClient.js +382 -0
- package/lib/e2ee/CryptoClient.js.map +1 -0
- package/lib/e2ee/ICryptoRoomInformation.d.ts +8 -0
- package/lib/e2ee/ICryptoRoomInformation.js +3 -0
- package/lib/e2ee/ICryptoRoomInformation.js.map +1 -0
- package/lib/e2ee/RoomTracker.d.ts +41 -0
- package/lib/e2ee/RoomTracker.js +96 -0
- package/lib/e2ee/RoomTracker.js.map +1 -0
- package/lib/e2ee/RustEngine.d.ts +149 -0
- package/lib/e2ee/RustEngine.js +467 -0
- package/lib/e2ee/RustEngine.js.map +1 -0
- package/lib/e2ee/decorators.d.ts +10 -0
- package/lib/e2ee/decorators.js +37 -0
- package/lib/e2ee/decorators.js.map +1 -0
- package/lib/helpers/MatrixEntity.d.ts +37 -0
- package/lib/helpers/MatrixEntity.js +67 -0
- package/lib/helpers/MatrixEntity.js.map +1 -0
- package/lib/helpers/MatrixGlob.d.ts +22 -0
- package/lib/helpers/MatrixGlob.js +35 -0
- package/lib/helpers/MatrixGlob.js.map +1 -0
- package/lib/helpers/MentionPill.d.ts +40 -0
- package/lib/helpers/MentionPill.js +91 -0
- package/lib/helpers/MentionPill.js.map +1 -0
- package/lib/helpers/Permalinks.d.ts +58 -0
- package/lib/helpers/Permalinks.js +73 -0
- package/lib/helpers/Permalinks.js.map +1 -0
- package/lib/helpers/ProfileCache.d.ts +48 -0
- package/lib/helpers/ProfileCache.js +114 -0
- package/lib/helpers/ProfileCache.js.map +1 -0
- package/lib/helpers/RichReply.d.ts +17 -0
- package/lib/helpers/RichReply.js +47 -0
- package/lib/helpers/RichReply.js.map +1 -0
- package/lib/helpers/UnpaddedBase64.d.ts +31 -0
- package/lib/helpers/UnpaddedBase64.js +45 -0
- package/lib/helpers/UnpaddedBase64.js.map +1 -0
- package/lib/http.d.ts +17 -0
- package/lib/http.js +147 -0
- package/lib/http.js.map +1 -0
- package/lib/identity/IdentityClient.d.ts +93 -0
- package/lib/identity/IdentityClient.js +248 -0
- package/lib/identity/IdentityClient.js.map +1 -0
- package/lib/index.d.ts +91 -0
- package/lib/index.js +125 -0
- package/lib/index.js.map +1 -0
- package/lib/logging/ConsoleLogger.d.ts +12 -0
- package/lib/logging/ConsoleLogger.js +28 -0
- package/lib/logging/ConsoleLogger.js.map +1 -0
- package/lib/logging/ILogger.d.ts +36 -0
- package/lib/logging/ILogger.js +3 -0
- package/lib/logging/ILogger.js.map +1 -0
- package/lib/logging/LogService.d.ts +100 -0
- package/lib/logging/LogService.js +169 -0
- package/lib/logging/LogService.js.map +1 -0
- package/lib/logging/RichConsoleLogger.d.ts +20 -0
- package/lib/logging/RichConsoleLogger.js +41 -0
- package/lib/logging/RichConsoleLogger.js.map +1 -0
- package/lib/metrics/IMetricListener.d.ts +52 -0
- package/lib/metrics/IMetricListener.js +3 -0
- package/lib/metrics/IMetricListener.js.map +1 -0
- package/lib/metrics/Metrics.d.ts +66 -0
- package/lib/metrics/Metrics.js +128 -0
- package/lib/metrics/Metrics.js.map +1 -0
- package/lib/metrics/contexts.d.ts +51 -0
- package/lib/metrics/contexts.js +3 -0
- package/lib/metrics/contexts.js.map +1 -0
- package/lib/metrics/decorators.d.ts +15 -0
- package/lib/metrics/decorators.js +92 -0
- package/lib/metrics/decorators.js.map +1 -0
- package/lib/metrics/names.d.ts +45 -0
- package/lib/metrics/names.js +49 -0
- package/lib/metrics/names.js.map +1 -0
- package/lib/mixins/AutojoinRoomsMixin.d.ts +10 -0
- package/lib/mixins/AutojoinRoomsMixin.js +25 -0
- package/lib/mixins/AutojoinRoomsMixin.js.map +1 -0
- package/lib/mixins/AutojoinUpgradedRoomsMixin.d.ts +10 -0
- package/lib/mixins/AutojoinUpgradedRoomsMixin.js +40 -0
- package/lib/mixins/AutojoinUpgradedRoomsMixin.js.map +1 -0
- package/lib/models/Account.d.ts +8 -0
- package/lib/models/Account.js +3 -0
- package/lib/models/Account.js.map +1 -0
- package/lib/models/CreateRoom.d.ts +125 -0
- package/lib/models/CreateRoom.js +3 -0
- package/lib/models/CreateRoom.js.map +1 -0
- package/lib/models/Crypto.d.ts +167 -0
- package/lib/models/Crypto.js +31 -0
- package/lib/models/Crypto.js.map +1 -0
- package/lib/models/EventContext.d.ts +19 -0
- package/lib/models/EventContext.js +3 -0
- package/lib/models/EventContext.js.map +1 -0
- package/lib/models/IdentityServerModels.d.ts +20 -0
- package/lib/models/IdentityServerModels.js +3 -0
- package/lib/models/IdentityServerModels.js.map +1 -0
- package/lib/models/MSC2176.d.ts +42 -0
- package/lib/models/MSC2176.js +3 -0
- package/lib/models/MSC2176.js.map +1 -0
- package/lib/models/MatrixError.d.ts +38 -0
- package/lib/models/MatrixError.js +30 -0
- package/lib/models/MatrixError.js.map +1 -0
- package/lib/models/MatrixProfile.d.ts +43 -0
- package/lib/models/MatrixProfile.js +43 -0
- package/lib/models/MatrixProfile.js.map +1 -0
- package/lib/models/OpenIDConnect.d.ts +10 -0
- package/lib/models/OpenIDConnect.js +3 -0
- package/lib/models/OpenIDConnect.js.map +1 -0
- package/lib/models/Policies.d.ts +25 -0
- package/lib/models/Policies.js +3 -0
- package/lib/models/Policies.js.map +1 -0
- package/lib/models/PowerLevelAction.d.ts +27 -0
- package/lib/models/PowerLevelAction.js +32 -0
- package/lib/models/PowerLevelAction.js.map +1 -0
- package/lib/models/PowerLevelBounds.d.ts +15 -0
- package/lib/models/PowerLevelBounds.js +3 -0
- package/lib/models/PowerLevelBounds.js.map +1 -0
- package/lib/models/Presence.d.ts +25 -0
- package/lib/models/Presence.js +38 -0
- package/lib/models/Presence.js.map +1 -0
- package/lib/models/ServerVersions.d.ts +8 -0
- package/lib/models/ServerVersions.js +3 -0
- package/lib/models/ServerVersions.js.map +1 -0
- package/lib/models/Spaces.d.ts +116 -0
- package/lib/models/Spaces.js +91 -0
- package/lib/models/Spaces.js.map +1 -0
- package/lib/models/Threepid.d.ts +8 -0
- package/lib/models/Threepid.js +3 -0
- package/lib/models/Threepid.js.map +1 -0
- package/lib/models/events/AliasesEvent.d.ts +27 -0
- package/lib/models/events/AliasesEvent.js +27 -0
- package/lib/models/events/AliasesEvent.js.map +1 -0
- package/lib/models/events/CanonicalAliasEvent.d.ts +23 -0
- package/lib/models/events/CanonicalAliasEvent.js +21 -0
- package/lib/models/events/CanonicalAliasEvent.js.map +1 -0
- package/lib/models/events/CreateEvent.d.ts +63 -0
- package/lib/models/events/CreateEvent.js +33 -0
- package/lib/models/events/CreateEvent.js.map +1 -0
- package/lib/models/events/EncryptedRoomEvent.d.ts +42 -0
- package/lib/models/events/EncryptedRoomEvent.js +28 -0
- package/lib/models/events/EncryptedRoomEvent.js.map +1 -0
- package/lib/models/events/EncryptionEvent.d.ts +47 -0
- package/lib/models/events/EncryptionEvent.js +42 -0
- package/lib/models/events/EncryptionEvent.js.map +1 -0
- package/lib/models/events/Event.d.ts +26 -0
- package/lib/models/events/Event.js +40 -0
- package/lib/models/events/Event.js.map +1 -0
- package/lib/models/events/EventKind.d.ts +15 -0
- package/lib/models/events/EventKind.js +20 -0
- package/lib/models/events/EventKind.js.map +1 -0
- package/lib/models/events/InvalidEventError.d.ts +14 -0
- package/lib/models/events/InvalidEventError.js +24 -0
- package/lib/models/events/InvalidEventError.js.map +1 -0
- package/lib/models/events/JoinRulesEvent.d.ts +29 -0
- package/lib/models/events/JoinRulesEvent.js +21 -0
- package/lib/models/events/JoinRulesEvent.js.map +1 -0
- package/lib/models/events/MembershipEvent.d.ts +54 -0
- package/lib/models/events/MembershipEvent.js +49 -0
- package/lib/models/events/MembershipEvent.js.map +1 -0
- package/lib/models/events/MessageEvent.d.ts +233 -0
- package/lib/models/events/MessageEvent.js +49 -0
- package/lib/models/events/MessageEvent.js.map +1 -0
- package/lib/models/events/PinnedEventsEvent.d.ts +23 -0
- package/lib/models/events/PinnedEventsEvent.js +21 -0
- package/lib/models/events/PinnedEventsEvent.js.map +1 -0
- package/lib/models/events/PowerLevelsEvent.d.ts +96 -0
- package/lib/models/events/PowerLevelsEvent.js +70 -0
- package/lib/models/events/PowerLevelsEvent.js.map +1 -0
- package/lib/models/events/PresenceEvent.d.ts +56 -0
- package/lib/models/events/PresenceEvent.js +21 -0
- package/lib/models/events/PresenceEvent.js.map +1 -0
- package/lib/models/events/RedactionEvent.d.ts +28 -0
- package/lib/models/events/RedactionEvent.js +36 -0
- package/lib/models/events/RedactionEvent.js.map +1 -0
- package/lib/models/events/RoomAvatarEvent.d.ts +28 -0
- package/lib/models/events/RoomAvatarEvent.js +21 -0
- package/lib/models/events/RoomAvatarEvent.js.map +1 -0
- package/lib/models/events/RoomEvent.d.ts +58 -0
- package/lib/models/events/RoomEvent.js +57 -0
- package/lib/models/events/RoomEvent.js.map +1 -0
- package/lib/models/events/RoomNameEvent.d.ts +20 -0
- package/lib/models/events/RoomNameEvent.js +21 -0
- package/lib/models/events/RoomNameEvent.js.map +1 -0
- package/lib/models/events/RoomTopicEvent.d.ts +20 -0
- package/lib/models/events/RoomTopicEvent.js +21 -0
- package/lib/models/events/RoomTopicEvent.js.map +1 -0
- package/lib/models/events/SpaceChildEvent.d.ts +53 -0
- package/lib/models/events/SpaceChildEvent.js +49 -0
- package/lib/models/events/SpaceChildEvent.js.map +1 -0
- package/lib/models/events/converter.d.ts +8 -0
- package/lib/models/events/converter.js +50 -0
- package/lib/models/events/converter.js.map +1 -0
- package/lib/models/unstable/MediaInfo.d.ts +16 -0
- package/lib/models/unstable/MediaInfo.js +3 -0
- package/lib/models/unstable/MediaInfo.js.map +1 -0
- package/lib/preprocessors/IPreprocessor.d.ts +22 -0
- package/lib/preprocessors/IPreprocessor.js +3 -0
- package/lib/preprocessors/IPreprocessor.js.map +1 -0
- package/lib/preprocessors/RichRepliesPreprocessor.d.ts +67 -0
- package/lib/preprocessors/RichRepliesPreprocessor.js +112 -0
- package/lib/preprocessors/RichRepliesPreprocessor.js.map +1 -0
- package/lib/request.d.ts +13 -0
- package/lib/request.js +23 -0
- package/lib/request.js.map +1 -0
- package/lib/simple-validation.d.ts +10 -0
- package/lib/simple-validation.js +30 -0
- package/lib/simple-validation.js.map +1 -0
- package/lib/storage/IAppserviceStorageProvider.d.ts +48 -0
- package/lib/storage/IAppserviceStorageProvider.js +3 -0
- package/lib/storage/IAppserviceStorageProvider.js.map +1 -0
- package/lib/storage/ICryptoStorageProvider.d.ts +32 -0
- package/lib/storage/ICryptoStorageProvider.js +3 -0
- package/lib/storage/ICryptoStorageProvider.js.map +1 -0
- package/lib/storage/IStorageProvider.d.ts +47 -0
- package/lib/storage/IStorageProvider.js +3 -0
- package/lib/storage/IStorageProvider.js.map +1 -0
- package/lib/storage/MemoryStorageProvider.d.ts +26 -0
- package/lib/storage/MemoryStorageProvider.js +55 -0
- package/lib/storage/MemoryStorageProvider.js.map +1 -0
- package/lib/storage/RustSdkCryptoStorageProvider.d.ts +38 -0
- package/lib/storage/RustSdkCryptoStorageProvider.js +69 -0
- package/lib/storage/RustSdkCryptoStorageProvider.js.map +1 -0
- package/lib/storage/SimpleFsStorageProvider.d.ts +31 -0
- package/lib/storage/SimpleFsStorageProvider.js +122 -0
- package/lib/storage/SimpleFsStorageProvider.js.map +1 -0
- package/lib/storage/SimplePostgresStorageProvider.d.ts +32 -0
- package/lib/storage/SimplePostgresStorageProvider.js +148 -0
- package/lib/storage/SimplePostgresStorageProvider.js.map +1 -0
- package/lib/strategies/AppserviceJoinRoomStrategy.d.ts +14 -0
- package/lib/strategies/AppserviceJoinRoomStrategy.js +54 -0
- package/lib/strategies/AppserviceJoinRoomStrategy.js.map +1 -0
- package/lib/strategies/JoinRoomStrategy.d.ts +11 -0
- package/lib/strategies/JoinRoomStrategy.js +44 -0
- package/lib/strategies/JoinRoomStrategy.js.map +1 -0
- package/package.json +136 -4
- package/src/AdminApis.ts +60 -0
- package/src/DMs.ts +164 -0
- package/src/IFilter.ts +7 -0
- package/src/MatrixAuth.ts +129 -0
- package/src/MatrixClient.ts +2046 -0
- package/src/PantalaimonClient.ts +62 -0
- package/src/SynapseAdminApis.ts +482 -0
- package/src/SynchronousMatrixClient.ts +129 -0
- package/src/UnstableApis.ts +75 -0
- package/src/appservice/Appservice.ts +1089 -0
- package/src/appservice/Intent.ts +342 -0
- package/src/appservice/MatrixBridge.ts +139 -0
- package/src/appservice/UnstableAppserviceApis.ts +63 -0
- package/src/appservice/http_responses.ts +77 -0
- package/src/b64.ts +63 -0
- package/src/e2ee/CrossSigningBootstrap.ts +141 -0
- package/src/e2ee/CryptoClient.ts +378 -0
- package/src/e2ee/ICryptoRoomInformation.ts +9 -0
- package/src/e2ee/RoomTracker.ts +99 -0
- package/src/e2ee/RustEngine.ts +580 -0
- package/src/e2ee/decorators.ts +38 -0
- package/src/helpers/MatrixEntity.ts +65 -0
- package/src/helpers/MatrixGlob.ts +38 -0
- package/src/helpers/MentionPill.ts +95 -0
- package/src/helpers/Permalinks.ts +101 -0
- package/src/helpers/ProfileCache.ts +119 -0
- package/src/helpers/RichReply.ts +46 -0
- package/src/helpers/UnpaddedBase64.ts +44 -0
- package/src/http.ts +166 -0
- package/src/identity/IdentityClient.ts +225 -0
- package/src/index.ts +125 -0
- package/src/logging/ConsoleLogger.ts +31 -0
- package/src/logging/ILogger.ts +40 -0
- package/src/logging/LogService.ts +167 -0
- package/src/logging/RichConsoleLogger.ts +70 -0
- package/src/metrics/IMetricListener.ts +57 -0
- package/src/metrics/Metrics.ts +134 -0
- package/src/metrics/contexts.ts +56 -0
- package/src/metrics/decorators.ts +94 -0
- package/src/metrics/names.ts +53 -0
- package/src/mixins/AutojoinRoomsMixin.ts +24 -0
- package/src/mixins/AutojoinUpgradedRoomsMixin.ts +37 -0
- package/src/models/Account.ts +8 -0
- package/src/models/CreateRoom.ts +144 -0
- package/src/models/Crypto.ts +188 -0
- package/src/models/EventContext.ts +23 -0
- package/src/models/IdentityServerModels.ts +18 -0
- package/src/models/MSC2176.ts +47 -0
- package/src/models/MatrixError.ts +39 -0
- package/src/models/MatrixProfile.ts +55 -0
- package/src/models/OpenIDConnect.ts +10 -0
- package/src/models/Policies.ts +28 -0
- package/src/models/PowerLevelAction.ts +31 -0
- package/src/models/PowerLevelBounds.ts +16 -0
- package/src/models/Presence.ts +38 -0
- package/src/models/ServerVersions.ts +8 -0
- package/src/models/Spaces.ts +167 -0
- package/src/models/Threepid.ts +8 -0
- package/src/models/events/AliasesEvent.ts +37 -0
- package/src/models/events/CanonicalAliasEvent.ts +30 -0
- package/src/models/events/CreateEvent.ts +82 -0
- package/src/models/events/EncryptedRoomEvent.ts +58 -0
- package/src/models/events/EncryptionEvent.ts +63 -0
- package/src/models/events/Event.ts +38 -0
- package/src/models/events/EventKind.ts +16 -0
- package/src/models/events/InvalidEventError.ts +19 -0
- package/src/models/events/JoinRulesEvent.ts +37 -0
- package/src/models/events/MembershipEvent.ts +77 -0
- package/src/models/events/MessageEvent.ts +292 -0
- package/src/models/events/PinnedEventsEvent.ts +30 -0
- package/src/models/events/PowerLevelsEvent.ts +135 -0
- package/src/models/events/PresenceEvent.ts +69 -0
- package/src/models/events/RedactionEvent.ts +44 -0
- package/src/models/events/RoomAvatarEvent.ts +36 -0
- package/src/models/events/RoomEvent.ts +83 -0
- package/src/models/events/RoomNameEvent.ts +27 -0
- package/src/models/events/RoomTopicEvent.ts +27 -0
- package/src/models/events/SpaceChildEvent.ts +74 -0
- package/src/models/events/_MissingEvents.md +35 -0
- package/src/models/events/converter.ts +47 -0
- package/src/models/unstable/MediaInfo.ts +16 -0
- package/src/preprocessors/IPreprocessor.ts +24 -0
- package/src/preprocessors/RichRepliesPreprocessor.ts +159 -0
- package/src/request.ts +21 -0
- package/src/simple-validation.ts +30 -0
- package/src/storage/IAppserviceStorageProvider.ts +54 -0
- package/src/storage/ICryptoStorageProvider.ts +36 -0
- package/src/storage/IStorageProvider.ts +53 -0
- package/src/storage/MemoryStorageProvider.ts +65 -0
- package/src/storage/RustSdkCryptoStorageProvider.ts +81 -0
- package/src/storage/SimpleFsStorageProvider.ts +142 -0
- package/src/storage/SimplePostgresStorageProvider.ts +168 -0
- package/src/strategies/AppserviceJoinRoomStrategy.ts +47 -0
- package/src/strategies/JoinRoomStrategy.ts +45 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DeviceKeyAlgorithm,
|
|
3
|
+
extractRequestError,
|
|
4
|
+
IAppserviceCryptoStorageProvider,
|
|
5
|
+
IAppserviceStorageProvider,
|
|
6
|
+
ICryptoStorageProvider,
|
|
7
|
+
LogService,
|
|
8
|
+
MatrixClient,
|
|
9
|
+
Metrics,
|
|
10
|
+
} from "..";
|
|
11
|
+
import { Appservice, IAppserviceOptions } from "./Appservice";
|
|
12
|
+
// noinspection TypeScriptPreferShortImport
|
|
13
|
+
import { timedIntentFunctionCall } from "../metrics/decorators";
|
|
14
|
+
import { UnstableAppserviceApis } from "./UnstableAppserviceApis";
|
|
15
|
+
import { MatrixError } from "../models/MatrixError";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* An Intent is an intelligent client that tracks things like the user's membership
|
|
19
|
+
* in rooms to ensure the action being performed is possible. This is very similar
|
|
20
|
+
* to how Intents work in the matrix-js-sdk in that the Intent will ensure that the
|
|
21
|
+
* user is joined to the room before posting a message, for example.
|
|
22
|
+
* @category Application services
|
|
23
|
+
*/
|
|
24
|
+
export class Intent {
|
|
25
|
+
/**
|
|
26
|
+
* The metrics instance for this intent. Note that this will not raise metrics
|
|
27
|
+
* for the underlying client - those will be available through this instance's
|
|
28
|
+
* parent (the appservice).
|
|
29
|
+
*/
|
|
30
|
+
public readonly metrics: Metrics;
|
|
31
|
+
|
|
32
|
+
private readonly storage: IAppserviceStorageProvider;
|
|
33
|
+
private readonly cryptoStorage: IAppserviceCryptoStorageProvider;
|
|
34
|
+
|
|
35
|
+
private client: MatrixClient;
|
|
36
|
+
private unstableApisInstance: UnstableAppserviceApis;
|
|
37
|
+
private knownJoinedRooms: string[] = [];
|
|
38
|
+
private cryptoSetupPromise: Promise<void>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new intent. Intended to be created by application services.
|
|
42
|
+
* @param {IAppserviceOptions} options The options for the application service.
|
|
43
|
+
* @param {string} impersonateUserId The user ID to impersonate.
|
|
44
|
+
* @param {Appservice} appservice The application service itself.
|
|
45
|
+
*/
|
|
46
|
+
constructor(private options: IAppserviceOptions, private impersonateUserId: string, private appservice: Appservice) {
|
|
47
|
+
this.metrics = new Metrics(appservice.metrics);
|
|
48
|
+
this.storage = options.storage;
|
|
49
|
+
this.cryptoStorage = options.cryptoStorage;
|
|
50
|
+
this.makeClient(false);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private makeClient(withCrypto: boolean, accessToken?: string) {
|
|
54
|
+
let cryptoStore: ICryptoStorageProvider;
|
|
55
|
+
const storage = this.storage?.storageForUser?.(this.userId);
|
|
56
|
+
if (withCrypto) {
|
|
57
|
+
cryptoStore = this.cryptoStorage?.storageForUser(this.userId);
|
|
58
|
+
if (!cryptoStore) {
|
|
59
|
+
throw new Error("Tried to set up client with crypto when not available");
|
|
60
|
+
}
|
|
61
|
+
if (!storage) {
|
|
62
|
+
throw new Error("Tried to set up client with crypto, but no persistent storage");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
this.client = new MatrixClient(this.options.homeserverUrl, accessToken ?? this.options.registration.as_token, storage, cryptoStore);
|
|
66
|
+
this.client.metrics = new Metrics(this.appservice.metrics); // Metrics only go up by one parent
|
|
67
|
+
this.unstableApisInstance = new UnstableAppserviceApis(this.client);
|
|
68
|
+
if (this.impersonateUserId !== this.appservice.botUserId) {
|
|
69
|
+
this.client.impersonateUserId(this.impersonateUserId);
|
|
70
|
+
}
|
|
71
|
+
if (this.options.joinStrategy) {
|
|
72
|
+
this.client.setJoinStrategy(this.options.joinStrategy);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Gets the user ID this intent is for.
|
|
78
|
+
*/
|
|
79
|
+
public get userId(): string {
|
|
80
|
+
return this.impersonateUserId;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Gets the underlying MatrixClient that powers this Intent.
|
|
85
|
+
*/
|
|
86
|
+
public get underlyingClient(): MatrixClient {
|
|
87
|
+
return this.client;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Gets the unstable API access class. This is generally not recommended to be
|
|
92
|
+
* used by appservices.
|
|
93
|
+
* @return {UnstableAppserviceApis} The unstable API access class.
|
|
94
|
+
*/
|
|
95
|
+
public get unstableApis(): UnstableAppserviceApis {
|
|
96
|
+
return this.unstableApisInstance;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Sets up crypto on the client if it hasn't already been set up.
|
|
101
|
+
* @returns {Promise<void>} Resolves when complete.
|
|
102
|
+
*/
|
|
103
|
+
@timedIntentFunctionCall()
|
|
104
|
+
public async enableEncryption(): Promise<void> {
|
|
105
|
+
if (!this.cryptoSetupPromise) {
|
|
106
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
107
|
+
this.cryptoSetupPromise = new Promise(async (resolve, reject) => {
|
|
108
|
+
try {
|
|
109
|
+
// Prepare a client first
|
|
110
|
+
await this.ensureRegistered();
|
|
111
|
+
const storage = this.storage?.storageForUser?.(this.userId);
|
|
112
|
+
this.client.impersonateUserId(this.userId); // make sure the devices call works
|
|
113
|
+
|
|
114
|
+
const cryptoStore = this.cryptoStorage?.storageForUser(this.userId);
|
|
115
|
+
if (!cryptoStore) {
|
|
116
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
117
|
+
throw new Error("Failed to create crypto store");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Try to impersonate a device ID
|
|
121
|
+
const ownDevices = await this.client.getOwnDevices();
|
|
122
|
+
let deviceId = await cryptoStore.getDeviceId();
|
|
123
|
+
if (!deviceId || !ownDevices.some(d => d.device_id === deviceId)) {
|
|
124
|
+
const deviceKeys = await this.client.getUserDevices([this.userId]);
|
|
125
|
+
const userDeviceKeys = deviceKeys.device_keys[this.userId];
|
|
126
|
+
if (userDeviceKeys) {
|
|
127
|
+
// We really should be validating signatures here, but we're actively looking
|
|
128
|
+
// for devices without keys to impersonate, so it should be fine. In theory,
|
|
129
|
+
// those devices won't even be present but we're cautious.
|
|
130
|
+
const devicesWithKeys = Array.from(Object.entries(userDeviceKeys))
|
|
131
|
+
.filter(d => d[0] === d[1].device_id && !!d[1].keys?.[`${DeviceKeyAlgorithm.Curve25519}:${d[1].device_id}`])
|
|
132
|
+
.map(t => t[0]); // grab device ID from tuple
|
|
133
|
+
deviceId = ownDevices.find(d => !devicesWithKeys.includes(d.device_id))?.device_id;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
let prepared = false;
|
|
137
|
+
if (deviceId) {
|
|
138
|
+
this.makeClient(true);
|
|
139
|
+
this.client.impersonateUserId(this.userId, deviceId);
|
|
140
|
+
|
|
141
|
+
// verify that the server supports impersonating the device
|
|
142
|
+
const respDeviceId = (await this.client.getWhoAmI()).device_id;
|
|
143
|
+
prepared = (respDeviceId === deviceId);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!prepared) {
|
|
147
|
+
// XXX: We work around servers that don't support device_id impersonation
|
|
148
|
+
const accessToken = await Promise.resolve(storage?.readValue("accessToken"));
|
|
149
|
+
if (!accessToken) {
|
|
150
|
+
const loginBody = {
|
|
151
|
+
type: "m.login.application_service",
|
|
152
|
+
identifier: {
|
|
153
|
+
type: "m.id.user",
|
|
154
|
+
user: this.userId,
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
const res = await this.client.doRequest("POST", "/_matrix/client/v3/login", {}, loginBody);
|
|
158
|
+
this.makeClient(true, res['access_token']);
|
|
159
|
+
storage.storeValue("accessToken", this.client.accessToken);
|
|
160
|
+
prepared = true;
|
|
161
|
+
} else {
|
|
162
|
+
this.makeClient(true, accessToken);
|
|
163
|
+
prepared = true;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!prepared) {// noinspection ExceptionCaughtLocallyJS
|
|
168
|
+
throw new Error("Unable to establish a device ID");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Now set up crypto
|
|
172
|
+
await this.client.crypto.prepare(await this.refreshJoinedRooms());
|
|
173
|
+
|
|
174
|
+
this.appservice.on("room.event", (roomId, event) => {
|
|
175
|
+
if (!this.knownJoinedRooms.includes(roomId)) return;
|
|
176
|
+
this.client.crypto.onRoomEvent(roomId, event);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
resolve();
|
|
180
|
+
} catch (e) {
|
|
181
|
+
reject(e);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return this.cryptoSetupPromise;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Gets the joined rooms for the intent. Note that by working around
|
|
190
|
+
* the intent to join rooms may yield inaccurate results.
|
|
191
|
+
* @returns {Promise<string[]>} Resolves to an array of room IDs where
|
|
192
|
+
* the intent is joined.
|
|
193
|
+
*/
|
|
194
|
+
@timedIntentFunctionCall()
|
|
195
|
+
public async getJoinedRooms(): Promise<string[]> {
|
|
196
|
+
await this.ensureRegistered();
|
|
197
|
+
if (this.knownJoinedRooms.length === 0) await this.refreshJoinedRooms();
|
|
198
|
+
return this.knownJoinedRooms.map(r => r); // clone
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Leaves the given room.
|
|
203
|
+
* @param {string} roomId The room ID to leave
|
|
204
|
+
* @param {string=} reason Optional reason to be included as the reason for leaving the room.
|
|
205
|
+
* @returns {Promise<any>} Resolves when the room has been left.
|
|
206
|
+
*/
|
|
207
|
+
@timedIntentFunctionCall()
|
|
208
|
+
public async leaveRoom(roomId: string, reason?: string): Promise<any> {
|
|
209
|
+
await this.ensureRegistered();
|
|
210
|
+
return this.client.leaveRoom(roomId, reason).then(async () => {
|
|
211
|
+
// Recalculate joined rooms now that we've left a room
|
|
212
|
+
await this.refreshJoinedRooms();
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Joins the given room
|
|
218
|
+
* @param {string} roomIdOrAlias the room ID or alias to join
|
|
219
|
+
* @returns {Promise<string>} resolves to the joined room ID
|
|
220
|
+
*/
|
|
221
|
+
@timedIntentFunctionCall()
|
|
222
|
+
public async joinRoom(roomIdOrAlias: string): Promise<string> {
|
|
223
|
+
await this.ensureRegistered();
|
|
224
|
+
return this.client.joinRoom(roomIdOrAlias).then(async roomId => {
|
|
225
|
+
// Recalculate joined rooms now that we've joined a room
|
|
226
|
+
await this.refreshJoinedRooms();
|
|
227
|
+
return roomId;
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Sends a text message to a room.
|
|
233
|
+
* @param {string} roomId The room ID to send text to.
|
|
234
|
+
* @param {string} body The message body to send.
|
|
235
|
+
* @param {"m.text" | "m.emote" | "m.notice"} msgtype The message type to send.
|
|
236
|
+
* @returns {Promise<string>} Resolves to the event ID of the sent message.
|
|
237
|
+
*/
|
|
238
|
+
@timedIntentFunctionCall()
|
|
239
|
+
public async sendText(roomId: string, body: string, msgtype: "m.text" | "m.emote" | "m.notice" = "m.text"): Promise<string> {
|
|
240
|
+
return this.sendEvent(roomId, { body: body, msgtype: msgtype });
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Sends an event to a room.
|
|
245
|
+
* @param {string} roomId The room ID to send the event to.
|
|
246
|
+
* @param {any} content The content of the event.
|
|
247
|
+
* @returns {Promise<string>} Resolves to the event ID of the sent event.
|
|
248
|
+
*/
|
|
249
|
+
@timedIntentFunctionCall()
|
|
250
|
+
public async sendEvent(roomId: string, content: any): Promise<string> {
|
|
251
|
+
await this.ensureRegisteredAndJoined(roomId);
|
|
252
|
+
return this.client.sendMessage(roomId, content);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Ensures the user is registered and joined to the given room.
|
|
257
|
+
* @param {string} roomId The room ID to join
|
|
258
|
+
* @returns {Promise<any>} Resolves when complete
|
|
259
|
+
*/
|
|
260
|
+
@timedIntentFunctionCall()
|
|
261
|
+
public async ensureRegisteredAndJoined(roomId: string) {
|
|
262
|
+
await this.ensureRegistered();
|
|
263
|
+
await this.ensureJoined(roomId);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Ensures the user is joined to the given room
|
|
268
|
+
* @param {string} roomId The room ID to join
|
|
269
|
+
* @returns {Promise<any>} Resolves when complete
|
|
270
|
+
*/
|
|
271
|
+
@timedIntentFunctionCall()
|
|
272
|
+
public async ensureJoined(roomId: string) {
|
|
273
|
+
if (this.knownJoinedRooms.indexOf(roomId) !== -1) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
await this.refreshJoinedRooms();
|
|
278
|
+
|
|
279
|
+
if (this.knownJoinedRooms.indexOf(roomId) !== -1) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const returnedRoomId = await this.client.joinRoom(roomId);
|
|
284
|
+
if (!this.knownJoinedRooms.includes(returnedRoomId)) {
|
|
285
|
+
this.knownJoinedRooms.push(returnedRoomId);
|
|
286
|
+
}
|
|
287
|
+
return returnedRoomId;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Refreshes which rooms the user is joined to, potentially saving time on
|
|
292
|
+
* calls like ensureJoined()
|
|
293
|
+
* @returns {Promise<string[]>} Resolves to the joined room IDs for the user.
|
|
294
|
+
*/
|
|
295
|
+
@timedIntentFunctionCall()
|
|
296
|
+
public async refreshJoinedRooms(): Promise<string[]> {
|
|
297
|
+
this.knownJoinedRooms = await this.client.getJoinedRooms();
|
|
298
|
+
return this.knownJoinedRooms.map(r => r); // clone
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Ensures the user is registered
|
|
303
|
+
* @param deviceId An optional device ID to register with.
|
|
304
|
+
* @returns {Promise<any>} Resolves when complete
|
|
305
|
+
*/
|
|
306
|
+
@timedIntentFunctionCall()
|
|
307
|
+
public async ensureRegistered(deviceId?: string) {
|
|
308
|
+
if (!(await Promise.resolve(this.storage.isUserRegistered(this.userId)))) {
|
|
309
|
+
try {
|
|
310
|
+
const result = await this.client.doRequest("POST", "/_matrix/client/v3/register", null, {
|
|
311
|
+
type: "m.login.application_service",
|
|
312
|
+
username: this.userId.substring(1).split(":")[0],
|
|
313
|
+
device_id: deviceId,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// HACK: Workaround for unit tests
|
|
317
|
+
if (result['errcode']) {
|
|
318
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
319
|
+
throw { body: result }; // eslint-disable-line no-throw-literal
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
this.client.impersonateUserId(this.userId, result["device_id"]);
|
|
323
|
+
} catch (err) {
|
|
324
|
+
if (err instanceof MatrixError && err.errcode === "M_USER_IN_USE") {
|
|
325
|
+
await Promise.resolve(this.storage.addRegisteredUser(this.userId));
|
|
326
|
+
if (this.userId === this.appservice.botUserId) {
|
|
327
|
+
return null;
|
|
328
|
+
} else {
|
|
329
|
+
LogService.error("Appservice", "Error registering user: User ID is in use");
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
} else {
|
|
333
|
+
LogService.error("Appservice", "Encountered error registering user: ");
|
|
334
|
+
LogService.error("Appservice", extractRequestError(err));
|
|
335
|
+
}
|
|
336
|
+
throw err;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
await Promise.resolve(this.storage.addRegisteredUser(this.userId));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Appservice } from "./Appservice";
|
|
2
|
+
import { Intent } from "./Intent";
|
|
3
|
+
|
|
4
|
+
export const REMOTE_USER_INFO_ACCOUNT_DATA_EVENT_TYPE = "io.t2bot.sdk.bot.remote_user_info";
|
|
5
|
+
export const REMOTE_ROOM_INFO_ACCOUNT_DATA_EVENT_TYPE = "io.t2bot.sdk.bot.remote_room_info";
|
|
6
|
+
export const REMOTE_USER_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX = "io.t2bot.sdk.bot.remote_user_map";
|
|
7
|
+
export const REMOTE_ROOM_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX = "io.t2bot.sdk.bot.remote_room_map";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @see MatrixBridge
|
|
11
|
+
* @category Application services
|
|
12
|
+
*/
|
|
13
|
+
export interface IRemoteRoomInfo {
|
|
14
|
+
/**
|
|
15
|
+
* A unique identifier for the remote user.
|
|
16
|
+
*/
|
|
17
|
+
id: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @see MatrixBridge
|
|
22
|
+
* @category Application services
|
|
23
|
+
*/
|
|
24
|
+
export interface IRemoteUserInfo {
|
|
25
|
+
/**
|
|
26
|
+
* A unique identifier for the remote room (or room equivalent).
|
|
27
|
+
*/
|
|
28
|
+
id: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Utility class for common operations performed by bridges (represented
|
|
33
|
+
* as appservices).
|
|
34
|
+
*
|
|
35
|
+
* The storage utilities are not intended for bridges which allow 1:many
|
|
36
|
+
* relationships with the remote network.
|
|
37
|
+
*
|
|
38
|
+
* Bridges are generally expected to create their own classes which extend
|
|
39
|
+
* the IRemoteRoomInfo and IRemoteUserInfo interfaces and serialize to JSON
|
|
40
|
+
* cleanly. The serialized version of these classes is persisted in various
|
|
41
|
+
* account data locations for future lookups.
|
|
42
|
+
* @category Application services
|
|
43
|
+
*/
|
|
44
|
+
export class MatrixBridge {
|
|
45
|
+
constructor(private appservice: Appservice) {
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Gets information about a remote user.
|
|
50
|
+
* @param {Intent} userIntent The Matrix user intent to get information on.
|
|
51
|
+
* @returns {Promise<IRemoteUserInfo>} Resolves to the remote user information.
|
|
52
|
+
*/
|
|
53
|
+
public async getRemoteUserInfo<T extends IRemoteUserInfo>(userIntent: Intent): Promise<T> {
|
|
54
|
+
await userIntent.ensureRegistered();
|
|
55
|
+
return <Promise<T>>userIntent.underlyingClient.getAccountData(REMOTE_USER_INFO_ACCOUNT_DATA_EVENT_TYPE);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Sets information about a remote user. Calling this function will map the
|
|
60
|
+
* provided remote user ID to the intent's owner.
|
|
61
|
+
* @param {Intent} userIntent The Matrix user intent to store information on.
|
|
62
|
+
* @param {IRemoteUserInfo} remoteInfo The remote user information to store
|
|
63
|
+
* @returns {Promise<any>} Resolves when the information has been updated.
|
|
64
|
+
*/
|
|
65
|
+
public async setRemoteUserInfo<T extends IRemoteUserInfo>(userIntent: Intent, remoteInfo: T): Promise<any> {
|
|
66
|
+
await userIntent.ensureRegistered();
|
|
67
|
+
await userIntent.underlyingClient.setAccountData(REMOTE_USER_INFO_ACCOUNT_DATA_EVENT_TYPE, remoteInfo);
|
|
68
|
+
await this.updateRemoteUserMapping(userIntent.userId, remoteInfo.id);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Gets information about a remote room.
|
|
73
|
+
* @param {string} matrixRoomId The Matrix room ID to get information on.
|
|
74
|
+
* @returns {Promise<IRemoteRoomInfo>} Resolves to the remote room information.
|
|
75
|
+
*/
|
|
76
|
+
public async getRemoteRoomInfo<T extends IRemoteRoomInfo>(matrixRoomId: string): Promise<T> {
|
|
77
|
+
const bridgeBot = this.appservice.botIntent;
|
|
78
|
+
await bridgeBot.ensureRegistered();
|
|
79
|
+
// We do not need to ensure the user is joined to the room because we can associate
|
|
80
|
+
// room account data with any arbitrary room.
|
|
81
|
+
return <Promise<T>>bridgeBot.underlyingClient.getRoomAccountData(REMOTE_ROOM_INFO_ACCOUNT_DATA_EVENT_TYPE, matrixRoomId);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Sets information about a remote room. Calling this function will map the
|
|
86
|
+
* provided remote room ID to the matrix room ID.
|
|
87
|
+
* @param {string} matrixRoomId The Matrix room ID to store information on.
|
|
88
|
+
* @param {IRemoteRoomInfo} remoteInfo The remote room information to store
|
|
89
|
+
* @returns {Promise<any>} Resolves when the information has been updated.
|
|
90
|
+
*/
|
|
91
|
+
public async setRemoteRoomInfo<T extends IRemoteRoomInfo>(matrixRoomId: string, remoteInfo: T): Promise<any> {
|
|
92
|
+
const bridgeBot = this.appservice.botIntent;
|
|
93
|
+
await bridgeBot.ensureRegistered();
|
|
94
|
+
// We do not need to ensure the user is joined to the room because we can associate
|
|
95
|
+
// room account data with any arbitrary room.
|
|
96
|
+
await bridgeBot.underlyingClient.setRoomAccountData(REMOTE_ROOM_INFO_ACCOUNT_DATA_EVENT_TYPE, matrixRoomId, remoteInfo);
|
|
97
|
+
await this.updateRemoteRoomMapping(matrixRoomId, remoteInfo.id);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Gets the Matrix room ID for the provided remote room ID.
|
|
102
|
+
* @param {string} remoteRoomId The remote room ID to look up.
|
|
103
|
+
* @returns {Promise<string>} Resolves to the Matrix room ID.
|
|
104
|
+
*/
|
|
105
|
+
public async getMatrixRoomIdForRemote(remoteRoomId: string): Promise<string> {
|
|
106
|
+
const eventType = `${REMOTE_ROOM_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX}.${remoteRoomId}`;
|
|
107
|
+
const bridgeBot = this.appservice.botIntent;
|
|
108
|
+
await bridgeBot.ensureRegistered();
|
|
109
|
+
const result = await bridgeBot.underlyingClient.getAccountData(eventType);
|
|
110
|
+
return result['id'];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Gets a Matrix user intent for the provided remote user ID.
|
|
115
|
+
* @param {string} remoteUserId The remote user ID to look up.
|
|
116
|
+
* @returns {Promise<Intent>} Resolves to the Matrix user intent.
|
|
117
|
+
*/
|
|
118
|
+
public async getIntentForRemote(remoteUserId: string): Promise<Intent> {
|
|
119
|
+
const eventType = `${REMOTE_USER_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX}.${remoteUserId}`;
|
|
120
|
+
const bridgeBot = this.appservice.botIntent;
|
|
121
|
+
await bridgeBot.ensureRegistered();
|
|
122
|
+
const result = await bridgeBot.underlyingClient.getAccountData(eventType);
|
|
123
|
+
return this.appservice.getIntentForUserId(result['id']);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private async updateRemoteUserMapping(matrixUserId: string, remoteUserId: string): Promise<any> {
|
|
127
|
+
const eventType = `${REMOTE_USER_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX}.${remoteUserId}`;
|
|
128
|
+
const bridgeBot = this.appservice.botIntent;
|
|
129
|
+
await bridgeBot.ensureRegistered();
|
|
130
|
+
await bridgeBot.underlyingClient.setAccountData(eventType, { id: matrixUserId });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private async updateRemoteRoomMapping(matrixRoomId: string, remoteRoomId: string): Promise<any> {
|
|
134
|
+
const eventType = `${REMOTE_ROOM_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX}.${remoteRoomId}`;
|
|
135
|
+
const bridgeBot = this.appservice.botIntent;
|
|
136
|
+
await bridgeBot.ensureRegistered();
|
|
137
|
+
await bridgeBot.underlyingClient.setAccountData(eventType, { id: matrixRoomId });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { MatrixClient } from "../MatrixClient";
|
|
2
|
+
import { MSC2716BatchSendResponse } from "../models/MSC2176";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Unstable APIs that shouldn't be used in most circumstances for appservices.
|
|
6
|
+
* @category Unstable APIs
|
|
7
|
+
*/
|
|
8
|
+
export class UnstableAppserviceApis {
|
|
9
|
+
private requestId = 0;
|
|
10
|
+
|
|
11
|
+
constructor(private client: MatrixClient) {
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Send several historical events into a room.
|
|
16
|
+
* @see https://github.com/matrix-org/matrix-doc/pull/2716
|
|
17
|
+
* @param {string} roomId The roomID to send to.
|
|
18
|
+
* @param {string} prevEventId The event ID where this batch will be inserted
|
|
19
|
+
* @param {string} chunkId The chunk ID returned from a previous call. Set falsy to start at the beginning.
|
|
20
|
+
* @param {any[]} events A set of event contents for events to be inserted into the room.
|
|
21
|
+
* @param {any[]} stateEventsAtStart A set of state events to be inserted into the room. Defaults to empty.
|
|
22
|
+
* @returns A set of eventIds and the next chunk ID
|
|
23
|
+
*/
|
|
24
|
+
public async sendHistoricalEventBatch(roomId: string, prevEventId: string, events: any[], stateEventsAtStart: any[] = [], chunkId?: string): Promise<MSC2716BatchSendResponse> {
|
|
25
|
+
return this.client.doRequest("POST", `/_matrix/client/unstable/org.matrix.msc2716/rooms/${encodeURIComponent(roomId)}/batch_send`, {
|
|
26
|
+
prev_event: prevEventId,
|
|
27
|
+
chunk_id: chunkId,
|
|
28
|
+
}, {
|
|
29
|
+
events,
|
|
30
|
+
state_events_at_start: stateEventsAtStart,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Sends an event to the given room with a given timestamp.
|
|
36
|
+
* @param {string} roomId the room ID to send the event to
|
|
37
|
+
* @param {string} eventType the type of event to send
|
|
38
|
+
* @param {string} content the event body to send
|
|
39
|
+
* @param {number} ts The origin_server_ts of the new event
|
|
40
|
+
* @returns {Promise<string>} resolves to the event ID that represents the event
|
|
41
|
+
*/
|
|
42
|
+
public async sendEventWithTimestamp(roomId: string, eventType: string, content: any, ts: number) {
|
|
43
|
+
const txnId = `${(new Date().getTime())}__inc_appts${++this.requestId}`;
|
|
44
|
+
const path = `/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`;
|
|
45
|
+
const response = await this.client.doRequest("PUT", path, { ts }, content);
|
|
46
|
+
return response.event_id;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Sends a state event to the given room with a given timestamp.
|
|
51
|
+
* @param {string} roomId the room ID to send the event to
|
|
52
|
+
* @param {string} type the event type to send
|
|
53
|
+
* @param {string} stateKey the state key to send, should not be null
|
|
54
|
+
* @param {string} content the event body to send
|
|
55
|
+
* @param {number} ts The origin_server_ts of the new event
|
|
56
|
+
* @returns {Promise<string>} resolves to the event ID that represents the message
|
|
57
|
+
*/
|
|
58
|
+
public async sendStateEventWithTimestamp(roomId: string, type: string, stateKey: string, content: any, ts: number): Promise<string> {
|
|
59
|
+
const path = `/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(type)}/${encodeURIComponent(stateKey)}`;
|
|
60
|
+
const response = await this.client.doRequest("PUT", path, { ts }, content);
|
|
61
|
+
return response.event_id;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the response format documented in
|
|
3
|
+
* https://matrix.org/docs/spec/application_service/r0.1.2#get-matrix-app-v1-thirdparty-protocol-protocol
|
|
4
|
+
* @category Application services
|
|
5
|
+
*/
|
|
6
|
+
export interface IApplicationServiceProtocol {
|
|
7
|
+
user_fields: string[];
|
|
8
|
+
location_fields: string[];
|
|
9
|
+
icon: string;
|
|
10
|
+
field_types: { [field: string]: IFieldType };
|
|
11
|
+
instances: { [name: string]: IProtocolInstance };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface IFieldType {
|
|
15
|
+
regexp: string;
|
|
16
|
+
placeholder: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface IProtocolInstance {
|
|
20
|
+
desc: string;
|
|
21
|
+
icon: string;
|
|
22
|
+
fields: { [field: string]: string };
|
|
23
|
+
network_id: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* This is the response format for an MSC3983 `/keys/claim` request.
|
|
28
|
+
* See https://github.com/matrix-org/matrix-spec-proposals/pull/3983
|
|
29
|
+
* @deprecated This can be removed at any time without notice as it is unstable functionality.
|
|
30
|
+
* @category Application services
|
|
31
|
+
*/
|
|
32
|
+
export interface MSC3983KeyClaimResponse {
|
|
33
|
+
[userId: string]: {
|
|
34
|
+
[deviceId: string]: {
|
|
35
|
+
[keyId: string]: {
|
|
36
|
+
// for signed_curve25519 keys
|
|
37
|
+
key: string;
|
|
38
|
+
signatures: {
|
|
39
|
+
[userId: string]: {
|
|
40
|
+
[keyId: string]: string;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* This is the response format for an MSC3984 `/keys/query` request.
|
|
50
|
+
* See https://github.com/matrix-org/matrix-spec-proposals/pull/3984
|
|
51
|
+
* @deprecated This can be removed at any time without notice as it is unstable functionality.
|
|
52
|
+
* @category Application services
|
|
53
|
+
*/
|
|
54
|
+
export interface MSC3984KeyQueryResponse {
|
|
55
|
+
device_keys: {
|
|
56
|
+
[userId: string]: {
|
|
57
|
+
[deviceId: string]: {
|
|
58
|
+
algorithms: string[];
|
|
59
|
+
device_id: string;
|
|
60
|
+
user_id: string;
|
|
61
|
+
keys: {
|
|
62
|
+
[keyId: string]: string;
|
|
63
|
+
};
|
|
64
|
+
signatures: {
|
|
65
|
+
[userId: string]: {
|
|
66
|
+
[keyId: string]: string;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
unsigned?: {
|
|
70
|
+
[key: string]: any;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// TODO: Cross-signing support
|
|
77
|
+
}
|
package/src/b64.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encodes Base64.
|
|
3
|
+
* @category Utilities
|
|
4
|
+
* @param {ArrayBuffer | Uint8Array} b The buffer to encode.
|
|
5
|
+
* @returns {string} The Base64 string.
|
|
6
|
+
*/
|
|
7
|
+
export function encodeBase64(b: ArrayBuffer | Uint8Array): string {
|
|
8
|
+
if (b instanceof ArrayBuffer) {
|
|
9
|
+
return Buffer.from(b).toString('base64');
|
|
10
|
+
} else {
|
|
11
|
+
return Buffer.from(b.buffer, b.byteOffset, b.byteLength).toString('base64');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Encodes Unpadded Base64.
|
|
17
|
+
* @category Utilities
|
|
18
|
+
* @param {ArrayBuffer | Uint8Array} b The buffer to encode.
|
|
19
|
+
* @returns {string} The Base64 string.
|
|
20
|
+
*/
|
|
21
|
+
export function encodeUnpaddedBase64(b: ArrayBuffer | Uint8Array): string {
|
|
22
|
+
return encodeBase64(b).replace(/=+/g, '');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Encodes URL-Safe Unpadded Base64.
|
|
27
|
+
* @category Utilities
|
|
28
|
+
* @param {ArrayBuffer | Uint8Array} b The buffer to encode.
|
|
29
|
+
* @returns {string} The Base64 string.
|
|
30
|
+
*/
|
|
31
|
+
export function encodeUnpaddedUrlSafeBase64(b: ArrayBuffer | Uint8Array): string {
|
|
32
|
+
return encodeUnpaddedBase64(b).replace(/\+/g, '-').replace(/\//g, '_');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Decodes Base64.
|
|
37
|
+
* @category Utilities
|
|
38
|
+
* @param {string} s The Base64 string.
|
|
39
|
+
* @returns {Uint8Array} The encoded data as a buffer.
|
|
40
|
+
*/
|
|
41
|
+
export function decodeBase64(s: string): Uint8Array {
|
|
42
|
+
return Buffer.from(s, 'base64');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Decodes Unpadded Base64.
|
|
47
|
+
* @category Utilities
|
|
48
|
+
* @param {string} s The Base64 string.
|
|
49
|
+
* @returns {Uint8Array} The encoded data as a buffer.
|
|
50
|
+
*/
|
|
51
|
+
export function decodeUnpaddedBase64(s: string): Uint8Array {
|
|
52
|
+
return decodeBase64(s); // yay, it's the same
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Decodes URL-Safe Unpadded Base64.
|
|
57
|
+
* @category Utilities
|
|
58
|
+
* @param {string} s The Base64 string.
|
|
59
|
+
* @returns {Uint8Array} The encoded data as a buffer.
|
|
60
|
+
*/
|
|
61
|
+
export function decodeUnpaddedUrlSafeBase64(s: string): Uint8Array {
|
|
62
|
+
return decodeUnpaddedBase64(s.replace(/-/g, '+').replace(/_/g, '/'));
|
|
63
|
+
}
|