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 support for message avatars #41

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/Message.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}

.MessageAvatar {
width: 48px;
border-radius: 32px;
margin-left: 16px;
}

.MessageBody{
margin-left: 24px;
margin-top: 6px;
margin-bottom: 8px;
min-width: 0;
flex: min-content;
}

.moreCommentsButton {
Expand Down
5 changes: 5 additions & 0 deletions src/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Message extends React.Component {
hidden: false,
uploadFile: null,
displayname: null,
avatarUrl: null,
noReply: this.props.noReply,
};
}
Expand All @@ -36,8 +37,10 @@ class Message extends React.Component {
}
try {
const profile = await this.context.client.getProfile(this.props.event.sender);
const avatarUrl = profile.avatar_url && this.context.client.thumbnailLink(profile.avatar_url, 'scale', 48, 48);
Copy link
Member

Choose a reason for hiding this comment

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

Bleh, this actually will be bad if we call /profile for every message rendered. Sure we cache, but on first render the cache will be empty so we'll spam the server for the same profile. We should probably add an in-flight cache on Client.getProfile so we return the same HTTP promise for the same user, so we can at least guarantee only 1 HTTP hit per user (currently it's 1:1 based on the number of messages).

this.setState({
displayname: profile.displayname,
avatarUrl,
});
} catch (ex) {
console.debug(`Failed to fetch profile for user ${this.props.event.sender}:`, ex);
Expand Down Expand Up @@ -81,6 +84,7 @@ class Message extends React.Component {
const profile = await this.context.client.getProfile(this.props.event.sender);
this.setState({
displayname: profile.displayname,
avatarUrl: profile.avatar_url && this.context.client.thumbnailLink(profile.avatar_url, 'scale', 48, 48),
});
} catch (ex) {
console.debug(`Failed to fetch profile for user ${this.props.event.sender}:`, ex);
Expand Down Expand Up @@ -375,6 +379,7 @@ class Message extends React.Component {
return (
<div className="Message">
{modal}
<img alt="" className="MessageAvatar" src={this.state.avatarUrl}></img>
{this.renderEvent()}
<div className="MessageButtons">
<span className="moreCommentsButton">{replies}</span>
Expand Down
4 changes: 2 additions & 2 deletions src/UserPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@

.userAvatar{
margin-right: 1em;
max-width: 64px;
max-height: 64px;
width: 64px;
border-radius: 64px;
}

.userSection {
Expand Down
7 changes: 5 additions & 2 deletions src/UserPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ class UserPage extends React.Component {
const userProfile = await this.props.client.getProfile(
this.props.userId
);
let profile = {
displayname: userProfile.displayname,
}
if (userProfile.avatar_url) {
userProfile.avatar_url = this.props.client.thumbnailLink(
profile.avatar_url = this.props.client.thumbnailLink(
userProfile.avatar_url,
"scale",
64,
64
);
}
this.setState({
userProfile,
userProfile: profile,
});
} catch (ex) {
console.warn(
Expand Down