Skip to content

Commit 4721aa1

Browse files
authored
Fix up more types & Sonar warnings (#2363)
* Fix up more types & Sonar warnings * Fix test * Add first test for callEventHandler
1 parent 4d4d6e1 commit 4721aa1

File tree

10 files changed

+119
-37
lines changed

10 files changed

+119
-37
lines changed

spec/integ/matrix-client-event-timeline.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,7 @@ describe("MatrixClient event timelines", function() {
526526
return {
527527
original_event: THREAD_ROOT,
528528
chunk: [THREAD_REPLY],
529-
next_batch: "next_batch_token0",
530-
prev_batch: "prev_batch_token0",
529+
// no next batch as this is the oldest end of the timeline
531530
};
532531
});
533532

@@ -536,8 +535,8 @@ describe("MatrixClient event timelines", function() {
536535

537536
const timeline = await timelinePromise;
538537

539-
expect(timeline.getEvents().find(e => e.getId() === THREAD_ROOT.event_id));
540-
expect(timeline.getEvents().find(e => e.getId() === THREAD_REPLY.event_id));
538+
expect(timeline.getEvents().find(e => e.getId() === THREAD_ROOT.event_id)).toBeTruthy();
539+
expect(timeline.getEvents().find(e => e.getId() === THREAD_REPLY.event_id)).toBeTruthy();
541540
});
542541
});
543542

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { TestClient } from '../../TestClient';
18+
import { ClientEvent, EventType, MatrixEvent, RoomEvent } from "../../../src";
19+
import { CallEventHandler, CallEventHandlerEvent } from "../../../src/webrtc/callEventHandler";
20+
import { SyncState } from "../../../src/sync";
21+
22+
describe("callEventHandler", () => {
23+
it("should ignore a call if invite & hangup come within a single sync", () => {
24+
const testClient = new TestClient();
25+
const client = testClient.client;
26+
client.callEventHandler = new CallEventHandler(client);
27+
client.callEventHandler.start();
28+
29+
// Fire off call invite then hangup within a single sync
30+
const callInvite = new MatrixEvent({
31+
type: EventType.CallInvite,
32+
content: {
33+
call_id: "123",
34+
},
35+
});
36+
client.emit(RoomEvent.Timeline, callInvite);
37+
38+
const callHangup = new MatrixEvent({
39+
type: EventType.CallHangup,
40+
content: {
41+
call_id: "123",
42+
},
43+
});
44+
client.emit(RoomEvent.Timeline, callHangup);
45+
46+
const incomingCallEmitted = jest.fn();
47+
client.on(CallEventHandlerEvent.Incoming, incomingCallEmitted);
48+
49+
client.getSyncState = jest.fn().mockReturnValue(SyncState.Syncing);
50+
client.emit(ClientEvent.Sync);
51+
52+
expect(incomingCallEmitted).not.toHaveBeenCalled();
53+
});
54+
});

src/@types/requests.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ limitations under the License.
1717
import { Callback } from "../client";
1818
import { IContent, IEvent } from "../models/event";
1919
import { Preset, Visibility } from "./partials";
20-
import { SearchKey } from "./search";
20+
import { IEventWithRoomId, SearchKey } from "./search";
2121
import { IRoomEventFilter } from "../filter";
2222
import { Direction } from "../models/event-timeline";
23+
import { PushRuleAction } from "./PushRules";
24+
import { IRoomEvent } from "../sync-accumulator";
2325

2426
// allow camelcase as these are things that go onto the wire
2527
/* eslint-disable camelcase */
@@ -155,4 +157,37 @@ export interface IRelationsResponse {
155157
prev_batch?: string;
156158
}
157159

160+
export interface IContextResponse {
161+
end: string;
162+
start: string;
163+
state: IEventWithRoomId[];
164+
events_before: IEventWithRoomId[];
165+
events_after: IEventWithRoomId[];
166+
event: IEventWithRoomId;
167+
}
168+
169+
export interface IEventsResponse {
170+
chunk: IEventWithRoomId[];
171+
end: string;
172+
start: string;
173+
}
174+
175+
export interface INotification {
176+
actions: PushRuleAction[];
177+
event: IRoomEvent;
178+
profile_tag?: string;
179+
read: boolean;
180+
room_id: string;
181+
ts: number;
182+
}
183+
184+
export interface INotificationsResponse {
185+
next_token: string;
186+
notifications: INotification[];
187+
}
188+
189+
export interface IFilterResponse {
190+
filter_id: string;
191+
}
192+
158193
/* eslint-enable camelcase */

