@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,225 @@
|
|
|
1
|
+
import * as crypto from "crypto";
|
|
2
|
+
|
|
3
|
+
import { OpenIDConnectToken } from "../models/OpenIDConnect";
|
|
4
|
+
import { doHttpRequest } from "../http";
|
|
5
|
+
import { timedIdentityClientFunctionCall } from "../metrics/decorators";
|
|
6
|
+
import { Policies, TranslatedPolicy } from "../models/Policies";
|
|
7
|
+
import { Metrics } from "../metrics/Metrics";
|
|
8
|
+
import { Threepid } from "../models/Threepid";
|
|
9
|
+
import { UnpaddedBase64 } from "../helpers/UnpaddedBase64";
|
|
10
|
+
import { MatrixClient } from "../MatrixClient";
|
|
11
|
+
import { MatrixProfile, MatrixProfileInfo } from "../models/MatrixProfile";
|
|
12
|
+
import { IdentityServerAccount, IdentityServerInvite } from "../models/IdentityServerModels";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A way to access an Identity Server using the Identity Service API from a MatrixClient.
|
|
16
|
+
* @category Identity Servers
|
|
17
|
+
*/
|
|
18
|
+
export class IdentityClient {
|
|
19
|
+
/**
|
|
20
|
+
* The metrics instance for this client. Note that metrics for the underlying MatrixClient will
|
|
21
|
+
* not be available here.
|
|
22
|
+
*/
|
|
23
|
+
public readonly metrics: Metrics;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* If truthy, this is a string that will be supplied as `?brand=$brand` where endpoints can
|
|
27
|
+
* result in communications to a user.
|
|
28
|
+
*/
|
|
29
|
+
public brand: string;
|
|
30
|
+
|
|
31
|
+
private constructor(public readonly accessToken: string, public readonly serverUrl: string, public readonly matrixClient: MatrixClient) {
|
|
32
|
+
this.metrics = new Metrics();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Gets account information for the logged in user.
|
|
37
|
+
* @returns {Promise<IdentityServerAccount>} Resolves to the account information
|
|
38
|
+
*/
|
|
39
|
+
@timedIdentityClientFunctionCall()
|
|
40
|
+
public getAccount(): Promise<IdentityServerAccount> {
|
|
41
|
+
return this.doRequest("GET", "/_matrix/identity/v2/account");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Gets the terms of service for which the identity server has.
|
|
46
|
+
* @returns {Promise<Policies>} Resolves to the policies of the server.
|
|
47
|
+
*/
|
|
48
|
+
@timedIdentityClientFunctionCall()
|
|
49
|
+
public getTermsOfService(): Promise<Policies> {
|
|
50
|
+
return this.doRequest("GET", "/_matrix/identity/v2/terms");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Accepts a given set of URLs from Policy objects returned by the server. This implies acceptance of
|
|
55
|
+
* the terms. Note that this will not update the user's account data to consider these terms accepted
|
|
56
|
+
* in the future - that is an exercise left to the caller.
|
|
57
|
+
* @param {string[]} termsUrls The URLs to count as accepted.
|
|
58
|
+
* @returns {Promise<void>} Resolves when complete.
|
|
59
|
+
*/
|
|
60
|
+
@timedIdentityClientFunctionCall()
|
|
61
|
+
public acceptTerms(termsUrls: string[]): Promise<void> {
|
|
62
|
+
return this.doRequest("POST", "/_matrix/identity/v2/terms", null, {
|
|
63
|
+
user_accepts: termsUrls,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Accepts all the terms of service offered by the identity server. Note that this is only meant to be
|
|
69
|
+
* used by automated bots where terms acceptance is implicit - the terms of service need to be presented
|
|
70
|
+
* to the user in most cases.
|
|
71
|
+
* @returns {Promise<void>} Resolves when complete.
|
|
72
|
+
*/
|
|
73
|
+
@timedIdentityClientFunctionCall()
|
|
74
|
+
public async acceptAllTerms(): Promise<void> {
|
|
75
|
+
const terms = await this.getTermsOfService();
|
|
76
|
+
const urls = new Set<string>();
|
|
77
|
+
for (const policy of Object.values(terms.policies)) {
|
|
78
|
+
let chosenLang = policy["en"] as TranslatedPolicy;
|
|
79
|
+
if (!chosenLang) {
|
|
80
|
+
chosenLang = policy[Object.keys(policy).find(k => k !== "version")] as TranslatedPolicy;
|
|
81
|
+
}
|
|
82
|
+
if (!chosenLang) continue; // skip - invalid
|
|
83
|
+
urls.add(chosenLang.url);
|
|
84
|
+
}
|
|
85
|
+
return await this.acceptTerms(Array.from(urls));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Looks up a series of third party identifiers (email addresses or phone numbers) to see if they have
|
|
90
|
+
* associated mappings. The returned array will be ordered the same as the input, and have falsey values
|
|
91
|
+
* in place of any failed/missing lookups (eg: no mapping).
|
|
92
|
+
* @param {Threepid[]} identifiers The identifiers to look up.
|
|
93
|
+
* @param {boolean} allowPlaintext If true, the function will accept the server's offer to use plaintext
|
|
94
|
+
* lookups when no other methods are available. The function will always prefer hashed methods.
|
|
95
|
+
* @returns {Promise<string[]>} Resolves to the user IDs (or falsey values) in the same order as the input.
|
|
96
|
+
*/
|
|
97
|
+
@timedIdentityClientFunctionCall()
|
|
98
|
+
public async lookup(identifiers: Threepid[], allowPlaintext = false): Promise<string[]> {
|
|
99
|
+
const hashInfo = await this.doRequest("GET", "/_matrix/identity/v2/hash_details");
|
|
100
|
+
if (!hashInfo?.["algorithms"]) throw new Error("Server not supported: invalid response");
|
|
101
|
+
|
|
102
|
+
const algorithms = hashInfo?.["algorithms"];
|
|
103
|
+
let algorithm = algorithms.find(a => a === "sha256");
|
|
104
|
+
if (!algorithm && allowPlaintext) algorithm = algorithms.find(a => a === "none");
|
|
105
|
+
if (!algorithm) throw new Error("No supported hashing algorithm found");
|
|
106
|
+
|
|
107
|
+
const body = {
|
|
108
|
+
algorithm,
|
|
109
|
+
pepper: hashInfo["lookup_pepper"],
|
|
110
|
+
addresses: [],
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
for (const pid of identifiers) {
|
|
114
|
+
let transformed = null;
|
|
115
|
+
switch (algorithm) {
|
|
116
|
+
case "none":
|
|
117
|
+
transformed = `${pid.address.toLowerCase()} ${pid.kind}`;
|
|
118
|
+
break;
|
|
119
|
+
case "sha256":
|
|
120
|
+
transformed = UnpaddedBase64.encodeBufferUrlSafe(crypto.createHash("sha256")
|
|
121
|
+
.update(`${pid.address.toLowerCase()} ${pid.kind} ${body.pepper}`).digest());
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
throw new Error("Unsupported hashing algorithm (programming error)");
|
|
125
|
+
}
|
|
126
|
+
body.addresses.push(transformed);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const resp = await this.doRequest("POST", "/_matrix/identity/v2/lookup", null, body);
|
|
130
|
+
const mappings = resp?.["mappings"] || {};
|
|
131
|
+
const mxids: string[] = [];
|
|
132
|
+
for (const addr of body.addresses) {
|
|
133
|
+
mxids.push(mappings[addr]);
|
|
134
|
+
}
|
|
135
|
+
return mxids;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Creates a third party email invite. This will store the invite in the identity server, but
|
|
140
|
+
* not publish the invite to the room - the caller is expected to handle the remaining part. Note
|
|
141
|
+
* that this function is not required to be called when using the Client-Server API for inviting
|
|
142
|
+
* third party addresses to a room. This will make several calls into the room state to populate
|
|
143
|
+
* the invite details, therefore the inviter (the client backing this identity client) must be
|
|
144
|
+
* present in the room.
|
|
145
|
+
* @param {string} emailAddress The email address to invite.
|
|
146
|
+
* @param {string} roomId The room ID to invite to.
|
|
147
|
+
* @returns {Promise<IdentityServerInvite>} Resolves to the identity server's stored invite.
|
|
148
|
+
*/
|
|
149
|
+
@timedIdentityClientFunctionCall()
|
|
150
|
+
public async makeEmailInvite(emailAddress: string, roomId: string): Promise<IdentityServerInvite> {
|
|
151
|
+
const req = {
|
|
152
|
+
address: emailAddress,
|
|
153
|
+
medium: "email",
|
|
154
|
+
room_id: roomId,
|
|
155
|
+
sender: await this.matrixClient.getUserId(),
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const tryFetch = async (eventType: string, stateKey: string): Promise<any> => {
|
|
159
|
+
try {
|
|
160
|
+
return await this.matrixClient.getRoomStateEvent(roomId, eventType, stateKey);
|
|
161
|
+
} catch (e) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const canonicalAlias = (await tryFetch("m.room.canonical_alias", ""))?.["alias"];
|
|
167
|
+
const roomName = (await tryFetch("m.room.name", ""))?.["name"];
|
|
168
|
+
|
|
169
|
+
req["room_alias"] = canonicalAlias;
|
|
170
|
+
req["room_avatar_url"] = (await tryFetch("m.room.avatar", ""))?.["url"];
|
|
171
|
+
req["room_name"] = roomName || canonicalAlias;
|
|
172
|
+
req["room_join_rules"] = (await tryFetch("m.room.join_rules", ""))?.["join_rule"];
|
|
173
|
+
|
|
174
|
+
let profileInfo: MatrixProfileInfo;
|
|
175
|
+
try {
|
|
176
|
+
profileInfo = await this.matrixClient.getUserProfile(await this.matrixClient.getUserId());
|
|
177
|
+
} catch (e) {
|
|
178
|
+
// ignore
|
|
179
|
+
}
|
|
180
|
+
const senderProfile = new MatrixProfile(await this.matrixClient.getUserId(), profileInfo);
|
|
181
|
+
req["sender_avatar_url"] = senderProfile.avatarUrl;
|
|
182
|
+
req["sender_display_name"] = senderProfile.displayName;
|
|
183
|
+
|
|
184
|
+
const inviteReq = {};
|
|
185
|
+
for (const entry of Object.entries(req)) {
|
|
186
|
+
if (entry[1]) inviteReq[entry[0]] = entry[1];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const qs = {};
|
|
190
|
+
if (this.brand) qs['brand'] = this.brand;
|
|
191
|
+
return await this.doRequest("POST", "/_matrix/identity/v2/store-invite", qs, inviteReq);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Performs a web request to the server, applying appropriate authorization headers for
|
|
196
|
+
* this client.
|
|
197
|
+
* @param {"GET"|"POST"|"PUT"|"DELETE"} method The HTTP method to use in the request
|
|
198
|
+
* @param {string} endpoint The endpoint to call. For example: "/_matrix/identity/v2/account"
|
|
199
|
+
* @param {any} qs The query string to send. Optional.
|
|
200
|
+
* @param {any} body The request body to send. Optional. Will be converted to JSON unless the type is a Buffer.
|
|
201
|
+
* @param {number} timeout The number of milliseconds to wait before timing out.
|
|
202
|
+
* @param {boolean} raw If true, the raw response will be returned instead of the response body.
|
|
203
|
+
* @param {string} contentType The content type to send. Only used if the `body` is a Buffer.
|
|
204
|
+
* @param {string} noEncoding Set to true to disable encoding, and return a Buffer. Defaults to false
|
|
205
|
+
* @returns {Promise<any>} Resolves to the response (body), rejected if a non-2xx status code was returned.
|
|
206
|
+
*/
|
|
207
|
+
@timedIdentityClientFunctionCall()
|
|
208
|
+
public doRequest(method, endpoint, qs = null, body = null, timeout = 60000, raw = false, contentType = "application/json", noEncoding = false): Promise<any> {
|
|
209
|
+
const headers = {};
|
|
210
|
+
if (this.accessToken) {
|
|
211
|
+
headers["Authorization"] = `Bearer ${this.accessToken}`;
|
|
212
|
+
}
|
|
213
|
+
return doHttpRequest(this.serverUrl, method, endpoint, qs, body, headers, timeout, raw, contentType, noEncoding);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Gets an instance of an identity client.
|
|
218
|
+
* @param {OpenIDConnectToken} oidc The OpenID Connect token to register to the identity server with.
|
|
219
|
+
* @param {string} serverUrl The full URL where the identity server can be reached at.
|
|
220
|
+
*/
|
|
221
|
+
public static async acquire(oidc: OpenIDConnectToken, serverUrl: string, mxClient: MatrixClient): Promise<IdentityClient> {
|
|
222
|
+
const account = await doHttpRequest(serverUrl, "POST", "/_matrix/identity/v2/account/register", null, oidc);
|
|
223
|
+
return new IdentityClient(account['token'], serverUrl, mxClient);
|
|
224
|
+
}
|
|
225
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Appservices
|
|
2
|
+
export * from "./appservice/Appservice";
|
|
3
|
+
export * from "./appservice/Intent";
|
|
4
|
+
export * from "./appservice/MatrixBridge";
|
|
5
|
+
export * from "./appservice/http_responses";
|
|
6
|
+
export * from "./appservice/UnstableAppserviceApis";
|
|
7
|
+
|
|
8
|
+
// Encryption
|
|
9
|
+
export * from "./e2ee/RoomTracker";
|
|
10
|
+
export * from "./e2ee/CryptoClient";
|
|
11
|
+
export * from "./e2ee/decorators";
|
|
12
|
+
// export * from "./e2ee/RustEngine";
|
|
13
|
+
export {
|
|
14
|
+
passwordUiaCallback,
|
|
15
|
+
UIACallback,
|
|
16
|
+
EnsureCrossSigningOptions,
|
|
17
|
+
EnsureCrossSigningResult,
|
|
18
|
+
EnsureSecretStorageResult,
|
|
19
|
+
} from "./e2ee/CrossSigningBootstrap";
|
|
20
|
+
export * from "./e2ee/ICryptoRoomInformation";
|
|
21
|
+
|
|
22
|
+
// Helpers
|
|
23
|
+
export * from "./helpers/RichReply";
|
|
24
|
+
export * from "./helpers/MentionPill";
|
|
25
|
+
export * from "./helpers/Permalinks";
|
|
26
|
+
export * from "./helpers/MatrixGlob";
|
|
27
|
+
export * from "./helpers/ProfileCache";
|
|
28
|
+
export * from "./helpers/MatrixEntity";
|
|
29
|
+
export * from "./helpers/UnpaddedBase64";
|
|
30
|
+
|
|
31
|
+
// Logging
|
|
32
|
+
export * from "./logging/ConsoleLogger";
|
|
33
|
+
export * from "./logging/RichConsoleLogger";
|
|
34
|
+
export * from "./logging/ILogger";
|
|
35
|
+
export * from "./logging/LogService";
|
|
36
|
+
|
|
37
|
+
// Metrics
|
|
38
|
+
export * from "./metrics/contexts";
|
|
39
|
+
export * from "./metrics/names";
|
|
40
|
+
export * from "./metrics/decorators";
|
|
41
|
+
export * from "./metrics/IMetricListener";
|
|
42
|
+
export * from "./metrics/Metrics";
|
|
43
|
+
|
|
44
|
+
// Mixins
|
|
45
|
+
export * from "./mixins/AutojoinRoomsMixin";
|
|
46
|
+
export * from "./mixins/AutojoinUpgradedRoomsMixin";
|
|
47
|
+
|
|
48
|
+
// Models
|
|
49
|
+
export * from "./models/Presence";
|
|
50
|
+
export * from "./models/MatrixProfile";
|
|
51
|
+
export * from "./models/EventContext";
|
|
52
|
+
export * from "./models/PowerLevelBounds";
|
|
53
|
+
export * from "./models/OpenIDConnect";
|
|
54
|
+
export * from "./models/Policies";
|
|
55
|
+
export * from "./models/Threepid";
|
|
56
|
+
export * from "./models/Spaces";
|
|
57
|
+
export * from "./models/IdentityServerModels";
|
|
58
|
+
export * from "./models/Crypto";
|
|
59
|
+
export * from "./models/MSC2176";
|
|
60
|
+
export * from "./models/Account";
|
|
61
|
+
export * from "./models/PowerLevelAction";
|
|
62
|
+
export * from "./models/ServerVersions";
|
|
63
|
+
export * from "./models/MatrixError";
|
|
64
|
+
export * from "./models/CreateRoom";
|
|
65
|
+
|
|
66
|
+
// Unstable models
|
|
67
|
+
export * from "./models/unstable/MediaInfo";
|
|
68
|
+
|
|
69
|
+
// Event models
|
|
70
|
+
export * from "./models/events/EventKind";
|
|
71
|
+
export * from "./models/events/converter";
|
|
72
|
+
export * from "./models/events/InvalidEventError";
|
|
73
|
+
export * from "./models/events/Event";
|
|
74
|
+
export * from "./models/events/RoomEvent";
|
|
75
|
+
export * from "./models/events/PresenceEvent";
|
|
76
|
+
export * from "./models/events/MembershipEvent";
|
|
77
|
+
export * from "./models/events/MessageEvent";
|
|
78
|
+
export * from "./models/events/AliasesEvent";
|
|
79
|
+
export * from "./models/events/CanonicalAliasEvent";
|
|
80
|
+
export * from "./models/events/CreateEvent";
|
|
81
|
+
export * from "./models/events/JoinRulesEvent";
|
|
82
|
+
export * from "./models/events/PowerLevelsEvent";
|
|
83
|
+
export * from "./models/events/RedactionEvent";
|
|
84
|
+
export * from "./models/events/PinnedEventsEvent";
|
|
85
|
+
export * from "./models/events/RoomAvatarEvent";
|
|
86
|
+
export * from "./models/events/RoomNameEvent";
|
|
87
|
+
export * from "./models/events/RoomTopicEvent";
|
|
88
|
+
export * from "./models/events/SpaceChildEvent";
|
|
89
|
+
export * from "./models/events/EncryptionEvent";
|
|
90
|
+
export * from "./models/events/EncryptedRoomEvent";
|
|
91
|
+
|
|
92
|
+
// Preprocessors
|
|
93
|
+
export * from "./preprocessors/IPreprocessor";
|
|
94
|
+
export * from "./preprocessors/RichRepliesPreprocessor";
|
|
95
|
+
|
|
96
|
+
// Storage stuff
|
|
97
|
+
export * from "./storage/IAppserviceStorageProvider";
|
|
98
|
+
export * from "./storage/IStorageProvider";
|
|
99
|
+
export * from "./storage/MemoryStorageProvider";
|
|
100
|
+
export * from "./storage/SimpleFsStorageProvider";
|
|
101
|
+
export * from "./storage/ICryptoStorageProvider";
|
|
102
|
+
export * from "./storage/RustSdkCryptoStorageProvider";
|
|
103
|
+
export * from "./storage/SimplePostgresStorageProvider";
|
|
104
|
+
|
|
105
|
+
// Strategies
|
|
106
|
+
export * from "./strategies/AppserviceJoinRoomStrategy";
|
|
107
|
+
export * from "./strategies/JoinRoomStrategy";
|
|
108
|
+
|
|
109
|
+
// Other clients
|
|
110
|
+
export * from "./identity/IdentityClient";
|
|
111
|
+
|
|
112
|
+
// Root-level stuff
|
|
113
|
+
export * from "./IFilter";
|
|
114
|
+
export * from "./MatrixClient";
|
|
115
|
+
export * from "./MatrixAuth";
|
|
116
|
+
export * from "./UnstableApis";
|
|
117
|
+
export * from "./AdminApis";
|
|
118
|
+
export * from "./request";
|
|
119
|
+
export * from "./PantalaimonClient";
|
|
120
|
+
export * from "./SynchronousMatrixClient";
|
|
121
|
+
export * from "./SynapseAdminApis";
|
|
122
|
+
export * from "./simple-validation";
|
|
123
|
+
export * from "./b64";
|
|
124
|
+
export * from "./http";
|
|
125
|
+
export * from "./DMs";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ILogger } from "./ILogger";
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-console */
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Logs to the console in a plain format. This is the default logger.
|
|
7
|
+
* @category Logging
|
|
8
|
+
*/
|
|
9
|
+
export class ConsoleLogger implements ILogger {
|
|
10
|
+
public trace(module: string, ...messageOrObject: any[]) {
|
|
11
|
+
console.trace(module, ...messageOrObject);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public debug(module: string, ...messageOrObject: any[]) {
|
|
15
|
+
console.debug(module, ...messageOrObject);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public error(module: string, ...messageOrObject: any[]) {
|
|
19
|
+
console.error(module, ...messageOrObject);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public info(module: string, ...messageOrObject: any[]) {
|
|
23
|
+
console.log(module, ...messageOrObject);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public warn(module: string, ...messageOrObject: any[]) {
|
|
27
|
+
console.warn(module, ...messageOrObject);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* eslint-enable no-console */
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a logger
|
|
3
|
+
* @category Logging
|
|
4
|
+
*/
|
|
5
|
+
export interface ILogger {
|
|
6
|
+
/**
|
|
7
|
+
* Logs to the INFO channel
|
|
8
|
+
* @param {string} module The module being logged
|
|
9
|
+
* @param {any[]} messageOrObject The data to log
|
|
10
|
+
*/
|
|
11
|
+
info(module: string, ...messageOrObject: any[]);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Logs to the WARN channel
|
|
15
|
+
* @param {string} module The module being logged
|
|
16
|
+
* @param {any[]} messageOrObject The data to log
|
|
17
|
+
*/
|
|
18
|
+
warn(module: string, ...messageOrObject: any[]);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Logs to the ERROR channel
|
|
22
|
+
* @param {string} module The module being logged
|
|
23
|
+
* @param {any[]} messageOrObject The data to log
|
|
24
|
+
*/
|
|
25
|
+
error(module: string, ...messageOrObject: any[]);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Logs to the DEBUG channel
|
|
29
|
+
* @param {string} module The module being logged
|
|
30
|
+
* @param {any[]} messageOrObject The data to log
|
|
31
|
+
*/
|
|
32
|
+
debug(module: string, ...messageOrObject: any[]);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Logs to the TRACE channel
|
|
36
|
+
* @param {string} module The module being logged
|
|
37
|
+
* @param {any[]} messageOrObject The data to log
|
|
38
|
+
*/
|
|
39
|
+
trace(module: string, ...messageOrObject: any[]);
|
|
40
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { MatrixError } from "../models/MatrixError";
|
|
2
|
+
import { ConsoleLogger } from "./ConsoleLogger";
|
|
3
|
+
import { ILogger } from "./ILogger";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The log levels to log at.
|
|
7
|
+
* @category Logging
|
|
8
|
+
*/
|
|
9
|
+
export class LogLevel {
|
|
10
|
+
/**
|
|
11
|
+
* The TRACE channel
|
|
12
|
+
*/
|
|
13
|
+
public static readonly TRACE = new LogLevel("TRACE", -1);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The DEBUG channel
|
|
17
|
+
*/
|
|
18
|
+
public static readonly DEBUG = new LogLevel("DEBUG", 0);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The INFO channel
|
|
22
|
+
*/
|
|
23
|
+
public static readonly INFO = new LogLevel("INFO", 1);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The WARN channel
|
|
27
|
+
*/
|
|
28
|
+
public static readonly WARN = new LogLevel("WARN", 2);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The ERROR channel
|
|
32
|
+
*/
|
|
33
|
+
public static readonly ERROR = new LogLevel("ERROR", 3);
|
|
34
|
+
|
|
35
|
+
private constructor(private level: string, private sequence: number) {
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public includes(level: LogLevel): boolean {
|
|
39
|
+
return level.sequence >= this.sequence;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public toString(): string {
|
|
43
|
+
return this.level;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static fromString(level: string, defaultLevel = LogLevel.DEBUG): LogLevel {
|
|
47
|
+
if (!level) return defaultLevel;
|
|
48
|
+
if (level.toUpperCase() === LogLevel.TRACE.level) return LogLevel.TRACE;
|
|
49
|
+
if (level.toUpperCase() === LogLevel.DEBUG.level) return LogLevel.DEBUG;
|
|
50
|
+
if (level.toUpperCase() === LogLevel.INFO.level) return LogLevel.INFO;
|
|
51
|
+
if (level.toUpperCase() === LogLevel.WARN.level) return LogLevel.WARN;
|
|
52
|
+
if (level.toUpperCase() === LogLevel.ERROR.level) return LogLevel.ERROR;
|
|
53
|
+
return defaultLevel;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Service class for logging in the bot-sdk
|
|
59
|
+
* @category Logging
|
|
60
|
+
*/
|
|
61
|
+
export class LogService {
|
|
62
|
+
private static logger: ILogger = new ConsoleLogger();
|
|
63
|
+
private static logLevel: LogLevel = LogLevel.INFO;
|
|
64
|
+
private static mutedModules: string[] = [];
|
|
65
|
+
|
|
66
|
+
private constructor() {
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The level at which the LogService is running.
|
|
71
|
+
*/
|
|
72
|
+
public static get level(): LogLevel {
|
|
73
|
+
return this.logLevel;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Sets the log level for this logger. Defaults to DEBUG.
|
|
78
|
+
* @param {LogLevel} level the new log level
|
|
79
|
+
*/
|
|
80
|
+
public static setLevel(level: LogLevel) {
|
|
81
|
+
LogService.logLevel = level || LogLevel.DEBUG;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Sets a new logger for the Log Service
|
|
86
|
+
* @param {ILogger} logger the new logger
|
|
87
|
+
*/
|
|
88
|
+
public static setLogger(logger: ILogger) {
|
|
89
|
+
LogService.logger = logger;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Mutes a module from the logger.
|
|
94
|
+
* @param {string} name The module name to mute.
|
|
95
|
+
*/
|
|
96
|
+
public static muteModule(name: string) {
|
|
97
|
+
LogService.mutedModules.push(name);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Logs to the TRACE channel
|
|
102
|
+
* @param {string} module The module being logged
|
|
103
|
+
* @param {any[]} messageOrObject The data to log
|
|
104
|
+
*/
|
|
105
|
+
public static trace(module: string, ...messageOrObject: any[]) {
|
|
106
|
+
if (!LogService.logLevel.includes(LogLevel.TRACE)) return;
|
|
107
|
+
if (LogService.mutedModules.includes(module)) return;
|
|
108
|
+
LogService.logger.trace(module, ...messageOrObject);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Logs to the DEBUG channel
|
|
113
|
+
* @param {string} module The module being logged
|
|
114
|
+
* @param {any[]} messageOrObject The data to log
|
|
115
|
+
*/
|
|
116
|
+
public static debug(module: string, ...messageOrObject: any[]) {
|
|
117
|
+
if (!LogService.logLevel.includes(LogLevel.DEBUG)) return;
|
|
118
|
+
if (LogService.mutedModules.includes(module)) return;
|
|
119
|
+
LogService.logger.debug(module, ...messageOrObject);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Logs to the ERROR channel
|
|
124
|
+
* @param {string} module The module being logged
|
|
125
|
+
* @param {any[]} messageOrObject The data to log
|
|
126
|
+
*/
|
|
127
|
+
public static error(module: string, ...messageOrObject: any[]) {
|
|
128
|
+
if (!LogService.logLevel.includes(LogLevel.ERROR)) return;
|
|
129
|
+
if (LogService.mutedModules.includes(module)) return;
|
|
130
|
+
LogService.logger.error(module, ...messageOrObject);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Logs to the INFO channel
|
|
135
|
+
* @param {string} module The module being logged
|
|
136
|
+
* @param {any[]} messageOrObject The data to log
|
|
137
|
+
*/
|
|
138
|
+
public static info(module: string, ...messageOrObject: any[]) {
|
|
139
|
+
if (!LogService.logLevel.includes(LogLevel.INFO)) return;
|
|
140
|
+
if (LogService.mutedModules.includes(module)) return;
|
|
141
|
+
LogService.logger.info(module, ...messageOrObject);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Logs to the WARN channel
|
|
146
|
+
* @param {string} module The module being logged
|
|
147
|
+
* @param {any[]} messageOrObject The data to log
|
|
148
|
+
*/
|
|
149
|
+
public static warn(module: string, ...messageOrObject: any[]) {
|
|
150
|
+
if (!LogService.logLevel.includes(LogLevel.WARN)) return;
|
|
151
|
+
if (LogService.mutedModules.includes(module)) return;
|
|
152
|
+
LogService.logger.warn(module, ...messageOrObject);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Extracts the useful part of a request's error into something loggable.
|
|
158
|
+
* @param {Error} err The error to parse.
|
|
159
|
+
* @returns {*} The extracted error, or the given error if unaltered.
|
|
160
|
+
* @category Logging
|
|
161
|
+
*/
|
|
162
|
+
export function extractRequestError(err: Error | MatrixError): any {
|
|
163
|
+
if (err && 'body' in err) {
|
|
164
|
+
return err.body;
|
|
165
|
+
}
|
|
166
|
+
return err;
|
|
167
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import * as chalk from "chalk";
|
|
2
|
+
|
|
3
|
+
import { ILogger } from "./ILogger";
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-console */
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Prints to the console with colors and a format.
|
|
9
|
+
* @category Logging
|
|
10
|
+
*/
|
|
11
|
+
export class RichConsoleLogger implements ILogger {
|
|
12
|
+
protected chalkDebug = chalk.cyan;
|
|
13
|
+
protected chalkInfo = chalk.green;
|
|
14
|
+
protected chalkWarning = chalk.yellow;
|
|
15
|
+
protected chalkError = chalk.bold.red;
|
|
16
|
+
protected chalkTimestamp = chalk.grey;
|
|
17
|
+
protected chalkModule = chalk.grey;
|
|
18
|
+
|
|
19
|
+
protected getTimestamp(): string {
|
|
20
|
+
const now = new Date(Date.now()).toUTCString();
|
|
21
|
+
return this.chalkTimestamp(now);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public trace(module: string, ...messageOrObject: any[]) {
|
|
25
|
+
console.trace(
|
|
26
|
+
this.getTimestamp(),
|
|
27
|
+
this.chalkDebug("[TRACE]"),
|
|
28
|
+
this.chalkModule(`[${module}]`),
|
|
29
|
+
...messageOrObject,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public debug(module: string, ...messageOrObject: any[]) {
|
|
34
|
+
console.debug(
|
|
35
|
+
this.getTimestamp(),
|
|
36
|
+
this.chalkDebug("[DEBUG]"),
|
|
37
|
+
this.chalkModule(`[${module}]`),
|
|
38
|
+
...messageOrObject,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public error(module: string, ...messageOrObject: any[]) {
|
|
43
|
+
console.error(
|
|
44
|
+
this.getTimestamp(),
|
|
45
|
+
this.chalkError("[ERROR]"),
|
|
46
|
+
this.chalkModule(`[${module}]`),
|
|
47
|
+
...messageOrObject,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public info(module: string, ...messageOrObject: any[]) {
|
|
52
|
+
console.log(
|
|
53
|
+
this.getTimestamp(),
|
|
54
|
+
this.chalkInfo("[INFO]"),
|
|
55
|
+
this.chalkModule(`[${module}]`),
|
|
56
|
+
...messageOrObject,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public warn(module: string, ...messageOrObject: any[]) {
|
|
61
|
+
console.warn(
|
|
62
|
+
this.getTimestamp(),
|
|
63
|
+
this.chalkWarning("[WARN]"),
|
|
64
|
+
this.chalkModule(`[${module}]`),
|
|
65
|
+
...messageOrObject,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/* eslint-enable no-console */
|