diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7aff655d..9bf10ac7 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -23,11 +23,12 @@ class UsersController < ApplicationController if user_params[:password] == '' && user_params[:password_confirmation] == '' # not trying to change the password if @user.update_attributes(user_params.except(:password, :password_confirmation)) + update_follow_settings(@user, params[:settings]) @user.image = nil if params[:remove_image] == '1' @user.save sign_in(@user, bypass: true) respond_to do |format| - format.html { redirect_to root_url, notice: 'Account updated!' } + format.html { redirect_to root_url, notice: 'Settings updated' } end else sign_in(@user, bypass: true) @@ -40,11 +41,12 @@ class UsersController < ApplicationController correct_pass = @user.valid_password?(params[:current_password]) if correct_pass && @user.update_attributes(user_params) + update_follow_settings(@user, params[:settings]) @user.image = nil if params[:remove_image] == '1' @user.save sign_in(@user, bypass: true) respond_to do |format| - format.html { redirect_to root_url, notice: 'Account updated!' } + format.html { redirect_to root_url, notice: 'Settings updated' } end else respond_to do |format| @@ -104,9 +106,16 @@ class UsersController < ApplicationController private + def update_follow_settings(user, settings) + user.settings.follow_topic_on_created = settings[:follow_topic_on_created] + user.settings.follow_topic_on_contributed = settings[:follow_topic_on_contributed] + user.settings.follow_map_on_created = settings[:follow_map_on_created] + user.settings.follow_map_on_contributed = settings[:follow_map_on_contributed] + end + def user_params params.require(:user).permit( - :name, :email, :image, :password, :password_confirmation, :emails_allowed + :name, :email, :image, :password, :password_confirmation, :emails_allowed, :settings ) end end diff --git a/app/models/map.rb b/app/models/map.rb index a149a760..dd5e5604 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -39,7 +39,7 @@ class Map < ApplicationRecord # Validate the attached image is image/jpg, image/png, etc validates_attachment_content_type :screenshot, content_type: %r{\Aimage/.*\Z} - after_create :after_created_async + after_create :after_created after_update :after_updated after_save :update_deferring_topics_and_synapses, if: :permission_changed? @@ -140,11 +140,10 @@ class Map < ApplicationRecord protected - def after_created_async + def after_created FollowService.follow(self, self.user, 'created') # notify users following the map creator end - handle_asynchronously :after_created_async def after_updated return unless ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) } diff --git a/app/models/user.rb b/app/models/user.rb index 33ddc8d3..2c7ddf13 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -62,6 +62,12 @@ class User < ApplicationRecord maps: following.where(followed_type: 'Map').to_a.map(&:followed_id) } end + if (_options[:follow_settings]) + json['follow_topic_on_created'] = settings.follow_topic_on_created == "1" + json['follow_topic_on_contributed'] = settings.follow_topic_on_contributed == "1" + json['follow_map_on_created'] = settings.follow_map_on_created == "1" + json['follow_map_on_contributed'] = settings.follow_map_on_contributed == "1" + end if (_options[:email]) json['email'] = email end @@ -127,8 +133,10 @@ class User < ApplicationRecord end def settings - # make sure we always return a UserPreference instance self[:settings] = UserPreference.new if self[:settings].nil? + if not self[:settings].respond_to?(:follow_topic_on_created) + self[:settings].initialize_follow_settings + end self[:settings] end diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 9ea37532..c881c4ac 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class UserPreference - attr_accessor :metacodes, :metacode_focus + attr_accessor :metacodes, :metacode_focus, :follow_topic_on_created, :follow_topic_on_contributed, + :follow_map_on_created, :follow_map_on_contributed def initialize array = [] @@ -16,5 +17,13 @@ class UserPreference end @metacodes = array @metacode_focus = array[0] + initialize_follow_settings + end + + def initialize_follow_settings + @follow_topic_on_created = false + @follow_topic_on_contributed = false + @follow_map_on_created = false + @follow_map_on_contributed = false end end diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb index 53add1cf..83d86bcd 100644 --- a/app/services/follow_service.rb +++ b/app/services/follow_service.rb @@ -5,6 +5,8 @@ class FollowService return unless is_tester(user) + return unless should_auto_follow(entity, user, reason) + follow = Follow.where(followed: entity, user: user).first_or_create if FollowReason::REASONS.include?(reason) && !follow.follow_reason.read_attribute(reason) follow.follow_reason.update_attribute(reason, true) @@ -28,6 +30,22 @@ class FollowService protected + def should_auto_follow(entity, user, reason) + if entity.class == Topic + if reason == 'created' + return user.settings.follow_topic_on_created == '1' + elsif reason == 'contributed' + return user.settings.follow_topic_on_contributed == '1' + end + elsif entity.class == Map + if reason == 'created' + return user.settings.follow_map_on_created == '1' + elsif reason == 'contributed' + return user.settings.follow_map_contributed == '1' + end + end + end + def is_tester(user) %w(connorturland@gmail.com devin@callysto.com chessscholar@gmail.com solaureum@gmail.com ishanshapiro@gmail.com).include?(user.email) end diff --git a/app/views/layouts/_account.html.erb b/app/views/layouts/_account.html.erb index 3d66f687..94f69f62 100644 --- a/app/views/layouts/_account.html.erb +++ b/app/views/layouts/_account.html.erb @@ -10,7 +10,7 @@