diff --git a/app/models/user.rb b/app/models/user.rb index f3407967..0c23c4cd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -49,6 +49,11 @@ class User < ApplicationRecord has_one :profile, dependent: :destroy has_one :theme, dependent: :destroy + has_many :user_bans, dependent: :destroy + has_many :banned_users, class_name: 'UserBan', + foreign_key: 'banned_by_id', + dependent: :nullify + SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i diff --git a/app/models/user_ban.rb b/app/models/user_ban.rb new file mode 100644 index 00000000..ee143412 --- /dev/null +++ b/app/models/user_ban.rb @@ -0,0 +1,4 @@ +class UserBan < ApplicationRecord + belongs_to :user + belongs_to :banned_by, class_name: 'User' +end diff --git a/db/migrate/20210814134115_create_user_bans.rb b/db/migrate/20210814134115_create_user_bans.rb new file mode 100644 index 00000000..a5146c90 --- /dev/null +++ b/db/migrate/20210814134115_create_user_bans.rb @@ -0,0 +1,28 @@ +class CreateUserBans < ActiveRecord::Migration[5.2] + def up + create_table :user_bans do |t| + t.bigint :user_id + t.string :reason + t.datetime :expires_at + t.bigint :banned_by_id, nullable: true + + t.timestamps + end + + # foxy's functional fqueries + execute "INSERT INTO user_bans + (user_id, reason, expires_at, created_at, updated_at) + SELECT users.id, users.ban_reason, users.banned_until, users.updated_at, NOW() FROM users + WHERE banned_until IS NOT NULL AND NOT permanently_banned;" + + + execute "INSERT INTO user_bans + (user_id, reason, expires_at, created_at, updated_at) + SELECT users.id, users.ban_reason, NULL, users.updated_at, NOW() FROM users + WHERE permanently_banned;" + end + + def down + drop_table :user_bans + end +end diff --git a/db/schema.rb b/db/schema.rb index a1294985..acb6f16a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -248,6 +248,15 @@ ActiveRecord::Schema.define(version: 2021_12_28_135426) do t.index ["user_id", "code"], name: "index_totp_recovery_codes_on_user_id_and_code" end + create_table "user_bans", force: :cascade do |t| + t.bigint "user_id" + t.string "reason" + t.datetime "expires_at" + t.bigint "banned_by_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", id: :bigint, default: -> { "gen_timestamp_id('users'::text)" }, force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false diff --git a/spec/models/user_ban_spec.rb b/spec/models/user_ban_spec.rb new file mode 100644 index 00000000..f7902eea --- /dev/null +++ b/spec/models/user_ban_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe UserBan, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end