Skip to content

Commit c6b7913

Browse files
committed
Add comment threads REST API
Previewers can fetch comment threads with comments for an entry's draft revision. This provides the data for rendering comment badges on content elements in the entry preview. REDMINE-21261
1 parent 3e008eb commit c6b7913

File tree

14 files changed

+246
-0
lines changed

14 files changed

+246
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Pageflow
2+
module Review
3+
# @api private
4+
class CommentThreadsController < Pageflow::ApplicationController
5+
respond_to :json
6+
before_action :authenticate_user!
7+
8+
def index
9+
entry = DraftEntry.find(params[:entry_id])
10+
authorize!(:read, entry.to_model)
11+
12+
@comment_threads = entry.comment_threads.includes(comments: :creator)
13+
end
14+
end
15+
end
16+
end

app/models/pageflow/comment.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Pageflow
2+
# @api private
3+
class Comment < ApplicationRecord
4+
include NestedRevisionComponent
5+
6+
belongs_to :comment_thread
7+
belongs_to :creator, class_name: 'User'
8+
9+
validates :body, presence: true
10+
11+
def entry_for_auto_generated_perma_id
12+
comment_thread.revision.entry
13+
end
14+
end
15+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Pageflow
2+
# @api private
3+
class CommentThread < ApplicationRecord
4+
include RevisionComponent
5+
6+
belongs_to :creator, class_name: 'User'
7+
has_many :comments, dependent: :destroy
8+
9+
nested_revision_components :comments
10+
11+
validates :subject_type, :subject_id, presence: true
12+
end
13+
end

app/models/pageflow/entry_at_revision.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def initialize(entry, revision, theme: nil)
3838
:author, :publisher, :keywords,
3939
:published_at,
4040
:noindex?,
41+
:comment_threads,
4142
:configuration,
4243
:structured_data_type_name,
4344
to: :revision)

app/models/pageflow/revision.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Revision < ApplicationRecord # rubocop:todo Style/Documentation
2727
has_many :chapters, -> { order(CHAPTER_ORDER) }, through: :storylines
2828
has_many :pages, -> { reorder(PAGE_ORDER) }, through: :storylines
2929

30+
has_many :comment_threads, dependent: :destroy
31+
3032
has_many :file_usages, dependent: :destroy
3133

3234
has_many :image_files, -> { extending WithFileUsageExtension },
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
json.key_format!(camelize: :lower)
2+
3+
json.call(comment_thread,
4+
:id,
5+
:perma_id,
6+
:subject_type,
7+
:subject_id,
8+
:creator_id,
9+
:resolved_at,
10+
:created_at,
11+
:updated_at)
12+
13+
json.comments(comment_thread.comments) do |comment|
14+
json.partial!('pageflow/review/comments/comment', comment:)
15+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
json.key_format!(camelize: :lower)
2+
3+
json.current_user do
4+
json.id current_user.id
5+
json.name current_user.full_name
6+
end
7+
8+
json.comment_threads(@comment_threads) do |comment_thread|
9+
json.partial!('pageflow/review/comment_threads/comment_thread', comment_thread:)
10+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
json.key_format!(camelize: :lower)
2+
3+
json.call(comment,
4+
:id,
5+
:perma_id,
6+
:creator_id,
7+
:body,
8+
:created_at,
9+
:updated_at)
10+
11+
json.creator_name comment.creator.full_name

config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
resource :oembed, only: [:show]
7878
end
7979

80+
namespace :review do
81+
resources :entries, only: [] do
82+
resources :comment_threads, only: [:index]
83+
end
84+
end
85+
8086
root to: redirect('/admin')
8187
end
8288

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class CreateCommentThreadsAndComments < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :pageflow_comment_threads do |t|
4+
t.integer :revision_id, null: false
5+
t.integer :perma_id
6+
t.string :subject_type, null: false
7+
t.integer :subject_id, null: false
8+
t.integer :creator_id, null: false
9+
t.datetime :resolved_at
10+
t.integer :resolved_by_id
11+
t.timestamps
12+
end
13+
14+
add_index :pageflow_comment_threads, :revision_id
15+
add_index :pageflow_comment_threads, :creator_id
16+
add_index :pageflow_comment_threads, [:revision_id, :subject_type, :subject_id],
17+
name: 'index_comment_threads_on_revision_and_subject'
18+
19+
create_table :pageflow_comments do |t|
20+
t.integer :comment_thread_id, null: false
21+
t.integer :perma_id
22+
t.integer :creator_id, null: false
23+
t.text :body, null: false
24+
t.timestamps
25+
end
26+
27+
add_index :pageflow_comments, :comment_thread_id
28+
add_index :pageflow_comments, :creator_id
29+
end
30+
end

0 commit comments

Comments
 (0)