Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type fixes needed to make matrix-appservice-irc build #196

Merged
merged 7 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/194.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bump matrix-js-sdk to 8.0.1
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 7 additions & 33 deletions src/components/intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>;
createAlias: (alias: string, roomId: string) => Promise<unknown>;
createRoom: (opts: Record<string, unknown>) => Promise<unknown>;
fetchRoomEvent: (roomId: string, eventId: string) => Promise<unknown>;
getStateEvent: (roomId: string, eventType: string, stateKey: string) => Promise<unknown>;
invite: (roomId: string, userId: string) => Promise<unknown>;
joinRoom: (roomId: string, opts: Record<string, unknown>) => Promise<unknown>;
kick: (roomId: string, target: string, reason: string) => Promise<unknown>;
leave: (roomId: string) => Promise<unknown>;
register: (localpart: string) => Promise<unknown>;
roomState: (roomId: string) => Promise<unknown>;
sendEvent: (roomId: string, type: string, content: Record<string, unknown>) => Promise<unknown>;
sendReadReceipt: (event: unknown) => Promise<unknown>;
sendStateEvent: (roomId: string, type: string, content: Record<string, unknown>, skey: string) => Promise<unknown>;
sendTyping: (roomId: string, isTyping: boolean) => Promise<unknown>;
setAvatarUrl: (url: string) => Promise<unknown>;
setDisplayName: (name: string) => Promise<unknown>;
setPowerLevel: (roomId: string, target: string, level: number, event: unknown) => Promise<unknown>;
// eslint-disable-next-line camelcase
setPresence: (presence: { presence: string, status_msg?: string }) => Promise<unknown>;
getProfileInfo: (userId: string, info: string) => Promise<unknown>;
unban: (roomId: string, target: string) => Promise<unknown>;
};

type BridgeErrorReason = "m.event_not_handled" | "m.event_too_old"
| "m.internal_error" | "m.foreign_network_error" | "m.event_unknown";
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/request-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type TimeoutFunction = (req: Request<unknown>) => void;
* A factory which can create {@link Request} objects. Useful for
* adding "default" handlers to requests.
*/
export class RequestFactory<T> {
export class RequestFactory {
private _resolves: HandlerFunction[] = [];
private _rejects: HandlerFunction[] = [];
private _timeouts: {fn: TimeoutFunction, timeout: number}[] = [];
Expand All @@ -33,8 +33,8 @@ export class RequestFactory<T> {
* @param opts The options to pass to the Request constructor, if any.
* @return A new request object
*/
public newRequest(opts: RequestOpts<T>) {
const req = new Request(opts);
public newRequest<T>(opts?: RequestOpts<T>) {
const req = new Request(opts || {data: null});
req.getPromise().then((res) => {
this._resolves.forEach((resolveFn) => {
resolveFn(req, res);
Expand Down
33 changes: 21 additions & 12 deletions src/components/state-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -33,14 +33,15 @@ interface StateLookupRoom {
};
}

interface StateLookupEvent {
export interface StateLookupEvent {
// eslint-disable-next-line camelcase
room_id: string;
// eslint-disable-next-line camelcase
state_key: string;
type: string;
// eslint-disable-next-line camelcase
event_id: string;
content: unknown;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is content guaranteed to be an Object or claimed as here unknown? That would require some more type checks in my PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably call it a Record<string,unknown>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if that's not guaranteed by the client lib, we'll get errors like the one #155 fixes, where a presumed object may be a string or a number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Content should really never be anything other than a object, if it is, we should reject it before insertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

const RETRY_STATE_IN_MS = 300;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for learning something: This broke the tests. It's not testing if these keys are missing (as claimed by the line below), but if they are truethy. The tests use empty strings, which aren't truethy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in deb2f38

// 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;
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "commonjs",
"allowJs": true,
"checkJs": false,
"declaration": false,
"declaration": true,
"sourceMap": true,
"outDir": "./lib",
"composite": false,
Expand Down