Skip to content

Commit 38c6dc0

Browse files
authored
Merge pull request #1548 from codidact/MoshiKoi/1025/remove-special-case-notifying-author-of-threads
Remove special case notifying post author of threads
2 parents 40b9dea + 7e2010d commit 38c6dc0

7 files changed

Lines changed: 45 additions & 19 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CommentsController < ApplicationController
2525
before_action :check_lock_thread_access, only: [:lock_thread]
2626
before_action :check_thread_access, only: [:thread, :thread_content, :thread_followers]
2727
before_action :check_unrestrict_access, only: [:thread_unrestrict]
28-
before_action :check_if_target_post_locked, only: [:create, :post_follow]
28+
before_action :check_if_target_post_locked, only: [:create, :create_thread]
2929
before_action :check_if_parent_post_locked, only: [:update, :destroy]
3030
before_action :verify_moderator, only: [:thread_followers]
3131

@@ -56,12 +56,9 @@ def create_thread
5656

5757
if success
5858
notification = "New comment thread on #{@comment.root.title}: #{@comment_thread.title}"
59-
unless @comment.post.user.same_as?(current_user)
60-
@comment.post.user.create_notification(notification, helpers.comment_link(@comment))
61-
end
6259

6360
NewThreadFollower.where(post: @post).each do |ntf|
64-
unless ntf.user.same_as?(current_user) || ntf.user.same_as?(@comment.post.user)
61+
unless ntf.user.same_as?(current_user)
6562
ntf.user.create_notification(notification, helpers.comment_link(@comment))
6663
end
6764
ThreadFollower.create(user: ntf.user, comment_thread: @comment_thread)

app/controllers/posts_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def create
110110

111111
if @post.save
112112
@post.update(last_activity: @post.created_at, last_activity_by: current_user)
113+
NewThreadFollower.create user: @post.user, post: @post
113114
if @post_type.has_parent?
114115
unless @post.user_id == @post.parent.user_id
115116
@post.parent.user.create_notification("New response to your post #{@post.parent.title}",

app/models/comment_thread.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ class CommentThread < ApplicationRecord
1515
validate :maximum_title_length
1616
validates :title, presence: { message: I18n.t('comments.errors.title_presence') }
1717

18-
after_create :create_follower
19-
2018
before_save :bump_last_activity
2119

2220
# Gets threads appropriately scoped for a given user & post
@@ -122,15 +120,4 @@ def remove_follower(user)
122120

123121
ThreadFollower.where(comment_thread: self, user: user).destroy_all.any?
124122
end
125-
126-
private
127-
128-
# Comment author and post author are automatically followed to the thread. Question author is NOT
129-
# automatically followed on new answer comment threads. Comment author follower creation is done
130-
# on the Comment model.
131-
def create_follower
132-
if post.user.preference('auto_follow_comment_threads') == 'true'
133-
ThreadFollower.create comment_thread: self, user: post.user
134-
end
135-
end
136123
end

config/config/preferences.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ display_import_labels:
7171
auto_follow_comment_threads:
7272
type: boolean
7373
description: >
74-
Automatically follow any comment thread you participate in.
74+
Automatically follow any comment thread you create or participate in.
7575
default: 'true'
7676
global: true
7777

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class FollowOwnPosts < ActiveRecord::Migration[7.2]
2+
def change
3+
to_insert = Post.where.not(post_type_id: [PolicyDoc.post_type_id, HelpDoc.post_type_id])
4+
.where.not(user_id: nil)
5+
.pluck(:id, :user_id)
6+
.map { |post_id, user_id| { post_id: post_id, user_id: user_id } }
7+
8+
NewThreadFollower.insert_all(to_insert)
9+
end
10+
end

test/controllers/comments/post_follow_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,25 @@ class CommentsControllerTest < ActionController::TestCase
4949
# Assert user still only follows post once
5050
assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
5151
end
52+
53+
test 'post author can unfollow post then follow' do
54+
user = users(:standard_user)
55+
sign_in user
56+
question = posts(:question_one)
57+
58+
# Assert user follows post
59+
assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
60+
61+
try_post_unfollow(question)
62+
assert_response(:found)
63+
64+
# Assert user does not follow post
65+
assert_equal 0, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
66+
67+
try_post_follow(question)
68+
assert_response(:found)
69+
70+
# Assert user follows post
71+
assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', question, user]).count
72+
end
5273
end

test/controllers/posts/create_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ class PostsControllerTest < ActionController::TestCase
132132
assert_redirected_to_sign_in
133133
end
134134

135+
test 'should make post author a thread follower of the post' do
136+
user = users(:standard_user)
137+
sign_in user
138+
try_create_post
139+
assert_response(:found)
140+
141+
# Assert user follows post
142+
assert_equal 1, NewThreadFollower.where(['post_id = ? AND user_id = ?', assigns(:post), user]).count
143+
end
144+
135145
private
136146

137147
# Attempts to create a post

0 commit comments

Comments
 (0)