From 9b2193ad8c72e57de42e81ed0946822b03c8b164 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Sun, 3 Sep 2017 15:11:52 -0400 Subject: [PATCH] show autofollow on client and mute on unfollow --- app/models/user.rb | 4 ++-- app/services/follow_service.rb | 6 +++++- app/services/notification_service.rb | 3 ++- db/migrate/20170903180840_add_muted_to_follows.rb | 5 +++++ db/schema.rb | 3 ++- frontend/src/Metamaps/DataModel/Topic.js | 4 ++++ 6 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20170903180840_add_muted_to_follows.rb diff --git a/app/models/user.rb b/app/models/user.rb index bb22f972..feac3d08 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -58,8 +58,8 @@ class User < ApplicationRecord admin: admin } if (_options[:follows]) json['follows'] = { - topics: following.where(followed_type: 'Topic').to_a.map(&:followed_id), - maps: following.where(followed_type: 'Map').to_a.map(&:followed_id) + topics: following.where(muted: false, followed_type: 'Topic').to_a.map(&:followed_id), + maps: following.where(muted: false, followed_type: 'Map').to_a.map(&:followed_id) } end if (_options[:follow_settings]) diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb index bab6b6fb..be70cf84 100644 --- a/app/services/follow_service.rb +++ b/app/services/follow_service.rb @@ -8,13 +8,15 @@ class FollowService return if (reason == 'created' || reason == 'contributed') && !should_auto_follow(entity, user, reason) follow = Follow.where(followed: entity, user: user).first_or_create + follow.update_attribute('muted', false) if FollowReason::REASONS.include?(reason) && !follow.follow_reason.read_attribute(reason) follow.follow_reason.update_attribute(reason, true) end end def unfollow(entity, user) - Follow.where(followed: entity, user: user).destroy_all + follow = Follow.where(followed: entity, user: user).first + follow.update_attribute('muted', true) end def remove_reason(entity, user, reason) @@ -31,6 +33,8 @@ class FollowService protected def should_auto_follow(entity, user, reason) + follow = Follow.where(followed: entity, user: user).first + return false if follow && follow.muted if entity.class == Topic if reason == 'created' return user.settings.follow_topic_on_created == '1' diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index c587e9ed..1db48e52 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -51,7 +51,8 @@ class NotificationService def self.notify_followers(entity, event_type, event, reason_filter = nil, exclude_follows = nil) follows = entity.follows.where.not(user_id: event.user.id) - + follows = follows.where(muted: false) + if !exclude_follows.nil? follows = follows.where.not(id: exclude_follows) end diff --git a/db/migrate/20170903180840_add_muted_to_follows.rb b/db/migrate/20170903180840_add_muted_to_follows.rb new file mode 100644 index 00000000..31649639 --- /dev/null +++ b/db/migrate/20170903180840_add_muted_to_follows.rb @@ -0,0 +1,5 @@ +class AddMutedToFollows < ActiveRecord::Migration[5.0] + def change + add_column :follows, :muted, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 21a2447b..6a6d3097 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: 20170209215911) do +ActiveRecord::Schema.define(version: 20170903180840) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -86,6 +86,7 @@ ActiveRecord::Schema.define(version: 20170209215911) do t.integer "followed_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "muted" t.index ["followed_type", "followed_id"], name: "index_follows_on_followed_type_and_followed_id", using: :btree t.index ["user_id"], name: "index_follows_on_user_id", using: :btree end diff --git a/frontend/src/Metamaps/DataModel/Topic.js b/frontend/src/Metamaps/DataModel/Topic.js index 10acc5b1..29c271d4 100644 --- a/frontend/src/Metamaps/DataModel/Topic.js +++ b/frontend/src/Metamaps/DataModel/Topic.js @@ -105,6 +105,10 @@ const Topic = Backbone.Model.extend({ var onPageWithTopicCard = Active.Map || Active.Topic var node = this.get('node') + if (Active.Mapper.get('follow_topic_on_contributed')) { + Active.Mapper.followTopic(this.id) + } + // update the node on the map if (onPageWithTopicCard && node) { node.name = this.get('name')