client/general: use resource versioning
This commit is contained in:
parent
8d04df38fd
commit
79b3973f9e
6 changed files with 49 additions and 34 deletions
|
@ -56,9 +56,9 @@ class Api extends events.EventTarget {
|
||||||
return this._process(url, request.put, data, files, options);
|
return this._process(url, request.put, data, files, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(url, options) {
|
delete(url, data, options) {
|
||||||
this.cache = {};
|
this.cache = {};
|
||||||
return this._process(url, request.delete, {}, {}, options);
|
return this._process(url, request.delete, data, {}, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
_process(url, requestFactory, data, files, options) {
|
_process(url, requestFactory, data, files, options) {
|
||||||
|
|
|
@ -33,21 +33,14 @@ class Comment extends events.EventTarget {
|
||||||
set text(value) { this._text = value; }
|
set text(value) { this._text = value; }
|
||||||
|
|
||||||
save() {
|
save() {
|
||||||
let promise = null;
|
const detail = {
|
||||||
if (this._id) {
|
version: this._version,
|
||||||
promise = api.put(
|
text: this._text,
|
||||||
'/comment/' + this._id,
|
};
|
||||||
{
|
let promise = this._id ?
|
||||||
text: this._text,
|
api.put('/comment/' + this._id, detail) :
|
||||||
});
|
api.post(
|
||||||
} else {
|
'/comments', Object.assign({postId: this._postId}, detail));
|
||||||
promise = api.post(
|
|
||||||
'/comments',
|
|
||||||
{
|
|
||||||
text: this._text,
|
|
||||||
postId: this._postId,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return promise.then(response => {
|
return promise.then(response => {
|
||||||
this._updateFromResponse(response);
|
this._updateFromResponse(response);
|
||||||
|
@ -63,7 +56,9 @@ class Comment extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
delete() {
|
delete() {
|
||||||
return api.delete('/comment/' + this._id)
|
return api.delete(
|
||||||
|
'/comment/' + this._id,
|
||||||
|
{version: this._version})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
this.dispatchEvent(new CustomEvent('delete', {
|
this.dispatchEvent(new CustomEvent('delete', {
|
||||||
detail: {
|
detail: {
|
||||||
|
@ -92,6 +87,7 @@ class Comment extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateFromResponse(response) {
|
_updateFromResponse(response) {
|
||||||
|
this._version = response.version;
|
||||||
this._id = response.id;
|
this._id = response.id;
|
||||||
this._postId = response.postId;
|
this._postId = response.postId;
|
||||||
this._text = response.text;
|
this._text = response.text;
|
||||||
|
|
|
@ -85,7 +85,7 @@ class Post extends events.EventTarget {
|
||||||
|
|
||||||
save() {
|
save() {
|
||||||
const files = [];
|
const files = [];
|
||||||
const detail = {};
|
const detail = {version: this._version};
|
||||||
|
|
||||||
// send only changed fields to avoid user privilege violation
|
// send only changed fields to avoid user privilege violation
|
||||||
if (this._safety !== this._orig._safety) {
|
if (this._safety !== this._orig._safety) {
|
||||||
|
@ -206,7 +206,9 @@ class Post extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
removeFromFavorites() {
|
removeFromFavorites() {
|
||||||
return api.delete('/post/' + this.id + '/favorite')
|
return api.delete(
|
||||||
|
'/post/' + this.id + '/favorite',
|
||||||
|
{version: this._version})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
const prevScore = this._ownScore;
|
const prevScore = this._ownScore;
|
||||||
this._updateFromResponse(response);
|
this._updateFromResponse(response);
|
||||||
|
@ -237,6 +239,7 @@ class Post extends events.EventTarget {
|
||||||
|
|
||||||
_updateFromResponse(response) {
|
_updateFromResponse(response) {
|
||||||
const map = () => ({
|
const map = () => ({
|
||||||
|
_version: response.version,
|
||||||
_id: response.id,
|
_id: response.id,
|
||||||
_type: response.type,
|
_type: response.type,
|
||||||
_mimeType: response.mimeType,
|
_mimeType: response.mimeType,
|
||||||
|
|
|
@ -42,7 +42,7 @@ class Tag extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
save() {
|
save() {
|
||||||
const detail = {};
|
const detail = {version: this._version};
|
||||||
|
|
||||||
// send only changed fields to avoid user privilege violation
|
// send only changed fields to avoid user privilege violation
|
||||||
if (misc.arraysDiffer(this._names, this._orig._names)) {
|
if (misc.arraysDiffer(this._names, this._orig._names)) {
|
||||||
|
@ -79,9 +79,15 @@ class Tag extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
merge(targetName) {
|
merge(targetName) {
|
||||||
return api.post('/tag-merge/', {
|
return api.get('/tag/' + targetName).then(response => {
|
||||||
remove: this._origName,
|
return api.post('/tag-merge/', {
|
||||||
mergeTo: targetName,
|
removeVersion: this._version,
|
||||||
|
remove: this._origName,
|
||||||
|
mergeToVersion: response.version,
|
||||||
|
mergeTo: targetName,
|
||||||
|
});
|
||||||
|
}, response => {
|
||||||
|
return Promise.reject(response);
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
this._updateFromResponse(response);
|
this._updateFromResponse(response);
|
||||||
this.dispatchEvent(new CustomEvent('change', {
|
this.dispatchEvent(new CustomEvent('change', {
|
||||||
|
@ -96,7 +102,9 @@ class Tag extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
delete() {
|
delete() {
|
||||||
return api.delete('/tag/' + this._origName)
|
return api.delete(
|
||||||
|
'/tag/' + this._origName,
|
||||||
|
{version: this._version})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
this.dispatchEvent(new CustomEvent('delete', {
|
this.dispatchEvent(new CustomEvent('delete', {
|
||||||
detail: {
|
detail: {
|
||||||
|
@ -111,6 +119,7 @@ class Tag extends events.EventTarget {
|
||||||
|
|
||||||
_updateFromResponse(response) {
|
_updateFromResponse(response) {
|
||||||
const map = {
|
const map = {
|
||||||
|
_version: response.version,
|
||||||
_origName: response.names ? response.names[0] : null,
|
_origName: response.names ? response.names[0] : null,
|
||||||
_names: response.names,
|
_names: response.names,
|
||||||
_category: response.category,
|
_category: response.category,
|
||||||
|
|
|
@ -30,21 +30,22 @@ class TagCategory extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
save() {
|
save() {
|
||||||
const data = {};
|
const detail = {version: this._version};
|
||||||
|
|
||||||
if (this.name !== this._origName) {
|
if (this.name !== this._origName) {
|
||||||
data.name = this.name;
|
detail.name = this.name;
|
||||||
}
|
}
|
||||||
if (this.color !== this._origColor) {
|
if (this.color !== this._origColor) {
|
||||||
data.color = this.color;
|
detail.color = this.color;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Object.keys(data).length) {
|
if (!Object.keys(detail).length) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
let promise = this._origName ?
|
let promise = this._origName ?
|
||||||
api.put('/tag-category/' + this._origName, data) :
|
api.put('/tag-category/' + this._origName, detail) :
|
||||||
api.post('/tag-categories', data);
|
api.post('/tag-categories', detail);
|
||||||
|
|
||||||
return promise
|
return promise
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
@ -61,7 +62,9 @@ class TagCategory extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
delete() {
|
delete() {
|
||||||
return api.delete('/tag-category/' + this._origName)
|
return api.delete(
|
||||||
|
'/tag-category/' + this._origName,
|
||||||
|
{version: this._version})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
this.dispatchEvent(new CustomEvent('delete', {
|
this.dispatchEvent(new CustomEvent('delete', {
|
||||||
detail: {
|
detail: {
|
||||||
|
@ -75,6 +78,7 @@ class TagCategory extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateFromResponse(response) {
|
_updateFromResponse(response) {
|
||||||
|
this._version = response.version;
|
||||||
this._name = response.name;
|
this._name = response.name;
|
||||||
this._color = response.color;
|
this._color = response.color;
|
||||||
this._isDefault = response.default;
|
this._isDefault = response.default;
|
||||||
|
|
|
@ -50,7 +50,7 @@ class User extends events.EventTarget {
|
||||||
|
|
||||||
save() {
|
save() {
|
||||||
const files = [];
|
const files = [];
|
||||||
const detail = {};
|
const detail = {version: this._version};
|
||||||
const transient = this._orig._name;
|
const transient = this._orig._name;
|
||||||
|
|
||||||
if (this._name !== this._orig._name) {
|
if (this._name !== this._orig._name) {
|
||||||
|
@ -91,7 +91,9 @@ class User extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
delete() {
|
delete() {
|
||||||
return api.delete('/user/' + this._orig._name)
|
return api.delete(
|
||||||
|
'/user/' + this._orig._name,
|
||||||
|
{version: this._version})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
this.dispatchEvent(new CustomEvent('delete', {
|
this.dispatchEvent(new CustomEvent('delete', {
|
||||||
detail: {
|
detail: {
|
||||||
|
@ -106,6 +108,7 @@ class User extends events.EventTarget {
|
||||||
|
|
||||||
_updateFromResponse(response) {
|
_updateFromResponse(response) {
|
||||||
const map = {
|
const map = {
|
||||||
|
_version: response.version,
|
||||||
_name: response.name,
|
_name: response.name,
|
||||||
_rank: response.rank,
|
_rank: response.rank,
|
||||||
_email: response.email,
|
_email: response.email,
|
||||||
|
|
Loading…
Reference in a new issue