From d23ec096e7046f3b34262f11a6f0a77e9f5bfc48 Mon Sep 17 00:00:00 2001 From: Marcos Ronaldo Date: Thu, 9 Jun 2016 11:32:19 -0300 Subject: [PATCH] Adds backend to profile followers feature --- app/helpers/action_tracker_helper.rb | 16 +++++++++++++--- app/jobs/notify_activity_to_profiles_job.rb | 3 +++ app/models/person.rb | 7 ++++++- app/models/profile.rb | 10 +++++++++- app/models/profile_follower.rb | 9 +++++++++ app/views/profile/_new_follower.html.erb | 1 + config/initializers/action_tracker.rb | 4 ++++ db/migrate/20160608123748_create_profile_followers_table.rb | 8 ++++++++ 8 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 app/models/profile_follower.rb create mode 100644 app/views/profile/_new_follower.html.erb create mode 100644 db/migrate/20160608123748_create_profile_followers_table.rb diff --git a/app/helpers/action_tracker_helper.rb b/app/helpers/action_tracker_helper.rb index ef21767..f8b0885 100644 --- a/app/helpers/action_tracker_helper.rb +++ b/app/helpers/action_tracker_helper.rb @@ -14,13 +14,23 @@ module ActionTrackerHelper } end + def new_follower_description ta + n_('has followed 1 new profile:
%{name}', 'has followed %{num} new profiles:
%{name}', ta.get_follower_name.size).html_safe % { + num: ta.get_follower_name.size, + name: safe_join(ta.collect_group_with_index(:follower_name) do |n,i| + link_to image_tag(ta.get_follower_profile_custom_icon[i] || default_or_themed_icon("/images/icons-app/person-icon.png")), + ta.get_follower_url[i], title: n + end) + } + end + def join_community_description ta - n_('has joined 1 community:
%{name}'.html_safe, 'has joined %{num} communities:
%{name}'.html_safe, ta.get_resource_name.size) % { + n_('has joined 1 community:
%{name}', 'has joined %{num} communities:
%{name}', ta.get_resource_name.size).html_safe % { num: ta.get_resource_name.size, - name: ta.collect_group_with_index(:resource_name) do |n,i| + name: safe_join(ta.collect_group_with_index(:resource_name) do |n,i| link = link_to image_tag(ta.get_resource_profile_custom_icon[i] || default_or_themed_icon("/images/icons-app/community-icon.png")), ta.get_resource_url[i], title: n - end.join.html_safe + end) } end diff --git a/app/jobs/notify_activity_to_profiles_job.rb b/app/jobs/notify_activity_to_profiles_job.rb index b9a5a80..a104bd2 100644 --- a/app/jobs/notify_activity_to_profiles_job.rb +++ b/app/jobs/notify_activity_to_profiles_job.rb @@ -22,6 +22,9 @@ class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id) # Notify all friends ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})") + # Notify all followers + ActionTrackerNotification.connection.execute("INSERT INTO action_tracker_notifications(profile_id, action_tracker_id) SELECT f.follower_id, #{tracked_action.id} FROM profile_followers AS f WHERE profile_id=#{tracked_action.user.id} AND (f.follower_id NOT IN (SELECT atn.profile_id FROM action_tracker_notifications AS atn WHERE atn.action_tracker_id = #{tracked_action.id})) AND (f.follower_id IN SELECT friendships.friend_id FROM friendships WHERE friendships.person_id = #{tracked_action.user.id} OR SELECT profiles.public FROM profiles where id=#{tracked_action.user.id})") + if tracked_action.user.is_a? Organization ActionTrackerNotification.connection.execute "insert into action_tracker_notifications(profile_id, action_tracker_id) " + "select distinct accessor_id, #{tracked_action.id} from role_assignments where resource_id = #{tracked_action.user.id} and resource_type='Profile' " + diff --git a/app/models/person.rb b/app/models/person.rb index 323e5b6..eb87533 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -580,9 +580,14 @@ class Person < Profile person.has_permission?(:manage_friends, self) end + def following_profiles + Profile.following_profiles self + end + protected def followed_by?(profile) - self == profile || self.is_a_friend?(profile) + self == profile || self.is_a_friend?(profile) || self.followers.include(profile) end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index b0f75db..499291c 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -203,6 +203,12 @@ class Profile < ApplicationRecord scope :more_active, -> { order 'activities_count DESC' } scope :more_recent, -> { order "created_at DESC" } + scope :following_profiles, -> person { + distinct.select('profiles.*'). + joins('join profile_followers ON profile_followers.profile_id = profiles.id'). + where('profile_followers.follower_id = ?', person.id) + } + acts_as_trackable :dependent => :destroy has_many :profile_activities @@ -215,6 +221,9 @@ class Profile < ApplicationRecord has_many :email_templates, :foreign_key => :owner_id + has_many :profile_followers + has_many :followers, :class_name => 'Person', :through => :profile_followers + # Although this should be a has_one relation, there are no non-silly names for # a foreign key on article to reference the template to which it is # welcome_page... =P @@ -1136,5 +1145,4 @@ private :generate_url, :url_options def allow_invitation_from(person) false end - end diff --git a/app/models/profile_follower.rb b/app/models/profile_follower.rb new file mode 100644 index 0000000..c979e30 --- /dev/null +++ b/app/models/profile_follower.rb @@ -0,0 +1,9 @@ +class ProfileFollower < ApplicationRecord + track_actions :new_follower, :after_create, :keep_params => ["follower.name", "follower.url", "follower.profile_custom_icon"], :custom_user => :profile + + belongs_to :profile, :foreign_key => :profile_id + belongs_to :follower, :class_name => 'Person', :foreign_key => :follower_id + + validates_presence_of :profile_id, :follower_id + +end diff --git a/app/views/profile/_new_follower.html.erb b/app/views/profile/_new_follower.html.erb new file mode 100644 index 0000000..3cc3227 --- /dev/null +++ b/app/views/profile/_new_follower.html.erb @@ -0,0 +1 @@ +<%= render :partial => 'default_activity', :locals => { :activity => activity, :tab_action => tab_action } %> diff --git a/config/initializers/action_tracker.rb b/config/initializers/action_tracker.rb index 7080775..9bfcfa3 100644 --- a/config/initializers/action_tracker.rb +++ b/config/initializers/action_tracker.rb @@ -12,6 +12,10 @@ ActionTrackerConfig.verbs = { type: :groupable }, + new_follower: { + type: :groupable + }, + join_community: { type: :groupable }, diff --git a/db/migrate/20160608123748_create_profile_followers_table.rb b/db/migrate/20160608123748_create_profile_followers_table.rb new file mode 100644 index 0000000..5419d40 --- /dev/null +++ b/db/migrate/20160608123748_create_profile_followers_table.rb @@ -0,0 +1,8 @@ +class CreateProfileFollowersTable < ActiveRecord::Migration + def change + create_table :profile_followers do |t| + t.column :profile_id, :integer + t.column :follower_id, :integer + end + end +end -- libgit2 0.21.2