Skip to content
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
12 changes: 10 additions & 2 deletions src/models/MSC3089Branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export class MSC3089Branch {
* The file ID.
*/
public get id(): string {
return this.indexEvent.getStateKey();
const stateKey = this.indexEvent.getStateKey();
if (!stateKey) {
throw new Error("State key not found for branch");
}
return stateKey;
}

/**
Expand Down Expand Up @@ -121,6 +125,10 @@ export class MSC3089Branch {
const file = event.getOriginalContent()['file'];
const httpUrl = this.client.mxcUrlToHttp(file['url']);

if (!httpUrl) {
throw new Error(`No HTTP URL available for ${file['url']}`);
}

return { info: file, httpUrl: httpUrl };
}

Expand Down Expand Up @@ -199,7 +207,7 @@ export class MSC3089Branch {
// XXX: This is a very inefficient search, but it's the best we can do with the
// relations structure we have in the SDK. As of writing, it is not worth the
// investment in improving the structure.
let childEvent: MatrixEvent;
let childEvent: MatrixEvent | undefined;
let parentEvent = await this.getFileEvent();
do {
childEvent = timelineEvents.find(e => e.replacingEventId() === parentEvent.getId());
Expand Down
36 changes: 24 additions & 12 deletions src/models/MSC3089TreeSpace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,11 @@ export class MSC3089TreeSpace {
const children = this.room.currentState.getStateEvents(EventType.SpaceChild);
for (const child of children) {
try {
const tree = this.client.unstableGetFileTreeSpace(child.getStateKey());
if (tree) trees.push(tree);
const stateKey = child.getStateKey();
if (stateKey) {
const tree = this.client.unstableGetFileTreeSpace(stateKey);
if (tree) trees.push(tree);
}
} catch (e) {
logger.warn("Unable to create tree space instance for listing. Are we joined?", e);
}
Expand All @@ -258,9 +261,9 @@ export class MSC3089TreeSpace {
* Gets a subdirectory of a given ID under this tree space. Note that this will not recurse
* into children and instead only look one level deep.
* @param {string} roomId The room ID (directory ID) to find.
* @returns {MSC3089TreeSpace} The directory, or falsy if not found.
* @returns {MSC3089TreeSpace | undefined} The directory, or undefined if not found.
*/
public getDirectory(roomId: string): MSC3089TreeSpace {
public getDirectory(roomId: string): MSC3089TreeSpace | undefined {
return this.getDirectories().find(r => r.roomId === roomId);
}

Expand All @@ -278,8 +281,12 @@ export class MSC3089TreeSpace {
const members = this.room.currentState.getStateEvents(EventType.RoomMember);
for (const member of members) {
const isNotUs = member.getStateKey() !== this.client.getUserId();
if (isNotUs && kickMemberships.includes(member.getContent()['membership'])) {
await this.client.kick(this.roomId, member.getStateKey(), "Room deleted");
if (isNotUs && kickMemberships.includes(member.getContent<string>()['membership'])) {
const stateKey = member.getStateKey();
if (!stateKey) {
throw new Error("State key not found for branch");
}
await this.client.kick(this.roomId, stateKey, "Room deleted");
}
}

Expand All @@ -288,7 +295,8 @@ export class MSC3089TreeSpace {

private getOrderedChildren(children: MatrixEvent[]): { roomId: string, order: string }[] {
const ordered: { roomId: string, order: string }[] = children
.map(c => ({ roomId: c.getStateKey(), order: c.getContent()['order'] }));
.map(c => ({ roomId: c.getStateKey(), order: c.getContent()['order'] }))
.filter(c => c.roomId) as { roomId: string, order: string }[];
ordered.sort((a, b) => {
if (a.order && !b.order) {
return -1;
Expand Down Expand Up @@ -321,7 +329,9 @@ export class MSC3089TreeSpace {

// XXX: We are assuming the parent is a valid tree space.
// We probably don't need to validate the parent room state for this usecase though.
const parentRoom = this.client.getRoom(parent.getStateKey());
const stateKey = parent.getStateKey();
if (!stateKey) throw new Error("No state key found for parent");
const parentRoom = this.client.getRoom(stateKey);
if (!parentRoom) throw new Error("Unable to locate room for parent");

return parentRoom;
Expand Down Expand Up @@ -413,7 +423,7 @@ export class MSC3089TreeSpace {
// We were asked by the order algorithm to prepare the moving space for a landing
// in the undefined order part of the order array, which means we need to update the
// spaces that come before it with a stable order value.
let lastOrder: string;
let lastOrder: string | undefined;
for (let i = 0; i <= index; i++) {
const target = ordered[i];
if (i === 0) {
Expand All @@ -432,7 +442,9 @@ export class MSC3089TreeSpace {
lastOrder = target.order;
}
}
newOrder = nextString(lastOrder);
if (lastOrder) {
newOrder = nextString(lastOrder);
}
}

// TODO: Deal with order conflicts by reordering
Expand Down Expand Up @@ -502,9 +514,9 @@ export class MSC3089TreeSpace {
/**
* Retrieves a file from the tree.
* @param {string} fileEventId The event ID of the file.
* @returns {MSC3089Branch} The file, or falsy if not found.
* @returns {MSC3089Branch | null} The file, or null if not found.
*/
public getFile(fileEventId: string): MSC3089Branch {
public getFile(fileEventId: string): MSC3089Branch | null {
const branch = this.room.currentState.getStateEvents(UNSTABLE_MSC3089_BRANCH.name, fileEventId);
return branch ? new MSC3089Branch(this.client, branch, this) : null;
}
Expand Down