Skip to content

A platform for community discussion. Free, open, simple.

add a custom user onebox (#5542)

Maja Komel018cb7f36b3f
diff --git a/app/assets/stylesheets/common/base/onebox.scss b/app/assets/stylesheets/common/base/onebox.scss
index c7b630b943d..61d92687fe2 100644
--- a/app/assets/stylesheets/common/base/onebox.scss
+++ b/app/assets/stylesheets/common/base/onebox.scss
@@ -212,6 +212,16 @@ aside.onebox {
}
}
+ &.user-onebox {
+ .fa {
+ margin-right: 5px;
+ }
+ .full-name,
+ .location {
+ margin-right: 10px;
+ }
+ }
+
}
}
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 694378b9181..d4c9a8a0276 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3684,3 +3684,5 @@ en:
search_logs:
graph_title: "Search Count"
+
+ joined: "Joined"
diff --git a/lib/onebox/engine/discourse_local_onebox.rb b/lib/onebox/engine/discourse_local_onebox.rb
index a1ce9ebe822..b4add001ac0 100644
--- a/lib/onebox/engine/discourse_local_onebox.rb
+++ b/lib/onebox/engine/discourse_local_onebox.rb
@@ -14,7 +14,7 @@ module Onebox
route = Discourse.route_for(url)
- !!(route[:controller] =~ /topics|uploads/)
+ !!(route[:controller] =~ /topics|uploads|users/)
rescue ActionController::RoutingError
false
end
@@ -27,6 +27,7 @@ module Onebox
case route[:controller]
when "uploads" then upload_html(path)
when "topics" then topic_html(route)
+ when "users" then user_html(route)
end
end
@@ -82,6 +83,33 @@ module Onebox
end
end
+ def user_html(route)
+ link = "<a href='#{@url}'>#{@url}</a>"
+ username = route[:username] || ''
+ user = User.find_by(username_lower: username.downcase)
+
+ if user
+ args = {
+ user_id: user.id,
+ username: user.username,
+ avatar: PrettyText.avatar_img(user.avatar_template, "extra_large"),
+ name: user.name,
+ bio: user.user_profile.bio_excerpt(230),
+ location: user.user_profile.location,
+ joined: I18n.t('joined'),
+ created_at: user.created_at.strftime(I18n.t('datetime_formats.formats.date_only')),
+ website: user.user_profile.website,
+ website_name: UserSerializer.new(user).website_name,
+ original_url: @url
+ }
+
+ template = File.read("#{Rails.root}/lib/onebox/templates/discourse_user_onebox.hbs")
+ Mustache.render(template, args)
+ else
+ return link
+ end
+ end
+
def can_see_post?(post, source_topic)
return false if post.nil? || post.hidden || post.trashed? || post.topic.nil?
Guardian.new.can_see_post?(post) || same_category?(post.topic.category, source_topic)
diff --git a/lib/onebox/templates/discourse_user_onebox.hbs b/lib/onebox/templates/discourse_user_onebox.hbs
new file mode 100644
index 00000000000..079e3268ec2
--- /dev/null
+++ b/lib/onebox/templates/discourse_user_onebox.hbs
@@ -0,0 +1,24 @@
+<aside class="onebox">
+ <article class="onebox-body user-onebox">
+ {{{avatar}}}
+ <h3><a href="{{original_url}}">{{{username}}}</a></h3>
+ <div>
+ {{#name}}
+ <span class="full-name">{{name}}</span>
+ {{/name}}
+ {{#location}}
+ <span class="location"><i class="fa fa-map-marker d-icon d-icon-map-marker"></i>{{{location}}}</span>
+ {{/location}}
+ {{#website}}
+ <span>
+ <i class="fa fa-globe d-icon d-icon-globe"></i><a href="{{website}}">{{{website_name}}}</a>
+ </span>
+ {{/website}}
+ </div>
+ {{#bio}}
+ <p>{{{bio}}}</p>
+ {{/bio}}
+ <span>{{joined}} {{created_at}}</span>
+ </article>
+ <div style="clear: both"></div>
+</aside>
diff --git a/spec/components/onebox/engine/discourse_local_onebox_spec.rb b/spec/components/onebox/engine/discourse_local_onebox_spec.rb
index 911ef40b312..efc60b97501 100644
--- a/spec/components/onebox/engine/discourse_local_onebox_spec.rb
+++ b/spec/components/onebox/engine/discourse_local_onebox_spec.rb
@@ -77,6 +77,23 @@ describe Onebox::Engine::DiscourseLocalOnebox do
end
end
+ context "for a link to a user profile" do
+ let(:user) { Fabricate(:user) }
+
+ it "returns a link if user isn't found" do
+ url = "#{Discourse.base_url}/u/none"
+ expect(Onebox.preview(url).to_s).to eq(build_link(url))
+ end
+
+ it "returns some onebox goodness if user exists" do
+ html = Onebox.preview("#{Discourse.base_url}/u/#{user.username}").to_s
+ expect(html).to include(user.username)
+ expect(html).to include(user.name)
+ expect(html).to include(user.created_at.strftime("%B %-d, %Y"))
+ expect(html).to include('<aside class="onebox">')
+ end
+ end
+
context "for a link to an internal audio or video file" do
let(:sha) { Digest::SHA1.hexdigest("discourse") }