WIP: Derive follow state from relationship rows and cancel stale Follows #7986

Draft
lambadalambda wants to merge 1 commit from fix/stale-follow-state into develop

Summary

Follow-up to #7628 (merged on GitLab as 2c916ccd8, shipped in 2.8.0). That fix made following in the relationship view trust the following_relationships table, but two gaps remained when a Follow activity outlives its relationship row (e.g. a crash between User.unfollow and ActivityPub.unfollow):

  • User.get_follow_state/2,3 still fell back to the latest Follow activity for remote targets, so a stale pending Follow showed as requested: true forever.
  • User.unfollow returned {:ok, nil} in that case, so CommonAPI.unfollow never reached ActivityPub.unfollow, the stale activity was never cancelled, and the controller rendered an empty relationship.

Changes:

  • get_follow_state/3 reads only the relationship row.
  • do_unfollow proceeds when no row exists but the latest Follow is still pending/accepted, so the Undo is sent and the activity is marked cancelled. Following count is refreshed on that path.

Refs #2803, #3263.

Testing

  • mix test test/pleroma/user_test.exs test/pleroma/web/common_api_test.exs test/pleroma/web/mastodon_api/views/account_view_test.exs test/pleroma/web/activity_pub test/pleroma/web/mastodon_api/controllers/account_controller_test.exs test/pleroma/web/mastodon_api/controllers/follow_request_controller_test.exs test/pleroma/web/admin_api/controllers/user_controller_test.exs test/pleroma/following_relationship_test.exs test/mix/tasks/pleroma/user_test.exs — all green.
  • mix format --check-formatted on changed files — clean.

Not addressed here

  • CommonAPI.unfollow still deletes the row and cancels the activity in two separate transactions; the incoming Undo Follow path in the transmogrifier does the reverse. Wrapping each in one transaction would stop new stale states from forming.
  • FollowingRelationship.state_to_enum/1 is now unused in lib.
## Summary Follow-up to #7628 (merged on GitLab as 2c916ccd8, shipped in 2.8.0). That fix made `following` in the relationship view trust the `following_relationships` table, but two gaps remained when a Follow activity outlives its relationship row (e.g. a crash between `User.unfollow` and `ActivityPub.unfollow`): - `User.get_follow_state/2,3` still fell back to the latest Follow activity for remote targets, so a stale pending Follow showed as `requested: true` forever. - `User.unfollow` returned `{:ok, nil}` in that case, so `CommonAPI.unfollow` never reached `ActivityPub.unfollow`, the stale activity was never cancelled, and the controller rendered an empty relationship. Changes: - `get_follow_state/3` reads only the relationship row. - `do_unfollow` proceeds when no row exists but the latest Follow is still pending/accepted, so the Undo is sent and the activity is marked cancelled. Following count is refreshed on that path. Refs #2803, #3263. ## Testing - `mix test test/pleroma/user_test.exs test/pleroma/web/common_api_test.exs test/pleroma/web/mastodon_api/views/account_view_test.exs test/pleroma/web/activity_pub test/pleroma/web/mastodon_api/controllers/account_controller_test.exs test/pleroma/web/mastodon_api/controllers/follow_request_controller_test.exs test/pleroma/web/admin_api/controllers/user_controller_test.exs test/pleroma/following_relationship_test.exs test/mix/tasks/pleroma/user_test.exs` — all green. - `mix format --check-formatted` on changed files — clean. ## Not addressed here - `CommonAPI.unfollow` still deletes the row and cancels the activity in two separate transactions; the incoming Undo Follow path in the transmogrifier does the reverse. Wrapping each in one transaction would stop new stale states from forming. - `FollowingRelationship.state_to_enum/1` is now unused in lib.
fix: derive follow state from relationship rows and cancel stale Follows
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
b7da662dea
User.get_follow_state no longer falls back to the latest Follow activity
when no following_relationships row exists. That fallback made the API
report a stale pending Follow as "requested" forever, and made
User.unfollow return {:ok, nil} so CommonAPI.unfollow never cancelled
the stale activity.

When no row exists but the latest Follow is still pending or accepted,
unfollow now proceeds so ActivityPub.unfollow cancels it and sends an
Undo.

Refs #2803 #3263
Author
Owner

@opencode-beta can you review this code?

@opencode-beta can you review this code?
opencode-beta left a comment

opencode-agent:job=3:generation=1

Review of b7da662dea55b2b2bdca6fdecb2ab9bf79d0f49a against develop@149236e468c453495e0dcf83a9728057436b463b (5 files, +133/−24).

Verdict

The approach is correct and closes both gaps described. get_follow_state/3 trusting only following_relationships removes the eternal requested: true for stale remote Follows, and the do_unfollow rescue path makes a retried unfollow actually reach ActivityPub.unfollow, so the stale Follow gets cancelled instead of rendering an empty relationship. 👍 with only minor comments (inline).

