2017-07-19 15:08:54 -04:00
|
|
|
require_dependency 'inline_oneboxer'
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
module PrettyText
|
|
|
|
module Helpers
|
|
|
|
extend self
|
|
|
|
|
|
|
|
# functions here are available to v8
|
|
|
|
def t(key, opts)
|
|
|
|
key = "js." + key
|
|
|
|
unless opts
|
|
|
|
I18n.t(key)
|
|
|
|
else
|
|
|
|
str = I18n.t(key, Hash[opts.entries].symbolize_keys).dup
|
2017-07-27 21:20:09 -04:00
|
|
|
opts.each { |k, v| str.gsub!("{{#{k.to_s}}}", v.to_s) }
|
2016-06-14 14:31:51 -04:00
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_template(username)
|
|
|
|
return "" unless username
|
|
|
|
user = User.find_by(username_lower: username.downcase)
|
|
|
|
return "" unless user.present?
|
|
|
|
|
|
|
|
# TODO: Add support for ES6 and call `avatar-template` directly
|
|
|
|
if !user.uploaded_avatar_id
|
|
|
|
avatar_template = User.default_template(username)
|
|
|
|
else
|
|
|
|
avatar_template = user.avatar_template
|
|
|
|
end
|
|
|
|
|
|
|
|
UrlHelper.schemaless UrlHelper.absolute avatar_template
|
|
|
|
end
|
|
|
|
|
|
|
|
def mention_lookup(name)
|
|
|
|
return false if name.blank?
|
2017-04-24 16:56:12 -04:00
|
|
|
return "group" if Group.exists?(name: name)
|
|
|
|
return "user" if User.exists?(username_lower: name.downcase)
|
2016-06-14 14:31:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def category_hashtag_lookup(category_slug)
|
|
|
|
if category = Category.query_from_hashtag_slug(category_slug)
|
|
|
|
[category.url_with_id, category_slug]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-22 11:46:15 -04:00
|
|
|
def lookup_image_urls(urls)
|
|
|
|
map = {}
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
urls.each do |url|
|
|
|
|
sha1 = Upload.sha1_from_short_url(url)
|
|
|
|
map[url] = sha1 if sha1
|
|
|
|
end
|
|
|
|
|
|
|
|
if map.length > 0
|
|
|
|
reverse_map = map.invert
|
|
|
|
|
|
|
|
Upload.where(sha1: map.values).pluck(:sha1, :url).each do |row|
|
|
|
|
sha1, url = row
|
|
|
|
|
|
|
|
if short_url = reverse_map[sha1]
|
|
|
|
result[short_url] = url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2017-07-19 15:08:54 -04:00
|
|
|
def lookup_inline_onebox(url)
|
|
|
|
InlineOneboxer.lookup(url)
|
|
|
|
end
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
def get_topic_info(topic_id)
|
2017-04-15 00:11:02 -04:00
|
|
|
return unless topic_id.is_a?(Integer)
|
2016-06-14 14:31:51 -04:00
|
|
|
# TODO this only handles public topics, secured one do not get this
|
|
|
|
topic = Topic.find_by(id: topic_id)
|
|
|
|
if topic && Guardian.new.can_see?(topic)
|
|
|
|
{
|
2017-01-09 14:52:45 -05:00
|
|
|
title: Rack::Utils.escape_html(topic.title),
|
2016-06-14 14:31:51 -04:00
|
|
|
href: topic.url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_tag_hashtag_lookup(text)
|
|
|
|
tag_postfix = '::tag'
|
|
|
|
is_tag = text =~ /#{tag_postfix}$/
|
|
|
|
|
|
|
|
if !is_tag && category = Category.query_from_hashtag_slug(text)
|
|
|
|
[category.url_with_id, text]
|
|
|
|
elsif is_tag && tag = Tag.find_by_name(text.gsub!("#{tag_postfix}", ''))
|
|
|
|
["#{Discourse.base_url}/tags/#{tag.name}", text]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2016-07-07 03:52:56 -04:00
|
|
|
|
|
|
|
def get_current_user(user_id)
|
2017-04-25 03:19:12 -04:00
|
|
|
return unless user_id.is_a?(Integer)
|
2017-04-24 16:56:12 -04:00
|
|
|
{ staff: User.where(id: user_id).where("moderator OR admin").exists? }
|
2016-07-07 03:52:56 -04:00
|
|
|
end
|
2016-06-14 14:31:51 -04:00
|
|
|
end
|
|
|
|
end
|