Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSC4133 - Extended profiles #4391

Merged
merged 8 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
173 changes: 173 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ export const UNSTABLE_MSC2666_QUERY_MUTUAL_ROOMS = "uk.half-shot.msc2666.query_m

export const UNSTABLE_MSC4140_DELAYED_EVENTS = "org.matrix.msc4140";

export const UNSTABLE_MSC4133_EXTENDED_PROFILES = "uk.tcpip.msc4133";

enum CrossSigningKeyType {
MasterKey = "master_key",
SelfSigningKey = "self_signing_key",
Expand Down Expand Up @@ -8806,6 +8808,177 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return this.http.authedRequest(Method.Get, path);
}

/**
* Determine if the server supports extended profiles, as described by MSC4133.
*
* @returns `true` if supported, otherwise `false`
*/
public async doesServerSupportExtendedProfiles(): Promise<boolean> {
// Needs https://github.com/element-hq/synapse/pull/17488/files#r1749146498 to be resolved.
return true;
//return this.doesServerSupportUnstableFeature(UNSTABLE_MSC4133_EXTENDED_PROFILES);
}

/**
* Fetch a user's *extended* profile, which may include additonal keys.
*
* @see https://github.com/tcpipuk/matrix-spec-proposals/blob/main/proposals/4133-extended-profiles.md
* @param userId The user ID to fetch the profile of.
* @returns A set of keys to property values.
*/
public async getExtendedProfile(userId: string): Promise<Record<string, unknown>> {
if (!(await this.doesServerSupportExtendedProfiles())) {
//throw new Error('Server does not support extended profiles');
}
return this.http.authedRequest(
Method.Get,
utils.encodeUri("/profile/$userId", { $userId: userId }),
undefined,
undefined,
{
prefix: (await this.doesServerSupportUnstableFeature("uk.tcpip.msc4133.stable"))
? undefined
: "/_matrix/client/unstable/uk.tcpip.msc4133",
},
);
}

/**
* Fetch a specific key from the user's *extended* profile.
*
* @see https://github.com/tcpipuk/matrix-spec-proposals/blob/main/proposals/4133-extended-profiles.md
* @param userId The user ID to fetch the profile of.
* @param key The key of the property to fetch.
* @returns A set of keys to property values.
*/
public async getExtendedProfileProperty(userId: string, key: string): Promise<unknown> {
if (!(await this.doesServerSupportExtendedProfiles())) {
//throw new Error('Server does not support extended profiles');
}
const profile = (await this.http.authedRequest(
Method.Get,
utils.encodeUri("/profile/$userId/$key", { $userId: userId, $key: key }),
undefined,
undefined,
{
prefix: (await this.doesServerSupportUnstableFeature("uk.tcpip.msc4133.stable"))
? undefined
: "/_matrix/client/unstable/uk.tcpip.msc4133",
},
)) as Record<string, unknown>;
return profile[key];
}

/**
* Set a property on your *extended* profile.
*
* @see https://github.com/tcpipuk/matrix-spec-proposals/blob/main/proposals/4133-extended-profiles.md
* @param key The key of the property to set.
* @param value The value to set on the propety.
* @returns A set of keys to property values.
*/
public async setExtendedProfileProperty(key: string, value: unknown): Promise<void> {
if (!(await this.doesServerSupportExtendedProfiles())) {
//throw new Error('Server does not support extended profiles');
}
const userId = this.getUserId();

await this.http.authedRequest(
Method.Put,
utils.encodeUri("/profile/$userId/$key", { $userId: userId, $key: key }),
undefined,
{ [key]: value },
{
prefix: (await this.doesServerSupportUnstableFeature("uk.tcpip.msc4133.stable"))
? undefined
: "/_matrix/client/unstable/uk.tcpip.msc4133",
},
);
}

/**
* Delete a property on your *extended* profile.
*
* @see https://github.com/tcpipuk/matrix-spec-proposals/blob/main/proposals/4133-extended-profiles.md
* @param userId The user ID to fetch the profile of.
* @param key The key of the property to fetch.
* @returns A set of keys to property values.
*/
public async deleteExtendedProfileProperty(key: string): Promise<void> {
if (!(await this.doesServerSupportExtendedProfiles())) {
//throw new Error('Server does not support extended profiles');
}
const userId = this.getUserId();

await this.http.authedRequest(
Method.Delete,
utils.encodeUri("/profile/$userId/$key", { $userId: userId, $key: key }),
undefined,
undefined,
{
prefix: (await this.doesServerSupportUnstableFeature("uk.tcpip.msc4133.stable"))
? undefined
: "/_matrix/client/unstable/uk.tcpip.msc4133",
},
);
}

/**
* Update multiple properties on your *extended* profile. This will
* update any existing keys and leave unspecified ones intact.
*
* @see https://github.com/tcpipuk/matrix-spec-proposals/blob/main/proposals/4133-extended-profiles.md
* @param userId The user ID to fetch the profile of.
* @param key The key of the property to fetch.
* @returns A set of keys to property values.
*/
public async patchExtendedProfile(profile: Record<string, unknown>): Promise<Record<string, unknown>> {
if (!(await this.doesServerSupportExtendedProfiles())) {
//throw new Error('Server does not support extended profiles');
}
const userId = this.getUserId();

return this.http.authedRequest(
Method.Patch,
utils.encodeUri("/profile/$userId", { $userId: userId }),
{},
profile,
{
prefix: (await this.doesServerSupportUnstableFeature("uk.tcpip.msc4133.stable"))
? undefined
: "/_matrix/client/unstable/uk.tcpip.msc4133",
},
);
}

/**
* Set multiple properties on your *extended* profile. This will completely
* replace the existing profile, removing any unspecified keys.
*
* @see https://github.com/tcpipuk/matrix-spec-proposals/blob/main/proposals/4133-extended-profiles.md
* @param userId The user ID to fetch the profile of.
* @param key The key of the property to fetch.
* @returns A set of keys to property values.
*/
public async setExtendedProfile(profile: Record<string, unknown>): Promise<Record<string, unknown>> {
if (!(await this.doesServerSupportExtendedProfiles())) {
//throw new Error('Server does not support extended profiles');
}
const userId = this.getUserId();

return this.http.authedRequest(
Method.Put,
utils.encodeUri("/profile/$userId", { $userId: userId }),
{},
profile,
{
prefix: (await this.doesServerSupportUnstableFeature("uk.tcpip.msc4133.stable"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit repetitive - maybe a extendedProfilePrefix or something would be useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

? undefined
: "/_matrix/client/unstable/uk.tcpip.msc4133",
},
);
}

/**
* @returns Promise which resolves to a list of the user's threepids.
* @returns Rejects: with an error response.
Expand Down
1 change: 1 addition & 0 deletions src/models/profile-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ProfileKeyMSC4175Timezone = "us.cloke.msc4175.tz";
Half-Shot marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions src/serverCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface ISetDisplayNameCapability extends ICapability {}

export interface ISetAvatarUrlCapability extends ICapability {}

export interface IProfileFieldsCapability extends ICapability {}

export enum RoomVersionStability {
Stable = "stable",
Unstable = "unstable",
Expand All @@ -61,6 +63,7 @@ export interface Capabilities {
"org.matrix.msc3882.get_login_token"?: IGetLoginTokenCapability;
"m.set_displayname"?: ISetDisplayNameCapability;
"m.set_avatar_url"?: ISetAvatarUrlCapability;
"uk.tcpip.msc4133.profile_fields"?: IProfileFieldsCapability;
}

type CapabilitiesResponse = {
Expand Down
Loading