From 9724d3bf420e7f24d70602e85ced1ab9b18c9528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 16 Aug 2013 00:24:21 +0200 Subject: [PATCH 1/2] FIX: avatars in discourse's topic oneboxes --- lib/oneboxer/discourse_local_onebox.rb | 12 ++++---- .../templates/discourse_topic_onebox.hbrs | 29 +++++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/oneboxer/discourse_local_onebox.rb b/lib/oneboxer/discourse_local_onebox.rb index 3d500acb92..8329737dc9 100644 --- a/lib/oneboxer/discourse_local_onebox.rb +++ b/lib/oneboxer/discourse_local_onebox.rb @@ -57,10 +57,12 @@ module Oneboxer post = topic.posts.first posters = topic.posters_summary.map do |p| - {username: p[:user][:username], - avatar: PrettyText.avatar_img(p[:user][:avatar_template], 'tiny'), - description: p[:description], - extras: p[:extras]} + { + username: p[:user].username, + avatar: PrettyText.avatar_img(p[:user].avatar_template, 'tiny'), + description: p[:description], + extras: p[:extras] + } end category = topic.category @@ -70,7 +72,7 @@ module Oneboxer quote = post.excerpt(SiteSetting.post_onebox_maxlength) args.merge! title: topic.title, - avatar: PrettyText.avatar_img(topic.user.username, 'tiny'), + avatar: PrettyText.avatar_img(topic.user.avatar_template, 'tiny'), posts_count: topic.posts_count, last_post: FreedomPatches::Rails4.time_ago_in_words(topic.last_posted_at, false, scope: :'datetime.distance_in_words_verbose'), age: FreedomPatches::Rails4.time_ago_in_words(topic.created_at, false, scope: :'datetime.distance_in_words_verbose'), diff --git a/lib/oneboxer/templates/discourse_topic_onebox.hbrs b/lib/oneboxer/templates/discourse_topic_onebox.hbrs index bcced04943..cce69d1bae 100644 --- a/lib/oneboxer/templates/discourse_topic_onebox.hbrs +++ b/lib/oneboxer/templates/discourse_topic_onebox.hbrs @@ -1,23 +1,20 @@ - - - From c9969738bfb4501f40cee5ebcee01703deb93396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 16 Aug 2013 00:26:22 +0200 Subject: [PATCH 2/2] FIX: N+1 query for avatars when searching for users --- app/models/user.rb | 9 +-------- lib/search/search_result.rb | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 56557b0e00..6fdae105e5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -297,13 +297,6 @@ class User < ActiveRecord::Base end end - def self.avatar_template(email) - user = User.select([:email, :use_uploaded_avatar, :uploaded_avatar_template, :uploaded_avatar_id]) - .where(email: Email.downcase(email)) - .first - user.avatar_template if user.present? - end - def self.gravatar_template(email) email_hash = self.email_hash(email) "//www.gravatar.com/avatar/#{email_hash}.png?s={size}&r=pg&d=identicon" @@ -314,7 +307,7 @@ class User < ActiveRecord::Base # - self oneboxes in open graph data # - emails def small_avatar_url - template = User.avatar_template(email) + template = avatar_template template.gsub("{size}", "60") end diff --git a/lib/search/search_result.rb b/lib/search/search_result.rb index 6e45bdb532..17ef418da0 100644 --- a/lib/search/search_result.rb +++ b/lib/search/search_result.rb @@ -32,7 +32,7 @@ class Search def self.from_user(u) SearchResult.new(type: :user, id: u.username_lower, title: u.username, url: "/users/#{u.username_lower}").tap do |r| - r.avatar_template = User.avatar_template(u.email) + r.avatar_template = u.avatar_template end end @@ -43,7 +43,7 @@ class Search def self.from_post(p) if p.post_number == 1 # we want the topic link when it's the OP - SearchResult.from_topic(p.topic) + SearchResult.from_topic(p.topic) else SearchResult.new(type: :topic, id: p.topic.id, title: p.topic.title, url: p.url) end