AI: #439 投稿の “閲覧済” を冪等にする (#468)

Refs #439

Branch mode: `new`; base: `main`; head: `feature/439`

Implementation model: `gpt-6-luna` / reasoning: `low`

## Automated implementation (requires human review)
`POST /posts/:id/viewed` の登録を `UserPostView.create_or_find_by!` に変更しました。同じユーザーが同じ投稿を繰り返し閲覧済みにしても、複合キーの重複エラーになりません。

テスト、lint、ビルド等は実行していません。

## Verification
- Tests/builds/migrations were not authorized and were not invoked by the controller.
- Codex was instructed not to run them; inspect its command transcript.
- The repository owner alone reviews changes. No AI review is executed.
- Do not merge before human acceptance and required checks.

---------

Co-authored-by: みてるぞ <miteruzo@naver.com>
Co-authored-by: Codex Night Worker <codex-night@localhost>
Reviewed-on: #468
Co-authored-by: テツノニジカ <969+codex-bot@noreply.localhost>
このコミットはプルリクエスト #468 でマージされました。
このコミットが含まれているのは:
2026-11-23 03:09:52 +09:00
committed by みてるぞ
co-authored by みてるぞ Codex Night Worker
コミット 66b364428d
2個のファイルの変更、18行の追加、1行の削除
+3 -1
ファイルの表示
@@ -314,7 +314,9 @@ class PostsController < ApplicationController
def viewed
return head :unauthorized unless current_user
current_user.viewed_posts << Post.find(params[:id])
UserPostView.create_or_find_by!(
user: current_user,
post: Post.find(params[:id]))
head :no_content
end
+15
ファイルの表示
@@ -2637,6 +2637,21 @@ RSpec.describe 'Posts API', type: :request do
expect(user.reload.viewed?(post_record)).to be(true)
end
it '204 and keeps one view on repeated requests' do
sign_in_as(user)
expect {
post "/posts/#{ post_record.id }/viewed"
}.to change(UserPostView, :count).by(1)
expect(response).to have_http_status(:no_content)
expect {
post "/posts/#{ post_record.id }/viewed"
}.not_to change(UserPostView, :count)
expect(response).to have_http_status(:no_content)
expect(user.reload.viewed?(post_record)).to be(true)
end
end
describe 'DELETE /posts/:id/viewed' do