From 9daef03e5e55961087ed2d97c4734fdfd1a4c994 Mon Sep 17 00:00:00 2001
From: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
Date: Fri, 2 Dec 2016 12:07:13 -0200
Subject: [PATCH] Added filter to activities

Signed-off-by: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br>
---
 app/controllers/v1/activities_controller.rb |  4 ++--
 app/controllers/v1/feed_controller.rb       |  2 +-
 app/models/concerns/trackable.rb            | 22 +++++++++++++++++++++
 app/models/user.rb                          | 21 ++++++++++++++++++++
 app/policies/activity_policy.rb             | 18 ++++++++++++++++-
 5 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/app/controllers/v1/activities_controller.rb b/app/controllers/v1/activities_controller.rb
index 4d2b7cca..e8de993b 100644
--- a/app/controllers/v1/activities_controller.rb
+++ b/app/controllers/v1/activities_controller.rb
@@ -18,13 +18,13 @@ class V1::ActivitiesController < ApplicationController
   # Only followers can see user activities
   def user_activities
     raise ::Pundit::NotAuthorizedError unless ::ActivityPolicy.new(current_user, resource_model).user_activities?
-    activities = paginate resource_model.activities
+    activities = paginate resource_model.activities_filtered
     render json: activities
   end
 
   def me
     authorize :activity, :index?
-    activities = paginate current_user.activities
+    activities = paginate current_user.activities_filtered
     render json: activities
   end
 end
diff --git a/app/controllers/v1/feed_controller.rb b/app/controllers/v1/feed_controller.rb
index 3a1d8944..cd1aeb11 100644
--- a/app/controllers/v1/feed_controller.rb
+++ b/app/controllers/v1/feed_controller.rb
@@ -15,7 +15,7 @@ class V1::FeedController < ApplicationController
   def activities_followed
     activities = []
     current_user.watching.each do |watching|
-      activities.push(*watching.activities.to_a)
+      activities.push(*watching.activities_filtered.to_a)
     end
     activities
   end
diff --git a/app/models/concerns/trackable.rb b/app/models/concerns/trackable.rb
index f3ee5930..4a6712ce 100644
--- a/app/models/concerns/trackable.rb
+++ b/app/models/concerns/trackable.rb
@@ -6,5 +6,27 @@ module Trackable
     tracked owner: proc { |controller, model| model.try(:user) || model.try(:owner) || controller.try(:current_user) }
     tracked recipient: proc { |_controller, model| model.try(:recipient) || model }
     tracked privacy: proc { |_controller, model| model.try(:privacy) || "public" }
+
+  end
+
+  def activities_filtered
+    p "--------------------- LOLOLOLOLLOLO"
+    @activities.where.not(key: activities_filter)
+  end
+
+  def activities_filter
+    [ 'complaint.update', 'complaint.destroy',
+      'complaint_reason.create', 'complaint_reason.update', 'complaint_reason.destroy',
+      'institution.create', 'institution.update', 'institution.destroy',
+      'review.update', 'review.destroy',
+      'rate.create', 'rate.update', 'rate.destroy',
+      'follow.create', 'follow.update', 'follow.destroy',
+      'share.create', 'share.update', 'share.destroy',
+      'view.create', 'view.update', 'view.destroy',
+      'tagging.create', 'tagging.update', 'tagging.destroy',
+      'bookmark.create', 'bookmark.update', 'bookmark.destroy',
+      'download.update', 'download.destroy',
+      'like.update'
+    ]
   end
 end
diff --git a/app/models/user.rb b/app/models/user.rb
index 471b39b8..b71382e6 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -177,8 +177,29 @@ class User < ApplicationRecord
 
   # ~~~~ end followable actions ~~~~ #
 
+  def activities_filtered
+    p "--------------------- LOLOLOLOLLOLO"
+    self.activities.where.not(key: activities_filter)
+  end
+
   private
 
+  def activities_filter
+    [ 'complaint.update', 'complaint.destroy',
+      'complaint_reason.create', 'complaint_reason.update', 'complaint_reason.destroy',
+      'institution.create', 'institution.update', 'institution.destroy',
+      'review.update', 'review.destroy',
+      'rate.create', 'rate.update', 'rate.destroy',
+      'follow.create', 'follow.update', 'follow.destroy',
+      'share.create', 'share.update', 'share.destroy',
+      'view.create', 'view.update', 'view.destroy',
+      'tagging.create', 'tagging.update', 'tagging.destroy',
+      'bookmark.create', 'bookmark.update', 'bookmark.destroy',
+      'download.update', 'download.destroy',
+      'like.update'
+    ]
+  end
+
   def default_role
     roles << Role.teacher
   end
diff --git a/app/policies/activity_policy.rb b/app/policies/activity_policy.rb
index cf550a90..77abe14d 100644
--- a/app/policies/activity_policy.rb
+++ b/app/policies/activity_policy.rb
@@ -8,7 +8,23 @@ class ActivityPolicy < ApplicationPolicy
     end
 
     def resolve
-      scope.includes(:owner,:recipient).where("privacy = 'public'").order('created_at DESC').all
+      scope.includes(:owner,:recipient).where("privacy = 'public'").where.not(key: except).order('created_at DESC').all
+    end
+
+    def except
+      [ 'complaint.update', 'complaint.destroy',
+        'complaint_reason.create', 'complaint_reason.update', 'complaint_reason.destroy',
+        'institution.create', 'institution.update', 'institution.destroy',
+        'review.update', 'review.destroy',
+        'rate.create', 'rate.update', 'rate.destroy',
+        'follow.create', 'follow.update', 'follow.destroy',
+        'share.create', 'share.update', 'share.destroy',
+        'view.create', 'view.update', 'view.destroy',
+        'tagging.create', 'tagging.update', 'tagging.destroy',
+        'bookmark.create', 'bookmark.update', 'bookmark.destroy',
+        'download.update', 'download.destroy',
+        'like.update'
+      ]
     end
   end
 
-- 
GitLab