Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 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
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
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
63 changes: 63 additions & 0 deletions db/migrate/20251203164131_create_new_thread_followers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class CreateNewThreadFollowers < ActiveRecord::Migration[7.2]
def up
create_table_new_thread_followers
if column_exists?(:thread_followers, :post_id)
move_rows_with_non_nil_post_id
remove_post_id_column_from_thread_followers
end
end

def down
if !column_exists?(:thread_followers, :post_id)
add_post_id_column_to_thread_followers
end
move_rows_back_from_new_thread_followers
delete_table_new_thread_followers
end

def create_table_new_thread_followers
create_table :new_thread_followers, if_not_exists: true do |t|
t.bigint :user_id
t.bigint :post_id

t.timestamps
end
add_index :new_thread_followers, [:user_id, :post_id], if_not_exists: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add indexes here for user_id and post_id separately as well. We're likely to be querying for thread followers by post or by user as well as by both.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revision after discussion in Discord: let's add an additional index for :post_id - the composite index will work for searches on :user_id.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining that the composite index works differently in MySQL. I've now added the index on post_id (and the corresponding removal for rollback).

add_index :new_thread_followers, :post_id, if_not_exists: true
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)
.to_a
.map(&:attributes)
)
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, if_exists: true
end

def add_post_id_column_to_thread_followers
add_reference :thread_followers, :post, index: true, foreign_key: true, if_not_exists: true
end

def move_rows_back_from_new_thread_followers
ThreadFollower.insert_all(
NewThreadFollower.select(:user_id, :post_id, :created_at, :updated_at)
.to_a
.map(&:attributes)
)
NewThreadFollower.delete_all
end

def delete_table_new_thread_followers
remove_index :new_thread_followers, :post_id, if_exists: true
remove_index :new_thread_followers, [:user_id, :post_id], if_exists: true
remove_reference :new_thread_followers, :user_id, foreign_key: true, if_exists: true
remove_reference :new_thread_followers, :post_id, foreign_key: true, if_exists: true
drop_table :new_thread_followers, if_exists: 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
14 changes: 14 additions & 0 deletions test/fixtures/community_users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,17 @@ sample_post_scorer:
is_admin: false
is_moderator: false
reputation: 1

sample_merge_source:
user: merge_source
community: sample
is_admin: false
is_moderator: false
reputation: 42

sample_merge_target:
user: merge_target
community: sample
is_admin: false
is_moderator: false
reputation: 24
8 changes: 8 additions & 0 deletions test/fixtures/new_thread_followers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
standard_author_question_one:
user: standard_user
post: question_one

merge_source_question_one:
user: merge_source
post: question_one
6 changes: 3 additions & 3 deletions test/fixtures/thread_followers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ standard_author_normal:
user: standard_user
comment_thread: normal

standard_author_question_one:
user: standard_user
post: question_one
merge_source_normal:
user: merge_source
comment_thread: normal
16 changes: 16 additions & 0 deletions test/fixtures/user_abilities.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ pp_eo:
community_user: sample_post_scorer
ability: everyone

ms_eo:
community_user: sample_merge_source
ability: everyone

mt_eo:
community_user: sample_merge_target
ability: everyone

stu_ur:
community_user: sample_standard_user
ability: unrestricted
Expand Down Expand Up @@ -62,6 +70,14 @@ pp_ur:
community_user: sample_post_scorer
ability: unrestricted

ms_ur:
community_user: sample_merge_source
ability: unrestricted

mt_ur:
community_user: sample_merge_target
ability: unrestricted

c_fc:
community_user: sample_closer
ability: flag_close
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,17 @@ post_scorer:
sign_in_count: 1337
username: post_scorer
confirmed_at: 2020-01-01T00:00:00.000000Z

merge_source:
email: merge-source@example.com
encrypted_password: '$2a$11$roUHXKxecjyQ72Qn7DWs3.9eRCCoRn176kX/UNb/xiue3aGqf7xEW'
sign_in_count: 1337
username: merge_source
confirmed_at: 2020-01-01T00:00:00.000000Z

merge_target:
email: merge-target@example.com
encrypted_password: '$2a$11$roUHXKxecjyQ72Qn7DWs3.9eRCCoRn176kX/UNb/xiue3aGqf7xEW'
sign_in_count: 1337
username: merge_target
confirmed_at: 2020-01-01T00:00:00.000000Z
46 changes: 46 additions & 0 deletions test/models/concerns/user_merge_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'test_helper'

class UserMergeTest < ActiveSupport::TestCase
test 'merge_into should destroy the old user upon success' do
merger = users(:global_admin)
src_usr = users(:merge_source)
tgt_usr = users(:merge_target)

src_usr.merge_into(tgt_usr, merger)

assert_raises ActiveRecord::RecordNotFound do
src_usr.reload
end
end

test 'merge_info should move followed threads / posts to the target user' do
merger = users(:global_admin)
src_usr = users(:merge_source)
tgt_usr = users(:merge_target)

src_new_threads_followed = NewThreadFollower.where(user: src_usr)
src_new_threads_followed_ids = src_new_threads_followed.map(&:id)
assert src_new_threads_followed_ids.any?

src_threads_followed = ThreadFollower.where(user: src_usr)
src_threads_followed_ids = src_threads_followed.map(&:id)
assert src_threads_followed_ids.any?

src_usr.merge_into(tgt_usr, merger)

src_new_threads_followed.reload
src_threads_followed.reload

assert src_new_threads_followed.none?
assert src_threads_followed.none?

tgt_new_threads_followed = NewThreadFollower.where(id: src_new_threads_followed_ids)
tgt_threads_followed = ThreadFollower.where(id: src_threads_followed_ids)

assert tgt_new_threads_followed.any?
assert(tgt_new_threads_followed.all? { |a| a.user.same_as?(tgt_usr) })

assert tgt_threads_followed.any?
assert(tgt_threads_followed.all? { |a| a.user.same_as?(tgt_usr) })
end
end
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
Loading