diff --git a/changelog.d/194.feature b/changelog.d/194.feature new file mode 100644 index 00000000..331e3765 --- /dev/null +++ b/changelog.d/194.feature @@ -0,0 +1 @@ +Bump matrix-js-sdk to 8.0.1 \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a7846c1f..4a8a5153 100644 --- a/package-lock.json +++ b/package-lock.json @@ -236,9 +236,9 @@ "dev": true }, "@babel/runtime": { - "version": "7.10.3", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.10.3.tgz", - "integrity": "sha512-RzGO0RLSdokm9Ipe/YD+7ww8X2Ro79qiXZF3HU9ljrM+qnJmH1Vqth+hbiQZy761LnMJTMitHDuKVYTk3k4dLw==", + "version": "7.11.2", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.11.2.tgz", + "integrity": "sha512-TeWkU52so0mPtDcaCTxNBI/IHiz0pZgr8VEFqXFtZWpYD08ZB6FaSwVAS8MKRQAP3bYKiVjwysOJgMFY28o6Tw==", "requires": { "regenerator-runtime": "^0.13.4" } @@ -2772,9 +2772,9 @@ } }, "matrix-js-sdk": { - "version": "6.2.2", - "resolved": "https://registry.npmjs.org/matrix-js-sdk/-/matrix-js-sdk-6.2.2.tgz", - "integrity": "sha512-uTXmKVzl7GXM3R70cl2E87H+mtwN3ILqPeB80Z/2ITN9Vaf9pMURCCAuHePEBXbhnD7DOZj7Cke42uP+ByR6Hw==", + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/matrix-js-sdk/-/matrix-js-sdk-8.0.1.tgz", + "integrity": "sha512-DT2YjWi8l2eHyNTKZOhBkN/EakIMDDEglSCg+RWY4QzFaXYlSwfwfzzCujjtq1hxVSKim8NC7KqxgNetOiBegA==", "requires": { "@babel/runtime": "^7.8.3", "another-json": "^0.2.0", @@ -3412,9 +3412,9 @@ } }, "regenerator-runtime": { - "version": "0.13.5", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.5.tgz", - "integrity": "sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA==" + "version": "0.13.7", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz", + "integrity": "sha512-a54FxoJDIr27pgf7IgeQGxmqUNYrcV338lf/6gH456HZ/PhX+5BcwHXG9ajESmwe6WRO0tAzRUrRmNONWgkrew==" }, "regex-not": { "version": "1.0.2", diff --git a/package.json b/package.json index 3eeb0743..476e72f7 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "is-my-json-valid": "^2.20.5", "js-yaml": "^3.14.0", "matrix-appservice": "^0.4.2", - "matrix-js-sdk": "^6.0.0", + "matrix-js-sdk": "^8.0.1", "nedb": "^1.8.0", "nopt": "^4.0.3", "p-queue": "^6.6.0", diff --git a/src/components/intent.ts b/src/components/intent.ts index db5ca4b2..5d77ac02 100644 --- a/src/components/intent.ts +++ b/src/components/intent.ts @@ -15,38 +15,12 @@ limitations under the License. import MatrixUser from "../models/users/matrix"; import JsSdk from "matrix-js-sdk"; + // eslint-disable-next-line @typescript-eslint/no-explicit-any const { MatrixEvent, RoomMember } = JsSdk as any; import ClientRequestCache from "./client-request-cache"; import { defer } from "../utils/promiseutil"; -type MatrixClient = { - credentials: { - userId: string; - }, - ban: (roomId: string, target: string, reason: string) => Promise; - createAlias: (alias: string, roomId: string) => Promise; - createRoom: (opts: Record) => Promise; - fetchRoomEvent: (roomId: string, eventId: string) => Promise; - getStateEvent: (roomId: string, eventType: string, stateKey: string) => Promise; - invite: (roomId: string, userId: string) => Promise; - joinRoom: (roomId: string, opts: Record) => Promise; - kick: (roomId: string, target: string, reason: string) => Promise; - leave: (roomId: string) => Promise; - register: (localpart: string) => Promise; - roomState: (roomId: string) => Promise; - sendEvent: (roomId: string, type: string, content: Record) => Promise; - sendReadReceipt: (event: unknown) => Promise; - sendStateEvent: (roomId: string, type: string, content: Record, skey: string) => Promise; - sendTyping: (roomId: string, isTyping: boolean) => Promise; - setAvatarUrl: (url: string) => Promise; - setDisplayName: (name: string) => Promise; - setPowerLevel: (roomId: string, target: string, level: number, event: unknown) => Promise; - // eslint-disable-next-line camelcase - setPresence: (presence: { presence: string, status_msg?: string }) => Promise; - getProfileInfo: (userId: string, info: string) => Promise; - unban: (roomId: string, target: string) => Promise; -}; type BridgeErrorReason = "m.event_not_handled" | "m.event_too_old" | "m.internal_error" | "m.foreign_network_error" | "m.event_unknown"; @@ -163,7 +137,7 @@ export class Intent { * @param opts.caching.ttl How long requests can stay in the cache, in milliseconds. * @param opts.caching.size How many entries should be kept in the cache, before the oldest is dropped. */ - constructor(private client: MatrixClient, private botClient: MatrixClient, opts: IntentOpts = {}) { + constructor(public readonly client: any, private readonly botClient: any, opts: IntentOpts = {}) { if (opts.backingStore) { if (!opts.backingStore.setPowerLevelContent || !opts.backingStore.getPowerLevelContent || @@ -326,9 +300,9 @@ export class Intent { * Set the power level of the given target. * @param roomId The room to set the power level in. * @param target The target user ID - * @param level The desired level + * @param level The desired level. Undefined will remove the users custom power level. */ - public async setPowerLevel(roomId: string, target: string, level: number) { + public async setPowerLevel(roomId: string, target: string, level: number|undefined) { await this._ensureJoined(roomId); const event = await this._ensureHasPowerLevelFor(roomId, "m.room.power_levels"); return this.client.setPowerLevel(roomId, target, level, event); @@ -471,7 +445,7 @@ export class Intent { * @param reason Optional. The reason for the kick. * @return Resolved when kickked, else rejected with an error. */ - public async kick(roomId: string, target: string, reason: string) { + public async kick(roomId: string, target: string, reason?: string) { await this._ensureJoined(roomId); return this.client.kick(roomId, target, reason); } @@ -485,7 +459,7 @@ export class Intent { * @param reason Optional. The reason for the ban. * @return Resolved when banned, else rejected with an error. */ - public async ban(roomId: string, target: string, reason: string) { + public async ban(roomId: string, target: string, reason?: string) { await this._ensureJoined(roomId); return this.client.ban(roomId, target, reason); } @@ -511,7 +485,7 @@ export class Intent { * @param viaServers The server names to try and join through in * addition to those that are automatically chosen. */ - public async join(roomId: string, viaServers: string[]) { + public async join(roomId: string, viaServers?: string[]) { await this._ensureJoined(roomId, false, viaServers); } diff --git a/src/components/request-factory.ts b/src/components/request-factory.ts index 40cdda33..676d45d1 100644 --- a/src/components/request-factory.ts +++ b/src/components/request-factory.ts @@ -22,7 +22,7 @@ type TimeoutFunction = (req: Request) => void; * A factory which can create {@link Request} objects. Useful for * adding "default" handlers to requests. */ -export class RequestFactory { +export class RequestFactory { private _resolves: HandlerFunction[] = []; private _rejects: HandlerFunction[] = []; private _timeouts: {fn: TimeoutFunction, timeout: number}[] = []; @@ -33,8 +33,8 @@ export class RequestFactory { * @param opts The options to pass to the Request constructor, if any. * @return A new request object */ - public newRequest(opts: RequestOpts) { - const req = new Request(opts); + public newRequest(opts?: RequestOpts) { + const req = new Request(opts || {data: null}); req.getPromise().then((res) => { this._resolves.forEach((resolveFn) => { resolveFn(req, res); diff --git a/src/components/state-lookup.ts b/src/components/state-lookup.ts index 7f604240..99a65817 100644 --- a/src/components/state-lookup.ts +++ b/src/components/state-lookup.ts @@ -18,7 +18,7 @@ import PQueue from "p-queue"; interface StateLookupOpts { // eslint-disable-next-line @typescript-eslint/no-explicit-any client: any; //TODO: Needs to be MatrixClient (once that becomes TypeScript) - stateLookupConcurrency: number; + stateLookupConcurrency?: number; eventTypes?: string[]; retryStateInMs?: number; } @@ -33,7 +33,7 @@ interface StateLookupRoom { }; } -interface StateLookupEvent { +export interface StateLookupEvent { // eslint-disable-next-line camelcase room_id: string; // eslint-disable-next-line camelcase @@ -41,6 +41,7 @@ interface StateLookupEvent { type: string; // eslint-disable-next-line camelcase event_id: string; + content: unknown; } const RETRY_STATE_IN_MS = 300; @@ -96,7 +97,7 @@ export class StateLookup { * array of events, which may be empty. * @return {?Object|Object[]} */ - public getState(roomId: string, eventType: string, stateKey?: string): unknown|unknown[] { + public getState(roomId: string, eventType: string, stateKey?: string): null|StateLookupEvent|StateLookupEvent[] { const r = this.dict[roomId]; if (!r) { return stateKey === undefined ? [] : null; @@ -121,12 +122,7 @@ export class StateLookup { () => this._client.roomState(roomId) ); events.forEach((ev) => { - if (this.eventTypes[ev.type]) { - if (!r.events[ev.type]) { - r.events[ev.type] = {}; - } - r.events[ev.type][ev.state_key] = ev; - } + this.insertEvent(r, ev); }); return r; } @@ -191,9 +187,22 @@ export class StateLookup { } // blunt update - if (!r.events[event.type]) { - r.events[event.type] = {}; + this.insertEvent(r, event); + } + + private insertEvent(roomSet: StateLookupRoom, event: StateLookupEvent) { + if (typeof event.content !== "object") { + // Reject - unexpected content type + return; + } + if (!event.type || !event.state_key) { + // Reject - missing keys + return; + } + // blunt update + if (!roomSet.events[event.type]) { + roomSet.events[event.type] = {}; } - r.events[event.type][event.state_key] = event; + roomSet.events[event.type][event.state_key] = event; } } diff --git a/src/index.ts b/src/index.ts index 8c2c1664..88ac7c5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,11 +19,11 @@ export * from "./components/request-factory"; export * from "./components/client-factory"; export * from "./components/intent"; +export * from "./components/state-lookup"; /* eslint-disable @typescript-eslint/no-var-requires */ module.exports.AppServiceBot = require("./components/app-service-bot"); -module.exports.StateLookup = require("./components/state-lookup").StateLookup; // Config and CLI module.exports.Cli = require("./components/cli"); @@ -50,7 +50,7 @@ module.exports.AppServiceRegistration = ( const jsSdk = require("matrix-js-sdk"); -module.exports.ContentRepo = { +export const ContentRepo = { getHttpUriForMxc: jsSdk.getHttpUriForMxc, getIdenticonUri: jsSdk.getIdenticonUri, } diff --git a/tsconfig.json b/tsconfig.json index 68621ecf..6a59f0b2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "module": "commonjs", "allowJs": true, "checkJs": false, - "declaration": false, + "declaration": true, "sourceMap": true, "outDir": "./lib", "composite": false,