src/client.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ import {
113113
RoomMemberEventHandlerMap,
114114
RoomStateEvent,
115115
RoomStateEventHandlerMap,
116+
INotificationsResponse,
117+
IFilterResponse,
116118
} from "./matrix";
117119
import {
118120
CrossSigningKey,
@@ -132,6 +134,7 @@ import { Room } from "./models/room";
132134
import {
133135
IAddThreePidOnlyBody,
134136
IBindThreePidBody,
137+
IContextResponse,
135138
ICreateRoomOpts,
136139
IEventSearchOpts,
137140
IGuestAccessOpts,
@@ -5245,7 +5248,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
52455248
}
52465249

52475250
// TODO: we should implement a backoff (as per scrollback()) to deal more nicely with HTTP errors.
5248-
const res = await this.http.authedRequest<any>(undefined, Method.Get, path, params); // TODO types
5251+
const res = await this.http.authedRequest<IContextResponse>(undefined, Method.Get, path, params);
52495252
if (!res.event) {
52505253
throw new Error("'event' not in '/context' result - homeserver too old?");
52515254
}
@@ -5424,7 +5427,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
54245427
params.from = token;
54255428
}
54265429

5427-
promise = this.http.authedRequest<any>( // TODO types
5430+
promise = this.http.authedRequest<INotificationsResponse>(
54285431
undefined, Method.Get, path, params, undefined,
54295432
).then(async (res) => {
54305433
const token = res.next_token;
@@ -6101,15 +6104,13 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
61016104
const path = utils.encodeUri("/user/$userId/filter", {
61026105
$userId: this.credentials.userId,
61036106
});
6104-
// TODO types
6105-
return this.http.authedRequest<any>(undefined, Method.Post, path, undefined, content).then((response) => {
6106-
// persist the filter
6107-
const filter = Filter.fromJson(
6108-
this.credentials.userId, response.filter_id, content,
6109-
);
6110-
this.store.storeFilter(filter);
6111-
return filter;
6112-
});
6107+
return this.http.authedRequest<IFilterResponse>(undefined, Method.Post, path, undefined, content)
6108+
.then((response) => {
6109+
// persist the filter
6110+
const filter = Filter.fromJson(this.credentials.userId, response.filter_id, content);
6111+
this.store.storeFilter(filter);
6112+
return filter;
6113+
});
61136114
}
61146115

61156116
/**

src/crypto/DeviceList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ async function updateStoredDeviceKeysForUser(
942942
async function storeDeviceKeys(
943943
olmDevice: OlmDevice,
944944
userStore: Record<string, DeviceInfo>,
945-
deviceResult: any, // TODO types
945+
deviceResult: IDownloadKeyResult["device_keys"]["user_id"]["device_id"],
946946
): Promise<boolean> {
947947
if (!deviceResult.keys) {
948948
// no keys?

src/crypto/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import { keyFromPassphrase } from './key_passphrase';
5858
import { decodeRecoveryKey, encodeRecoveryKey } from './recoverykey';
5959
import { VerificationRequest } from "./verification/request/VerificationRequest";
6060
import { InRoomChannel, InRoomRequests } from "./verification/request/InRoomChannel";
61-
import { ToDeviceChannel, ToDeviceRequests } from "./verification/request/ToDeviceChannel";
61+
import { ToDeviceChannel, ToDeviceRequests, Request } from "./verification/request/ToDeviceChannel";
6262
import { IllegalMethod } from "./verification/IllegalMethod";
6363
import { KeySignatureUploadError } from "../errors";
6464
import { calculateKeyCheck, decryptAES, encryptAES } from './aes';
@@ -2318,8 +2318,8 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
23182318
userId: string,
23192319
deviceId: string,
23202320
transactionId: string = null,
2321-
): any { // TODO types
2322-
let request;
2321+
): VerificationBase<any, any> {
2322+
let request: Request;
23232323
if (transactionId) {
23242324
request = this.toDeviceVerificationRequests.getRequestBySenderAndTxnId(userId, transactionId);
23252325
if (!request) {

src/models/event.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ export class MatrixEvent extends TypedEventEmitter<EmittedEvents, MatrixEventHan
10431043
* caused a change in the actual visibility of this event, either by making it
10441044
* visible (if it was hidden), by making it hidden (if it was visible) or by
10451045
* changing the reason (if it was hidden).
1046-
* @param visibilityEvent event holding a hide/unhide payload, or nothing
1046+
* @param visibilityChange event holding a hide/unhide payload, or nothing
10471047
* if the event is being reset to its original visibility (presumably
10481048
* by a visibility event being redacted).
10491049
*/
@@ -1065,9 +1065,7 @@ export class MatrixEvent extends TypedEventEmitter<EmittedEvents, MatrixEventHan
10651065
reason: reason,
10661066
});
10671067
}
1068-
if (change) {
1069-
this.emit(MatrixEventEvent.VisibilityChange, this, visible);
1070-
}
1068+
this.emit(MatrixEventEvent.VisibilityChange, this, visible);
10711069
}
10721070
}
10731071

