Skip to content

Instantly share code, notes, and snippets.

@DeanMarkTaylor
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeanMarkTaylor/e127929df45022289378 to your computer and use it in GitHub Desktop.
Save DeanMarkTaylor/e127929df45022289378 to your computer and use it in GitHub Desktop.
Discourse - Add "Private Message" Post Button
// Show Private Message button (for specific categories)
// NOTE: In order for the button to display you need to 'message' to the post_menu site setting.
(function (require, Discourse, I18n, Em) {
var Button = require('discourse/views/post-menu').Button;
Discourse.PostMenuView.reopen({
buttonForMessage: function (post) {
// NOTE: just set whiteListedCategories to null / empty array to make buttons visible everywhere.
var whiteListedCategories = [37, 71, 72, 73, 74, 75, 76, 77, 78],
currentUser = Discourse.User.current();
// Only create the button if the post author is not the current user.
if (currentUser && (currentUser.id === post.user_id)) {
return null;
}
// Don't display for private messages.
if (post.topic && post.topic.archetype === 'private_message') {
return null;
}
// Only display button for specific categories
if (whiteListedCategories && whiteListedCategories.length > 0) {
if (post.topic &&
post.topic.category_id &&
whiteListedCategories.indexOf(post.topic.category_id) === -1) {
return null;
}
}
return new Button('message', 'topic.private_message', 'envelope', {
className: 'message-button',
disabled: false
});
},
clickMessage: function (post) {
this.get('controller').send('replyAsNewPrivateMessage', post);
}
});
Discourse.TopicController.reopen({
actions: {
replyAsNewPrivateMessage: function (post) {
var composerController = this.get('controllers.composer'),
quoteController = this.get('controllers.quote-button'),
quotedText = Discourse.Quote.build(quoteController.get('post'), quoteController.get('buffer')),
topic = post ? post.get('topic') : this.get('model'),
self = this;
quoteController.deselectText();
composerController.open({
action: Discourse.Composer.PRIVATE_MESSAGE,
usernames: post.get('username'),
archetypeId: 'private_message',
draftKey: 'new_private_message'
}).then(function () {
return Em.isEmpty(quotedText) ? Discourse.Post.loadQuote(post.get('id')) : quotedText;
}).then(function (q) {
var postUrl = '' + location.protocol + '//' + location.host + (post.get('url')),
postLink = '[' + self.get('title') + '](' + postUrl + ')';
composerController.appendText(I18n.t('post.continue_discussion', {postLink: postLink}) + '\n\n' + q);
});
return false;
}
}
});
})(window.require, window.Discourse, window.I18n, window.Em);
@DeanMarkTaylor
Copy link
Author

Updated to prevent display of private message button on private message posts / topics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment