Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Incorporate requested changes
Browse files Browse the repository at this point in the history
Signed-off-by: Lars Wickel <git@herkulessi.de>
  • Loading branch information
Lars Wickel committed Oct 15, 2023
1 parent a324024 commit bbe4618
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
21 changes: 6 additions & 15 deletions src/utils/permalinks/ElementPermalinkConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,23 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
}
}

public forEvent(roomId: string, eventId: string, serverCandidates: string[], ispill = false): string {
if (ispill) {
return `https://matrix.to/#/${roomId}/${eventId}${this.encodeServerCandidates(serverCandidates)}`;
}
public forEvent(roomId: string, eventId: string, serverCandidates: string[]): string {
return `${this.elementUrl}/#/room/${roomId}/${eventId}${this.encodeServerCandidates(serverCandidates)}`;
}

public forRoom(roomIdOrAlias: string, serverCandidates?: string[], ispill = false): string {
if (ispill) {
return `https://matrix.to/#/${roomIdOrAlias}${this.encodeServerCandidates(serverCandidates)}`;
}
public forRoom(roomIdOrAlias: string, serverCandidates?: string[]): string {
return `${this.elementUrl}/#/room/${roomIdOrAlias}${this.encodeServerCandidates(serverCandidates)}`;
}

public forUser(userId: string, ispill = false): string {
if (ispill) {
return `https://matrix.to/#/${userId}`;
}
public forUser(userId: string): string {
return `${this.elementUrl}/#/user/${userId}`;
}

public forEntity(entityId: string, ispill = false): string {
public forEntity(entityId: string): string {
if (entityId[0] === "!" || entityId[0] === "#") {
return this.forRoom(entityId, [], ispill);
return this.forRoom(entityId);
} else if (entityId[0] === "@") {
return this.forUser(entityId, ispill);
return this.forUser(entityId);
} else throw new Error("Unrecognized entity");
}

Expand Down
8 changes: 4 additions & 4 deletions src/utils/permalinks/PermalinkConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ limitations under the License.
* TODO: Convert this to a real TypeScript interface
*/
export default class PermalinkConstructor {
public forEvent(roomId: string, eventId: string, serverCandidates: string[] = [], ispill = false): string {
public forEvent(roomId: string, eventId: string, serverCandidates: string[] = []): string {
throw new Error("Not implemented");
}

public forRoom(roomIdOrAlias: string, serverCandidates: string[] = [], ispill = false): string {
public forRoom(roomIdOrAlias: string, serverCandidates: string[] = []): string {
throw new Error("Not implemented");
}

public forUser(userId: string, ispill = false): string {
public forUser(userId: string): string {
throw new Error("Not implemented");
}

public forEntity(entityId: string, ispill = false): string {
public forEntity(entityId: string): string {
throw new Error("Not implemented");
}

Expand Down
46 changes: 37 additions & 9 deletions src/utils/permalinks/Permalinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,26 +269,48 @@ export class RoomPermalinkCreator {
};
}

export function makeGenericPermalink(entityId: string, ispill = false): string {
return getPermalinkConstructor().forEntity(entityId, ispill);
/**
* Creates a permalink for an Entity. If isPill is set it uses a spec-compliant
* prefix for the permalink, instead of permalink_prefix
* @param {string} entityId The entity to link to.
* @param {boolean} isPill Link should be pillifyable.
* @returns {string|null} The transformed permalink or null if unable.
*/
export function makeGenericPermalink(entityId: string, isPill = false): string {
return getPermalinkConstructor(isPill).forEntity(entityId);
}

export function makeUserPermalink(userId: string, ispill = false): string {
return getPermalinkConstructor().forUser(userId);
/**
* Creates a permalink for a User. If isPill is set it uses a spec-compliant
* prefix for the permalink, instead of permalink_prefix
* @param {string} userId The user to link to.
* @param {boolean} isPill Link should be pillifyable.
* @returns {string|null} The transformed permalink or null if unable.
*/
export function makeUserPermalink(userId: string, isPill = false): string {
return getPermalinkConstructor(isPill).forUser(userId);
}

export function makeRoomPermalink(matrixClient: MatrixClient, roomId: string, ispill = false): string {
/**
* Creates a permalink for a room. If isPill is set it uses a spec-compliant
* prefix for the permalink, instead of permalink_prefix
* @param {MatrixClient} matrixClient The MatrixClient to use
* @param {string} roomId The user to link to.
* @param {boolean} isPill Link should be pillifyable.
* @returns {string|null} The transformed permalink or null if unable.
*/
export function makeRoomPermalink(matrixClient: MatrixClient, roomId: string, isPill = false): string {
if (!roomId) {
throw new Error("can't permalink a falsy roomId");
}

// If the roomId isn't actually a room ID, don't try to list the servers.
// Aliases are already routable, and don't need extra information.
if (roomId[0] !== "!") return getPermalinkConstructor().forRoom(roomId, [], ispill);
if (roomId[0] !== "!") return getPermalinkConstructor(isPill).forRoom(roomId, []);

const room = matrixClient.getRoom(roomId);
if (!room) {
return getPermalinkConstructor().forRoom(roomId, [], ispill);
return getPermalinkConstructor(isPill).forRoom(roomId, []);
}
const permalinkCreator = new RoomPermalinkCreator(room);
permalinkCreator.load();
Expand Down Expand Up @@ -409,9 +431,15 @@ export function getPrimaryPermalinkEntity(permalink: string): string | null {
return null;
}

function getPermalinkConstructor(): PermalinkConstructor {
/**
* Returns the correct PermalinkConstructor based on permalink_prefix
* and isPill
* @param {boolean} isPill Should constructed links be pillifyable.
* @returns {string|null} The transformed permalink or null if unable.
*/
function getPermalinkConstructor(isPill = false): PermalinkConstructor {
const elementPrefix = SdkConfig.get("permalink_prefix");
if (elementPrefix && elementPrefix !== matrixtoBaseUrl) {
if (elementPrefix && elementPrefix !== matrixtoBaseUrl && !isPill) {
return new ElementPermalinkConstructor(elementPrefix);
}

Expand Down

0 comments on commit bbe4618

Please sign in to comment.