@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.
Files changed (377) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -2
  3. package/lib/AdminApis.d.ts +52 -0
  4. package/lib/AdminApis.js +29 -0
  5. package/lib/AdminApis.js.map +1 -0
  6. package/lib/DMs.d.ts +48 -0
  7. package/lib/DMs.js +160 -0
  8. package/lib/DMs.js.map +1 -0
  9. package/lib/IFilter.d.ts +7 -0
  10. package/lib/IFilter.js +3 -0
  11. package/lib/IFilter.js.map +1 -0
  12. package/lib/MatrixAuth.d.ts +45 -0
  13. package/lib/MatrixAuth.js +125 -0
  14. package/lib/MatrixAuth.js.map +1 -0
  15. package/lib/MatrixClient.d.ts +834 -0
  16. package/lib/MatrixClient.js +2268 -0
  17. package/lib/MatrixClient.js.map +1 -0
  18. package/lib/PantalaimonClient.d.ts +42 -0
  19. package/lib/PantalaimonClient.js +44 -0
  20. package/lib/PantalaimonClient.js.map +1 -0
  21. package/lib/SynapseAdminApis.d.ts +350 -0
  22. package/lib/SynapseAdminApis.js +244 -0
  23. package/lib/SynapseAdminApis.js.map +1 -0
  24. package/lib/SynchronousMatrixClient.d.ts +82 -0
  25. package/lib/SynchronousMatrixClient.js +130 -0
  26. package/lib/SynchronousMatrixClient.js.map +1 -0
  27. package/lib/UnstableApis.d.ts +43 -0
  28. package/lib/UnstableApis.js +74 -0
  29. package/lib/UnstableApis.js.map +1 -0
  30. package/lib/appservice/Appservice.d.ts +328 -0
  31. package/lib/appservice/Appservice.js +803 -0
  32. package/lib/appservice/Appservice.js.map +1 -0
  33. package/lib/appservice/Intent.d.ts +113 -0
  34. package/lib/appservice/Intent.js +363 -0
  35. package/lib/appservice/Intent.js.map +1 -0
  36. package/lib/appservice/MatrixBridge.d.ts +85 -0
  37. package/lib/appservice/MatrixBridge.js +111 -0
  38. package/lib/appservice/MatrixBridge.js.map +1 -0
  39. package/lib/appservice/UnstableAppserviceApis.d.ts +41 -0
  40. package/lib/appservice/UnstableAppserviceApis.js +62 -0
  41. package/lib/appservice/UnstableAppserviceApis.js.map +1 -0
  42. package/lib/appservice/http_responses.d.ts +77 -0
  43. package/lib/appservice/http_responses.js +3 -0
  44. package/lib/appservice/http_responses.js.map +1 -0
  45. package/lib/b64.d.ts +42 -0
  46. package/lib/b64.js +68 -0
  47. package/lib/b64.js.map +1 -0
  48. package/lib/e2ee/CrossSigningBootstrap.d.ts +73 -0
  49. package/lib/e2ee/CrossSigningBootstrap.js +86 -0
  50. package/lib/e2ee/CrossSigningBootstrap.js.map +1 -0
  51. package/lib/e2ee/CryptoClient.d.ts +163 -0
  52. package/lib/e2ee/CryptoClient.js +382 -0
  53. package/lib/e2ee/CryptoClient.js.map +1 -0
  54. package/lib/e2ee/ICryptoRoomInformation.d.ts +8 -0
  55. package/lib/e2ee/ICryptoRoomInformation.js +3 -0
  56. package/lib/e2ee/ICryptoRoomInformation.js.map +1 -0
  57. package/lib/e2ee/RoomTracker.d.ts +41 -0
  58. package/lib/e2ee/RoomTracker.js +96 -0
  59. package/lib/e2ee/RoomTracker.js.map +1 -0
  60. package/lib/e2ee/RustEngine.d.ts +149 -0
  61. package/lib/e2ee/RustEngine.js +467 -0
  62. package/lib/e2ee/RustEngine.js.map +1 -0
  63. package/lib/e2ee/decorators.d.ts +10 -0
  64. package/lib/e2ee/decorators.js +37 -0
  65. package/lib/e2ee/decorators.js.map +1 -0
  66. package/lib/helpers/MatrixEntity.d.ts +37 -0
  67. package/lib/helpers/MatrixEntity.js +67 -0
  68. package/lib/helpers/MatrixEntity.js.map +1 -0
  69. package/lib/helpers/MatrixGlob.d.ts +22 -0
  70. package/lib/helpers/MatrixGlob.js +35 -0
  71. package/lib/helpers/MatrixGlob.js.map +1 -0
  72. package/lib/helpers/MentionPill.d.ts +40 -0
  73. package/lib/helpers/MentionPill.js +91 -0
  74. package/lib/helpers/MentionPill.js.map +1 -0
  75. package/lib/helpers/Permalinks.d.ts +58 -0
  76. package/lib/helpers/Permalinks.js +73 -0
  77. package/lib/helpers/Permalinks.js.map +1 -0
  78. package/lib/helpers/ProfileCache.d.ts +48 -0
  79. package/lib/helpers/ProfileCache.js +114 -0
  80. package/lib/helpers/ProfileCache.js.map +1 -0
  81. package/lib/helpers/RichReply.d.ts +17 -0
  82. package/lib/helpers/RichReply.js +47 -0
  83. package/lib/helpers/RichReply.js.map +1 -0
  84. package/lib/helpers/UnpaddedBase64.d.ts +31 -0
  85. package/lib/helpers/UnpaddedBase64.js +45 -0
  86. package/lib/helpers/UnpaddedBase64.js.map +1 -0
  87. package/lib/http.d.ts +17 -0
  88. package/lib/http.js +147 -0
  89. package/lib/http.js.map +1 -0
  90. package/lib/identity/IdentityClient.d.ts +93 -0
  91. package/lib/identity/IdentityClient.js +248 -0
  92. package/lib/identity/IdentityClient.js.map +1 -0
  93. package/lib/index.d.ts +91 -0
  94. package/lib/index.js +125 -0
  95. package/lib/index.js.map +1 -0
  96. package/lib/logging/ConsoleLogger.d.ts +12 -0
  97. package/lib/logging/ConsoleLogger.js +28 -0
  98. package/lib/logging/ConsoleLogger.js.map +1 -0
  99. package/lib/logging/ILogger.d.ts +36 -0
  100. package/lib/logging/ILogger.js +3 -0
  101. package/lib/logging/ILogger.js.map +1 -0
  102. package/lib/logging/LogService.d.ts +100 -0
  103. package/lib/logging/LogService.js +169 -0
  104. package/lib/logging/LogService.js.map +1 -0
  105. package/lib/logging/RichConsoleLogger.d.ts +20 -0
  106. package/lib/logging/RichConsoleLogger.js +41 -0
  107. package/lib/logging/RichConsoleLogger.js.map +1 -0
  108. package/lib/metrics/IMetricListener.d.ts +52 -0
  109. package/lib/metrics/IMetricListener.js +3 -0
  110. package/lib/metrics/IMetricListener.js.map +1 -0
  111. package/lib/metrics/Metrics.d.ts +66 -0
  112. package/lib/metrics/Metrics.js +128 -0
  113. package/lib/metrics/Metrics.js.map +1 -0
  114. package/lib/metrics/contexts.d.ts +51 -0
  115. package/lib/metrics/contexts.js +3 -0
  116. package/lib/metrics/contexts.js.map +1 -0
  117. package/lib/metrics/decorators.d.ts +15 -0
  118. package/lib/metrics/decorators.js +92 -0
  119. package/lib/metrics/decorators.js.map +1 -0
  120. package/lib/metrics/names.d.ts +45 -0
  121. package/lib/metrics/names.js +49 -0
  122. package/lib/metrics/names.js.map +1 -0
  123. package/lib/mixins/AutojoinRoomsMixin.d.ts +10 -0
  124. package/lib/mixins/AutojoinRoomsMixin.js +25 -0
  125. package/lib/mixins/AutojoinRoomsMixin.js.map +1 -0
  126. package/lib/mixins/AutojoinUpgradedRoomsMixin.d.ts +10 -0
  127. package/lib/mixins/AutojoinUpgradedRoomsMixin.js +40 -0
  128. package/lib/mixins/AutojoinUpgradedRoomsMixin.js.map +1 -0
  129. package/lib/models/Account.d.ts +8 -0
  130. package/lib/models/Account.js +3 -0
  131. package/lib/models/Account.js.map +1 -0
  132. package/lib/models/CreateRoom.d.ts +125 -0
  133. package/lib/models/CreateRoom.js +3 -0
  134. package/lib/models/CreateRoom.js.map +1 -0
  135. package/lib/models/Crypto.d.ts +167 -0
  136. package/lib/models/Crypto.js +31 -0
  137. package/lib/models/Crypto.js.map +1 -0
  138. package/lib/models/EventContext.d.ts +19 -0
  139. package/lib/models/EventContext.js +3 -0
  140. package/lib/models/EventContext.js.map +1 -0
  141. package/lib/models/IdentityServerModels.d.ts +20 -0
  142. package/lib/models/IdentityServerModels.js +3 -0
  143. package/lib/models/IdentityServerModels.js.map +1 -0
  144. package/lib/models/MSC2176.d.ts +42 -0
  145. package/lib/models/MSC2176.js +3 -0
  146. package/lib/models/MSC2176.js.map +1 -0
  147. package/lib/models/MatrixError.d.ts +38 -0
  148. package/lib/models/MatrixError.js +30 -0
  149. package/lib/models/MatrixError.js.map +1 -0
  150. package/lib/models/MatrixProfile.d.ts +43 -0
  151. package/lib/models/MatrixProfile.js +43 -0
  152. package/lib/models/MatrixProfile.js.map +1 -0
  153. package/lib/models/OpenIDConnect.d.ts +10 -0
  154. package/lib/models/OpenIDConnect.js +3 -0
  155. package/lib/models/OpenIDConnect.js.map +1 -0
  156. package/lib/models/Policies.d.ts +25 -0
  157. package/lib/models/Policies.js +3 -0
  158. package/lib/models/Policies.js.map +1 -0
  159. package/lib/models/PowerLevelAction.d.ts +27 -0
  160. package/lib/models/PowerLevelAction.js +32 -0
  161. package/lib/models/PowerLevelAction.js.map +1 -0
  162. package/lib/models/PowerLevelBounds.d.ts +15 -0
  163. package/lib/models/PowerLevelBounds.js +3 -0
  164. package/lib/models/PowerLevelBounds.js.map +1 -0
  165. package/lib/models/Presence.d.ts +25 -0
  166. package/lib/models/Presence.js +38 -0
  167. package/lib/models/Presence.js.map +1 -0
  168. package/lib/models/ServerVersions.d.ts +8 -0
  169. package/lib/models/ServerVersions.js +3 -0
  170. package/lib/models/ServerVersions.js.map +1 -0
  171. package/lib/models/Spaces.d.ts +116 -0
  172. package/lib/models/Spaces.js +91 -0
  173. package/lib/models/Spaces.js.map +1 -0
  174. package/lib/models/Threepid.d.ts +8 -0
  175. package/lib/models/Threepid.js +3 -0
  176. package/lib/models/Threepid.js.map +1 -0
  177. package/lib/models/events/AliasesEvent.d.ts +27 -0
  178. package/lib/models/events/AliasesEvent.js +27 -0
  179. package/lib/models/events/AliasesEvent.js.map +1 -0
  180. package/lib/models/events/CanonicalAliasEvent.d.ts +23 -0
  181. package/lib/models/events/CanonicalAliasEvent.js +21 -0
  182. package/lib/models/events/CanonicalAliasEvent.js.map +1 -0
  183. package/lib/models/events/CreateEvent.d.ts +63 -0
  184. package/lib/models/events/CreateEvent.js +33 -0
  185. package/lib/models/events/CreateEvent.js.map +1 -0
  186. package/lib/models/events/EncryptedRoomEvent.d.ts +42 -0
  187. package/lib/models/events/EncryptedRoomEvent.js +28 -0
  188. package/lib/models/events/EncryptedRoomEvent.js.map +1 -0
  189. package/lib/models/events/EncryptionEvent.d.ts +47 -0
  190. package/lib/models/events/EncryptionEvent.js +42 -0
  191. package/lib/models/events/EncryptionEvent.js.map +1 -0
  192. package/lib/models/events/Event.d.ts +26 -0
  193. package/lib/models/events/Event.js +40 -0
  194. package/lib/models/events/Event.js.map +1 -0
  195. package/lib/models/events/EventKind.d.ts +15 -0
  196. package/lib/models/events/EventKind.js +20 -0
  197. package/lib/models/events/EventKind.js.map +1 -0
  198. package/lib/models/events/InvalidEventError.d.ts +14 -0
  199. package/lib/models/events/InvalidEventError.js +24 -0
  200. package/lib/models/events/InvalidEventError.js.map +1 -0
  201. package/lib/models/events/JoinRulesEvent.d.ts +29 -0
  202. package/lib/models/events/JoinRulesEvent.js +21 -0
  203. package/lib/models/events/JoinRulesEvent.js.map +1 -0
  204. package/lib/models/events/MembershipEvent.d.ts +54 -0
  205. package/lib/models/events/MembershipEvent.js +49 -0
  206. package/lib/models/events/MembershipEvent.js.map +1 -0
  207. package/lib/models/events/MessageEvent.d.ts +233 -0
  208. package/lib/models/events/MessageEvent.js +49 -0
  209. package/lib/models/events/MessageEvent.js.map +1 -0
  210. package/lib/models/events/PinnedEventsEvent.d.ts +23 -0
  211. package/lib/models/events/PinnedEventsEvent.js +21 -0
  212. package/lib/models/events/PinnedEventsEvent.js.map +1 -0
  213. package/lib/models/events/PowerLevelsEvent.d.ts +96 -0
  214. package/lib/models/events/PowerLevelsEvent.js +70 -0
  215. package/lib/models/events/PowerLevelsEvent.js.map +1 -0
  216. package/lib/models/events/PresenceEvent.d.ts +56 -0
  217. package/lib/models/events/PresenceEvent.js +21 -0
  218. package/lib/models/events/PresenceEvent.js.map +1 -0
  219. package/lib/models/events/RedactionEvent.d.ts +28 -0
  220. package/lib/models/events/RedactionEvent.js +36 -0
  221. package/lib/models/events/RedactionEvent.js.map +1 -0
  222. package/lib/models/events/RoomAvatarEvent.d.ts +28 -0
  223. package/lib/models/events/RoomAvatarEvent.js +21 -0
  224. package/lib/models/events/RoomAvatarEvent.js.map +1 -0
  225. package/lib/models/events/RoomEvent.d.ts +58 -0
  226. package/lib/models/events/RoomEvent.js +57 -0
  227. package/lib/models/events/RoomEvent.js.map +1 -0
  228. package/lib/models/events/RoomNameEvent.d.ts +20 -0
  229. package/lib/models/events/RoomNameEvent.js +21 -0
  230. package/lib/models/events/RoomNameEvent.js.map +1 -0
  231. package/lib/models/events/RoomTopicEvent.d.ts +20 -0
  232. package/lib/models/events/RoomTopicEvent.js +21 -0
  233. package/lib/models/events/RoomTopicEvent.js.map +1 -0
  234. package/lib/models/events/SpaceChildEvent.d.ts +53 -0
  235. package/lib/models/events/SpaceChildEvent.js +49 -0
  236. package/lib/models/events/SpaceChildEvent.js.map +1 -0
  237. package/lib/models/events/converter.d.ts +8 -0
  238. package/lib/models/events/converter.js +50 -0
  239. package/lib/models/events/converter.js.map +1 -0
  240. package/lib/models/unstable/MediaInfo.d.ts +16 -0
  241. package/lib/models/unstable/MediaInfo.js +3 -0
  242. package/lib/models/unstable/MediaInfo.js.map +1 -0
  243. package/lib/preprocessors/IPreprocessor.d.ts +22 -0
  244. package/lib/preprocessors/IPreprocessor.js +3 -0
  245. package/lib/preprocessors/IPreprocessor.js.map +1 -0
  246. package/lib/preprocessors/RichRepliesPreprocessor.d.ts +67 -0
  247. package/lib/preprocessors/RichRepliesPreprocessor.js +112 -0
  248. package/lib/preprocessors/RichRepliesPreprocessor.js.map +1 -0
  249. package/lib/request.d.ts +13 -0
  250. package/lib/request.js +23 -0
  251. package/lib/request.js.map +1 -0
  252. package/lib/simple-validation.d.ts +10 -0
  253. package/lib/simple-validation.js +30 -0
  254. package/lib/simple-validation.js.map +1 -0
  255. package/lib/storage/IAppserviceStorageProvider.d.ts +48 -0
  256. package/lib/storage/IAppserviceStorageProvider.js +3 -0
  257. package/lib/storage/IAppserviceStorageProvider.js.map +1 -0
  258. package/lib/storage/ICryptoStorageProvider.d.ts +32 -0
  259. package/lib/storage/ICryptoStorageProvider.js +3 -0
  260. package/lib/storage/ICryptoStorageProvider.js.map +1 -0
  261. package/lib/storage/IStorageProvider.d.ts +47 -0
  262. package/lib/storage/IStorageProvider.js +3 -0
  263. package/lib/storage/IStorageProvider.js.map +1 -0
  264. package/lib/storage/MemoryStorageProvider.d.ts +26 -0
  265. package/lib/storage/MemoryStorageProvider.js +55 -0
  266. package/lib/storage/MemoryStorageProvider.js.map +1 -0
  267. package/lib/storage/RustSdkCryptoStorageProvider.d.ts +38 -0
  268. package/lib/storage/RustSdkCryptoStorageProvider.js +69 -0
  269. package/lib/storage/RustSdkCryptoStorageProvider.js.map +1 -0
  270. package/lib/storage/SimpleFsStorageProvider.d.ts +31 -0
  271. package/lib/storage/SimpleFsStorageProvider.js +122 -0
  272. package/lib/storage/SimpleFsStorageProvider.js.map +1 -0
  273. package/lib/storage/SimplePostgresStorageProvider.d.ts +32 -0
  274. package/lib/storage/SimplePostgresStorageProvider.js +148 -0
  275. package/lib/storage/SimplePostgresStorageProvider.js.map +1 -0
  276. package/lib/strategies/AppserviceJoinRoomStrategy.d.ts +14 -0
  277. package/lib/strategies/AppserviceJoinRoomStrategy.js +54 -0
  278. package/lib/strategies/AppserviceJoinRoomStrategy.js.map +1 -0
  279. package/lib/strategies/JoinRoomStrategy.d.ts +11 -0
  280. package/lib/strategies/JoinRoomStrategy.js +44 -0
  281. package/lib/strategies/JoinRoomStrategy.js.map +1 -0
  282. package/package.json +136 -4
  283. package/src/AdminApis.ts +60 -0
  284. package/src/DMs.ts +164 -0
  285. package/src/IFilter.ts +7 -0
  286. package/src/MatrixAuth.ts +129 -0
  287. package/src/MatrixClient.ts +2046 -0
  288. package/src/PantalaimonClient.ts +62 -0
  289. package/src/SynapseAdminApis.ts +482 -0
  290. package/src/SynchronousMatrixClient.ts +129 -0
  291. package/src/UnstableApis.ts +75 -0
  292. package/src/appservice/Appservice.ts +1089 -0
  293. package/src/appservice/Intent.ts +342 -0
  294. package/src/appservice/MatrixBridge.ts +139 -0
  295. package/src/appservice/UnstableAppserviceApis.ts +63 -0
  296. package/src/appservice/http_responses.ts +77 -0
  297. package/src/b64.ts +63 -0
  298. package/src/e2ee/CrossSigningBootstrap.ts +141 -0
  299. package/src/e2ee/CryptoClient.ts +378 -0
  300. package/src/e2ee/ICryptoRoomInformation.ts +9 -0
  301. package/src/e2ee/RoomTracker.ts +99 -0
  302. package/src/e2ee/RustEngine.ts +580 -0
  303. package/src/e2ee/decorators.ts +38 -0
  304. package/src/helpers/MatrixEntity.ts +65 -0
  305. package/src/helpers/MatrixGlob.ts +38 -0
  306. package/src/helpers/MentionPill.ts +95 -0
  307. package/src/helpers/Permalinks.ts +101 -0
  308. package/src/helpers/ProfileCache.ts +119 -0
  309. package/src/helpers/RichReply.ts +46 -0
  310. package/src/helpers/UnpaddedBase64.ts +44 -0
  311. package/src/http.ts +166 -0
  312. package/src/identity/IdentityClient.ts +225 -0
  313. package/src/index.ts +125 -0
  314. package/src/logging/ConsoleLogger.ts +31 -0
  315. package/src/logging/ILogger.ts +40 -0
  316. package/src/logging/LogService.ts +167 -0
  317. package/src/logging/RichConsoleLogger.ts +70 -0
  318. package/src/metrics/IMetricListener.ts +57 -0
  319. package/src/metrics/Metrics.ts +134 -0
  320. package/src/metrics/contexts.ts +56 -0
  321. package/src/metrics/decorators.ts +94 -0
  322. package/src/metrics/names.ts +53 -0
  323. package/src/mixins/AutojoinRoomsMixin.ts +24 -0
  324. package/src/mixins/AutojoinUpgradedRoomsMixin.ts +37 -0
  325. package/src/models/Account.ts +8 -0
  326. package/src/models/CreateRoom.ts +144 -0
  327. package/src/models/Crypto.ts +188 -0
  328. package/src/models/EventContext.ts +23 -0
  329. package/src/models/IdentityServerModels.ts +18 -0
  330. package/src/models/MSC2176.ts +47 -0
  331. package/src/models/MatrixError.ts +39 -0
  332. package/src/models/MatrixProfile.ts +55 -0
  333. package/src/models/OpenIDConnect.ts +10 -0
  334. package/src/models/Policies.ts +28 -0
  335. package/src/models/PowerLevelAction.ts +31 -0
  336. package/src/models/PowerLevelBounds.ts +16 -0
  337. package/src/models/Presence.ts +38 -0
  338. package/src/models/ServerVersions.ts +8 -0
  339. package/src/models/Spaces.ts +167 -0
  340. package/src/models/Threepid.ts +8 -0
  341. package/src/models/events/AliasesEvent.ts +37 -0
  342. package/src/models/events/CanonicalAliasEvent.ts +30 -0
  343. package/src/models/events/CreateEvent.ts +82 -0
  344. package/src/models/events/EncryptedRoomEvent.ts +58 -0
  345. package/src/models/events/EncryptionEvent.ts +63 -0
  346. package/src/models/events/Event.ts +38 -0
  347. package/src/models/events/EventKind.ts +16 -0
  348. package/src/models/events/InvalidEventError.ts +19 -0
  349. package/src/models/events/JoinRulesEvent.ts +37 -0
  350. package/src/models/events/MembershipEvent.ts +77 -0
  351. package/src/models/events/MessageEvent.ts +292 -0
  352. package/src/models/events/PinnedEventsEvent.ts +30 -0
  353. package/src/models/events/PowerLevelsEvent.ts +135 -0
  354. package/src/models/events/PresenceEvent.ts +69 -0
  355. package/src/models/events/RedactionEvent.ts +44 -0
  356. package/src/models/events/RoomAvatarEvent.ts +36 -0
  357. package/src/models/events/RoomEvent.ts +83 -0
  358. package/src/models/events/RoomNameEvent.ts +27 -0
  359. package/src/models/events/RoomTopicEvent.ts +27 -0
  360. package/src/models/events/SpaceChildEvent.ts +74 -0
  361. package/src/models/events/_MissingEvents.md +35 -0
  362. package/src/models/events/converter.ts +47 -0
  363. package/src/models/unstable/MediaInfo.ts +16 -0
  364. package/src/preprocessors/IPreprocessor.ts +24 -0
  365. package/src/preprocessors/RichRepliesPreprocessor.ts +159 -0
  366. package/src/request.ts +21 -0
  367. package/src/simple-validation.ts +30 -0
  368. package/src/storage/IAppserviceStorageProvider.ts +54 -0
  369. package/src/storage/ICryptoStorageProvider.ts +36 -0
  370. package/src/storage/IStorageProvider.ts +53 -0
  371. package/src/storage/MemoryStorageProvider.ts +65 -0
  372. package/src/storage/RustSdkCryptoStorageProvider.ts +81 -0
  373. package/src/storage/SimpleFsStorageProvider.ts +142 -0
  374. package/src/storage/SimplePostgresStorageProvider.ts +168 -0
  375. package/src/strategies/AppserviceJoinRoomStrategy.ts +47 -0
  376. package/src/strategies/JoinRoomStrategy.ts +45 -0
  377. 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
+ }