Update comment 実装のための下調べ : hkob の雑記録 (469)

はじめに

hkob の雑記録の第469回目(連続42日目)は、Update comment を実装するための下調べを行います。

テスト用コメントの作成

まずはテストするコメントを作成しました。

コメントを作成

このコメントへのリンクを取得します。実はリンクで取得される d= の後ろにある id は comment_id ではなく、Discussion の id になります。

https://www.notion.so/notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77?d=4475361640994a5f97c653eb758e7a9d&source=copy_link

実際にこの URL から page を取得してみます。

page = Page.find "https://www.notion.so/notion_ruby_mapping_test_data-c01166c613ae45cbb96818b4ef2f5a77?d=4475361640994a5f97c653eb758e7a9d&source=copy_link"
=> NotionRubyMapping::Page-c01166c613ae45cbb96818b4ef2f5a77

この page で comments を取得してみます。discussion_id をキーとし、 DiscussionThread オブジェクトとしたハッシュが取得できています。

=> 
{"4475361640994a5f97c653eb758e7a9d" =>
  #<NotionRubyMapping::DiscussionThread:0x0000000125fb6370
   @comments=
    [#<NotionRubyMapping::CommentObject:0x0000000125fb6410
      @json=
       {"object" => "comment",
        "id" => "4a815db3-cc21-4f34-88b9-6c60d9da5261",
        "parent" => {"type" => "page_id", "page_id" => "c01166c6-13ae-45cb-b968-18b4ef2f5a77"},
        "discussion_id" => "44753616-4099-4a5f-97c6-53eb758e7a9d",
        "created_time" => "2022-08-03T11:30:00.000Z",
        "last_edited_time" => "2022-08-03T11:30:00.000Z",
        "created_by" => {"object" => "user", "id" => "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"},
        "rich_text" =>
         [{"type" => "text",
           "text" => {"content" => "Comment to a page", "link" => nil},
           "annotations" => {"bold" => false, "italic" => false, "strikethrough" => false, "underline" => false, "code" => false, "color" => "default"},
           "plain_text" => "Comment to a page",
           "href" => nil}],
        "display_name" => {"type" => "user", "resolved_name" => "Hiroyuki KOBAYASHI"}},
      @text_objects=
       #<NotionRubyMapping::RichTextArray:0x0000000125fb63e8
        @key="rich_text",
        @rich_text_objects=
         [#<NotionRubyMapping::TextObject:0x0000000126f61680
           @options=
            {"plain_text" => "Comment to a page",
             "bold" => false,
             "italic" => false,
             "strikethrough" => false,
             "underline" => false,
             "code" => false,
             "color" => "default",
             "href" => nil},
           @text="Comment to a page",
           @type="text",
           @will_update=false>],
        @will_update=false>,
      @will_update=false>,
      (中略)
     #<NotionRubyMapping::CommentObject:0x0000000125fb6000
      @json=
       {"object" => "comment",
        "id" => "348d8e4e-98ab-8033-acfa-001d47d0987b",
        "parent" => {"type" => "page_id", "page_id" => "c01166c6-13ae-45cb-b968-18b4ef2f5a77"},
        "discussion_id" => "44753616-4099-4a5f-97c6-53eb758e7a9d",
        "created_time" => "2026-04-20T10:52:00.000Z",
        "last_edited_time" => "2026-04-20T10:52:00.000Z",
        "created_by" => {"object" => "user", "id" => "2200a911-6a96-44bb-bd38-6bfb1e01b9f6"},
        "rich_text" =>
         [{"type" => "text",
           "text" => {"content" => "Comment for edit test", "link" => nil},
           "annotations" => {"bold" => false, "italic" => false, "strikethrough" => false, "underline" => false, "code" => false, "color" => "default"},
           "plain_text" => "Comment for edit test",
           "href" => nil}],
        "display_name" => {"type" => "user", "resolved_name" => "Hiroyuki KOBAYASHI"}},
      @text_objects=
       #<NotionRubyMapping::RichTextArray:0x0000000125fb5fd8
        @key="rich_text",
        @rich_text_objects=
         [#<NotionRubyMapping::TextObject:0x0000000126f60f00
           @options=
            {"plain_text" => "Comment for edit test",
             "bold" => false,
             "italic" => false,
             "strikethrough" => false,
             "underline" => false,
             "code" => false,
             "color" => "default",
             "href" => nil},
           @text="Comment for edit test",
           @type="text",
           @will_update=false>],
        @will_update=false>,
      @will_update=false>],
   @discussion_id="4475361640994a5f97c653eb758e7a9d">}      

