Class: RailsMarkup::AnnotationsController

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/rails_markup/annotations_controller.rb

Instance Method Summary collapse

Instance Method Details

#acknowledgeObject

POST /feedback/api/annotations/:id/acknowledge



115
116
117
118
# File 'app/controllers/rails_markup/annotations_controller.rb', line 115

def acknowledge
  @annotation.acknowledge!
  render json: @annotation.as_api_json
end

#createObject

POST /feedback/api/sessions/:session_id/annotations



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/rails_markup/annotations_controller.rb', line 19

def create
  return  if client_supplied_author?

  attributes = annotation_params
  return render_invalid_legacy_client_id unless attributes

  annotation = Annotation.new(attributes)
  assign_current_user(annotation)

  if annotation.client_uuid.present? && (existing = Annotation.find_by(client_uuid: annotation.client_uuid))
    return render_duplicate(existing, annotation)
  end

  if annotation.save
    fire_create_callback(annotation)
    render json: annotation.as_api_json, status: :created
  else
    render json: { errors: annotation.errors.full_messages }, status: :unprocessable_entity
  end
rescue ActiveRecord::RecordNotUnique
  raise if annotation.client_uuid.blank?

  render_duplicate(Annotation.find_by!(client_uuid: annotation.client_uuid), annotation)
end

#create_sessionObject

POST /feedback/api/sessions



14
15
16
# File 'app/controllers/rails_markup/annotations_controller.rb', line 14

def create_session
  render json: { id: "rm-#{SecureRandom.hex(8)}", url: params[:url] }
end

#destroyObject

DELETE /feedback/api/annotations/:client_uuid



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/rails_markup/annotations_controller.rb', line 88

def destroy
  client_uuid = normalized_route_uuid
  return render_invalid_uuid unless client_uuid
  base_revision = normalized_base_revision
  return render_invalid_base_revision unless base_revision

  annotation = Annotation.find_by(client_uuid: client_uuid)
  return head :no_content unless annotation

  annotation.with_lock do
    raise Annotation::RevisionConflict, annotation unless annotation.revision == base_revision

    annotation.destroy!
  end
  head :no_content
rescue Annotation::RevisionConflict => error
  render_revision_conflict(error)
rescue ActiveRecord::RecordNotFound
  head :no_content
end

#dismissObject

POST /feedback/api/annotations/:id/dismiss



127
128
129
130
# File 'app/controllers/rails_markup/annotations_controller.rb', line 127

def dismiss
  @annotation.dismiss!(reason: params[:reason])
  render json: @annotation.as_api_json
end

#healthObject

GET /feedback/api/health



110
111
112
# File 'app/controllers/rails_markup/annotations_controller.rb', line 110

def health
  render json: { ok: true }
end

#indexObject

GET /feedback/api/annotations?page_url=/current?page=variant



45
46
47
48
49
50
51
52
# File 'app/controllers/rails_markup/annotations_controller.rb', line 45

def index
  annotations = Annotation.for_page(params[:page_url]).recent.to_a
  unless annotations.all? { |annotation| Annotation.valid_client_uuid?(annotation.client_uuid) }
    return render json: { error: "annotation identities require repair" }, status: :service_unavailable
  end

  render json: annotations.map(&:as_api_json)
end

#replyObject

POST /feedback/api/annotations/:id/reply



133
134
135
136
137
138
# File 'app/controllers/rails_markup/annotations_controller.rb', line 133

def reply
  return render json: { error: "message is required" }, status: :unprocessable_entity if params[:message].blank?

  @annotation.add_reply!(message: params[:message], role: "agent")
  render json: @annotation.as_api_json
end

#resolveObject

POST /feedback/api/annotations/:id/resolve



121
122
123
124
# File 'app/controllers/rails_markup/annotations_controller.rb', line 121

def resolve
  @annotation.resolve!(summary: params[:summary])
  render json: @annotation.as_api_json
end

#upsertObject

PUT /feedback/api/annotations/:client_uuid



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
# File 'app/controllers/rails_markup/annotations_controller.rb', line 55

def upsert
  client_uuid = normalized_route_uuid
  return render_invalid_uuid unless client_uuid
  return  if client_supplied_author?

  dirty_fields = normalized_dirty_fields
  return render_invalid_dirty_fields unless dirty_fields
  base_revision = normalized_base_revision
  return render_invalid_base_revision unless base_revision

  attributes = browser_attributes
  return render_invalid_status if dirty_fields.include?("status") && !Annotation::STATUSES.include?(attributes["status"])

  annotation = Annotation.find_or_initialize_by(client_uuid: client_uuid)
  created = annotation.new_record?
  save_browser_state!(annotation, attributes, dirty_fields, base_revision)
  fire_create_callback(annotation) if created
  render json: annotation.as_api_json
rescue Annotation::RevisionConflict => error
  render_revision_conflict(error)
rescue ActiveRecord::RecordInvalid => error
  render json: { errors: error.record.errors.full_messages }, status: :unprocessable_entity
rescue ActiveRecord::RecordNotUnique
  annotation = Annotation.find_by!(client_uuid: client_uuid)
  begin
    save_browser_state!(annotation, attributes, dirty_fields, base_revision)
    render json: annotation.as_api_json
  rescue Annotation::RevisionConflict => error
    render_revision_conflict(error)
  end
end