REFACTOR: post model (#7659)
This commit is contained in:
parent
69f75b2a81
commit
0fd7f74664
|
@ -20,7 +20,7 @@ const Post = RestModel.extend({
|
||||||
@computed("url")
|
@computed("url")
|
||||||
shareUrl(url) {
|
shareUrl(url) {
|
||||||
const user = Discourse.User.current();
|
const user = Discourse.User.current();
|
||||||
const userSuffix = user ? "?u=" + user.get("username_lower") : "";
|
const userSuffix = user ? `?u=${user.username_lower}` : "";
|
||||||
|
|
||||||
if (this.firstPost) {
|
if (this.firstPost) {
|
||||||
return this.get("topic.url") + userSuffix;
|
return this.get("topic.url") + userSuffix;
|
||||||
|
@ -56,23 +56,16 @@ const Post = RestModel.extend({
|
||||||
|
|
||||||
@computed("post_number", "topic_id", "topic.slug")
|
@computed("post_number", "topic_id", "topic.slug")
|
||||||
url(postNr, topicId, slug) {
|
url(postNr, topicId, slug) {
|
||||||
return postUrl(
|
return postUrl(slug || slug, topicId || this.get("topic.id"), postNr);
|
||||||
slug || this.topic_slug,
|
|
||||||
topicId || this.get("topic.id"),
|
|
||||||
postNr
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Don't drop the /1
|
// Don't drop the /1
|
||||||
@computed("post_number", "url")
|
@computed("post_number", "url")
|
||||||
urlWithNumber(postNumber, baseUrl) {
|
urlWithNumber(postNumber, baseUrl) {
|
||||||
return postNumber === 1 ? baseUrl + "/1" : baseUrl;
|
return postNumber === 1 ? `${baseUrl}/1` : baseUrl;
|
||||||
},
|
},
|
||||||
|
|
||||||
@computed("username")
|
@computed("username") usernameUrl: userPath,
|
||||||
usernameUrl(username) {
|
|
||||||
return userPath(username);
|
|
||||||
},
|
|
||||||
|
|
||||||
topicOwner: propertyEqual("topic.details.created_by.id", "user_id"),
|
topicOwner: propertyEqual("topic.details.created_by.id", "user_id"),
|
||||||
|
|
||||||
|
@ -81,9 +74,7 @@ const Post = RestModel.extend({
|
||||||
data[field] = value;
|
data[field] = value;
|
||||||
|
|
||||||
return ajax(`/posts/${this.id}/${field}`, { type: "PUT", data })
|
return ajax(`/posts/${this.id}/${field}`, { type: "PUT", data })
|
||||||
.then(() => {
|
.then(() => this.set(field, value))
|
||||||
this.set(field, value);
|
|
||||||
})
|
|
||||||
.catch(popupAjaxError);
|
.catch(popupAjaxError);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -102,9 +93,9 @@ const Post = RestModel.extend({
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.site.get("flagTypes").filter(item => {
|
return this.site.flagTypes.filter(item =>
|
||||||
return this.get(`actionByName.${item.get("name_key")}.can_act`);
|
this.get(`actionByName.${item.name_key}.can_act`)
|
||||||
});
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
afterUpdate(res) {
|
afterUpdate(res) {
|
||||||
|
@ -131,9 +122,9 @@ const Post = RestModel.extend({
|
||||||
// Put the metaData into the request
|
// Put the metaData into the request
|
||||||
if (metaData) {
|
if (metaData) {
|
||||||
data.meta_data = {};
|
data.meta_data = {};
|
||||||
Object.keys(metaData).forEach(function(key) {
|
Object.keys(metaData).forEach(
|
||||||
data.meta_data[key] = metaData.get(key);
|
key => (data.meta_data[key] = metaData[key])
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
@ -194,7 +185,7 @@ const Post = RestModel.extend({
|
||||||
|
|
||||||
// Moderators can delete posts. Users can only trigger a deleted at message, unless delete_removed_posts_after is 0.
|
// Moderators can delete posts. Users can only trigger a deleted at message, unless delete_removed_posts_after is 0.
|
||||||
if (
|
if (
|
||||||
deletedBy.get("staff") ||
|
deletedBy.staff ||
|
||||||
Discourse.SiteSettings.delete_removed_posts_after === 0
|
Discourse.SiteSettings.delete_removed_posts_after === 0
|
||||||
) {
|
) {
|
||||||
this.setProperties({
|
this.setProperties({
|
||||||
|
@ -260,10 +251,9 @@ const Post = RestModel.extend({
|
||||||
is already found in an identity map.
|
is already found in an identity map.
|
||||||
**/
|
**/
|
||||||
updateFromPost(otherPost) {
|
updateFromPost(otherPost) {
|
||||||
const self = this;
|
Object.keys(otherPost).forEach(key => {
|
||||||
Object.keys(otherPost).forEach(function(key) {
|
|
||||||
let value = otherPost[key],
|
let value = otherPost[key],
|
||||||
oldValue = self[key];
|
oldValue = this[key];
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
value = null;
|
value = null;
|
||||||
|
@ -282,28 +272,27 @@ const Post = RestModel.extend({
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!skip) {
|
if (!skip) {
|
||||||
self.set(key, value);
|
this.set(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
expandHidden() {
|
expandHidden() {
|
||||||
return ajax("/posts/" + this.id + "/cooked.json").then(result => {
|
return ajax(`/posts/${this.id}/cooked.json`).then(result => {
|
||||||
this.setProperties({ cooked: result.cooked, cooked_hidden: false });
|
this.setProperties({ cooked: result.cooked, cooked_hidden: false });
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
rebake() {
|
rebake() {
|
||||||
return ajax("/posts/" + this.id + "/rebake", { type: "PUT" });
|
return ajax(`/posts/${this.id}/rebake`, { type: "PUT" });
|
||||||
},
|
},
|
||||||
|
|
||||||
unhide() {
|
unhide() {
|
||||||
return ajax("/posts/" + this.id + "/unhide", { type: "PUT" });
|
return ajax(`/posts/${this.id}/unhide`, { type: "PUT" });
|
||||||
},
|
},
|
||||||
|
|
||||||
toggleBookmark() {
|
toggleBookmark() {
|
||||||
const self = this;
|
|
||||||
let bookmarkedTopic;
|
let bookmarkedTopic;
|
||||||
|
|
||||||
this.toggleProperty("bookmarked");
|
this.toggleProperty("bookmarked");
|
||||||
|
@ -316,13 +305,11 @@ const Post = RestModel.extend({
|
||||||
// need to wait to hear back from server (stuff may not be loaded)
|
// need to wait to hear back from server (stuff may not be loaded)
|
||||||
|
|
||||||
return Discourse.Post.updateBookmark(this.id, this.bookmarked)
|
return Discourse.Post.updateBookmark(this.id, this.bookmarked)
|
||||||
.then(function(result) {
|
.then(result => this.set("topic.bookmarked", result.topic_bookmarked))
|
||||||
self.set("topic.bookmarked", result.topic_bookmarked);
|
.catch(error => {
|
||||||
})
|
this.toggleProperty("bookmarked");
|
||||||
.catch(function(error) {
|
|
||||||
self.toggleProperty("bookmarked");
|
|
||||||
if (bookmarkedTopic) {
|
if (bookmarkedTopic) {
|
||||||
self.set("topic.bookmarked", false);
|
this.set("topic.bookmarked", false);
|
||||||
}
|
}
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
});
|
});
|
||||||
|
@ -348,7 +335,7 @@ Post.reopenClass({
|
||||||
const lookup = Ember.Object.create();
|
const lookup = Ember.Object.create();
|
||||||
|
|
||||||
// this area should be optimized, it is creating way too many objects per post
|
// this area should be optimized, it is creating way too many objects per post
|
||||||
json.actions_summary = json.actions_summary.map(function(a) {
|
json.actions_summary = json.actions_summary.map(a => {
|
||||||
a.actionType = Discourse.Site.current().postActionTypeById(a.id);
|
a.actionType = Discourse.Site.current().postActionTypeById(a.id);
|
||||||
a.count = a.count || 0;
|
a.count = a.count || 0;
|
||||||
const actionSummary = ActionSummary.create(a);
|
const actionSummary = ActionSummary.create(a);
|
||||||
|
@ -366,13 +353,14 @@ Post.reopenClass({
|
||||||
if (json && json.reply_to_user) {
|
if (json && json.reply_to_user) {
|
||||||
json.reply_to_user = Discourse.User.create(json.reply_to_user);
|
json.reply_to_user = Discourse.User.create(json.reply_to_user);
|
||||||
}
|
}
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
},
|
},
|
||||||
|
|
||||||
updateBookmark(postId, bookmarked) {
|
updateBookmark(postId, bookmarked) {
|
||||||
return ajax("/posts/" + postId + "/bookmark", {
|
return ajax(`/posts/${postId}/bookmark`, {
|
||||||
type: "PUT",
|
type: "PUT",
|
||||||
data: { bookmarked: bookmarked }
|
data: { bookmarked }
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -391,27 +379,27 @@ Post.reopenClass({
|
||||||
},
|
},
|
||||||
|
|
||||||
loadRevision(postId, version) {
|
loadRevision(postId, version) {
|
||||||
return ajax("/posts/" + postId + "/revisions/" + version + ".json").then(
|
return ajax(`/posts/${postId}/revisions/${version}.json`).then(result =>
|
||||||
result => Ember.Object.create(result)
|
Ember.Object.create(result)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
hideRevision(postId, version) {
|
hideRevision(postId, version) {
|
||||||
return ajax("/posts/" + postId + "/revisions/" + version + "/hide", {
|
return ajax(`/posts/${postId}/revisions/${version}/hide`, {
|
||||||
type: "PUT"
|
type: "PUT"
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
showRevision(postId, version) {
|
showRevision(postId, version) {
|
||||||
return ajax("/posts/" + postId + "/revisions/" + version + "/show", {
|
return ajax(`/posts/${postId}/revisions/${version}/show`, {
|
||||||
type: "PUT"
|
type: "PUT"
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
loadQuote(postId) {
|
loadQuote(postId) {
|
||||||
return ajax("/posts/" + postId + ".json").then(result => {
|
return ajax(`/posts/${postId}.json`).then(result => {
|
||||||
const post = Discourse.Post.create(result);
|
const post = Discourse.Post.create(result);
|
||||||
return Quote.build(post, post.get("raw"), { raw: true, full: true });
|
return Quote.build(post, post.raw, { raw: true, full: true });
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue