Skip to content

Commit

Permalink
0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtOfCode- committed Sep 23, 2022
1 parent a416c45 commit 5ef877b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
23 changes: 20 additions & 3 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CommentsController < ApplicationController
before_action :set_comment, only: [:update, :destroy, :undelete, :show]
before_action :set_thread, only: [:thread, :thread_rename, :thread_restrict, :thread_unrestrict, :thread_followers]
before_action :check_privilege, only: [:update, :destroy, :undelete]
before_action :check_if_target_post_locked, only: [:create]
before_action :check_if_target_post_locked, only: [:create, :post_follow]
before_action :check_if_parent_post_locked, only: [:update, :destroy]

def create_thread
Expand Down Expand Up @@ -40,9 +40,16 @@ def create_thread
end

if success
notification = "New comment thread on #{@comment.root.title}: #{@comment_thread.title}"
unless @comment.post.user == current_user
@comment.post.user.create_notification("New comment thread on #{@comment.root.title}: #{@comment_thread.title}",
helpers.comment_link(@comment))
@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))
end
ThreadFollower.create(user: tf.user, comment_thread: @comment_thread)
end

apply_pings pings
Expand Down Expand Up @@ -245,6 +252,16 @@ def post
end
end

def post_follow
@post = Post.find(params[:post_id])
if CommentThread.post_followed?(@post, current_user)
ThreadFollower.where(post: @post, user: current_user).destroy_all
else
ThreadFollower.create(post: @post, user: current_user)
end
redirect_to post_path(@post)
end

def pingable
thread = params[:id] == '-1' ? CommentThread.new(post_id: params[:post]) : CommentThread.find(params[:id])
ids = helpers.get_pingable(thread)
Expand Down
4 changes: 4 additions & 0 deletions app/models/comment_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def can_access?(user)
post.can_access?(user)
end

def self.post_followed?(post, user)
ThreadFollower.where(post: post, user: user).any?
end

private

# Comment author and post author are automatically followed to the thread. Question author is NOT
Expand Down
13 changes: 12 additions & 1 deletion app/models/thread_follower.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
class ThreadFollower < ApplicationRecord
belongs_to :comment_thread
belongs_to :comment_thread, optional: true
belongs_to :post, optional: true
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
15 changes: 15 additions & 0 deletions app/views/posts/_expanded.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,21 @@
<h4 class="has-margin-0">
<%= pluralize(public_count, 'comment thread') %>
</h4>
<% if user_signed_in? %>
<p class="has-font-size-caption">
<% if CommentThread.post_followed?(post, current_user) %>
<%= link_to follow_post_comments_path(post_id: post.id), method: :post,
title: 'Don\'t follow new comment threads on this post' do %>
<i class="fas fa-fw fa-minus"></i> unfollow new
<% end %>
<% else %>
<%= link_to follow_post_comments_path(post_id: post.id), method: :post,
title: 'Follow all new comment threads on this post' do %>
<i class="fas fa-fw fa-plus"></i> follow new
<% end %>
<% end %>
</p>
<% end %>
<div class="post--comments-container">
<%= render 'comments/post', comment_threads: comment_threads.first(5) %>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
post 'thread/:id/unrestrict', to: 'comments#thread_unrestrict', as: :unrestrict_comment_thread
get 'thread/:id/followers', to: 'comments#thread_followers', as: :comment_thread_followers
get 'post/:post_id', to: 'comments#post', as: :post_comments
post 'post/:post_id/follow', to: 'comments#post_follow', as: :follow_post_comments
get ':id', to: 'comments#show', as: :comment
get 'thread/:id', to: 'comments#thread', as: :comment_thread
post ':id/edit', to: 'comments#update', as: :update_comment
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20220913183826_add_post_to_thread_followers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddPostToThreadFollowers < ActiveRecord::Migration[7.0]
def change
add_reference :thread_followers, :post, null: true, foreign_key: true
change_column_null :thread_followers, :comment_thread_id, true
end
end
5 changes: 4 additions & 1 deletion 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.0].define(version: 2022_09_03_174045) do
ActiveRecord::Schema[7.0].define(version: 2022_09_13_183826) 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 @@ -614,7 +614,9 @@
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 @@ -761,6 +763,7 @@
add_foreign_key "suggested_edits", "users", column: "decided_by_id"
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 "users", "users", column: "deleted_by_id"
Expand Down

0 comments on commit 5ef877b

Please sign in to comment.