2015-07-15 12:30:11 +08:00
|
|
|
/*global ColorThief*/
|
|
|
|
|
|
|
|
import Model from 'flarum/Model';
|
|
|
|
import mixin from 'flarum/utils/mixin';
|
|
|
|
import stringToColor from 'flarum/utils/stringToColor';
|
|
|
|
import ItemList from 'flarum/utils/ItemList';
|
2015-04-25 20:58:39 +08:00
|
|
|
import computed from 'flarum/utils/computed';
|
2015-07-15 12:30:11 +08:00
|
|
|
import Badge from 'flarum/components/Badge';
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
export default class User extends mixin(Model, {
|
|
|
|
username: Model.attribute('username'),
|
|
|
|
email: Model.attribute('email'),
|
|
|
|
isConfirmed: Model.attribute('isConfirmed'),
|
|
|
|
password: Model.attribute('password'),
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
avatarUrl: Model.attribute('avatarUrl'),
|
|
|
|
bio: Model.attribute('bio'),
|
2015-07-22 14:33:48 +08:00
|
|
|
bioHtml: computed('bio', bio => '<p>' + $('<div/>').text(bio).html() + '</p>'),
|
2015-07-15 12:30:11 +08:00
|
|
|
preferences: Model.attribute('preferences'),
|
|
|
|
groups: Model.hasMany('groups'),
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
joinTime: Model.attribute('joinTime', Model.transformDate),
|
|
|
|
lastSeenTime: Model.attribute('lastSeenTime', Model.transformDate),
|
|
|
|
readTime: Model.attribute('readTime', Model.transformDate),
|
|
|
|
unreadNotificationsCount: Model.attribute('unreadNotificationsCount'),
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
discussionsCount: Model.attribute('discussionsCount'),
|
|
|
|
commentsCount: Model.attribute('commentsCount'),
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
canEdit: Model.attribute('canEdit'),
|
|
|
|
canDelete: Model.attribute('canDelete'),
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
avatarColor: null,
|
|
|
|
color: computed('username', 'avatarUrl', 'avatarColor', function(username, avatarUrl, avatarColor) {
|
|
|
|
// 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);
|
|
|
|
})
|
|
|
|
}) {
|
|
|
|
/**
|
|
|
|
* Check whether or not the user has been seen in the last 5 minutes.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
isOnline() {
|
|
|
|
return this.lastSeenTime() > moment().subtract(5, 'minutes').toDate();
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Get the Badge components that apply to this user.
|
|
|
|
*
|
|
|
|
* @return {ItemList}
|
|
|
|
*/
|
|
|
|
badges() {
|
|
|
|
const items = new ItemList();
|
2015-05-06 09:55:19 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
this.groups().forEach(group => {
|
2015-07-17 16:13:28 +08:00
|
|
|
const name = group.nameSingular();
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
items.add('group' + group.id(),
|
2015-05-06 09:55:19 +08:00
|
|
|
Badge.component({
|
2015-07-17 16:13:28 +08:00
|
|
|
label: app.trans('core.group_' + name.toLowerCase(), undefined, name),
|
2015-05-06 09:55:19 +08:00
|
|
|
icon: group.icon(),
|
|
|
|
style: {backgroundColor: group.color()}
|
|
|
|
})
|
|
|
|
);
|
2015-07-15 12:30:11 +08:00
|
|
|
});
|
2015-05-06 09:55:19 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
image.onload = function() {
|
|
|
|
const colorThief = new ColorThief();
|
|
|
|
user.avatarColor = colorThief.getColor(this);
|
|
|
|
user.freshness = new Date();
|
|
|
|
m.redraw();
|
|
|
|
};
|
|
|
|
image.src = this.avatarUrl();
|
|
|
|
}
|
|
|
|
}
|