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)
= 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 = .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
|