aboutsummaryrefslogtreecommitdiff
path: root/client/js/models
diff options
context:
space:
mode:
authorRuin0x112020-05-04 15:15:30 -0700
committerRuin0x112020-05-04 15:15:51 -0700
commit6b8e3f251f69c9d7d560148100cdb8df941a81c2 (patch)
tree2370acc1c3119b506ad016e4350d482af98908fb /client/js/models
parentffba010ae4b6efebd791a077125df92ef57274d0 (diff)
Implement pool merging
Diffstat (limited to 'client/js/models')
-rw-r--r--client/js/models/post.js199
1 files changed, 107 insertions, 92 deletions
diff --git a/client/js/models/post.js b/client/js/models/post.js
index e3388ec..6022ae0 100644
--- a/client/js/models/post.js
+++ b/client/js/models/post.js
@@ -8,6 +8,7 @@ const TagList = require('./tag_list.js');
const NoteList = require('./note_list.js');
const CommentList = require('./comment_list.js');
const PoolList = require('./pool_list.js');
+const Pool = require('./pool.js');
const misc = require('../util/misc.js');
class Post extends events.EventTarget {
@@ -98,22 +99,36 @@ class Post extends events.EventTarget {
_savePoolPosts() {
const difference = (a, b) => a.filter(post => !b.hasPoolId(post.id));
+
+ // find the pools where the post was added or removed
const added = difference(this.pools, this._orig._pools);
const removed = difference(this._orig._pools, this.pools);
+
let ops = [];
+ // update each pool's list of posts
for (let pool of added) {
- if (!pool.posts.hasPostId(this._id)) {
- pool.posts.addById(this._id);
- ops.push(pool.save());
- }
+ let op = Pool.get(pool.id).then(response => {
+ if (!response.posts.hasPostId(this._id)) {
+ response.posts.addById(this._id);
+ return response.save();
+ } else {
+ return Promise.resolve(response);
+ }
+ });
+ ops.push(op);
}
for (let pool of removed) {
- if (pool.posts.hasPostId(this._id)) {
- pool.posts.removeById(this._id);
- ops.push(pool.save());
- }
+ let op = Pool.get(pool.id).then(response => {
+ if (response.posts.hasPostId(this._id)) {
+ response.posts.removeById(this._id);
+ return response.save();
+ } else {
+ return Promise.resolve(response);
+ }
+ });
+ ops.push(op);
}
return Promise.all(ops);
@@ -156,34 +171,34 @@ class Post extends events.EventTarget {
}
let apiPromise = this._id ?
- api.put(uri.formatApiLink('post', this.id), detail, files) :
- api.post(uri.formatApiLink('posts'), detail, files);
+ api.put(uri.formatApiLink('post', this.id), detail, files) :
+ api.post(uri.formatApiLink('posts'), detail, files);
return apiPromise.then(response => {
- if (this._pools !== this._orig._pools) {
+ if (misc.arraysDiffer(this._pools, this._orig._pools)) {
return this._savePoolPosts()
- .then(() => Promise.resolve(response));
+ .then(() => Promise.resolve(response));
}
return Promise.resolve(response);
}).then(response => {
this._updateFromResponse(response);
this.dispatchEvent(
- new CustomEvent('change', {detail: {post: this}}));
+ new CustomEvent('change', {detail: {post: this}}));
if (this._newContent) {
this.dispatchEvent(
- new CustomEvent('changeContent', {detail: {post: this}}));
+ new CustomEvent('changeContent', {detail: {post: this}}));
}
if (this._newThumbnail) {
this.dispatchEvent(
- new CustomEvent('changeThumbnail', {detail: {post: this}}));
+ new CustomEvent('changeThumbnail', {detail: {post: this}}));
}
return Promise.resolve();
}, error => {
if (error.response &&
- error.response.name === 'PostAlreadyUploadedError') {
+ error.response.name === 'PostAlreadyUploadedError') {
error.message =
- `Post already uploaded (@${error.response.otherPostId})`;
+ `Post already uploaded (@${error.response.otherPostId})`;
}
return Promise.reject(error);
});
@@ -191,118 +206,118 @@ class Post extends events.EventTarget {
feature() {
return api.post(
- uri.formatApiLink('featured-post'),
- {id: this._id})
- .then(response => {
- return Promise.resolve();
- });
+ uri.formatApiLink('featured-post'),
+ {id: this._id})
+ .then(response => {
+ return Promise.resolve();
+ });
}
delete() {
return api.delete(
- uri.formatApiLink('post', this.id),
- {version: this._version})
- .then(response => {
- this.dispatchEvent(new CustomEvent('delete', {
- detail: {
- post: this,
- },
- }));
- return Promise.resolve();
- });
+ uri.formatApiLink('post', this.id),
+ {version: this._version})
+ .then(response => {
+ this.dispatchEvent(new CustomEvent('delete', {
+ detail: {
+ post: this,
+ },
+ }));
+ return Promise.resolve();
+ });
}
merge(targetId, useOldContent) {
return api.get(uri.formatApiLink('post', targetId))
- .then(response => {
- return api.post(uri.formatApiLink('post-merge'), {
- removeVersion: this._version,
- remove: this._id,
- mergeToVersion: response.version,
- mergeTo: targetId,
- replaceContent: useOldContent,
+ .then(response => {
+ return api.post(uri.formatApiLink('post-merge'), {
+ removeVersion: this._version,
+ remove: this._id,
+ mergeToVersion: response.version,
+ mergeTo: targetId,
+ replaceContent: useOldContent,
+ });
+ }).then(response => {
+ this._updateFromResponse(response);
+ this.dispatchEvent(new CustomEvent('change', {
+ detail: {
+ post: this,
+ },
+ }));
+ return Promise.resolve();
});
- }).then(response => {
- this._updateFromResponse(response);
- this.dispatchEvent(new CustomEvent('change', {
- detail: {
- post: this,
- },
- }));
- return Promise.resolve();
- });
}
setScore(score) {
return api.put(
- uri.formatApiLink('post', this.id, 'score'),
- {score: score})
- .then(response => {
- const prevFavorite = this._ownFavorite;
- this._updateFromResponse(response);
- if (this._ownFavorite !== prevFavorite) {
- this.dispatchEvent(new CustomEvent('changeFavorite', {
+ uri.formatApiLink('post', this.id, 'score'),
+ {score: score})
+ .then(response => {
+ const prevFavorite = this._ownFavorite;
+ this._updateFromResponse(response);
+ if (this._ownFavorite !== prevFavorite) {
+ this.dispatchEvent(new CustomEvent('changeFavorite', {
+ detail: {
+ post: this,
+ },
+ }));
+ }
+ this.dispatchEvent(new CustomEvent('changeScore', {
detail: {
post: this,
},
}));
- }
- this.dispatchEvent(new CustomEvent('changeScore', {
- detail: {
- post: this,
- },
- }));
- return Promise.resolve();
- });
+ return Promise.resolve();
+ });
}
addToFavorites() {
return api.post(uri.formatApiLink('post', this.id, 'favorite'))
- .then(response => {
- const prevScore = this._ownScore;
- this._updateFromResponse(response);
- if (this._ownScore !== prevScore) {
- this.dispatchEvent(new CustomEvent('changeScore', {
+ .then(response => {
+ const prevScore = this._ownScore;
+ this._updateFromResponse(response);
+ if (this._ownScore !== prevScore) {
+ this.dispatchEvent(new CustomEvent('changeScore', {
+ detail: {
+ post: this,
+ },
+ }));
+ }
+ this.dispatchEvent(new CustomEvent('changeFavorite', {
detail: {
post: this,
},
}));
- }
- this.dispatchEvent(new CustomEvent('changeFavorite', {
- detail: {
- post: this,
- },
- }));
- return Promise.resolve();
- });
+ return Promise.resolve();
+ });
}
removeFromFavorites() {
return api.delete(uri.formatApiLink('post', this.id, 'favorite'))
- .then(response => {
- const prevScore = this._ownScore;
- this._updateFromResponse(response);
- if (this._ownScore !== prevScore) {
- this.dispatchEvent(new CustomEvent('changeScore', {
+ .then(response => {
+ const prevScore = this._ownScore;
+ this._updateFromResponse(response);
+ if (this._ownScore !== prevScore) {
+ this.dispatchEvent(new CustomEvent('changeScore', {
+ detail: {
+ post: this,
+ },
+ }));
+ }
+ this.dispatchEvent(new CustomEvent('changeFavorite', {
detail: {
post: this,
},
}));
- }
- this.dispatchEvent(new CustomEvent('changeFavorite', {
- detail: {
- post: this,
- },
- }));
- return Promise.resolve();
- });
+ return Promise.resolve();
+ });
}
mutateContentUrl() {
this._contentUrl =
- this._orig._contentUrl +
- '?bypass-cache=' +
- Math.round(Math.random() * 1000);
+ this._orig._contentUrl +
+ '?bypass-cache=' +
+ Math.round(Math.random() * 1000);
}
_updateFromResponse(response) {

© 2015 - 2026 Jakob L. Kreuze