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



96
97
98
99
# File 'app/controllers/rails_markup/annotations_controller.rb', line 96

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



82
83
84
85
86
87
88
# File 'app/controllers/rails_markup/annotations_controller.rb', line 82

def destroy
  client_uuid = normalized_route_uuid
  return render_invalid_uuid unless client_uuid

  Annotation.find_by(client_uuid: client_uuid)&.destroy!
  head :no_content
end

#dismissObject

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



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

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

#healthObject

GET /feedback/api/health



91
92
93
# File 'app/controllers/rails_markup/annotations_controller.rb', line 91

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



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

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



102
103
104
105
# File 'app/controllers/rails_markup/annotations_controller.rb', line 102

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
# 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

  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?
  apply_desired_state(annotation, attributes, dirty_fields)
  annotation.save!
  fire_create_callback(annotation) if created
  render json: annotation.as_api_json
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)
  apply_desired_state(annotation, attributes, dirty_fields)
  annotation.save!
  render json: annotation.as_api_json
end