2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2017-10-06 14:00:39 -04:00
|
|
|
UrlHelper.schemaless(UrlHelper.absolute(user.avatar_template))
|
2016-06-14 14:31:51 -04:00
|
|
|
end
|
|
|
|
|
2017-11-03 09:51:40 -04:00
|
|
|
def lookup_primary_user_group(username)
|
|
|
|
return "" unless username
|
|
|
|
user = User.find_by(username_lower: username.downcase)
|
|
|
|
return "" unless user.present?
|
|
|
|
|
|
|
|
user.primary_group.try(:name) || ""
|
|
|
|
end
|
|
|
|
|
2017-11-20 16:28:03 -05:00
|
|
|
# Overwrite this in a plugin to change how markdown can format
|
|
|
|
# usernames on the server side
|
|
|
|
def format_username(username)
|
|
|
|
username
|
|
|
|
end
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
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
|
|
|
|
|
2019-05-28 21:00:25 -04:00
|
|
|
def lookup_upload_urls(urls)
|
2017-08-22 11:46:15 -04:00
|
|
|
map = {}
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
urls.each do |url|
|
|
|
|
sha1 = Upload.sha1_from_short_url(url)
|
|
|
|
map[url] = sha1 if sha1
|
|
|
|
end
|
|
|
|
|
|
|
|
if map.length > 0
|
2018-09-05 09:46:43 -04:00
|
|
|
reverse_map = {}
|
|
|
|
|
|
|
|
map.each do |key, value|
|
|
|
|
reverse_map[value] ||= []
|
|
|
|
reverse_map[value] << key
|
|
|
|
end
|
2017-08-22 11:46:15 -04:00
|
|
|
|
2019-05-28 21:00:25 -04:00
|
|
|
Upload.where(sha1: map.values).pluck(:sha1, :url, :extension).each do |row|
|
|
|
|
sha1, url, extension = row
|
2017-08-22 11:46:15 -04:00
|
|
|
|
2018-09-05 09:46:43 -04:00
|
|
|
if short_urls = reverse_map[sha1]
|
2019-05-28 21:00:25 -04:00
|
|
|
short_urls.each do |short_url|
|
|
|
|
result[short_url] = {
|
|
|
|
url: url,
|
2019-06-10 21:15:45 -04:00
|
|
|
short_path: Upload.short_path(sha1: sha1, extension: extension),
|
|
|
|
base62_sha1: Upload.base62_sha1(sha1)
|
2019-05-28 21:00:25 -04:00
|
|
|
}
|
|
|
|
end
|
2017-08-22 11:46:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
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
|
|
|
|
}
|
2018-12-02 13:22:40 -05:00
|
|
|
elsif topic
|
|
|
|
{
|
|
|
|
title: I18n.t("on_another_topic"),
|
|
|
|
href: Discourse.base_url + topic.slugless_url
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
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]
|
2017-10-03 01:54:50 -04:00
|
|
|
elsif (!is_tag && tag = Tag.find_by(name: text)) ||
|
|
|
|
(is_tag && tag = Tag.find_by(name: text.gsub!("#{tag_postfix}", '')))
|
2016-06-14 14:31:51 -04:00
|
|
|
["#{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
|