2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
module PrettyText
|
|
|
|
module Helpers
|
|
|
|
extend self
|
|
|
|
|
2020-07-14 00:13:17 +08:00
|
|
|
TAG_HASHTAG_POSTFIX = "::tag"
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
# 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
|
|
|
|
opts.each { |k, v| str.gsub!("{{#{k.to_s}}}", v.to_s) }
|
|
|
|
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-07 02:00:39 +08:00
|
|
|
UrlHelper.schemaless(UrlHelper.absolute(user.avatar_template))
|
2016-06-15 02:31:51 +08:00
|
|
|
end
|
|
|
|
|
2017-11-03 21:51:40 +08: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-21 05:28:03 +08: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-15 02:31:51 +08:00
|
|
|
def category_hashtag_lookup(category_slug)
|
|
|
|
if category = Category.query_from_hashtag_slug(category_slug)
|
2020-06-18 16:32:14 +08:00
|
|
|
[category.url, category_slug]
|
2016-06-15 02:31:51 +08:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-29 09:00:25 +08:00
|
|
|
def lookup_upload_urls(urls)
|
2017-08-22 23:46:15 +08: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 21:46:43 +08:00
|
|
|
reverse_map = {}
|
|
|
|
|
|
|
|
map.each do |key, value|
|
|
|
|
reverse_map[value] ||= []
|
|
|
|
reverse_map[value] << key
|
|
|
|
end
|
2017-08-22 23:46:15 +08:00
|
|
|
|
2019-11-18 09:25:42 +08:00
|
|
|
Upload.where(sha1: map.values).pluck(:sha1, :url, :extension, :original_filename, :secure).each do |row|
|
|
|
|
sha1, url, extension, original_filename, secure = row
|
2017-08-22 23:46:15 +08:00
|
|
|
|
2018-09-05 21:46:43 +08:00
|
|
|
if short_urls = reverse_map[sha1]
|
2020-03-04 07:11:08 +08:00
|
|
|
secure_media = SiteSetting.secure_media? && secure
|
2019-11-18 09:25:42 +08:00
|
|
|
|
2019-05-29 09:00:25 +08:00
|
|
|
short_urls.each do |short_url|
|
|
|
|
result[short_url] = {
|
2020-01-24 09:59:30 +08:00
|
|
|
url: secure_media ? Upload.secure_media_url_from_upload_url(url) : Discourse.store.cdn_url(url),
|
2019-06-11 09:15:45 +08:00
|
|
|
short_path: Upload.short_path(sha1: sha1, extension: extension),
|
|
|
|
base62_sha1: Upload.base62_sha1(sha1)
|
2019-05-29 09:00:25 +08:00
|
|
|
}
|
|
|
|
end
|
2017-08-22 23:46:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
def get_topic_info(topic_id)
|
2017-04-15 12:11:02 +08:00
|
|
|
return unless topic_id.is_a?(Integer)
|
2016-06-15 02:31:51 +08: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-10 03:52:45 +08:00
|
|
|
title: Rack::Utils.escape_html(topic.title),
|
2016-06-15 02:31:51 +08:00
|
|
|
href: topic.url
|
|
|
|
}
|
2018-12-03 02:22:40 +08:00
|
|
|
elsif topic
|
|
|
|
{
|
|
|
|
title: I18n.t("on_another_topic"),
|
|
|
|
href: Discourse.base_url + topic.slugless_url
|
|
|
|
}
|
2016-06-15 02:31:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_tag_hashtag_lookup(text)
|
2020-07-14 00:13:17 +08:00
|
|
|
is_tag = text =~ /#{TAG_HASHTAG_POSTFIX}$/
|
2016-06-15 02:31:51 +08:00
|
|
|
|
|
|
|
if !is_tag && category = Category.query_from_hashtag_slug(text)
|
2020-06-18 16:32:14 +08:00
|
|
|
[category.url, text]
|
2017-10-03 13:54:50 +08:00
|
|
|
elsif (!is_tag && tag = Tag.find_by(name: text)) ||
|
2020-07-14 00:13:17 +08:00
|
|
|
(is_tag && tag = Tag.find_by(name: text.gsub!(TAG_HASHTAG_POSTFIX, '')))
|
2020-01-22 01:23:08 +08:00
|
|
|
["#{Discourse.base_url}/tag/#{tag.name}", text]
|
2016-06-15 02:31:51 +08:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2016-07-07 15:52:56 +08:00
|
|
|
|
|
|
|
def get_current_user(user_id)
|
2017-04-25 15:19:12 +08:00
|
|
|
return unless user_id.is_a?(Integer)
|
2017-04-25 04:56:12 +08:00
|
|
|
{ staff: User.where(id: user_id).where("moderator OR admin").exists? }
|
2016-07-07 15:52:56 +08:00
|
|
|
end
|
2016-06-15 02:31:51 +08:00
|
|
|
end
|
|
|
|
end
|