src/sync-accumulator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export interface IRoomEvent extends IMinimalEvent {
5050
event_id: string;
5151
sender: string;
5252
origin_server_ts: number;
53-
unsigned?: IUnsigned;
5453
/** @deprecated - legacy field */
5554
age?: number;
5655
}

src/sync.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { IPushRules } from "./@types/PushRules";
5454
import { RoomStateEvent } from "./models/room-state";
5555
import { RoomMemberEvent } from "./models/room-member";
5656
import { BeaconEvent } from "./models/beacon";
57+
import { IEventsResponse } from "./@types/requests";
5758

5859
const DEBUG = true;
5960

@@ -274,7 +275,7 @@ export class SyncApi {
274275
getFilterName(client.credentials.userId, "LEFT_ROOMS"), filter,
275276
).then(function(filterId) {
276277
qps.filter = filterId;
277-
return client.http.authedRequest<any>( // TODO types
278+
return client.http.authedRequest<ISyncResponse>(
278279
undefined, Method.Get, "/sync", qps as any, undefined, localTimeoutMs,
279280
);
280281
}).then(async (data) => {
@@ -409,8 +410,7 @@ export class SyncApi {
409410
}
410411

411412
// FIXME: gut wrenching; hard-coded timeout values
412-
// TODO types
413-
this.client.http.authedRequest<any>(undefined, Method.Get, "/events", {
413+
this.client.http.authedRequest<IEventsResponse>(undefined, Method.Get, "/events", {
414414
room_id: peekRoom.roomId,
415415
timeout: String(30 * 1000),
416416
from: token,
@@ -874,7 +874,7 @@ export class SyncApi {
874874

875875
private doSyncRequest(syncOptions: ISyncOptions, syncToken: string): IRequestPromise<ISyncResponse> {
876876
const qps = this.getSyncParams(syncOptions, syncToken);
877-
return this.client.http.authedRequest( // TODO types
877+
return this.client.http.authedRequest<ISyncResponse>(
878878
undefined, Method.Get, "/sync", qps as any, undefined,
879879
qps.timeout + BUFFER_PERIOD_MS,
880880
);

src/webrtc/callEventHandler.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,17 @@ export class CallEventHandler {
7272
this.client.decryptEventIfNeeded(event);
7373
}));
7474

75-
const ignoreCallIds = new Set<String>();
75+
const ignoreCallIds = new Set<string>();
7676
// inspect the buffer and mark all calls which have been answered
7777
// or hung up before passing them to the call event handler.
7878
for (const ev of this.callEventBuffer) {
79-
if (ev.getType() === EventType.CallAnswer ||
80-
ev.getType() === EventType.CallHangup) {
79+
if (ev.getType() === EventType.CallAnswer || ev.getType() === EventType.CallHangup) {
8180
ignoreCallIds.add(ev.getContent().call_id);
8281
}
8382
}
8483
// now loop through the buffer chronologically and inject them
8584
for (const e of this.callEventBuffer) {
86-
if (
87-
e.getType() === EventType.CallInvite &&
88-
ignoreCallIds.has(e.getContent().call_id)
89-
) {
85+
if (e.getType() === EventType.CallInvite && ignoreCallIds.has(e.getContent().call_id)) {
9086
// This call has previously been answered or hung up: ignore it
9187
continue;
9288
}
@@ -191,7 +187,7 @@ export class CallEventHandler {
191187
}
192188

193189
// Were we trying to call that user (room)?
194-
let existingCall;
190+
let existingCall: MatrixCall;
195191
for (const thisCall of this.calls.values()) {
196192
const isCalling = [CallState.WaitLocalMedia, CallState.CreateOffer, CallState.InviteSent].includes(
197193
thisCall.state,

0 commit comments

Comments
 (0)