2015-07-15 14:00:11 +09:30
|
|
|
/*global ColorThief*/
|
|
|
|
|
2018-06-20 13:20:31 +09:30
|
|
|
import Model from '../Model';
|
|
|
|
import stringToColor from '../utils/stringToColor';
|
|
|
|
import ItemList from '../utils/ItemList';
|
|
|
|
import computed from '../utils/computed';
|
|
|
|
import GroupBadge from '../components/GroupBadge';
|
2015-04-25 22:28:39 +09:30
|
|
|
|
2015-10-13 16:57:18 +10:30
|
|
|
export default class User extends Model {}
|
|
|
|
|
|
|
|
Object.assign(User.prototype, {
|
2015-07-15 14:00:11 +09:30
|
|
|
username: Model.attribute('username'),
|
2017-09-20 16:42:18 +09:30
|
|
|
displayName: Model.attribute('displayName'),
|
2015-07-15 14:00:11 +09:30
|
|
|
email: Model.attribute('email'),
|
2018-08-24 21:35:04 +09:30
|
|
|
isEmailConfirmed: Model.attribute('isEmailConfirmed'),
|
2015-07-15 14:00:11 +09:30
|
|
|
password: Model.attribute('password'),
|
2015-04-25 22:28:39 +09:30
|
|
|
|
2015-07-15 14:00:11 +09:30
|
|
|
avatarUrl: Model.attribute('avatarUrl'),
|
|
|
|
preferences: Model.attribute('preferences'),
|
|
|
|
groups: Model.hasMany('groups'),
|
2015-04-25 22:28:39 +09:30
|
|
|
|
2015-07-15 14:00:11 +09:30
|
|
|
joinTime: Model.attribute('joinTime', Model.transformDate),
|
2018-08-24 21:33:48 +09:30
|
|
|
lastSeenAt: Model.attribute('lastSeenAt', Model.transformDate),
|
2018-08-24 21:35:46 +09:30
|
|
|
markedAllAsReadAt: Model.attribute('markedAllAsReadAt', Model.transformDate),
|
2018-08-24 21:36:33 +09:30
|
|
|
unreadNotificationCount: Model.attribute('unreadNotificationCount'),
|
2018-08-24 21:37:51 +09:30
|
|
|
newNotificationCount: Model.attribute('newNotificationCount'),
|
2015-04-25 22:28:39 +09:30
|
|
|
|
2018-08-24 21:32:47 +09:30
|
|
|
discussionCount: Model.attribute('discussionCount'),
|
2018-08-24 21:33:18 +09:30
|
|
|
commentCount: Model.attribute('commentCount'),
|
2015-04-25 22:28:39 +09:30
|
|
|
|
2015-07-15 14:00:11 +09:30
|
|
|
canEdit: Model.attribute('canEdit'),
|
|
|
|
canDelete: Model.attribute('canDelete'),
|
2015-04-25 22:28:39 +09:30
|
|
|
|
2015-07-15 14:00:11 +09:30
|
|
|
avatarColor: null,
|
2020-04-17 11:57:55 +02:00
|
|
|
color: computed('username', 'avatarUrl', 'avatarColor', function (username, avatarUrl, avatarColor) {
|
2015-07-15 14:00:11 +09:30
|
|
|
// If we've already calculated and cached the dominant color of the user's
|
|
|
|
// avatar, then we can return that in RGB format. If we haven't, we'll want
|
|
|
|
// to calculate it. Unless the user doesn't have an avatar, in which case
|
|
|
|
// we generate a color from their username.
|
|
|
|
if (avatarColor) {
|
|
|
|
return 'rgb(' + avatarColor.join(', ') + ')';
|
|
|
|
} else if (avatarUrl) {
|
|
|
|
this.calculateAvatarColor();
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '#' + stringToColor(username);
|
2015-10-13 16:57:18 +10:30
|
|
|
}),
|
|
|
|
|
2015-07-15 14:00:11 +09:30
|
|
|
/**
|
|
|
|
* Check whether or not the user has been seen in the last 5 minutes.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
isOnline() {
|
2018-08-24 21:33:48 +09:30
|
|
|
return this.lastSeenAt() > moment().subtract(5, 'minutes').toDate();
|
2015-10-13 16:57:18 +10:30
|
|
|
},
|
2015-04-25 22:28:39 +09:30
|
|
|
|
2015-07-15 14:00:11 +09:30
|
|
|
/**
|
|
|
|
* Get the Badge components that apply to this user.
|
|
|
|
*
|
|
|
|
* @return {ItemList}
|
|
|
|
*/
|
|
|
|
badges() {
|
|
|
|
const items = new ItemList();
|
2015-07-28 13:35:07 +09:30
|
|
|
const groups = this.groups();
|
2015-05-06 11:25:19 +09:30
|
|
|
|
2015-07-28 13:35:07 +09:30
|
|
|
if (groups) {
|
2020-04-17 11:57:55 +02:00
|
|
|
groups.forEach((group) => {
|
|
|
|
items.add('group' + group.id(), GroupBadge.component({ group }));
|
2015-07-28 13:35:07 +09:30
|
|
|
});
|
|
|
|
}
|
2015-05-06 11:25:19 +09:30
|
|
|
|
2015-07-15 14:00:11 +09:30
|
|
|
return items;
|
2015-10-13 16:57:18 +10:30
|
|
|
},
|
2015-07-15 14:00:11 +09:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the dominant color of the user's avatar. The dominant color will
|
|
|
|
* be set to the `avatarColor` property once it has been calculated.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
calculateAvatarColor() {
|
|
|
|
const image = new Image();
|
|
|
|
const user = this;
|
2015-04-25 22:28:39 +09:30
|
|
|
|
2020-04-17 11:57:55 +02:00
|
|
|
image.onload = function () {
|
2015-07-15 14:00:11 +09:30
|
|
|
const colorThief = new ColorThief();
|
|
|
|
user.avatarColor = colorThief.getColor(this);
|
|
|
|
user.freshness = new Date();
|
|
|
|
m.redraw();
|
|
|
|
};
|
2019-10-28 16:35:31 +01:00
|
|
|
image.crossOrigin = 'anonymous';
|
2015-07-15 14:00:11 +09:30
|
|
|
image.src = this.avatarUrl();
|
2015-10-13 16:57:18 +10:30
|
|
|
},
|
2015-08-05 09:50:57 +09:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the user's preferences.
|
|
|
|
*
|
|
|
|
* @param {Object} newPreferences
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
savePreferences(newPreferences) {
|
|
|
|
const preferences = this.preferences();
|
|
|
|
|
|
|
|
Object.assign(preferences, newPreferences);
|
|
|
|
|
2020-04-17 11:57:55 +02:00
|
|
|
return this.save({ preferences });
|
|
|
|
},
|
2015-10-13 16:57:18 +10:30
|
|
|
});
|