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

Add Intent.ensureProfile #318

Merged
merged 2 commits into from
Mar 25, 2021
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
1 change: 1 addition & 0 deletions changelog.d/318.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `intent.ensureProfile` function.
19 changes: 19 additions & 0 deletions src/components/intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,25 @@ export class Intent {
return this.client.setAvatarUrl(url);
}

/**
* Ensure that the user has the given profile information set. If it does not,
* set it.
* @param displayname The displayname to set. Leave undefined to ignore.
* @param avatarUrl The avatar to set. Leave undefined to ignore.
*/
public async ensureProfile(displayname?: string, avatarUrl?: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be a bit too much of an edge case:
How to ensure the display name or avatarUrl is unset?

Falsy values will get ignored or cause the function to throw.

Copy link
Contributor Author

@Half-Shot Half-Shot Mar 24, 2021

Choose a reason for hiding this comment

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

Hmm, good question. In fairness the spec doesn't actually give you a way to unset a profile https://spec.matrix.org/unstable/client-server-api/#profiles so it's always been a bit troublesome. I'd probably say for now that the bridge should explicitly unset a profile key if they want to remove it rather than calling this function.

It's also fairly edge-casey

if (!displayname && !avatarUrl) {
throw Error('At least one of displayname,avatarUrl must be defined');
}
const profile = await this.getProfileInfo(this.userId, null, false);
if (displayname && profile.displayname !== displayname) {
await this.setDisplayName(displayname);
}
if (avatarUrl && profile.avatar_url !== avatarUrl) {
await this.setAvatarUrl(avatarUrl);
}
}

public async setRoomUserProfile(roomId: string, profile: UserProfile) {
const userId = this.client.getUserId();
const currProfile = this.opts.backingStore.getMemberProfile(roomId, userId);
Expand Down