Skip to content

Commit 7e93b75

Browse files
committed
API Change: better fallback for room avatars
1 parent 549d992 commit 7e93b75

1 file changed

Lines changed: 58 additions & 18 deletions

File tree

‎src/controllers/atoms/RoomAvatar.js‎

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ limitations under the License.
1818

1919
var MatrixClientPeg = require('../../MatrixClientPeg');
2020

21+
/*
22+
* View class should provide:
23+
* - getUrlList() returning an array of URLs to try for the room avatar
24+
in order of preference from the most preferred at index 0. null entries
25+
in the array will be skipped over.
26+
*/
2127
module.exports = {
2228
getDefaultProps: function() {
2329
return {
@@ -27,35 +33,69 @@ module.exports = {
2733
}
2834
},
2935

30-
avatarUrlForRoom: function(room) {
31-
var url = room.getAvatarUrl(
36+
getInitialState: function() {
37+
this.urlList = this.getUrlList();
38+
this.urlListIndex = -1;
39+
return {
40+
imageUrl: this._nextUrl()
41+
};
42+
},
43+
44+
_nextUrl: function() {
45+
do {
46+
++this.urlListIndex;
47+
} while (
48+
this.urlList[this.urlListIndex] === null &&
49+
this.urlListIndex < this.urlList.length
50+
);
51+
if (this.urlListIndex < this.urlList.length) {
52+
return this.urlList[this.urlListIndex];
53+
} else {
54+
return null;
55+
}
56+
},
57+
58+
// provided to the view class for convenience
59+
roomAvatarUrl: function() {
60+
var url = this.props.room.getAvatarUrl(
3261
MatrixClientPeg.get().getHomeserverUrl(),
3362
this.props.width, this.props.height, this.props.resizeMethod,
3463
false
3564
);
36-
if (url === null) {
37-
url = this.defaultAvatarUrl(room);
38-
}
3965
return url;
4066
},
4167

42-
defaultAvatarUrl: function(member) {
43-
return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADRJREFUeNrszQENADAIACB9QjNbxSKP4eagAFnTseHFErFYLBaLxWKxWCwWi8Vi8cX4CzAABSwCRWJw31gAAAAASUVORK5CYII=";
68+
// provided to the view class for convenience
69+
getOneToOneAvatar: function() {
70+
var userIds = Object.keys(this.props.room.currentState.members);
71+
72+
if (userIds.length == 2) {
73+
var theOtherGuy = null;
74+
if (this.props.room.currentState.members[userIds[0]].userId == MatrixClientPeg.get().credentials.userId) {
75+
theOtherGuy = this.props.room.currentState.members[userIds[1]];
76+
} else {
77+
theOtherGuy = this.props.room.currentState.members[userIds[0]];
78+
}
79+
return theOtherGuy.getAvatarUrl(
80+
MatrixClientPeg.get().getHomeserverUrl(),
81+
this.props.width, this.props.height, this.props.resizeMethod,
82+
false
83+
);
84+
} else if (userIds.length == 1) {
85+
return this.props.room.currentState.members[userIds[0]].getAvatarUrl(
86+
MatrixClientPeg.get().getHomeserverUrl(),
87+
this.props.width, this.props.height, this.props.resizeMethod,
88+
false
89+
);
90+
} else {
91+
return null;
92+
}
4493
},
4594

95+
4696
onError: function(ev) {
47-
// don't tightloop if the browser can't load a data url
48-
if (ev.target.src == this.defaultAvatarUrl(this.props.room)) {
49-
return;
50-
}
5197
this.setState({
52-
imageUrl: this.defaultAvatarUrl(this.props.room)
98+
imageUrl: this._nextUrl()
5399
});
54-
},
55-
56-
getInitialState: function() {
57-
return {
58-
imageUrl: this.avatarUrlForRoom(this.props.room)
59-
};
60100
}
61101
};

0 commit comments

Comments
 (0)