最後のコメントを取得するには、DiscusstionThread を取得し、そこからさらに comments で CommentObjects の Array を取得する必要があります。今回は一番最後のコメントが欲しいので、last で取得しましょう。

comment = page.comments.values.first.comments.last
=> 
#<NotionRubyMapping::CommentObject:0x0000000124790e08

comment.id としましたが、そんなメソッドは作っていませんでした。

comment.id
(irb):15:in '<main>': undefined method 'id' for an instance of NotionRubyMapping::CommentObject (NoMethodError)
        from /Users/hkob/.local/share/mise/installs/ruby/4.0.2/lib/ruby/gems/4.0.0/gems/irb-1.17.0/exe/irb:9:in '<top (required)>'
        from /Users/hkob/.local/share/mise/installs/ruby/4.0.2/lib/ruby/site_ruby/4.0.0/rubygems.rb:304:in 'Kernel#load'
        from /Users/hkob/.local/share/mise/installs/ruby/4.0.2/lib/ruby/site_ruby/4.0.0/rubygems.rb:304:in 'Gem.activate_and_load_bin_path'
        from /Users/hkob/.local/share/mise/installs/ruby/4.0.2/bin/irb:25:in '<main>'

JSON の中から id は取得できました。とりあえず id メソッドは作成しておきましょう。

comment.json["id"]
=> "348d8e4e-98ab-8033-acfa-001d47d0987b"

Update a comment

まずは Reference を確認します。これまではだいたい rich_text での対応だけだったのですが、最近の傾向で markdown も使用できるようです。

まず、スクリプトを書いてみました。

curl -X PATCH 'https://api.notion.com/v1/comments/348d8e4e98ab8033acfa001d47d0987b' \
  -H 'Notion-Version: 2026-03-11' \
  -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
  -H 'Content-Type: application/json' \
  --data '{
    "rich_text": [
      {
        "type": "text",
        "text": {
          "content": "update comment by rich text",
          "link": null
        },
        "plain_text": "update comment by rich text",
        "href": null
      }
    ]
  }'

実行すると以下のように object_not_found になりました。

{
  "object": "error",
  "status": 404,
  "code": "object_not_found",
  "message": "Could not find comment with ID: 348d8e4e-98ab-8033-acfa-001d47d0987b. Make sure the relevant pages and databases are shared with your integration \"input_tasks\".",
  "additional_data": {
    "integration_id": "40673a87-d8ed-41e0-aa55-7f0e8ace24cd"
  },
  "request_id": "76740c6f-56e5-4885-b0f0-3d591fa515ed"
}

Reference を確認したところ、以下のような描画がありました。

インテグレーションは、自分が作成したコメントのみ更新できます。他のユーザーや別のインテグレーションが作成したコメントを更新しようとすると、404 エラーが返ります。

404 error

試しに一つ前の notion_ruby_mapping integration が作成した 37921658-0840-45ae-924a-d9ce1d60375b に変更して実行してみます。

curl -X PATCH 'https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b' \
  -H 'Notion-Version: 2026-03-11' \
  -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
  -H 'Content-Type: application/json' \
  --data '{
    "rich_text": [
      {
        "type": "text",
        "text": {
          "content": "update comment by rich text",
          "link": null
        },
        "plain_text": "update comment by rich text",
        "href": null
      }
    ]
  }'

今度は正しく変更されました。編集済みになっています。下のコメントは要らなくなったので消しておきます。

updated comment

おわりに

とりあえずテスト作成する前に考えなければならないことがたくさんありました。NotionRubyMapping 側では rich_text=markdown= の二つの更新メソッドの作成を検討する必要がありそうです。明日テストをどう記述するかを検討してみます。

https://hkob.notion.site/hkob-16dd8e4e98ab807cbe3cf3cc94cdfe0f?pvs=4hkob.notion.site