mirror of
https://github.com/discourse/discourse-solved.git
synced 2025-07-04 21:02:20 +00:00
FIX: Apply username formatter to solved post
This commit is contained in:
parent
5b81cc9293
commit
3a5604cb46
@ -1,24 +1,27 @@
|
|||||||
import Topic from 'discourse/models/topic';
|
import Topic from "discourse/models/topic";
|
||||||
import User from 'discourse/models/user';
|
import User from "discourse/models/user";
|
||||||
import TopicStatus from 'discourse/raw-views/topic-status';
|
import TopicStatus from "discourse/raw-views/topic-status";
|
||||||
import { popupAjaxError } from 'discourse/lib/ajax-error';
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
||||||
import { withPluginApi } from 'discourse/lib/plugin-api';
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
||||||
import { ajax } from 'discourse/lib/ajax';
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import PostCooked from 'discourse/widgets/post-cooked';
|
import PostCooked from "discourse/widgets/post-cooked";
|
||||||
|
import { formatUsername } from "discourse/lib/utilities";
|
||||||
|
|
||||||
function clearAccepted(topic) {
|
function clearAccepted(topic) {
|
||||||
const posts = topic.get('postStream.posts');
|
const posts = topic.get("postStream.posts");
|
||||||
posts.forEach(post => {
|
posts.forEach(post => {
|
||||||
if (post.get('post_number') > 1 ) {
|
if (post.get("post_number") > 1) {
|
||||||
post.set('accepted_answer',false);
|
post.set("accepted_answer", false);
|
||||||
post.set('can_accept_answer',true);
|
post.set("can_accept_answer", true);
|
||||||
post.set('can_unaccept_answer',false);
|
post.set("can_unaccept_answer", false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function unacceptPost(post) {
|
function unacceptPost(post) {
|
||||||
if (!post.get('can_unaccept_answer')) { return; }
|
if (!post.get("can_unaccept_answer")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const topic = post.topic;
|
const topic = post.topic;
|
||||||
|
|
||||||
post.setProperties({
|
post.setProperties({
|
||||||
@ -26,11 +29,11 @@ function unacceptPost(post) {
|
|||||||
can_unaccept_answer: false,
|
can_unaccept_answer: false,
|
||||||
accepted_answer: false
|
accepted_answer: false
|
||||||
});
|
});
|
||||||
topic.set('accepted_answer', undefined);
|
topic.set("accepted_answer", undefined);
|
||||||
|
|
||||||
ajax("/solution/unaccept", {
|
ajax("/solution/unaccept", {
|
||||||
type: 'POST',
|
type: "POST",
|
||||||
data: { id: post.get('id') }
|
data: { id: post.get("id") }
|
||||||
}).catch(popupAjaxError);
|
}).catch(popupAjaxError);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,83 +48,96 @@ function acceptPost(post) {
|
|||||||
accepted_answer: true
|
accepted_answer: true
|
||||||
});
|
});
|
||||||
|
|
||||||
topic.set('accepted_answer', {
|
topic.set("accepted_answer", {
|
||||||
username: post.get('username'),
|
username: post.get("username"),
|
||||||
post_number: post.get('post_number'),
|
post_number: post.get("post_number"),
|
||||||
excerpt: post.get('cooked'),
|
excerpt: post.get("cooked")
|
||||||
});
|
});
|
||||||
|
|
||||||
ajax("/solution/accept", {
|
ajax("/solution/accept", {
|
||||||
type: 'POST',
|
type: "POST",
|
||||||
data: { id: post.get('id') }
|
data: { id: post.get("id") }
|
||||||
}).catch(popupAjaxError);
|
}).catch(popupAjaxError);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeWithApi(api) {
|
function initializeWithApi(api) {
|
||||||
const currentUser = api.getCurrentUser();
|
const currentUser = api.getCurrentUser();
|
||||||
|
|
||||||
api.includePostAttributes('can_accept_answer', 'can_unaccept_answer', 'accepted_answer');
|
api.includePostAttributes(
|
||||||
|
"can_accept_answer",
|
||||||
|
"can_unaccept_answer",
|
||||||
|
"accepted_answer"
|
||||||
|
);
|
||||||
|
|
||||||
if (api.addDiscoveryQueryParam) {
|
if (api.addDiscoveryQueryParam) {
|
||||||
api.addDiscoveryQueryParam('solved', {replace: true, refreshModel: true});
|
api.addDiscoveryQueryParam("solved", { replace: true, refreshModel: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
api.addPostMenuButton('solved', attrs => {
|
api.addPostMenuButton("solved", attrs => {
|
||||||
const canAccept = attrs.can_accept_answer;
|
const canAccept = attrs.can_accept_answer;
|
||||||
const canUnaccept = attrs.can_unaccept_answer;
|
const canUnaccept = attrs.can_unaccept_answer;
|
||||||
const accepted = attrs.accepted_answer;
|
const accepted = attrs.accepted_answer;
|
||||||
const isOp = currentUser && currentUser.id === attrs.topicCreatedById;
|
const isOp = currentUser && currentUser.id === attrs.topicCreatedById;
|
||||||
const position = (!accepted && canAccept && !isOp) ? 'second-last-hidden' : 'first';
|
const position =
|
||||||
|
!accepted && canAccept && !isOp ? "second-last-hidden" : "first";
|
||||||
|
|
||||||
if (canAccept) {
|
if (canAccept) {
|
||||||
return {
|
return {
|
||||||
action: 'acceptAnswer',
|
action: "acceptAnswer",
|
||||||
icon: 'check-square-o',
|
icon: "check-square-o",
|
||||||
className: 'unaccepted',
|
className: "unaccepted",
|
||||||
title: 'solved.accept_answer',
|
title: "solved.accept_answer",
|
||||||
position
|
position
|
||||||
};
|
};
|
||||||
} else if (canUnaccept || accepted) {
|
} else if (canUnaccept || accepted) {
|
||||||
const title = canUnaccept ? 'solved.unaccept_answer' : 'solved.accepted_answer';
|
const title = canUnaccept
|
||||||
|
? "solved.unaccept_answer"
|
||||||
|
: "solved.accepted_answer";
|
||||||
return {
|
return {
|
||||||
action: 'unacceptAnswer',
|
action: "unacceptAnswer",
|
||||||
icon: 'check-square',
|
icon: "check-square",
|
||||||
title,
|
title,
|
||||||
className: 'accepted fade-out',
|
className: "accepted fade-out",
|
||||||
position,
|
position,
|
||||||
beforeButton(h) {
|
beforeButton(h) {
|
||||||
return h('span.accepted-text', I18n.t('solved.solution'));
|
return h("span.accepted-text", I18n.t("solved.solution"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
api.decorateWidget('post-contents:after-cooked', dec => {
|
api.decorateWidget("post-contents:after-cooked", dec => {
|
||||||
if (dec.attrs.post_number === 1) {
|
if (dec.attrs.post_number === 1) {
|
||||||
const postModel = dec.getModel();
|
const postModel = dec.getModel();
|
||||||
if (postModel) {
|
if (postModel) {
|
||||||
const topic = postModel.get('topic');
|
const topic = postModel.get("topic");
|
||||||
if (topic.get('accepted_answer')) {
|
if (topic.get("accepted_answer")) {
|
||||||
const hasExcerpt = !!topic.get('accepted_answer').excerpt;
|
const hasExcerpt = !!topic.get("accepted_answer").excerpt;
|
||||||
|
|
||||||
const withExcerpt = `
|
const withExcerpt = `
|
||||||
<aside class='quote' data-post="${topic.get('accepted_answer').post_number}" data-topic="${topic.get('id')}">
|
<aside class='quote' data-post="${
|
||||||
|
topic.get("accepted_answer").post_number
|
||||||
|
}" data-topic="${topic.get("id")}">
|
||||||
<div class='title'>
|
<div class='title'>
|
||||||
${topic.get('acceptedAnswerHtml')} <div class="quote-controls"><\/div>
|
${topic.get(
|
||||||
|
"acceptedAnswerHtml"
|
||||||
|
)} <div class="quote-controls"><\/div>
|
||||||
</div>
|
</div>
|
||||||
<blockquote>
|
<blockquote>
|
||||||
${topic.get('accepted_answer').excerpt}
|
${topic.get("accepted_answer").excerpt}
|
||||||
</blockquote>
|
</blockquote>
|
||||||
</aside>`;
|
</aside>`;
|
||||||
|
|
||||||
const withoutExcerpt = `
|
const withoutExcerpt = `
|
||||||
<aside class='quote'>
|
<aside class='quote'>
|
||||||
<div class='title title-only'>
|
<div class='title title-only'>
|
||||||
${topic.get('acceptedAnswerHtml')}
|
${topic.get("acceptedAnswerHtml")}
|
||||||
</div>
|
</div>
|
||||||
</aside>`;
|
</aside>`;
|
||||||
|
|
||||||
var cooked = new PostCooked({ cooked: hasExcerpt ? withExcerpt : withoutExcerpt });
|
var cooked = new PostCooked({
|
||||||
|
cooked: hasExcerpt ? withExcerpt : withoutExcerpt
|
||||||
|
});
|
||||||
|
|
||||||
var html = cooked.init();
|
var html = cooked.init();
|
||||||
|
|
||||||
@ -131,50 +147,54 @@ function initializeWithApi(api) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
api.attachWidgetAction('post', 'acceptAnswer', function() {
|
api.attachWidgetAction("post", "acceptAnswer", function() {
|
||||||
const post = this.model;
|
const post = this.model;
|
||||||
const current = post.get('topic.postStream.posts').filter(p => {
|
const current = post.get("topic.postStream.posts").filter(p => {
|
||||||
return p.get('post_number') === 1 || p.get('accepted_answer');
|
return p.get("post_number") === 1 || p.get("accepted_answer");
|
||||||
});
|
});
|
||||||
acceptPost(post);
|
acceptPost(post);
|
||||||
|
|
||||||
current.forEach(p => this.appEvents.trigger('post-stream:refresh', { id: p.id }));
|
current.forEach(p =>
|
||||||
|
this.appEvents.trigger("post-stream:refresh", { id: p.id })
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
api.attachWidgetAction('post', 'unacceptAnswer', function() {
|
api.attachWidgetAction("post", "unacceptAnswer", function() {
|
||||||
const post = this.model;
|
const post = this.model;
|
||||||
const op = post.get('topic.postStream.posts').find(p => p.get('post_number') === 1);
|
const op = post
|
||||||
|
.get("topic.postStream.posts")
|
||||||
|
.find(p => p.get("post_number") === 1);
|
||||||
unacceptPost(post);
|
unacceptPost(post);
|
||||||
this.appEvents.trigger('post-stream:refresh', { id: op.get('id') });
|
this.appEvents.trigger("post-stream:refresh", { id: op.get("id") });
|
||||||
});
|
});
|
||||||
|
|
||||||
if (api.registerConnectorClass) {
|
if (api.registerConnectorClass) {
|
||||||
api.registerConnectorClass('user-activity-bottom', 'solved-list', {
|
api.registerConnectorClass("user-activity-bottom", "solved-list", {
|
||||||
shouldRender(args, component) {
|
shouldRender(args, component) {
|
||||||
return component.siteSettings.solved_enabled;
|
return component.siteSettings.solved_enabled;
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
api.registerConnectorClass('user-summary-stat', 'solved-count', {
|
api.registerConnectorClass("user-summary-stat", "solved-count", {
|
||||||
shouldRender(args, component) {
|
shouldRender(args, component) {
|
||||||
return component.siteSettings.solved_enabled && args.model.solved_count > 0;
|
return (
|
||||||
|
component.siteSettings.solved_enabled && args.model.solved_count > 0
|
||||||
|
);
|
||||||
},
|
},
|
||||||
setupComponent() {
|
setupComponent() {
|
||||||
this.set('classNames', ['linked-stat']);
|
this.set("classNames", ["linked-stat"]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'extend-for-solved-button',
|
name: "extend-for-solved-button",
|
||||||
initialize() {
|
initialize() {
|
||||||
|
|
||||||
Topic.reopen({
|
Topic.reopen({
|
||||||
// keeping this here cause there is complex localization
|
// keeping this here cause there is complex localization
|
||||||
acceptedAnswerHtml: function() {
|
acceptedAnswerHtml: function() {
|
||||||
const username = this.get('accepted_answer.username');
|
const username = this.get("accepted_answer.username");
|
||||||
const postNumber = this.get('accepted_answer.post_number');
|
const postNumber = this.get("accepted_answer.post_number");
|
||||||
|
|
||||||
if (!username || !postNumber) {
|
if (!username || !postNumber) {
|
||||||
return "";
|
return "";
|
||||||
@ -182,40 +202,47 @@ export default {
|
|||||||
|
|
||||||
return I18n.t("solved.accepted_html", {
|
return I18n.t("solved.accepted_html", {
|
||||||
username_lower: username.toLowerCase(),
|
username_lower: username.toLowerCase(),
|
||||||
username,
|
username: formatUsername(username),
|
||||||
post_path: this.get('url') + "/" + postNumber,
|
post_path: this.get("url") + "/" + postNumber,
|
||||||
post_number: postNumber,
|
post_number: postNumber,
|
||||||
user_path: User.create({username: username}).get('path')
|
user_path: User.create({ username }).get("path")
|
||||||
});
|
});
|
||||||
}.property('accepted_answer', 'id')
|
}.property("accepted_answer", "id")
|
||||||
});
|
});
|
||||||
|
|
||||||
TopicStatus.reopen({
|
TopicStatus.reopen({
|
||||||
statuses: function(){
|
statuses: function() {
|
||||||
const results = this._super();
|
const results = this._super();
|
||||||
if (this.topic.has_accepted_answer) {
|
if (this.topic.has_accepted_answer) {
|
||||||
results.push({
|
results.push({
|
||||||
openTag: 'span',
|
openTag: "span",
|
||||||
closeTag: 'span',
|
closeTag: "span",
|
||||||
title: I18n.t('solved.has_accepted_answer'),
|
title: I18n.t("solved.has_accepted_answer"),
|
||||||
icon: 'check-square-o'
|
icon: "check-square-o"
|
||||||
});
|
});
|
||||||
}else if(this.topic.can_have_answer && this.siteSettings.solved_enabled && this.siteSettings.empty_box_on_unsolved){
|
} else if (
|
||||||
|
this.topic.can_have_answer &&
|
||||||
|
this.siteSettings.solved_enabled &&
|
||||||
|
this.siteSettings.empty_box_on_unsolved
|
||||||
|
) {
|
||||||
results.push({
|
results.push({
|
||||||
openTag: 'span',
|
openTag: "span",
|
||||||
closeTag: 'span',
|
closeTag: "span",
|
||||||
title: I18n.t('solved.has_no_accepted_answer'),
|
title: I18n.t("solved.has_no_accepted_answer"),
|
||||||
icon: 'square-o'
|
icon: "square-o"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}.property()
|
}.property()
|
||||||
});
|
});
|
||||||
|
|
||||||
withPluginApi('0.1', initializeWithApi);
|
withPluginApi("0.1", initializeWithApi);
|
||||||
|
|
||||||
withPluginApi('0.8.10', api => {
|
withPluginApi("0.8.10", api => {
|
||||||
api.replaceIcon('notification.solved.accepted_notification', 'check-square');
|
api.replaceIcon(
|
||||||
|
"notification.solved.accepted_notification",
|
||||||
|
"check-square"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user