Class: Pinmark::AnnotationsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
# File 'app/controllers/pinmark/annotations_controller.rb', line 13

def create
  payload = JSON.parse(request.body.read)
  comments = Array(payload["comments"])
  path = payload["path"]
  tree = payload["tree"]
  replace_match = payload["replace_match"] == true

  queue = Pinmark::Mcp::Queue.new
  data = queue.read
  existing = data.fetch("annotations", [])

  tree_lookup = flatten_tree(tree).index_by { |n| n["id"] }

  new_entries = comments.map do |c|
    annotation_id = c["node_id"] || c["pm_id"]
    node = tree_lookup[annotation_id]
    {
      "id" => SecureRandom.uuid,
      "status" => "pending",
      "received_at" => Time.current.iso8601,
      "page_path" => path,
      "component" => c["component"] || node&.dig("component"),
      "source" => c["source"] || node&.dig("source"),
      "node_id" => annotation_id,
      "selector" => c["selector"],
      "text_excerpt" => c["text_excerpt"],
      "comment" => c["comment"],
      "captured_at" => c["captured_at"],
      "ancestry" => component_ancestry(annotation_id, tree_lookup)
    }
  end

  remaining = if replace_match
                drop_matching(existing, new_entries, path)
              else
                existing
              end

  queue.write({ "annotations" => remaining + new_entries })

  render json: {
    ok: true,
    count: new_entries.size,
    queue: queue.path.relative_path_from(Rails.root).to_s
  }
end

#destroyObject



60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/pinmark/annotations_controller.rb', line 60

def destroy
  queue = Pinmark::Mcp::Queue.new
  data = queue.read
  before = data.fetch("annotations", []).size
  data["annotations"] = data.fetch("annotations", []).reject { |entry| entry["id"] == params[:id] }
  removed = before - data["annotations"].size
  queue.write(data)

  render json: { ok: true, removed: }
end

#indexObject



8
9
10
11
# File 'app/controllers/pinmark/annotations_controller.rb', line 8

def index
  queue = Pinmark::Mcp::Queue.new
  render json: { annotations: queue.read.fetch("annotations", []) }
end