Skip to content

Commit

Permalink
[E] Make comments bulk deletable
Browse files Browse the repository at this point in the history
  • Loading branch information
scryptmouse authored and 1aurend committed Dec 19, 2024
1 parent ad072de commit a1d76da
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 7 deletions.
6 changes: 5 additions & 1 deletion api/app/controllers/api/v1/bulk_deletions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ module V1
class BulkDeletionsController < ApplicationController
include MonadicControllerActions

authority_actions annotations: "bulk_delete", reading_groups: "bulk_delete", users: "bulk_delete"
authority_actions annotations: "bulk_delete", comments: "bulk_delete", reading_groups: "bulk_delete", users: "bulk_delete"

def annotations
bulk_delete! ::Annotation
end

def comments
bulk_delete! ::Comment
end

def reading_groups
bulk_delete! ::ReadingGroup
end
Expand Down
7 changes: 4 additions & 3 deletions api/app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Comment < ApplicationRecord
include FlaggableResource
include TrackedCreator
include Filterable
include SoftDeletable

# Scopes
scope :by_creator, ->(creator) { where(creator: creator) }
Expand All @@ -28,9 +29,9 @@ class Comment < ApplicationRecord
where(id: ids)
}

scope :with_flags, ->(value = nil) {
where(arel_table[:unresolved_flags_count].gteq(1)) if value.present?
}
scope :with_flags, ->(value = nil) do
where(arel_table[:unresolved_flags_count].gteq(1)) if value.present?
end

scope :with_order, ->(by = nil) do
case by
Expand Down
2 changes: 1 addition & 1 deletion api/app/models/concerns/soft_deletable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def soft_destroy_dependent_records!

case association.options[:dependent]
when :destroy
associated_records.find_each(&:soft_destroy)
associated_records.find_each(&:soft_delete)
when :delete_all
associated_records.sans_deleted.update_all(deleted_at: Time.current)
end
Expand Down
1 change: 1 addition & 0 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

scope as: :bulk_delete, controller: :bulk_deletions, path: "bulk_delete" do
delete :annotations
delete :comments
delete :reading_groups
delete :users
end
Expand Down
13 changes: 13 additions & 0 deletions api/db/migrate/20241218212943_make_comments_soft_deletable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class MakeCommentsSoftDeletable < ActiveRecord::Migration[6.1]
def change
change_table :comments do |t|
t.timestamp :deleted_at, null: true
t.timestamp :marked_for_purge_at, null: true

t.index :deleted_at
t.index :marked_for_purge_at
end
end
end
21 changes: 19 additions & 2 deletions api/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ CREATE TABLE public.comments (
events_count integer DEFAULT 0,
resolved_flags_count bigint DEFAULT 0 NOT NULL,
unresolved_flags_count bigint DEFAULT 0 NOT NULL,
flagger_ids uuid[] DEFAULT '{}'::uuid[] NOT NULL
flagger_ids uuid[] DEFAULT '{}'::uuid[] NOT NULL,
deleted_at timestamp without time zone,
marked_for_purge_at timestamp without time zone
);


Expand Down Expand Up @@ -4217,6 +4219,20 @@ CREATE INDEX index_comments_on_created_at ON public.comments USING brin (created
CREATE INDEX index_comments_on_creator_id ON public.comments USING btree (creator_id);


--
-- Name: index_comments_on_deleted_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_comments_on_deleted_at ON public.comments USING btree (deleted_at);


--
-- Name: index_comments_on_marked_for_purge_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_comments_on_marked_for_purge_at ON public.comments USING btree (marked_for_purge_at);


--
-- Name: index_comments_on_parent_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -7312,6 +7328,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20241206175512'),
('20241210200353'),
('20241212183245'),
('20241212214525');
('20241212214525'),
('20241218212943');


17 changes: 17 additions & 0 deletions api/spec/requests/api/v1/bulk_deletions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@
end
end

describe "DELETE /api/v1/bulk_delete/comments" do
let_it_be(:comment, refind: true) { FactoryBot.create :comment }
let_it_be(:subcomment, refind: true) { FactoryBot.create :comment, parent: comment }

let(:ids) { [comment.id] }

it "deletes the record and its children", :aggregate_failures do
expect do
delete(api_v1_bulk_delete_comments_path, **request_options)
end.to change(Comment.only_deleted, :count).by(2)
.and have_enqueued_job(SoftDeletions::PurgeJob).once.with(comment)
.and have_enqueued_job(SoftDeletions::PurgeJob).exactly(0).times.with(subcomment)

expect(response).to have_http_status :ok
end
end

describe "DELETE /api/v1/bulk_delete/reading_groups" do
let_it_be(:reading_group_1, refind: true) { FactoryBot.create :reading_group }
let_it_be(:reading_group_2, refind: true) { FactoryBot.create :reading_group }
Expand Down

0 comments on commit a1d76da

Please sign in to comment.