Skip to content

Commit

Permalink
Merge pull request #318 from matrix-org/hs/ensure-profile
Browse files Browse the repository at this point in the history
Add Intent.ensureProfile
  • Loading branch information
Half-Shot authored Mar 25, 2021
2 parents 6046d31 + 4ec78dc commit 6831acc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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) {
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

0 comments on commit 6831acc

Please sign in to comment.