2015-11-26 14:45:04 +01:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-26 14:45:04 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2016-01-15 18:31:32 +01:00
|
|
|
var ContentRepo = require("matrix-js-sdk").ContentRepo;
|
2015-11-26 14:49:39 +01:00
|
|
|
var MatrixClientPeg = require('./MatrixClientPeg');
|
2015-11-26 14:45:04 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
avatarUrlForMember: function(member, width, height, resizeMethod) {
|
|
|
|
var url = member.getAvatarUrl(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
2017-04-21 00:05:52 +02:00
|
|
|
window.devicePixelRatio > 1.2 ? 96 : width,
|
|
|
|
window.devicePixelRatio > 1.2 ? 96 : height,
|
2015-11-30 15:39:29 +01:00
|
|
|
resizeMethod,
|
|
|
|
false,
|
|
|
|
false
|
2015-11-26 14:45:04 +01:00
|
|
|
);
|
|
|
|
if (!url) {
|
|
|
|
// member can be null here currently since on invites, the JS SDK
|
|
|
|
// does not have enough info to build a RoomMember object for
|
|
|
|
// the inviter.
|
|
|
|
url = this.defaultAvatarUrlForString(member ? member.userId : '');
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2016-01-15 18:31:32 +01:00
|
|
|
avatarUrlForUser: function(user, width, height, resizeMethod) {
|
|
|
|
var url = ContentRepo.getHttpUriForMxc(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(), user.avatarUrl,
|
2017-04-21 00:05:52 +02:00
|
|
|
window.devicePixelRatio > 1.2 ? 96 : width,
|
|
|
|
window.devicePixelRatio > 1.2 ? 96 : height,
|
|
|
|
resizeMethod
|
2016-01-15 18:31:32 +01:00
|
|
|
);
|
|
|
|
if (!url || url.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2015-11-26 14:45:04 +01:00
|
|
|
defaultAvatarUrlForString: function(s) {
|
2017-01-20 15:22:27 +01:00
|
|
|
var images = ['76cfa6', '50e2c2', 'f4c371'];
|
2015-11-26 14:45:04 +01:00
|
|
|
var total = 0;
|
|
|
|
for (var i = 0; i < s.length; ++i) {
|
|
|
|
total += s.charCodeAt(i);
|
|
|
|
}
|
2015-11-30 15:39:29 +01:00
|
|
|
return 'img/' + images[total % images.length] + '.png';
|
2015-11-26 14:45:04 +01:00
|
|
|
}
|
2017-01-20 15:22:27 +01:00
|
|
|
};
|