Skip to content

Commit

Permalink
add report model
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Nov 27, 2024
1 parent 6a7a3d6 commit 91e7cc6
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 12 deletions.
12 changes: 11 additions & 1 deletion app/models/federails/moderation/report.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
class Federails::Moderation::Report
class Federails::Moderation::Report < ApplicationRecord
belongs_to :federails_actor, class_name: "Federails::Actor"
belongs_to :object, polymorphic: true

def resolve!
update!(resolved_at: DateTime.now, resolution: "resolved")
end

def ignore!
update!(resolved_at: DateTime.now, resolution: "ignored")
end
end
13 changes: 13 additions & 0 deletions db/migrate/20241127105043_create_federails_moderation_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateFederailsModerationReports < ActiveRecord::Migration[8.0]
def change
create_table :federails_moderation_reports do |t|
t.string :federated_url
t.references :federails_actor, null: false, foreign_key: true
t.string :content
t.references :object, polymorphic: true, null: false
t.datetime :resolved_at
t.string :resolution
t.timestamps
end
end
end
3 changes: 3 additions & 0 deletions lib/federails/moderation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@

module Federails
module Moderation
def self.table_name_prefix
"federails_moderation_"
end
end
end
42 changes: 31 additions & 11 deletions spec/dummy/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: 2024_11_26_114006) do
ActiveRecord::Schema[8.0].define(version: 2024_11_27_105043) do
create_table "federails_activities", force: :cascade do |t|
t.string "entity_type", null: false
t.integer "entity_id", null: false
Expand All @@ -19,9 +19,9 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "uuid"
t.index [ "actor_id" ], name: "index_federails_activities_on_actor_id"
t.index [ "entity_type", "entity_id" ], name: "index_federails_activities_on_entity"
t.index [ "uuid" ], name: "index_federails_activities_on_uuid", unique: true
t.index ["actor_id"], name: "index_federails_activities_on_actor_id"

Check failure on line 22 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 22 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
t.index ["entity_type", "entity_id"], name: "index_federails_activities_on_entity"

Check failure on line 23 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 23 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
t.index ["uuid"], name: "index_federails_activities_on_uuid", unique: true

Check failure on line 24 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 24 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
end

create_table "federails_actors", force: :cascade do |t|
Expand All @@ -41,9 +41,9 @@
t.string "uuid"
t.text "public_key"
t.text "private_key"
t.index [ "entity_type", "entity_id" ], name: "index_federails_actors_on_entity", unique: true
t.index [ "federated_url" ], name: "index_federails_actors_on_federated_url", unique: true
t.index [ "uuid" ], name: "index_federails_actors_on_uuid", unique: true
t.index ["entity_type", "entity_id"], name: "index_federails_actors_on_entity", unique: true

Check failure on line 44 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 44 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
t.index ["federated_url"], name: "index_federails_actors_on_federated_url", unique: true

Check failure on line 45 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 45 in spec/dummy/db/schema.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
t.index ["uuid"], name: "index_federails_actors_on_uuid", unique: true
end

create_table "federails_followings", force: :cascade do |t|
Expand All @@ -54,13 +54,33 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "uuid"
t.index [ "actor_id", "target_actor_id" ], name: "index_federails_followings_on_actor_id_and_target_actor_id", unique: true
t.index [ "actor_id" ], name: "index_federails_followings_on_actor_id"
t.index [ "target_actor_id" ], name: "index_federails_followings_on_target_actor_id"
t.index [ "uuid" ], name: "index_federails_followings_on_uuid", unique: true
t.index ["actor_id", "target_actor_id"], name: "index_federails_followings_on_actor_id_and_target_actor_id", unique: true
t.index ["actor_id"], name: "index_federails_followings_on_actor_id"
t.index ["target_actor_id"], name: "index_federails_followings_on_target_actor_id"
t.index ["uuid"], name: "index_federails_followings_on_uuid", unique: true
end

create_table "federails_moderation_reports", force: :cascade do |t|
t.string "federated_url"
t.integer "federails_actor_id", null: false
t.string "content"
t.string "object_type", null: false
t.integer "object_id", null: false
t.datetime "resolved_at"
t.string "resolution"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["federails_actor_id"], name: "index_federails_moderation_reports_on_federails_actor_id"
t.index ["object_type", "object_id"], name: "index_federails_moderation_reports_on_object"
end

create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_foreign_key "federails_activities", "federails_actors", column: "actor_id"
add_foreign_key "federails_followings", "federails_actors", column: "actor_id"
add_foreign_key "federails_followings", "federails_actors", column: "target_actor_id"
add_foreign_key "federails_moderation_reports", "federails_actors"
end
13 changes: 13 additions & 0 deletions spec/factories/actors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FactoryBot.define do
factory :distant_actor, class: 'Federails::Actor' do
entity { nil }
federated_url { "https://example.com/actors/#{rand(1...10_000)}" }
username { Faker::Internet.username separators: [ '-', '_' ] }
server { 'example.com' }
inbox_url { "#{federated_url}/inbox" }
outbox_url { "#{federated_url}/outbox" }
followers_url { "#{federated_url}/followers" }
followings_url { "#{federated_url}/followings" }
profile_url { "https://example.com/users/#{federated_url.split('/').last}" }
end
end
7 changes: 7 additions & 0 deletions spec/factories/reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :report, class: 'Federails::Moderation::Report' do
federails_actor { create :distant_actor }
object { create :user }
content { Faker::Lorem.sentence }
end
end
37 changes: 37 additions & 0 deletions spec/models/report_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
RSpec.describe Federails::Moderation::Report do
let(:actor) { create :distant_actor }
let(:object) { create :user }
let(:report) { create :report, federails_actor: actor, object: object }

it "is reported by an actor" do
expect(report.federails_actor).to eq actor
end

it "is associated with a polymorphic object" do
expect(report.object).to eq object
end

it "has text content" do
expect(report.content).to be_present
end

it "can be resolved" do
report.resolve!
expect(report.resolution).to eq "resolved"
end

it "can be ignored" do
report.ignore!
expect(report.resolution).to eq "ignored"
end

it "stores resolution timestamp when resolved" do
report.resolve!
expect(report.resolved_at).to be_present
end

it "stores resolution timestamp when ignored" do
report.ignore!
expect(report.resolved_at).to be_present
end
end

0 comments on commit 91e7cc6

Please sign in to comment.