diff --git a/app/assets/javascripts/discourse/lib/text.js.es6 b/app/assets/javascripts/discourse/lib/text.js.es6 index fba637345b8..01c15add3c0 100644 --- a/app/assets/javascripts/discourse/lib/text.js.es6 +++ b/app/assets/javascripts/discourse/lib/text.js.es6 @@ -7,6 +7,7 @@ export function cook(text) { const opts = { getURL: Discourse.getURLWithCDN, + currentUser: Discourse.__container__.lookup('current-user:main'), siteSettings }; diff --git a/app/assets/javascripts/pretty-text/pretty-text.js.es6 b/app/assets/javascripts/pretty-text/pretty-text.js.es6 index e254d7dbc42..a70da34ccf5 100644 --- a/app/assets/javascripts/pretty-text/pretty-text.js.es6 +++ b/app/assets/javascripts/pretty-text/pretty-text.js.es6 @@ -12,7 +12,17 @@ export function registerOption(fn) { export function buildOptions(state) { setup(); - const { siteSettings, getURL, lookupAvatar, getTopicInfo, topicId, categoryHashtagLookup } = state; + const { + siteSettings, + getURL, + lookupAvatar, + getTopicInfo, + topicId, + categoryHashtagLookup, + userId, + getCurrentUser, + currentUser + } = state; const features = { 'bold-italics': true, @@ -34,6 +44,9 @@ export function buildOptions(state) { getTopicInfo, topicId, categoryHashtagLookup, + userId, + getCurrentUser, + currentUser, mentionLookup: state.mentionLookup, }; diff --git a/app/models/post.rb b/app/models/post.rb index 1946d4fa71f..8bb5ef151ed 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -197,12 +197,16 @@ class Post < ActiveRecord::Base if cook_method == Post.cook_methods[:email] cooked = EmailCook.new(raw).cook else - cooked = if !self.user || SiteSetting.tl3_links_no_follow || !self.user.has_trust_level?(TrustLevel[3]) + cloned = args.dup + cloned[1] ||= {} + + post_user = self.user + cloned[1][:user_id] = post_user.id if post_user + + cooked = if !post_user || SiteSetting.tl3_links_no_follow || !post_user.has_trust_level?(TrustLevel[3]) post_analyzer.cook(*args) else # At trust level 3, we don't apply nofollow to links - cloned = args.dup - cloned[1] ||= {} cloned[1][:omit_nofollow] = true post_analyzer.cook(*cloned) end diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 6259f4107aa..85ba6df90b5 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -114,7 +114,7 @@ module PrettyText end end - def self.markdown(text, opts=nil) + def self.markdown(text, opts={}) # we use the exact same markdown converter as the client # TODO: use the same extensions on both client and server (in particular the template for mentions) baked = nil @@ -143,7 +143,10 @@ module PrettyText context.eval("__optInput.topicId = #{opts[:topicId].to_i};") end + context.eval("__optInput.userId = #{opts[:user_id].to_i};") if opts[:user_id] + context.eval("__optInput.getURL = __getURL;") + context.eval("__optInput.getCurrentUser = __getCurrentUser;") context.eval("__optInput.lookupAvatar = __lookupAvatar;") context.eval("__optInput.getTopicInfo = __getTopicInfo;") context.eval("__optInput.categoryHashtagLookup = __categoryLookup;") diff --git a/lib/pretty_text/helpers.rb b/lib/pretty_text/helpers.rb index 81f419977db..fa61e9efc2f 100644 --- a/lib/pretty_text/helpers.rb +++ b/lib/pretty_text/helpers.rb @@ -68,6 +68,12 @@ module PrettyText nil end end + + def get_current_user(user_id) + user = User.find_by(id: user_id) + staff = user ? user.staff? : false + { staff: staff } + end end end diff --git a/lib/pretty_text/shims.js b/lib/pretty_text/shims.js index 040d56b0ac3..c156fdb0cd7 100644 --- a/lib/pretty_text/shims.js +++ b/lib/pretty_text/shims.js @@ -46,6 +46,10 @@ function __lookupAvatar(p) { return __utils.avatarImg({size: "tiny", avatarTemplate: __helpers.avatar_template(p) }, __getURL); } +function __getCurrentUser(userId) { + return __helpers.get_current_user(userId); +} + I18n = { t: function(a,b) { return __helpers.t(a,b); } }; diff --git a/plugins/poll/assets/javascripts/initializers/add-poll-ui-builder.js.es6 b/plugins/poll/assets/javascripts/initializers/add-poll-ui-builder.js.es6 index 82b830c3e16..66fbb764047 100644 --- a/plugins/poll/assets/javascripts/initializers/add-poll-ui-builder.js.es6 +++ b/plugins/poll/assets/javascripts/initializers/add-poll-ui-builder.js.es6 @@ -2,6 +2,10 @@ import { withPluginApi } from 'discourse/lib/plugin-api'; import showModal from 'discourse/lib/show-modal'; function initializePollUIBuilder(api) { + const siteSettings = api.container.lookup('site-settings:main'); + + if (!siteSettings.poll_enabled && (api.getCurrentUser() && !api.getCurrentUser().staff)) return; + const ComposerController = api.container.lookupFactory("controller:composer"); ComposerController.reopen({ actions: { diff --git a/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 b/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 index c182c2439ba..927aa5cc522 100644 --- a/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 +++ b/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 @@ -7,7 +7,9 @@ const WHITELISTED_ATTRIBUTES = ["type", "name", "min", "max", "step", "order", " const ATTRIBUTES_REGEX = new RegExp("(" + WHITELISTED_ATTRIBUTES.join("|") + ")=['\"]?[^\\s\\]]+['\"]?", "g"); registerOption((siteSettings, opts) => { - opts.features.poll = !!siteSettings.poll_enabled; + const currentUser = (opts.getCurrentUser && opts.getCurrentUser(opts.userId)) || opts.currentUser; + + opts.features.poll = !!siteSettings.poll_enabled || currentUser.staff; opts.pollMaximumOptions = siteSettings.poll_maximum_options; }); @@ -179,11 +181,11 @@ export function setup(helper) { /*! * Joseph Myer's md5() algorithm wrapped in a self-invoked function to prevent * global namespace polution, modified to hash unicode characters as UTF-8. - * + * * Copyright 1999-2010, Joseph Myers, Paul Johnston, Greg Holt, Will Bond * http://www.myersdaily.org/joseph/javascript/md5-text.html * http://pajhome.org.uk/crypt/md5 - * + * * Released under the BSD license * http://www.opensource.org/licenses/bsd-license */ diff --git a/plugins/poll/lib/polls_validator.rb b/plugins/poll/lib/polls_validator.rb index c02920bf383..2321f7f0af4 100644 --- a/plugins/poll/lib/polls_validator.rb +++ b/plugins/poll/lib/polls_validator.rb @@ -7,7 +7,7 @@ module DiscoursePoll def validate_polls polls = {} - extracted_polls = DiscoursePoll::Poll::extract(@post.raw, @post.topic_id) + extracted_polls = DiscoursePoll::Poll::extract(@post.raw, @post.topic_id, @post.user_id) extracted_polls.each do |poll| # polls should have a unique name diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index c752e0dfcdd..91f070a1b25 100644 --- a/plugins/poll/plugin.rb +++ b/plugins/poll/plugin.rb @@ -4,8 +4,6 @@ # authors: Vikhyat Korrapati (vikhyat), RĂ©gis Hanol (zogstrip) # url: https://github.com/discourse/discourse/tree/master/plugins/poll -enabled_site_setting :poll_enabled - register_asset "stylesheets/common/poll.scss" register_asset "stylesheets/common/poll-ui-builder.scss" register_asset "stylesheets/desktop/poll.scss", :desktop @@ -145,10 +143,10 @@ after_initialize do end end - def extract(raw, topic_id) + def extract(raw, topic_id, user_id = nil) # TODO: we should fix the callback mess so that the cooked version is available # in the validators instead of cooking twice - cooked = PrettyText.cook(raw, topic_id: topic_id) + cooked = PrettyText.cook(raw, topic_id: topic_id, user_id: user_id) parsed = Nokogiri::HTML(cooked) extracted_polls = [] @@ -252,6 +250,8 @@ after_initialize do end validate(:post, :validate_polls) do + return if !SiteSetting.poll_enabled? && (self.user && !self.user.staff?) + # only care when raw has changed! return unless self.raw_changed?