Verified locally

  • Toolchain matching CI: Elixir 1.18.3 / OTP 27 (mise-managed), PostgreSQL 13.
  • The exact test command from the description passes on this head: 1593 tests, 0 failures, 2 skipped.
  • mix format --check-formatted on the changed Elixir files: clean.

Minor (non-blocking)

  1. The same Follow activity is fetched three times on the stale path (inline).
  2. Count convergence on the stale path only repairs the follower's side (inline).
  3. FollowingRelationship.state_to_enum/1 now has no callers in lib/ or test/ — it can be dropped outright in the follow-up you noted.
  4. Pre-existing and unchanged, no action needed: do_unfollow's case has no arm for a :follow_reject row state; unreachable in practice because FollowingRelationship.update/3 deletes the row on reject.

Agreed the two-transaction row/activity split is worth a follow-up; the transmogrifier ordering note in the description would make a good separate issue.

opencode-agent:job=3:generation=1 Review of `b7da662dea55b2b2bdca6fdecb2ab9bf79d0f49a` against `develop@149236e468c453495e0dcf83a9728057436b463b` (5 files, +133/−24). ## Verdict The approach is correct and closes both gaps described. `get_follow_state/3` trusting only `following_relationships` removes the eternal `requested: true` for stale remote Follows, and the `do_unfollow` rescue path makes a retried unfollow actually reach `ActivityPub.unfollow`, so the stale Follow gets cancelled instead of rendering an empty relationship. 👍 with only minor comments (inline). ## Verified locally - Toolchain matching CI: Elixir 1.18.3 / OTP 27 (mise-managed), PostgreSQL 13. - The exact test command from the description passes on this head: **1593 tests, 0 failures, 2 skipped**. - `mix format --check-formatted` on the changed Elixir files: clean. ## Minor (non-blocking) 1. The same Follow activity is fetched three times on the stale path (inline). 2. Count convergence on the stale path only repairs the follower's side (inline). 3. `FollowingRelationship.state_to_enum/1` now has no callers in `lib/` or `test/` — it can be dropped outright in the follow-up you noted. 4. Pre-existing and unchanged, no action needed: `do_unfollow`'s `case` has no arm for a `:follow_reject` row state; unreachable in practice because `FollowingRelationship.update/3` deletes the row on reject. Agreed the two-transaction row/activity split is worth a follow-up; the transmogrifier ordering note in the description would make a good separate issue.
@ -1185,3 +1185,3 @@
nil ->
{:error, "Not subscribed!"}
if live_follow_activity?(follower, followed) do
First-time contributor

opencode-agent:job=3:generation=1

nit: this fetches the Follow activity a second time (after live_follow_activity?), and User.unfollow/2 fetches it a third time for its return value before ActivityPub.do_unfollow fetches it again. Threading the already-validated activity through would drop redundant indexed queries and close the small window where a concurrent cancellation between the checks makes the Undo reference an activity you did not just validate.

opencode-agent:job=3:generation=1 nit: this fetches the Follow activity a second time (after `live_follow_activity?`), and `User.unfollow/2` fetches it a third time for its return value before `ActivityPub.do_unfollow` fetches it again. Threading the already-validated activity through would drop redundant indexed queries and close the small window where a concurrent cancellation between the checks makes the Undo reference an activity you did not just validate.
@ -1188,0 +1187,4 @@
if live_follow_activity?(follower, followed) do
# The relationship row is gone but the Follow activity was never
# cancelled. Let the caller undo it so the stale state gets cleared.
with {:ok, follower} <- update_following_count(follower) do
First-time contributor

opencode-agent:job=3:generation=1

convergence nit: the clean path refreshes both sides via FollowingRelationship.after_update/3, but here only the follower's following_count is repaired. If the original crash landed between the row delete and User.update_follower_count(followed), the followed user's cached follower_count stays stale and this retry won't fix it. Consider refreshing both to match the clean-unfollow end state.

opencode-agent:job=3:generation=1 convergence nit: the clean path refreshes both sides via `FollowingRelationship.after_update/3`, but here only the follower's `following_count` is repaired. If the original crash landed between the row delete and `User.update_follower_count(followed)`, the followed user's cached `follower_count` stays stale and this retry won't fix it. Consider refreshing both to match the clean-unfollow end state.
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
This pull request is marked as a work in progress.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin fix/stale-follow-state:fix/stale-follow-state
git switch fix/stale-follow-state

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch develop
git merge --no-ff fix/stale-follow-state
git switch fix/stale-follow-state
git rebase develop
git switch develop
git merge --ff-only fix/stale-follow-state
git switch fix/stale-follow-state
git rebase develop
git switch develop
git merge --no-ff fix/stale-follow-state
git switch develop
git merge --squash fix/stale-follow-state
git switch develop
git merge --ff-only fix/stale-follow-state
git switch develop
git merge fix/stale-follow-state
git push origin develop
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
pleroma/pleroma!7986
No description provided.