Skip to content

Commit

Permalink
Tweaks to code
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Nov 17, 2021
1 parent 2937c1f commit 159b2b4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 39 deletions.
21 changes: 7 additions & 14 deletions src/components/encrypted-intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Logging from "./logging";
import { ReadStream } from "fs";
import BotSdk, { MatrixClient } from "matrix-bot-sdk";
import { FileUploadOpts, Intent, IntentOpts } from "./intent";
import { WeakStateEvent } from "./event-types";

const log = Logging.get("EncryptedIntent");

Expand All @@ -19,7 +20,7 @@ export interface EncryptedIntentOpts {
*/
export class EncryptedIntent extends Intent {

private readonly encryptedRooms = new Map<string, string|false>();
private readonly encryptedRooms = new Map<string, boolean>();
private encryptionReadyPromise?: Promise<void>;
// A client that talks directly to the homeserver, bypassing pantalaimon.
private encryptionHsClient: MatrixClient;
Expand Down Expand Up @@ -52,19 +53,11 @@ export class EncryptedIntent extends Intent {
return super.uploadContent(content, opts);
}

public onEvent(event: {
type: string,
// eslint-disable-next-line camelcase
content: {membership: UserMembership, displayname?: string, avatar_url?: string, algorithm?: string},
// eslint-disable-next-line camelcase
state_key: unknown,
// eslint-disable-next-line camelcase
room_id: string
}): void {
public onEvent(event: WeakStateEvent): void {
super.onEvent(event);
if (event.type === "m.room.encryption" && typeof event.content.algorithm === "string") {
log.info(`Room ${event.room_id} enabled encryption (${event.content.algorithm})`);
this.encryptedRooms.set(event.room_id, event.content.algorithm);
this.encryptedRooms.set(event.room_id, true);
}
}

Expand Down Expand Up @@ -207,7 +200,7 @@ export class EncryptedIntent extends Intent {
* @param roomId The room ID to be checked
* @returns The encryption algorithm or false
*/
public async isRoomEncrypted(roomId: string): Promise<string|false> {
public async isRoomEncrypted(roomId: string): Promise<boolean> {
const existing = this.encryptedRooms.get(roomId);
if (existing !== undefined) {
return existing;
Expand All @@ -220,8 +213,8 @@ export class EncryptedIntent extends Intent {
}
const algo = ev.algorithm as unknown;
if (typeof algo === 'string' && algo) {
this.encryptedRooms.set(roomId, algo);
return algo;
this.encryptedRooms.set(roomId, true);
return true;
}
// Return false if missing, not a string or empty.
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/components/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const log = Logging.get("EncryptedEventBroker");
export const APPSERVICE_LOGIN_TYPE = "uk.half-shot.msc2778.login.application_service";
const EVENT_CACHE_FOR_MS = 5 * 60000; // 5 minutes

interface PanWeakEvent extends WeakEvent {
interface PantalaimonWeakEvent extends WeakEvent {
decrypted: true;
}

Expand Down Expand Up @@ -184,7 +184,7 @@ export class EncryptedEventBroker {
return false;
}

private onSyncEvent(roomId: string, event: PanWeakEvent): void {
private onSyncEvent(roomId: string, event: PantalaimonWeakEvent): void {
if (!event.decrypted) {
// We only care about encrypted events, and pantalaimon appends a decrypted key to each event.
return;
Expand Down
15 changes: 5 additions & 10 deletions src/components/event-types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/* eslint-disable camelcase */
export interface WeakEvent extends Record<string, unknown> {
// eslint-disable-next-line camelcase
event_id: string;
// eslint-disable-next-line camelcase
room_id: string;
sender: string;
content: Record<string, unknown>;
unsigned?: {
age?: number;
}
// eslint-disable-next-line camelcase
origin_server_ts: number;
// eslint-disable-next-line camelcase
state_key?: string;
type: string;
}
Expand All @@ -21,7 +18,6 @@ export interface TypingEvent {
// eslint-disable-next-line camelcase
user_ids: string[];
}
// eslint-disable-next-line camelcase
room_id: string;
}

Expand All @@ -36,24 +32,23 @@ export interface ReadReceiptEvent {
}
}
type: "m.receipt";
// eslint-disable-next-line camelcase
room_id: string;
}

export interface PresenceEvent {
content: {
// eslint-disable-next-line camelcase
avatar_url?: string;
// eslint-disable-next-line camelcase
currently_active?: boolean;
// eslint-disable-next-line camelcase
last_active_ago?: number;
presence: "online"|"offline"|"unavailable";
// eslint-disable-next-line camelcase
status_msg?: string;
},
sender: string;
type: "m.presence";
}

export interface WeakStateEvent extends WeakEvent {
state_key: string;
}

export type EphemeralEvent = TypingEvent|ReadReceiptEvent|PresenceEvent;
17 changes: 5 additions & 12 deletions src/components/intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import BridgeErrorReason = unstable.BridgeErrorReason;
import Logging from "./logging";
import { ReadStream } from "fs";
import BotSdk, { MatrixClient, MatrixProfileInfo, PresenceState } from "matrix-bot-sdk";
import { WeakStateEvent } from "./event-types";

const log = Logging.get("Intent");
export type IntentBackingStore = {
Expand Down Expand Up @@ -853,15 +854,7 @@ export class Intent {
* if a backing store was provided to the Intent.
* @param event The incoming event JSON
*/
public onEvent(event: {
type: string,
// eslint-disable-next-line camelcase
content: {membership: UserMembership, displayname?: string, avatar_url?: string, algorithm?: string},
// eslint-disable-next-line camelcase
state_key: unknown,
// eslint-disable-next-line camelcase
room_id: string
}) {
public onEvent(event: WeakStateEvent): void {
if (event.state_key === undefined) {
// We MUST operate on state events exclusively
return;
Expand All @@ -876,13 +869,13 @@ export class Intent {
event.state_key === this.userId &&
event.content.membership) {
const profile: MatrixProfileInfo = {};
if (event.content.displayname) {
if (typeof event.content.displayname === "string") {
profile.displayname = event.content.displayname;
}
if (event.content.avatar_url) {
if (typeof event.content.avatar_url === "string") {
profile.avatar_url = event.content.avatar_url;
}
this._membershipStates[event.room_id] = [event.content.membership, profile];
this._membershipStates[event.room_id] = [event.content.membership as UserMembership, profile];
}
else if (event.type === "m.room.power_levels") {
this.opts.backingStore.setPowerLevelContent(event.room_id, event.content as unknown as PowerLevelContent);
Expand Down
2 changes: 1 addition & 1 deletion src/components/membership-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class MembershipCache {
* Is a user considered registered with the homeserver.
* @param userId A Matrix userId
*/
public isUserRegistered(userId: string): boolean {
public isUserRegistered(userId: string): boolean {
return this.registeredUsers.has(userId);
}

Expand Down

0 comments on commit 159b2b4

Please sign in to comment.