From 4721aa1d241a46601601259ec7ca6db9ff1bb5fb Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 12 May 2022 10:12:39 +0100 Subject: [PATCH] Fix up more types & Sonar warnings (#2363) * Fix up more types & Sonar warnings * Fix test * Add first test for callEventHandler --- .../matrix-client-event-timeline.spec.js | 7 ++- spec/unit/webrtc/callEventHandler.spec.ts | 54 +++++++++++++++++++ src/@types/requests.ts | 37 ++++++++++++- src/client.ts | 23 ++++---- src/crypto/DeviceList.ts | 2 +- src/crypto/index.ts | 6 +-- src/models/event.ts | 6 +-- src/sync-accumulator.ts | 1 - src/sync.ts | 8 +-- src/webrtc/callEventHandler.ts | 12 ++--- 10 files changed, 119 insertions(+), 37 deletions(-) create mode 100644 spec/unit/webrtc/callEventHandler.spec.ts diff --git a/spec/integ/matrix-client-event-timeline.spec.js b/spec/integ/matrix-client-event-timeline.spec.js index 6e93a063a..6df4a9a81 100644 --- a/spec/integ/matrix-client-event-timeline.spec.js +++ b/spec/integ/matrix-client-event-timeline.spec.js @@ -526,8 +526,7 @@ describe("MatrixClient event timelines", function() { return { original_event: THREAD_ROOT, chunk: [THREAD_REPLY], - next_batch: "next_batch_token0", - prev_batch: "prev_batch_token0", + // no next batch as this is the oldest end of the timeline }; }); @@ -536,8 +535,8 @@ describe("MatrixClient event timelines", function() { const timeline = await timelinePromise; - expect(timeline.getEvents().find(e => e.getId() === THREAD_ROOT.event_id)); - expect(timeline.getEvents().find(e => e.getId() === THREAD_REPLY.event_id)); + expect(timeline.getEvents().find(e => e.getId() === THREAD_ROOT.event_id)).toBeTruthy(); + expect(timeline.getEvents().find(e => e.getId() === THREAD_REPLY.event_id)).toBeTruthy(); }); }); diff --git a/spec/unit/webrtc/callEventHandler.spec.ts b/spec/unit/webrtc/callEventHandler.spec.ts new file mode 100644 index 000000000..d60ae2997 --- /dev/null +++ b/spec/unit/webrtc/callEventHandler.spec.ts @@ -0,0 +1,54 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { TestClient } from '../../TestClient'; +import { ClientEvent, EventType, MatrixEvent, RoomEvent } from "../../../src"; +import { CallEventHandler, CallEventHandlerEvent } from "../../../src/webrtc/callEventHandler"; +import { SyncState } from "../../../src/sync"; + +describe("callEventHandler", () => { + it("should ignore a call if invite & hangup come within a single sync", () => { + const testClient = new TestClient(); + const client = testClient.client; + client.callEventHandler = new CallEventHandler(client); + client.callEventHandler.start(); + + // Fire off call invite then hangup within a single sync + const callInvite = new MatrixEvent({ + type: EventType.CallInvite, + content: { + call_id: "123", + }, + }); + client.emit(RoomEvent.Timeline, callInvite); + + const callHangup = new MatrixEvent({ + type: EventType.CallHangup, + content: { + call_id: "123", + }, + }); + client.emit(RoomEvent.Timeline, callHangup); + + const incomingCallEmitted = jest.fn(); + client.on(CallEventHandlerEvent.Incoming, incomingCallEmitted); + + client.getSyncState = jest.fn().mockReturnValue(SyncState.Syncing); + client.emit(ClientEvent.Sync); + + expect(incomingCallEmitted).not.toHaveBeenCalled(); + }); +}); diff --git a/src/@types/requests.ts b/src/@types/requests.ts index a3c950ab1..f3ff33e4a 100644 --- a/src/@types/requests.ts +++ b/src/@types/requests.ts @@ -17,9 +17,11 @@ limitations under the License. import { Callback } from "../client"; import { IContent, IEvent } from "../models/event"; import { Preset, Visibility } from "./partials"; -import { SearchKey } from "./search"; +import { IEventWithRoomId, SearchKey } from "./search"; import { IRoomEventFilter } from "../filter"; import { Direction } from "../models/event-timeline"; +import { PushRuleAction } from "./PushRules"; +import { IRoomEvent } from "../sync-accumulator"; // allow camelcase as these are things that go onto the wire /* eslint-disable camelcase */ @@ -155,4 +157,37 @@ export interface IRelationsResponse { prev_batch?: string; } +export interface IContextResponse { + end: string; + start: string; + state: IEventWithRoomId[]; + events_before: IEventWithRoomId[]; + events_after: IEventWithRoomId[]; + event: IEventWithRoomId; +} + +export interface IEventsResponse { + chunk: IEventWithRoomId[]; + end: string; + start: string; +} + +export interface INotification { + actions: PushRuleAction[]; + event: IRoomEvent; + profile_tag?: string; + read: boolean; + room_id: string; + ts: number; +} + +export interface INotificationsResponse { + next_token: string; + notifications: INotification[]; +} + +export interface IFilterResponse { + filter_id: string; +} + /* eslint-enable camelcase */ diff --git a/src/client.ts b/src/client.ts index 12e02a227..386152f92 100644 --- a/src/client.ts +++ b/src/client.ts @@ -113,6 +113,8 @@ import { RoomMemberEventHandlerMap, RoomStateEvent, RoomStateEventHandlerMap, + INotificationsResponse, + IFilterResponse, } from "./matrix"; import { CrossSigningKey, @@ -132,6 +134,7 @@ import { Room } from "./models/room"; import { IAddThreePidOnlyBody, IBindThreePidBody, + IContextResponse, ICreateRoomOpts, IEventSearchOpts, IGuestAccessOpts, @@ -5245,7 +5248,7 @@ export class MatrixClient extends TypedEventEmitter(undefined, Method.Get, path, params); // TODO types + const res = await this.http.authedRequest(undefined, Method.Get, path, params); if (!res.event) { throw new Error("'event' not in '/context' result - homeserver too old?"); } @@ -5424,7 +5427,7 @@ export class MatrixClient extends TypedEventEmitter( // TODO types + promise = this.http.authedRequest( undefined, Method.Get, path, params, undefined, ).then(async (res) => { const token = res.next_token; @@ -6101,15 +6104,13 @@ export class MatrixClient extends TypedEventEmitter(undefined, Method.Post, path, undefined, content).then((response) => { - // persist the filter - const filter = Filter.fromJson( - this.credentials.userId, response.filter_id, content, - ); - this.store.storeFilter(filter); - return filter; - }); + return this.http.authedRequest(undefined, Method.Post, path, undefined, content) + .then((response) => { + // persist the filter + const filter = Filter.fromJson(this.credentials.userId, response.filter_id, content); + this.store.storeFilter(filter); + return filter; + }); } /** diff --git a/src/crypto/DeviceList.ts b/src/crypto/DeviceList.ts index 000e79f93..c203ce5da 100644 --- a/src/crypto/DeviceList.ts +++ b/src/crypto/DeviceList.ts @@ -942,7 +942,7 @@ async function updateStoredDeviceKeysForUser( async function storeDeviceKeys( olmDevice: OlmDevice, userStore: Record, - deviceResult: any, // TODO types + deviceResult: IDownloadKeyResult["device_keys"]["user_id"]["device_id"], ): Promise { if (!deviceResult.keys) { // no keys? diff --git a/src/crypto/index.ts b/src/crypto/index.ts index a83702385..474bfd3f6 100644 --- a/src/crypto/index.ts +++ b/src/crypto/index.ts @@ -58,7 +58,7 @@ import { keyFromPassphrase } from './key_passphrase'; import { decodeRecoveryKey, encodeRecoveryKey } from './recoverykey'; import { VerificationRequest } from "./verification/request/VerificationRequest"; import { InRoomChannel, InRoomRequests } from "./verification/request/InRoomChannel"; -import { ToDeviceChannel, ToDeviceRequests } from "./verification/request/ToDeviceChannel"; +import { ToDeviceChannel, ToDeviceRequests, Request } from "./verification/request/ToDeviceChannel"; import { IllegalMethod } from "./verification/IllegalMethod"; import { KeySignatureUploadError } from "../errors"; import { calculateKeyCheck, decryptAES, encryptAES } from './aes'; @@ -2318,8 +2318,8 @@ export class Crypto extends TypedEventEmitter { + let request: Request; if (transactionId) { request = this.toDeviceVerificationRequests.getRequestBySenderAndTxnId(userId, transactionId); if (!request) { diff --git a/src/models/event.ts b/src/models/event.ts index 227036be5..e188e8607 100644 --- a/src/models/event.ts +++ b/src/models/event.ts @@ -1043,7 +1043,7 @@ export class MatrixEvent extends TypedEventEmitter