Module: PrettyText::Helpers
Constant Summary collapse
- TAG_HASHTAG_POSTFIX =
"::tag"
Instance Method Summary collapse
- #avatar_template(username) ⇒ Object
-
#format_username(username) ⇒ Object
Overwrite this in a plugin to change how markdown can format usernames on the server side.
- #get_current_user(user_id) ⇒ Object
- #get_topic_info(topic_id) ⇒ Object
- #hashtag_lookup(slug, cooking_user_id, types_in_priority_order) ⇒ Object
- #lookup_primary_user_group(username) ⇒ Object
- #lookup_upload_urls(urls) ⇒ Object
-
#t(key, opts) ⇒ Object
functions here are available to v8.
Instance Method Details
#avatar_template(username) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/pretty_text/helpers.rb', line 18 def avatar_template(username) return "" unless username user = User.find_by(username_lower: username.downcase) return "" if user.blank? # TODO: Add support for ES6 and call `avatar-template` directly UrlHelper.schemaless(UrlHelper.absolute(user.avatar_template)) end |
#format_username(username) ⇒ Object
Overwrite this in a plugin to change how markdown can format usernames on the server side
37 38 39 |
# File 'lib/pretty_text/helpers.rb', line 37 def format_username(username) username end |
#get_current_user(user_id) ⇒ Object
138 139 140 141 |
# File 'lib/pretty_text/helpers.rb', line 138 def get_current_user(user_id) return unless user_id.is_a?(Integer) { staff: User.where(id: user_id).where("moderator OR admin").exists? } end |
#get_topic_info(topic_id) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/pretty_text/helpers.rb', line 92 def get_topic_info(topic_id) return unless topic_id.is_a?(Integer) # 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) { title: Rack::Utils.escape_html(topic.title), href: topic.url } elsif topic { title: I18n.t("on_another_topic"), href: topic.slugless_url } end end |
#hashtag_lookup(slug, cooking_user_id, types_in_priority_order) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/pretty_text/helpers.rb', line 103 def hashtag_lookup(slug, cooking_user_id, types_in_priority_order) # NOTE: This is _somewhat_ expected since we need to be able to cook posts # etc. without a user sometimes, but it is still an edge case. # # The Discourse.system_user is usually an admin with access to _all_ # categories, however if the suppress_secured_categories_from_admin # site setting is activated then this user will not be able to access # secure categories, so hashtags that are secure will not render. if cooking_user_id.blank? cooking_user = Discourse.system_user else cooking_user = User.find(cooking_user_id) end types_in_priority_order = types_in_priority_order.select do |type| HashtagAutocompleteService.data_source_types.include?(type) end result = HashtagAutocompleteService.new(Guardian.new(cooking_user)).lookup( [slug], types_in_priority_order, ) found_hashtag = nil types_in_priority_order.each do |type| if result[type.to_sym].any? found_hashtag = result[type.to_sym].first.to_h break end end found_hashtag end |
#lookup_primary_user_group(username) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/pretty_text/helpers.rb', line 27 def lookup_primary_user_group(username) return "" unless username user = User.find_by(username_lower: username.downcase) return "" if user.blank? user.primary_group.try(:name) || "" end |
#lookup_upload_urls(urls) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pretty_text/helpers.rb', line 41 def lookup_upload_urls(urls) map = {} result = {} urls.each do |url| sha1 = Upload.sha1_from_short_url(url) if (url.split(".")[1].nil?) # video sha1 without extension for thumbnail thumbnail = Upload.where("original_filename LIKE ?", "#{sha1}.%").last sha1 = thumbnail.sha1 if thumbnail end map[url] = sha1 if sha1 end if map.length > 0 reverse_map = {} map.each do |key, value| reverse_map[value] ||= [] reverse_map[value] << key end Upload .where(sha1: map.values) .pluck(:sha1, :url, :extension, :original_filename, :secure) .each do |row| sha1, url, extension, original_filename, secure = row if short_urls = reverse_map[sha1] secure_uploads = SiteSetting.secure_uploads? && secure short_urls.each do |short_url| result[short_url] = { url: ( if secure_uploads Upload.secure_uploads_url_from_upload_url(url) else Discourse.store.cdn_url(url) end ), short_path: Upload.short_path(sha1: sha1, extension: extension), base62_sha1: Upload.base62_sha1(sha1), } end end end end result end |
#t(key, opts) ⇒ Object
functions here are available to v8
10 11 12 13 14 15 16 |
# File 'lib/pretty_text/helpers.rb', line 10 def t(key, opts) key = "js." + key return I18n.t(key) if opts.blank? str = I18n.t(key, Hash[opts.entries].symbolize_keys).dup opts.each { |k, v| str.gsub!("{{#{k}}}", v.to_s) } str end |