From 06016453bd91882a53e91c11fc80f2c75fd474bb Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 7 Oct 2016 13:17:56 +0200 Subject: [PATCH] Adding user settings (model and mailer), no form yet --- Gemfile | 1 + Gemfile.lock | 3 +++ app/mailers/notification_mailer.rb | 5 ++++- app/models/user.rb | 4 ++++ ...20161006213403_rails_settings_migration.rb | 21 +++++++++++++++++++ db/schema.rb | 13 +++++++++++- 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20161006213403_rails_settings_migration.rb diff --git a/Gemfile b/Gemfile index cf9b34cea..f145531a3 100644 --- a/Gemfile +++ b/Gemfile @@ -38,6 +38,7 @@ gem 'simple_form' gem 'will_paginate' gem 'rack-attack' gem 'sidekiq' +gem 'ledermann-rails-settings' gem 'react-rails' gem 'browserify-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 8e33e85ce..e34f01ea7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -145,6 +145,8 @@ GEM json (1.8.3) launchy (2.4.3) addressable (~> 2.3) + ledermann-rails-settings (2.4.2) + activerecord (>= 3.1) letter_opener (1.4.1) launchy (~> 2.2) libv8 (3.16.14.15) @@ -364,6 +366,7 @@ DEPENDENCIES httplog jbuilder (~> 2.0) jquery-rails + ledermann-rails-settings letter_opener link_header lograge diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 1414c2295..6c81df269 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -1,11 +1,11 @@ class NotificationMailer < ApplicationMailer helper StreamEntriesHelper - helper AtomBuilderHelper def mention(mentioned_account, status) @me = mentioned_account @status = status + return unless @me.user.settings(:notification_emails).mention mail to: @me.user.email, subject: "You were mentioned by #{@status.account.acct}" end @@ -13,6 +13,7 @@ class NotificationMailer < ApplicationMailer @me = followed_account @account = follower + return unless @me.user.settings(:notification_emails).follow mail to: @me.user.email, subject: "#{@account.acct} is now following you" end @@ -21,6 +22,7 @@ class NotificationMailer < ApplicationMailer @account = from_account @status = target_status + return unless @me.user.settings(:notification_emails).favourite mail to: @me.user.email, subject: "#{@account.acct} favourited your status" end @@ -29,6 +31,7 @@ class NotificationMailer < ApplicationMailer @account = from_account @status = target_status + return unless @me.user.settings(:notification_emails).reblog mail to: @me.user.email, subject: "#{@account.acct} reblogged your status" end end diff --git a/app/models/user.rb b/app/models/user.rb index c1243e9ac..bd5512a31 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,4 +9,8 @@ class User < ApplicationRecord scope :prolific, -> { joins('inner join statuses on statuses.account_id = users.account_id').select('users.*, count(statuses.id) as statuses_count').group('users.id').order('statuses_count desc') } scope :recent, -> { order('created_at desc') } scope :admins, -> { where(admin: true) } + + has_settings do |s| + s.key :notification_emails, defaults: { follow: true, reblog: true, favourite: true, mention: true } + end end diff --git a/db/migrate/20161006213403_rails_settings_migration.rb b/db/migrate/20161006213403_rails_settings_migration.rb new file mode 100644 index 000000000..3bfd8af8e --- /dev/null +++ b/db/migrate/20161006213403_rails_settings_migration.rb @@ -0,0 +1,21 @@ +MIGRATION_BASE_CLASS = if ActiveRecord::VERSION::MAJOR >= 5 + ActiveRecord::Migration[5.0] +else + ActiveRecord::Migration +end + +class RailsSettingsMigration < MIGRATION_BASE_CLASS + def self.up + create_table :settings do |t| + t.string :var, :null => false + t.text :value + t.references :target, :null => false, :polymorphic => true + t.timestamps :null => true + end + add_index :settings, [ :target_type, :target_id, :var ], :unique => true + end + + def self.down + drop_table :settings + end +end diff --git a/db/schema.rb b/db/schema.rb index c4421bec7..6c21013b2 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: 20161003145426) do +ActiveRecord::Schema.define(version: 20161006213403) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -126,6 +126,17 @@ ActiveRecord::Schema.define(version: 20161003145426) do t.index ["uid"], name: "index_oauth_applications_on_uid", unique: true, using: :btree end + create_table "settings", force: :cascade do |t| + t.string "var", null: false + t.text "value" + t.string "target_type", null: false + t.integer "target_id", null: false + t.datetime "created_at" + t.datetime "updated_at" + t.index ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true, using: :btree + t.index ["target_type", "target_id"], name: "index_settings_on_target_type_and_target_id", using: :btree + end + create_table "statuses", force: :cascade do |t| t.string "uri" t.integer "account_id", null: false