diff options
author | Claire <claire.github-309c@sitedethib.com> | 2022-07-17 23:10:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-17 23:10:31 +0200 |
commit | ab1488a6ad93f572e1d184cb9653f76fd408474f (patch) | |
tree | f1dd126f4745eb99243f390169fb957a72f5acc6 /db | |
parent | 325ebb76b19bd20e1b76d8bc26c11cab02f6571c (diff) | |
parent | 6c0d73a675d62f676b005c06593fd69e9a7bc0e5 (diff) |
Merge pull request #1804 from ClearlyClaire/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20220611210335_create_user_roles.rb | 13 | ||||
-rw-r--r-- | db/migrate/20220611212541_add_role_id_to_users.rb | 8 | ||||
-rw-r--r-- | db/migrate/20220710102457_add_display_name_to_tags.rb | 5 | ||||
-rw-r--r-- | db/migrate/20220714171049_create_tag_follows.rb | 12 | ||||
-rw-r--r-- | db/post_migrate/20220617202502_migrate_roles.rb | 26 | ||||
-rw-r--r-- | db/post_migrate/20220704024901_migrate_settings_to_user_roles.rb | 41 | ||||
-rw-r--r-- | db/schema.rb | 27 | ||||
-rw-r--r-- | db/seeds.rb | 12 | ||||
-rw-r--r-- | db/seeds/01_web_app.rb | 1 | ||||
-rw-r--r-- | db/seeds/02_instance_actor.rb | 1 | ||||
-rw-r--r-- | db/seeds/03_roles.rb | 9 | ||||
-rw-r--r-- | db/seeds/04_admin.rb | 8 |
12 files changed, 153 insertions, 10 deletions
diff --git a/db/migrate/20220611210335_create_user_roles.rb b/db/migrate/20220611210335_create_user_roles.rb new file mode 100644 index 000000000..6b7f2b637 --- /dev/null +++ b/db/migrate/20220611210335_create_user_roles.rb @@ -0,0 +1,13 @@ +class CreateUserRoles < ActiveRecord::Migration[6.1] + def change + create_table :user_roles do |t| + t.string :name, null: false, default: '' + t.string :color, null: false, default: '' + t.integer :position, null: false, default: 0 + t.bigint :permissions, null: false, default: 0 + t.boolean :highlighted, null: false, default: false + + t.timestamps + end + end +end diff --git a/db/migrate/20220611212541_add_role_id_to_users.rb b/db/migrate/20220611212541_add_role_id_to_users.rb new file mode 100644 index 000000000..2fda647d4 --- /dev/null +++ b/db/migrate/20220611212541_add_role_id_to_users.rb @@ -0,0 +1,8 @@ +class AddRoleIdToUsers < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + def change + safety_assured { add_reference :users, :role, foreign_key: { to_table: 'user_roles', on_delete: :nullify }, index: false } + add_index :users, :role_id, algorithm: :concurrently, where: 'role_id IS NOT NULL' + end +end diff --git a/db/migrate/20220710102457_add_display_name_to_tags.rb b/db/migrate/20220710102457_add_display_name_to_tags.rb new file mode 100644 index 000000000..aa7867645 --- /dev/null +++ b/db/migrate/20220710102457_add_display_name_to_tags.rb @@ -0,0 +1,5 @@ +class AddDisplayNameToTags < ActiveRecord::Migration[6.1] + def change + add_column :tags, :display_name, :string + end +end diff --git a/db/migrate/20220714171049_create_tag_follows.rb b/db/migrate/20220714171049_create_tag_follows.rb new file mode 100644 index 000000000..a393e90f5 --- /dev/null +++ b/db/migrate/20220714171049_create_tag_follows.rb @@ -0,0 +1,12 @@ +class CreateTagFollows < ActiveRecord::Migration[6.1] + def change + create_table :tag_follows do |t| + t.belongs_to :tag, null: false, foreign_key: { on_delete: :cascade } + t.belongs_to :account, null: false, foreign_key: { on_delete: :cascade }, index: false + + t.timestamps + end + + add_index :tag_follows, [:account_id, :tag_id], unique: true + end +end diff --git a/db/post_migrate/20220617202502_migrate_roles.rb b/db/post_migrate/20220617202502_migrate_roles.rb new file mode 100644 index 000000000..b7a7b2201 --- /dev/null +++ b/db/post_migrate/20220617202502_migrate_roles.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class MigrateRoles < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + class UserRole < ApplicationRecord; end + class User < ApplicationRecord; end + + def up + load Rails.root.join('db', 'seeds', '03_roles.rb') + + admin_role = UserRole.find_by(name: 'Admin') + moderator_role = UserRole.find_by(name: 'Moderator') + + User.where(admin: true).in_batches.update_all(role_id: admin_role.id) + User.where(moderator: true).in_batches.update_all(role_id: moderator_role.id) + end + + def down + admin_role = UserRole.find_by(name: 'Admin') + moderator_role = UserRole.find_by(name: 'Moderator') + + User.where(role_id: admin_role.id).in_batches.update_all(admin: true) if admin_role + User.where(role_id: moderator_role.id).in_batches.update_all(moderator: true) if moderator_role + end +end diff --git a/db/post_migrate/20220704024901_migrate_settings_to_user_roles.rb b/db/post_migrate/20220704024901_migrate_settings_to_user_roles.rb new file mode 100644 index 000000000..254690cc3 --- /dev/null +++ b/db/post_migrate/20220704024901_migrate_settings_to_user_roles.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +class MigrateSettingsToUserRoles < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + class UserRole < ApplicationRecord; end + + def up + owner_role = UserRole.find_by(name: 'Owner') + admin_role = UserRole.find_by(name: 'Admin') + moderator_role = UserRole.find_by(name: 'Moderator') + everyone_role = UserRole.find_by(id: -99) + + min_invite_role = Setting.min_invite_role + show_staff_badge = Setting.show_staff_badge + + if everyone_role + everyone_role.permissions &= ~::UserRole::FLAGS[:invite_users] unless min_invite_role == 'user' + everyone_role.save + end + + if owner_role + owner_role.highlighted = show_staff_badge + owner_role.save + end + + if admin_role + admin_role.permissions |= ::UserRole::FLAGS[:invite_users] if %w(admin moderator).include?(min_invite_role) + admin_role.highlighted = show_staff_badge + admin_role.save + end + + if moderator_role + moderator_role.permissions |= ::UserRole::FLAGS[:invite_users] if %w(moderator).include?(min_invite_role) + moderator_role.highlighted = show_staff_badge + moderator_role.save + end + end + + def down; end +end diff --git a/db/schema.rb b/db/schema.rb index 8a1565e97..7a8262dfa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_06_13_110903) do +ActiveRecord::Schema.define(version: 2022_07_14_171049) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -931,6 +931,15 @@ ActiveRecord::Schema.define(version: 2022_06_13_110903) do t.datetime "updated_at", null: false end + create_table "tag_follows", force: :cascade do |t| + t.bigint "tag_id", null: false + t.bigint "account_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["account_id", "tag_id"], name: "index_tag_follows_on_account_id_and_tag_id", unique: true + t.index ["tag_id"], name: "index_tag_follows_on_tag_id" + end + create_table "tags", force: :cascade do |t| t.string "name", default: "", null: false t.datetime "created_at", null: false @@ -943,6 +952,7 @@ ActiveRecord::Schema.define(version: 2022_06_13_110903) do t.datetime "last_status_at" t.float "max_score" t.datetime "max_score_at" + t.string "display_name" t.index "lower((name)::text) text_pattern_ops", name: "index_tags_on_name_lower_btree", unique: true end @@ -971,6 +981,16 @@ ActiveRecord::Schema.define(version: 2022_06_13_110903) do t.index ["user_id"], name: "index_user_invite_requests_on_user_id" end + create_table "user_roles", force: :cascade do |t| + t.string "name", default: "", null: false + t.string "color", default: "", null: false + t.integer "position", default: 0, null: false + t.bigint "permissions", default: 0, null: false + t.boolean "highlighted", default: false, null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.datetime "created_at", null: false @@ -1006,11 +1026,13 @@ ActiveRecord::Schema.define(version: 2022_06_13_110903) do t.string "webauthn_id" t.inet "sign_up_ip" t.boolean "skip_sign_in_token" + t.bigint "role_id" t.index ["account_id"], name: "index_users_on_account_id" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["created_by_application_id"], name: "index_users_on_created_by_application_id", where: "(created_by_application_id IS NOT NULL)" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, opclass: :text_pattern_ops, where: "(reset_password_token IS NOT NULL)" + t.index ["role_id"], name: "index_users_on_role_id", where: "(role_id IS NOT NULL)" end create_table "web_push_subscriptions", force: :cascade do |t| @@ -1157,11 +1179,14 @@ ActiveRecord::Schema.define(version: 2022_06_13_110903) do add_foreign_key "statuses", "statuses", column: "reblog_of_id", on_delete: :cascade add_foreign_key "statuses_tags", "statuses", on_delete: :cascade add_foreign_key "statuses_tags", "tags", name: "fk_3081861e21", on_delete: :cascade + add_foreign_key "tag_follows", "accounts", on_delete: :cascade + add_foreign_key "tag_follows", "tags", on_delete: :cascade add_foreign_key "tombstones", "accounts", on_delete: :cascade add_foreign_key "user_invite_requests", "users", on_delete: :cascade add_foreign_key "users", "accounts", name: "fk_50500f500d", on_delete: :cascade add_foreign_key "users", "invites", on_delete: :nullify add_foreign_key "users", "oauth_applications", column: "created_by_application_id", on_delete: :nullify + add_foreign_key "users", "user_roles", column: "role_id", on_delete: :nullify add_foreign_key "web_push_subscriptions", "oauth_access_tokens", column: "access_token_id", on_delete: :cascade add_foreign_key "web_push_subscriptions", "users", on_delete: :cascade add_foreign_key "web_settings", "users", name: "fk_11910667b2", on_delete: :cascade diff --git a/db/seeds.rb b/db/seeds.rb index 0bfb5d0db..1ca300de7 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,11 +1,5 @@ -Doorkeeper::Application.create!(name: 'Web', superapp: true, redirect_uri: Doorkeeper.configuration.native_redirect_uri, scopes: 'read write follow push') +# frozen_string_literal: true -domain = ENV['LOCAL_DOMAIN'] || Rails.configuration.x.local_domain -account = Account.find_or_initialize_by(id: -99, actor_type: 'Application', locked: true, username: domain) -account.save! - -if Rails.env.development? - admin = Account.where(username: 'admin').first_or_initialize(username: 'admin') - admin.save(validate: false) - User.where(email: "admin@#{domain}").first_or_initialize(email: "admin@#{domain}", password: 'mastodonadmin', password_confirmation: 'mastodonadmin', confirmed_at: Time.now.utc, admin: true, account: admin, agreement: true, approved: true).save! +Dir[Rails.root.join('db', 'seeds', '*.rb')].sort.each do |seed| + load seed end diff --git a/db/seeds/01_web_app.rb b/db/seeds/01_web_app.rb new file mode 100644 index 000000000..a457a883b --- /dev/null +++ b/db/seeds/01_web_app.rb @@ -0,0 +1 @@ +Doorkeeper::Application.create_with(name: 'Web', redirect_uri: Doorkeeper.configuration.native_redirect_uri, scopes: 'read write follow push').find_or_create_by(superapp: true) diff --git a/db/seeds/02_instance_actor.rb b/db/seeds/02_instance_actor.rb new file mode 100644 index 000000000..39186b273 --- /dev/null +++ b/db/seeds/02_instance_actor.rb @@ -0,0 +1 @@ +Account.create_with(actor_type: 'Application', locked: true, username: ENV['LOCAL_DOMAIN'] || Rails.configuration.x.local_domain).find_or_create_by(id: -99) diff --git a/db/seeds/03_roles.rb b/db/seeds/03_roles.rb new file mode 100644 index 000000000..7fedf0f71 --- /dev/null +++ b/db/seeds/03_roles.rb @@ -0,0 +1,9 @@ +# Pre-create base role +UserRole.everyone + +# Create default roles defined in config file +default_roles = YAML.load_file(Rails.root.join('config', 'roles.yml')) + +default_roles.each do |_, config| + UserRole.create_with(position: config['position'], permissions_as_keys: config['permissions'], highlighted: true).find_or_create_by(name: config['name']) +end diff --git a/db/seeds/04_admin.rb b/db/seeds/04_admin.rb new file mode 100644 index 000000000..a67040e4e --- /dev/null +++ b/db/seeds/04_admin.rb @@ -0,0 +1,8 @@ +if Rails.env.development? + domain = ENV['LOCAL_DOMAIN'] || Rails.configuration.x.local_domain + + admin = Account.where(username: 'admin').first_or_initialize(username: 'admin') + admin.save(validate: false) + + User.where(email: "admin@#{domain}").first_or_initialize(email: "admin@#{domain}", password: 'mastodonadmin', password_confirmation: 'mastodonadmin', confirmed_at: Time.now.utc, role: UserRole.find_by(name: 'Owner'), account: admin, agreement: true, approved: true).save! +end |