Class: DraftsController

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

Constant Summary collapse

INDEX_LIMIT =
50

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



30
31
32
33
34
35
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/drafts_controller.rb', line 30

def create
  raise Discourse::NotFound.new if params[:draft_key].blank?

  if params[:data].size > SiteSetting.max_draft_length
    raise Discourse::InvalidParameters.new(:data)
  end

  begin
    data = JSON.parse(params[:data])
  rescue JSON::ParserError
    raise Discourse::InvalidParameters.new(:data)
  end

  if reached_max_drafts_per_user?(params)
    render_json_error I18n.t("draft.too_many_drafts.title"),
                      status: 403,
                      extras: {
                        description:
                          I18n.t(
                            "draft.too_many_drafts.description",
                            base_url: Discourse.base_url,
                          ),
                      }
    return
  end

  sequence =
    begin
      Draft.set(
        current_user,
        params[:draft_key],
        params[:sequence].to_i,
        params[:data],
        params[:owner],
        force_save: params[:force_save],
      )
    rescue Draft::OutOfSequence
      begin
        if !Draft.exists?(user_id: current_user.id, draft_key: params[:draft_key])
          Draft.set(
            current_user,
            params[:draft_key],
            DraftSequence.current(current_user, params[:draft_key]),
            params[:data],
            params[:owner],
          )
        else
          raise Draft::OutOfSequence
        end
      rescue Draft::OutOfSequence
        render_json_error I18n.t("draft.sequence_conflict_error.title"),
                          status: 409,
                          extras: {
                            description: I18n.t("draft.sequence_conflict_error.description"),
                          }
        return
      end
    end

  json = success_json.merge(draft_sequence: sequence)

  if data.present?
    # this is a bit of a kludge we need to remove (all the parsing) too many special cases here
    # we need to catch action edit and action editSharedDraft
    if data["postId"].present? && data["originalText"].present? &&
         data["action"].to_s.start_with?("edit")
      post = Post.find_by(id: data["postId"])
      if post && post.raw != data["originalText"]
        conflict_user = BasicUserSerializer.new(post.last_editor, root: false)
        render json: json.merge(conflict_user: conflict_user)
        return
      end
    end
  end

  render json: json
end

#destroyObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/drafts_controller.rb', line 108

def destroy
  user =
    if is_api?
      if @guardian.is_admin?
        fetch_user_from_params
      else
        raise Discourse::InvalidAccess
      end
    else
      current_user
    end

  begin
    Draft.clear(user, params[:id], params[:sequence].to_i)
  rescue Draft::OutOfSequence
    # nothing really we can do here, if try clearing a draft that is not ours, just skip it.
    # rendering an error causes issues in the composer
  rescue StandardError => e
    return render json: failed_json.merge(errors: e), status: 401
  end

  render json: success_json
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/drafts_controller.rb', line 10

def index
  params.permit(:offset)

  stream =
    Draft.stream(
      user: current_user,
      offset: params[:offset],
      limit: fetch_limit_from_params(default: nil, max: INDEX_LIMIT),
    )

  render json: { drafts: stream ? serialize_data(stream, DraftSerializer) : [] }
end

#showObject



23
24
25
26
27
28
# File 'app/controllers/drafts_controller.rb', line 23

def show
  raise Discourse::NotFound.new if params[:id].blank?

  seq = params[:sequence] || DraftSequence.current(current_user, params[:id])
  render json: { draft: Draft.get(current_user, params[:id], seq), draft_sequence: seq }
end