Class: SidebarSectionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/sidebar_sections_controller.rb

Constant Summary

Constants inherited from ApplicationController

ApplicationController::LEGACY_NO_THEMES, ApplicationController::LEGACY_NO_UNOFFICIAL_PLUGINS, ApplicationController::NO_PLUGINS, ApplicationController::NO_THEMES, ApplicationController::NO_UNOFFICIAL_PLUGINS, ApplicationController::SAFE_MODE

Constants included from CanonicalURL::ControllerExtensions

CanonicalURL::ControllerExtensions::ALLOWED_CANONICAL_PARAMS

Instance Attribute Summary

Attributes inherited from ApplicationController

#theme_id

Instance Method Summary collapse

Methods inherited from ApplicationController

#application_layout, #can_cache_content?, #clear_notifications, #conditionally_allow_site_embedding, #current_homepage, #discourse_expires_in, #dont_cache_page, #ember_cli_required?, #fetch_user_from_params, #guardian, #handle_permalink, #handle_theme, #handle_unverified_request, #has_escaped_fragment?, #immutable_for, #no_cookies, #perform_refresh_session, #post_ids_including_replies, #preload_json, #rate_limit_second_factor!, #redirect_with_client_support, #render_json_dump, #render_serialized, requires_plugin, #rescue_discourse_actions, #resolve_safe_mode, #secure_session, #serialize_data, #set_current_user_for_logs, #set_layout, #set_mobile_view, #set_mp_snapshot_fields, #show_browser_update?, #store_preloaded, #use_crawler_layout?, #with_resolved_locale

Methods included from VaryHeader

#ensure_vary_header

Methods included from ReadOnlyMixin

#add_readonly_header, #allowed_in_staff_writes_only_mode?, #block_if_readonly_mode, #check_readonly_mode, included, #staff_writes_only_mode?

Methods included from Hijack

#hijack

Methods included from GlobalPath

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

Methods included from JsonError

#create_errors_json

Methods included from CanonicalURL::ControllerExtensions

#canonical_url, #default_canonical, included

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

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/sidebar_sections_controller.rb', line 19

def create
  sidebar_section =
    SidebarSection.create!(section_params.merge(sidebar_urls_attributes: links_params))

  if sidebar_section.public?
    StaffActionLogger.new(current_user).log_create_public_sidebar_section(sidebar_section)
    MessageBus.publish("/refresh-sidebar-sections", nil)
    Site.clear_anon_cache!
  end

  render_serialized(sidebar_section, SidebarSectionSerializer)
rescue ActiveRecord::RecordInvalid => e
  render_json_error(e.record.errors.full_messages.first)
rescue ActiveRecord::NestedAttributes::TooManyRecords => e
  render_json_error(e.message)
end

#destroyObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/sidebar_sections_controller.rb', line 97

def destroy
  sidebar_section = SidebarSection.find_by(id: section_params["id"])
  @guardian.ensure_can_delete!(sidebar_section)
  sidebar_section.destroy!

  if sidebar_section.public?
    StaffActionLogger.new(current_user).log_destroy_public_sidebar_section(sidebar_section)
    MessageBus.publish("/refresh-sidebar-sections", nil)
  end

  render json: success_json
rescue Discourse::InvalidAccess
  render json: failed_json, status: 403
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/sidebar_sections_controller.rb', line 7

def index
  sections =
    SidebarSection
      .strict_loading
      .includes(:sidebar_urls)
      .where("public OR user_id = ?", current_user.id)
      .order("(public IS TRUE) DESC, title ASC")
      .map { |section| SidebarSectionSerializer.new(section, root: false) }

  render json: sections
end


118
119
120
# File 'app/controllers/sidebar_sections_controller.rb', line 118

def links_params
  params.permit(links: %i[icon name value id _destroy segment])["links"]
end

#reorderObject



86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/sidebar_sections_controller.rb', line 86

def reorder
  sidebar_section = SidebarSection.find_by(id: reorder_params["sidebar_section_id"])
  @guardian.ensure_can_edit!(sidebar_section)
  order = reorder_params["links_order"].map(&:to_i).each_with_index.to_h
  set_order(sidebar_section, order)

  render_serialized(sidebar_section, SidebarSectionSerializer)
rescue Discourse::InvalidAccess
  render json: failed_json, status: 403
end

#reorder_paramsObject



122
123
124
# File 'app/controllers/sidebar_sections_controller.rb', line 122

def reorder_params
  params.permit(:sidebar_section_id, links_order: [])
end

#resetObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/sidebar_sections_controller.rb', line 73

def reset
  sidebar_section = SidebarSection.find_by(id: params[:id])
  raise Discourse::InvalidParameters if !sidebar_section
  @guardian.ensure_can_edit!(sidebar_section)

  case sidebar_section.section_type
  when "community"
    sidebar_section.reset_community!
  end

  render_serialized(sidebar_section, SidebarSectionSerializer)
end

#section_paramsObject



112
113
114
115
116
# File 'app/controllers/sidebar_sections_controller.rb', line 112

def section_params
  section_params = params.permit(:id, :title, :public)
  section_params.merge!(user: current_user) if !params[:public]
  section_params
end

#updateObject



36
37
38
39
40
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
# File 'app/controllers/sidebar_sections_controller.rb', line 36

def update
  sidebar_section = SidebarSection.find_by(id: section_params["id"])
  @guardian.ensure_can_edit!(sidebar_section)

  ActiveRecord::Base.transaction do
    sidebar_section.update!(section_params.merge(sidebar_urls_attributes: links_params))
    sidebar_section.sidebar_section_links.update_all(user_id: sidebar_section.user_id)

    order =
      sidebar_section
        .sidebar_urls
        .sort_by do |url|
          links_params.index { |link| link["name"] == url.name && link["value"] == url.value } ||
            -1
        end
        .each_with_index
        .map { |url, index| [url.id, index] }
        .to_h

    set_order(sidebar_section, order)
  end

  if sidebar_section.public?
    StaffActionLogger.new(current_user).log_update_public_sidebar_section(sidebar_section)
    MessageBus.publish("/refresh-sidebar-sections", nil)
    Site.clear_anon_cache!
  end

  render_serialized(sidebar_section.reload, SidebarSectionSerializer)
rescue ActiveRecord::RecordInvalid => e
  render_json_error(e.record.errors.full_messages.first)
rescue ActiveRecord::NestedAttributes::TooManyRecords => e
  render_json_error(e.message)
rescue Discourse::InvalidAccess
  render json: failed_json, status: 403
end