Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
17986cd
Add NewThreadFollower model
trichoplax Dec 3, 2025
ffd7791
Amend code to use NewThreadFollower model instead
trichoplax Dec 3, 2025
8882785
Use single quotes for rubocop
trichoplax Dec 3, 2025
9a3e5c0
Include data migration and column removal in migration
trichoplax Dec 4, 2025
97c2649
Fix tests by rearranging fixtures for new table
trichoplax Dec 4, 2025
5da697b
Fix migration to remove moved rows and post reference
trichoplax Dec 4, 2025
f7c79f0
Add model tests for ThreadFollower and NewThreadFollower
trichoplax Dec 6, 2025
ac4f738
Tidying thanks to rubocop
trichoplax Dec 6, 2025
c82fd1f
Fix data insertion in NewThreadFollower migration
trichoplax Dec 7, 2025
a2af203
Defend migration against new_thread_followers table already existing
trichoplax Dec 7, 2025
5b77c12
Check if post_id column exists during NewThreadFollower migration
trichoplax Dec 7, 2025
5eb10d1
Add up and down to make NewThreadFollower migration reversible
trichoplax Dec 7, 2025
59ca21c
fixed typo in the create_new_thread_followers migration
Oaphi Dec 8, 2025
8d26314
made create_new_thread_followers more resilient to partial state
Oaphi Dec 8, 2025
c0acdf0
drop new_thread_followers table only if it exists
Oaphi Dec 8, 2025
b890d38
added thread follower tests for the user merge concern
Oaphi Dec 9, 2025
58848e8
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi Dec 9, 2025
c1b8251
Include specific post_id index in migration
trichoplax Dec 12, 2025
6d8e9a2
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
trichoplax Dec 12, 2025
74b2c2e
Rename migration to later before merging an earlier one
trichoplax Dec 26, 2025
550bf1f
Merge branch '0valt/1930/comments-last-activity' into trichoplax/sepa…
trichoplax Dec 26, 2025
0304650
Remove post_id condition since column has been removed
trichoplax Dec 26, 2025
b030287
Add job for new thread followers duplicates following split
trichoplax Dec 26, 2025
c19a3a7
Remove validation now that post_id column has been removed
trichoplax Dec 26, 2025
f94f99f
Add new runner script for new thread followers duplicate job
trichoplax Dec 26, 2025
393e1c1
Update schema following migration
trichoplax Dec 26, 2025
0117d76
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi Jan 10, 2026
9c7b116
added test for the CleanUpNewThreadFollowersJob job
Oaphi Jan 10, 2026
6a9472b
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi Jan 15, 2026
17f88ca
Use same_as? method instead of direct comparisons of users
trichoplax Jan 20, 2026
6e712b7
Add safe navigation consistency caught by rubocop
trichoplax Jan 20, 2026
8ca50ed
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi Jan 28, 2026
239aaa7
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi Jan 29, 2026
fb23244
rubocop fixes
Oaphi Jan 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def create_thread
@comment.post.user.create_notification(notification, helpers.comment_link(@comment))
end

ThreadFollower.where(post: @post).each do |tf|
unless tf.user == current_user || tf.user == @comment.post.user
tf.user.create_notification(notification, helpers.comment_link(@comment))
NewThreadFollower.where(post: @post).each do |ntf|
unless ntf.user == current_user || ntf.user == @comment.post.user
Comment thread
Oaphi marked this conversation as resolved.
Outdated
ntf.user.create_notification(notification, helpers.comment_link(@comment))
end
ThreadFollower.create(user: tf.user, comment_thread: @comment_thread)
ThreadFollower.create(user: ntf.user, comment_thread: @comment_thread)
end

apply_pings(pings)
Expand Down Expand Up @@ -330,8 +330,8 @@ def post
end

def post_follow
if ThreadFollower.where(post: @post, user: current_user).none?
ThreadFollower.create(post: @post, user: current_user)
if NewThreadFollower.where(post: @post, user: current_user).none?
NewThreadFollower.create(post: @post, user: current_user)
end

respond_to do |format|
Expand All @@ -341,7 +341,7 @@ def post_follow
end

def post_unfollow
ThreadFollower.where(post: @post, user: current_user).destroy_all
NewThreadFollower.where(post: @post, user: current_user).destroy_all

respond_to do |format|
format.html { redirect_to post_path(@post) }
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/user_merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def update_thread_references(target_user)
CommentThread.where(archived_by_id: id).update_all(archived_by_id: target_user.id)
CommentThread.where(deleted_by_id: id).update_all(deleted_by_id: target_user.id)
ThreadFollower.where(user_id: id).update_all(user_id: target_user.id)
NewThreadFollower.where(user_id: id).update_all(user_id: target_user.id)
end

def update_post_action_references(target_user)
Expand Down
4 changes: 4 additions & 0 deletions app/models/new_thread_follower.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class NewThreadFollower < ApplicationRecord
belongs_to :user
belongs_to :post
end
2 changes: 1 addition & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def deleted_by_owner?
# @param user [User] user to check
# @return [Boolean] check result
def followed_by?(user)
ThreadFollower.where(post: self, user: user).any?
NewThreadFollower.where(post: self, user: user).any?
end

# Is the post an imported post?
Expand Down
13 changes: 1 addition & 12 deletions app/models/thread_follower.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
class ThreadFollower < ApplicationRecord
belongs_to :comment_thread, optional: true
belongs_to :post, optional: true
belongs_to :comment_thread
belongs_to :user

