2016-06-19 17:16:40 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const api = require('../api.js');
|
2017-01-20 20:51:04 +00:00
|
|
|
const uri = require('../util/uri.js');
|
2016-06-19 17:16:40 +00:00
|
|
|
const events = require('../events.js');
|
|
|
|
|
|
|
|
class User extends events.EventTarget {
|
|
|
|
constructor() {
|
|
|
|
super();
|
2016-07-26 19:02:21 +00:00
|
|
|
this._orig = {};
|
2016-07-26 19:01:52 +00:00
|
|
|
this._updateFromResponse({});
|
2016-06-19 17:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get name() { return this._name; }
|
|
|
|
get rank() { return this._rank; }
|
|
|
|
get email() { return this._email; }
|
|
|
|
get avatarStyle() { return this._avatarStyle; }
|
|
|
|
get avatarUrl() { return this._avatarUrl; }
|
|
|
|
get creationTime() { return this._creationTime; }
|
|
|
|
get lastLoginTime() { return this._lastLoginTime; }
|
|
|
|
get commentCount() { return this._commentCount; }
|
|
|
|
get favoritePostCount() { return this._favoritePostCount; }
|
|
|
|
get uploadedPostCount() { return this._uploadedPostCount; }
|
|
|
|
get likedPostCount() { return this._likedPostCount; }
|
|
|
|
get dislikedPostCount() { return this._dislikedPostCount; }
|
|
|
|
get rankName() { return api.rankNames.get(this.rank); }
|
|
|
|
get avatarContent() { throw 'Invalid operation'; }
|
|
|
|
get password() { throw 'Invalid operation'; }
|
|
|
|
|
|
|
|
set name(value) { this._name = value; }
|
|
|
|
set rank(value) { this._rank = value; }
|
|
|
|
set email(value) { this._email = value || null; }
|
|
|
|
set avatarStyle(value) { this._avatarStyle = value; }
|
|
|
|
set avatarContent(value) { this._avatarContent = value; }
|
|
|
|
set password(value) { this._password = value; }
|
|
|
|
|
|
|
|
static fromResponse(response) {
|
|
|
|
const ret = new User();
|
|
|
|
ret._updateFromResponse(response);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static get(name) {
|
2017-01-20 20:51:04 +00:00
|
|
|
return api.get(uri.formatApiLink('user', name))
|
2016-06-19 17:16:40 +00:00
|
|
|
.then(response => {
|
|
|
|
return Promise.resolve(User.fromResponse(response));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
const files = [];
|
2016-08-06 20:44:04 +00:00
|
|
|
const detail = {version: this._version};
|
2016-07-26 19:02:21 +00:00
|
|
|
const transient = this._orig._name;
|
2016-06-19 17:16:40 +00:00
|
|
|
|
2016-07-26 19:02:21 +00:00
|
|
|
if (this._name !== this._orig._name) {
|
|
|
|
detail.name = this._name;
|
2016-06-19 17:16:40 +00:00
|
|
|
}
|
2016-07-26 19:02:21 +00:00
|
|
|
if (this._email !== this._orig._email) {
|
|
|
|
detail.email = this._email;
|
2016-06-19 17:16:40 +00:00
|
|
|
}
|
2016-07-26 19:02:21 +00:00
|
|
|
if (this._rank !== this._orig._rank) {
|
|
|
|
detail.rank = this._rank;
|
|
|
|
}
|
|
|
|
if (this._avatarStyle !== this._orig._avatarStyle) {
|
|
|
|
detail.avatarStyle = this._avatarStyle;
|
2016-06-19 17:16:40 +00:00
|
|
|
}
|
|
|
|
if (this._avatarContent) {
|
2016-08-13 09:50:39 +00:00
|
|
|
detail.avatarStyle = this._avatarStyle;
|
2016-06-19 17:16:40 +00:00
|
|
|
files.avatar = this._avatarContent;
|
|
|
|
}
|
2016-07-26 19:02:21 +00:00
|
|
|
if (this._password) {
|
|
|
|
detail.password = this._password;
|
|
|
|
}
|
2016-06-19 17:16:40 +00:00
|
|
|
|
2016-07-26 19:02:21 +00:00
|
|
|
let promise = this._orig._name ?
|
2016-08-16 18:09:03 +00:00
|
|
|
api.put(
|
2017-01-20 20:51:04 +00:00
|
|
|
uri.formatApiLink('user', this._orig._name), detail, files) :
|
|
|
|
api.post(uri.formatApiLink('users'), detail, files);
|
2016-06-19 17:16:40 +00:00
|
|
|
|
|
|
|
return promise
|
|
|
|
.then(response => {
|
|
|
|
this._updateFromResponse(response);
|
|
|
|
this.dispatchEvent(new CustomEvent('change', {
|
|
|
|
detail: {
|
|
|
|
user: this,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
delete() {
|
2016-08-06 20:44:04 +00:00
|
|
|
return api.delete(
|
2017-01-20 20:51:04 +00:00
|
|
|
uri.formatApiLink('user', this._orig._name),
|
2016-08-06 20:44:04 +00:00
|
|
|
{version: this._version})
|
2016-06-19 17:16:40 +00:00
|
|
|
.then(response => {
|
|
|
|
this.dispatchEvent(new CustomEvent('delete', {
|
|
|
|
detail: {
|
|
|
|
user: this,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateFromResponse(response) {
|
2016-07-26 19:02:21 +00:00
|
|
|
const map = {
|
2016-08-06 20:44:04 +00:00
|
|
|
_version: response.version,
|
2016-07-26 19:02:21 +00:00
|
|
|
_name: response.name,
|
|
|
|
_rank: response.rank,
|
|
|
|
_email: response.email,
|
|
|
|
_avatarStyle: response.avatarStyle,
|
|
|
|
_avatarUrl: response.avatarUrl,
|
|
|
|
_creationTime: response.creationTime,
|
|
|
|
_lastLoginTime: response.lastLoginTime,
|
|
|
|
_commentCount: response.commentCount,
|
|
|
|
_favoritePostCount: response.favoritePostCount,
|
|
|
|
_uploadedPostCount: response.uploadedPostCount,
|
|
|
|
_likedPostCount: response.likedPostCount,
|
|
|
|
_dislikedPostCount: response.dislikedPostCount,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(this, map);
|
|
|
|
Object.assign(this._orig, map);
|
2016-06-19 17:16:40 +00:00
|
|
|
|
|
|
|
this._password = null;
|
|
|
|
this._avatarContent = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = User;
|