From 6a68e8c27284b542e344ea3b1a8d81311b62e39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 29 Jan 2015 22:53:48 +0100 Subject: [PATCH] FIX: use CDN for user card/profile background and user avatars (for real this time) --- app/assets/javascripts/discourse.js | 6 ++++++ .../discourse/dialects/quote_dialect.js | 1 + app/assets/javascripts/discourse/models/user.js | 14 +++++--------- .../javascripts/discourse/views/user-card.js.es6 | 3 ++- lib/pretty_text.rb | 12 ++++++++---- spec/components/pretty_text_spec.rb | 8 ++++---- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index 27b12c53abb..feb375d90c0 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -24,6 +24,12 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { return u + url; }, + getURLWithCDN: function(url) { + url = this.getURL(url); + if (Discourse.CDN) { url = Discourse.CDN + url; } + return url; + }, + Resolver: DiscourseResolver, _titleChanged: function() { diff --git a/app/assets/javascripts/discourse/dialects/quote_dialect.js b/app/assets/javascripts/discourse/dialects/quote_dialect.js index b0b8f5bbb85..3c50598cfcb 100644 --- a/app/assets/javascripts/discourse/dialects/quote_dialect.js +++ b/app/assets/javascripts/discourse/dialects/quote_dialect.js @@ -1,4 +1,5 @@ var esc = Handlebars.Utils.escapeExpression; + Discourse.BBCode.register('quote', {noWrap: true, singlePara: true}, function(contents, bbParams, options) { var params = {'class': 'quote'}, username = null; diff --git a/app/assets/javascripts/discourse/models/user.js b/app/assets/javascripts/discourse/models/user.js index 7a5bd5c37c5..fde76922559 100644 --- a/app/assets/javascripts/discourse/models/user.js +++ b/app/assets/javascripts/discourse/models/user.js @@ -72,10 +72,9 @@ Discourse.User = Discourse.Model.extend({ @type {String} **/ profileBackground: function() { - var background = this.get('profile_background'); - if(Em.isEmpty(background) || !Discourse.SiteSettings.allow_profile_backgrounds) { return; } - - return 'background-image: url(' + background + ')'; + var url = this.get('profile_background'); + if (Em.isEmpty(url) || !Discourse.SiteSettings.allow_profile_backgrounds) { return; } + return 'background-image: url(' + Discourse.getURLWithCDN(url) + ')'; }.property('profile_background'), /** @@ -442,6 +441,7 @@ Discourse.User.reopenClass(Discourse.Singleton, { avatarTemplate: function(username, uploadedAvatarId) { var url; + if (uploadedAvatarId) { url = "/user_avatar/" + Discourse.BaseUrl + @@ -456,11 +456,7 @@ Discourse.User.reopenClass(Discourse.Singleton, { Discourse.LetterAvatarVersion + ".png"; } - url = Discourse.getURL(url); - if (Discourse.CDN) { - url = Discourse.CDN + url; - } - return url; + return Discourse.getURLWithCDN(url); }, /** diff --git a/app/assets/javascripts/discourse/views/user-card.js.es6 b/app/assets/javascripts/discourse/views/user-card.js.es6 index 12394202978..66e62b88d58 100644 --- a/app/assets/javascripts/discourse/views/user-card.js.es6 +++ b/app/assets/javascripts/discourse/views/user-card.js.es6 @@ -11,6 +11,7 @@ export default Discourse.View.extend(CleansUp, { addBackground: function() { var url = this.get('controller.user.card_background'); + if (!this.get('allowBackgrounds')) { return; } var $this = this.$(); @@ -19,7 +20,7 @@ export default Discourse.View.extend(CleansUp, { if (Ember.isEmpty(url)) { $this.css('background-image', '').addClass('no-bg'); } else { - $this.css('background-image', "url(" + url + ")").removeClass('no-bg'); + $this.css('background-image', "url(" + Discourse.getURLWithCDN(url) + ")").removeClass('no-bg'); } }.observes('controller.user.card_background'), diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index ee5aa578331..d095bd0c6a3 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -1,11 +1,13 @@ require 'v8' require 'nokogiri' +require_dependency 'url_helper' require_dependency 'excerpt_parser' require_dependency 'post' module PrettyText class Helpers + include UrlHelper def t(key, opts) key = "js." + key @@ -21,15 +23,15 @@ module PrettyText # function here are available to v8 def avatar_template(username) return "" unless username - user = User.find_by(username_lower: username.downcase) - user.avatar_template if user.present? + return "" unless user.present? + schemaless absolute user.avatar_template end def is_username_valid(username) return false unless username username = username.downcase - return User.exec_sql('SELECT 1 FROM users WHERE username_lower = ?', username).values.length == 1 + User.exec_sql('SELECT 1 FROM users WHERE username_lower = ?', username).values.length == 1 end end @@ -128,7 +130,9 @@ module PrettyText context.eval("Discourse.SiteSettings = #{SiteSetting.client_settings_json};") context.eval("Discourse.CDN = '#{Rails.configuration.action_controller.asset_host}';") context.eval("Discourse.BaseUrl = 'http://#{RailsMultisite::ConnectionManagement.current_hostname}';") - context.eval("Discourse.getURL = function(url) {return '#{Discourse::base_uri}' + url};") + + context.eval("Discourse.getURL = function(url) { return '#{Discourse::base_uri}' + url };") + context.eval("Discourse.getURLWithCDN = function(url) { url = Discourse.getURL(url); if (Discourse.CDN) { url = Discourse.CDN + url; } return url; };") end def self.markdown(text, opts=nil) diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 7dda67ae999..00bb220717e 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -12,20 +12,20 @@ describe PrettyText do before(:each) do eviltrout = User.new - eviltrout.stubs(:avatar_template).returns("http://test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/{size}.png") + eviltrout.stubs(:avatar_template).returns("//test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/{size}.png") User.expects(:find_by).with(username_lower: "eviltrout").returns(eviltrout) end it "produces a quote even with new lines in it" do - expect(PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]ddd\n[/quote]")).to match_html "" + expect(PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]ddd\n[/quote]")).to match_html "" end it "should produce a quote" do - expect(PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]ddd[/quote]")).to match_html "" + expect(PrettyText.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]ddd[/quote]")).to match_html "" end it "trims spaces on quote params" do - expect(PrettyText.cook("[quote=\"EvilTrout, post:555, topic: 666\"]ddd[/quote]")).to match_html "" + expect(PrettyText.cook("[quote=\"EvilTrout, post:555, topic: 666\"]ddd[/quote]")).to match_html "" end end