add a custom user onebox (#5542)

* add custom user onebox

* add specs
This commit is contained in:
Maja Komel 2018-01-30 11:03:08 +01:00 committed by Régis Hanol
parent 95e0aa059c
commit 018cb7f36b
5 changed files with 82 additions and 1 deletions

View File

@ -212,6 +212,16 @@ aside.onebox {
}
}
&.user-onebox {
.fa {
margin-right: 5px;
}
.full-name,
.location {
margin-right: 10px;
}
}
}
}

View File

@ -3684,3 +3684,5 @@ en:
search_logs:
graph_title: "Search Count"
joined: "Joined"

View File

@ -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)

View File

@ -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>

View File

@ -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") }