validate :thread_or_post

private

def thread_or_post
if comment_thread.nil? && post.nil?
errors.add(:base, 'Must refer to either a comment thread or a post.')
end
end
end
28 changes: 28 additions & 0 deletions db/migrate/20251203164131_create_new_thread_followers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class CreateNewThreadFollowers < ActiveRecord::Migration[7.2]
def change
create_table_new_thread_followers
move_rows_with_non_nil_post_id
remove_post_id_column_from_thread_followers
end

def create_table_new_thread_followers
create_table :new_thread_followers do |t|
Comment thread
Oaphi marked this conversation as resolved.
Outdated
t.bigint :user_id
t.bigint :post_id

t.timestamps
end
add_index :new_thread_followers, [:user_id, :post_id]
end

def move_rows_with_non_nil_post_id
NewThreadFollower.insert_all(
ThreadFollower.select(:user_id, :post_id, :created_at, :updated_at).where.not(post_id:nil)
)
Comment thread
Oaphi marked this conversation as resolved.
ThreadFollower.where.not(post_id:nil).delete_all
end

def remove_post_id_column_from_thread_followers
remove_reference :thread_followers, :post, index: true, foreign_key: true
end
end
13 changes: 9 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2025_10_15_121326) do
ActiveRecord::Schema[7.2].define(version: 2025_12_03_164131) do
create_table "abilities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "community_id"
t.string "name"
Expand Down Expand Up @@ -401,6 +401,14 @@
t.index ["user_id"], name: "index_micro_auth_tokens_on_user_id"
end

create_table "new_thread_followers", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.bigint "user_id"
t.bigint "post_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id", "post_id"], name: "index_new_thread_followers_on_user_id_and_post_id"
end

create_table "notifications", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.text "content"
t.string "link"
Expand Down Expand Up @@ -723,9 +731,7 @@
t.bigint "user_id"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.bigint "post_id"
t.index ["comment_thread_id"], name: "index_thread_followers_on_comment_thread_id"
t.index ["post_id"], name: "index_thread_followers_on_post_id"
t.index ["user_id"], name: "index_thread_followers_on_user_id"
end

Expand Down Expand Up @@ -893,7 +899,6 @@
add_foreign_key "tag_synonyms", "tags"
add_foreign_key "tags", "communities"
add_foreign_key "tags", "tags", column: "parent_id"
add_foreign_key "thread_followers", "posts"
add_foreign_key "user_abilities", "abilities"
add_foreign_key "user_abilities", "community_users"
add_foreign_key "user_websites", "users"
Expand Down
12 changes: 6 additions & 6 deletions test/controllers/comments/post_follow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class CommentsControllerTest < ActionController::TestCase
question = posts(:question_one)

# Assert user follows post
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count

try_post_unfollow(question)
assert_response(:found)

# Assert user does not follow post
assert_equal 0, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
assert_equal 0, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
end

test 'non-follower can follow post' do
Expand All @@ -26,13 +26,13 @@ class CommentsControllerTest < ActionController::TestCase
question = posts(:question_one)

# Assert user does not follow post
assert_equal 0, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
assert_equal 0, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count

try_post_follow(question)
assert_response(:found)

# Assert user follows post
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
end

test 'follower cannot duplicate the following of a post' do
Expand All @@ -41,12 +41,12 @@ class CommentsControllerTest < ActionController::TestCase
question = posts(:question_one)

# Assert user follows post
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count

try_post_follow(question)
assert_response(:found)

# Assert user still only follows post once
assert_equal 1, ThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
end
end
4 changes: 4 additions & 0 deletions test/fixtures/new_thread_followers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
standard_author_question_one:
user: standard_user
post: question_one
4 changes: 0 additions & 4 deletions test/fixtures/thread_followers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ deleter_normal:
standard_author_normal:
user: standard_user
comment_thread: normal

standard_author_question_one:
user: standard_user
post: question_one
22 changes: 22 additions & 0 deletions test/models/new_thread_follower_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'test_helper'

class NewThreadFollowerTest < ActiveSupport::TestCase
test 'save succeeds with user and post' do
new_thread_follower = NewThreadFollower.new
new_thread_follower.user = users(:basic_user)
new_thread_follower.post = posts(:question_one)
assert new_thread_follower.save
end

test 'save fails without user' do
new_thread_follower = NewThreadFollower.new
new_thread_follower.post = posts(:question_one)
assert_not new_thread_follower.save
end

test 'save fails without post' do
new_thread_follower = NewThreadFollower.new
new_thread_follower.user = users(:basic_user)
assert_not new_thread_follower.save
end
end
21 changes: 18 additions & 3 deletions test/models/thread_follower_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
require 'test_helper'

class ThreadFollowerTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test 'save succeeds with user and thread' do
new_thread_follower = ThreadFollower.new
new_thread_follower.user = users(:basic_user)
new_thread_follower.comment_thread = comment_threads(:normal)
assert new_thread_follower.save
end

test 'save fails without user' do
new_thread_follower = ThreadFollower.new
new_thread_follower.comment_thread = comment_threads(:normal)
assert_not new_thread_follower.save
end

test 'save fails without thread' do
new_thread_follower = ThreadFollower.new
new_thread_follower.user = users(:basic_user)
assert_not new_thread_follower.save
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
WarningTemplate,
ModWarning,
ThreadFollower,
NewThreadFollower,
Comment,
CommentThread,
Reaction,
Expand Down