Module: PostsHelper

Includes:
ApplicationHelper
Defined in:
app/helpers/posts_helper.rb

Constant Summary collapse

CACHE_URL_DURATION =
12.hours.to_i

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

#add_resource_preload_list, #admin?, #age_words, all_connectors, #allow_plugins?, #allow_third_party_plugins?, #application_logo_dark_url, #application_logo_url, #authentication_data, #body_classes, #build_plugin_html, #can_sign_up?, #category_badge, #client_side_setup_data, #crawlable_meta_data, #crawler_layout?, #current_homepage, #customization_disabled?, #dark_color_scheme?, #dark_scheme_id, #discourse_color_scheme_stylesheets, #discourse_config_environment, #discourse_csrf_tags, #discourse_preload_color_scheme_stylesheets, #discourse_stylesheet_link_tag, #discourse_stylesheet_preload_tag, #discourse_theme_color_meta_tags, #escape_unicode, extra_body_classes, #format_topic_title, #ga_universal_json, #get_absolute_image_url, #google_tag_manager_json, #google_tag_manager_nonce_placeholder, #google_universal_analytics_json, #gsub_emoji_to_unicode, #guardian, #html_classes, #html_lang, #include_crawler_content?, #include_ios_native_app_banner?, #include_splash_screen?, #ios_app_argument, #ios_device?, #is_brotli_req?, #is_gzip_req?, #loading_admin?, #login_path, #manifest_url, #mobile_device?, #mobile_view?, #moderator?, #modern_mobile_device?, #normalized_safe_mode, #preload_script, #preload_script_url, #preloaded_json, #render_sitelinks_search_tag, #replace_plugin_html, #rss_creator, #rtl?, #scheme_id, #script_asset_path, #server_plugin_outlet, #shared_session_key, #short_date, #staff?, #stylesheet_manager, #text_size_class, #theme_id, #theme_js_lookup, #theme_lookup, #theme_translations_lookup, #topic_featured_link_domain, #waving_hand_url, #with_format

Methods included from GlobalPath

#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path

Methods included from ConfigurableUrls

#faq_path, #privacy_policy_url, #tos_url

Methods included from CanonicalURL::Helpers

#canonical_link_tag

Methods included from CurrentUser

#clear_current_user, #current_user, has_auth_cookie?, #is_api?, #is_user_api?, #log_off_user, #log_on_user, lookup_from_env, #refresh_session

Class Method Details

.canonical_redis_key(post) ⇒ Object



13
14
15
# File 'app/helpers/posts_helper.rb', line 13

def self.canonical_redis_key(post)
  "post_canonical_url_#{post.id}"
end

.clear_canonical_cache!(post) ⇒ Object



8
9
10
11
# File 'app/helpers/posts_helper.rb', line 8

def self.clear_canonical_cache!(post)
  key = canonical_redis_key(post)
  Discourse.redis.del(key)
end

Instance Method Details

#cached_post_url(post, use_canonical:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/posts_helper.rb', line 17

def cached_post_url(post, use_canonical:)
  if use_canonical
    # this is very expensive to calculate page, we cache it for 12 hours
    key = PostsHelper.canonical_redis_key(post)

    url = Discourse.redis.get(key)

    # break cache if either slug or topic_id changes
    url = nil if url && !url.start_with?(post.topic.url)

    if !url
      url = post.canonical_url
      Discourse.redis.setex(key, CACHE_URL_DURATION, url)
    end

    url
  else
    post.full_url
  end
end