[Git][noosfero/noosfero][master] 2 commits: adding scrap to api

Leandro Nunes gitlab at mg.gitlab.com
Thu Sep 26 10:53:09 BRT 2019



Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
d201e40f by Leandro Nunes dos Santos at 2019-09-26T12:59:06Z
adding scrap to api

- - - - -
24359f04 by Leandro Nunes at 2019-09-26T13:53:05Z
Merge branch 'add-scrap-api' into 'master'

adding scrap to api

See merge request noosfero/noosfero!1747
- - - - -


5 changed files:

- app/api/app.rb
- app/api/entities.rb
- + app/api/v1/scraps.rb
- app/models/profile.rb
- test/models/profile_test.rb


Changes:

=====================================
app/api/app.rb
=====================================
@@ -52,6 +52,7 @@ module Api
     mount V1::Roles
     mount V1::Domains
     mount V1::Settings
+    mount V1::Scraps
 
     # hook point which allow plugins to add Grape::API extensions to Api::App
     # finds for plugins which has api mount points classes defined (the class should extends Grape::API)


=====================================
app/api/entities.rb
=====================================
@@ -141,7 +141,7 @@ module Api
       expose :layout_template
       expose :permissions do |profile, options|
         Entities.permissions_for_entity(profile, options[:current_person],
-                                        :allow_post_content?, :allow_edit?, :allow_destroy?, :allow_edit_design?)
+                                        :allow_post_content?, :allow_edit?, :allow_destroy?, :allow_edit_design? , :allow_post_scrap?)
       end
       expose :theme do |profile, options|
         profile.theme || profile.environment.theme
@@ -347,9 +347,23 @@ module Api
         type_map.first.represent(activity.target) unless type_map.nil?
       end
       expose :params, if: lambda { |activity, options| activity.kind_of?(ActionTracker::Record) }
-      expose :content, if: lambda { |activity, options| activity.kind_of?(Scrap) }
+      expose :content, if: lambda { |activity, options| activity.kind_of?(::Scrap) }
       expose :verb do |activity, options|
-        activity.kind_of?(Scrap) ? "leave_scrap" : activity.verb
+        activity.kind_of?(::Scrap) ? "leave_scrap" : activity.verb
+      end
+
+      expose :comments_count do |activity, opts|
+        activity.kind_of?(::Scrap) ? activity.replies.count : activity.comments_count
+      end
+    end
+
+    class Scrap < Entity
+      expose :id, :created_at, :updated_at, :scrap_id
+      expose :receiver, using: Profile
+      expose :sender, using: Profile      
+      expose :content
+      expose :replies_count do |scrap, opts|
+        scrap.replies.count
       end
     end
 


=====================================
app/api/v1/scraps.rb
=====================================
@@ -0,0 +1,27 @@
+module Api
+  module V1
+    class Scraps < Grape::API::Instance
+      resource :profiles do
+        post ":profile_id/scraps" do
+          profile = environment.profiles.find_by id: params[:profile_id]
+          return forbidden! unless profile.allow_post_scrap?(current_person)
+          begin
+            scrap = Scrap.new(params[:scrap])
+            scrap.sender = current_person
+	    scrap.receiver = profile
+	    scrap.save
+            present_partial scrap, with: Entities::Scrap
+          rescue ActiveRecord::RecordInvalid
+            render_model_errors!(scrap.errors)
+          end
+        end
+
+        get ":profile_id/scraps/:id/replies" do
+          profile = environment.profiles.visible.find_by id: params[:profile_id]
+	  scrap = profile.scraps.find(params[:id])
+	  present_partial scrap.replies.order(created_at: :desc), with: Entities::Scrap
+        end
+      end
+    end
+  end
+end


=====================================
app/models/profile.rb
=====================================
@@ -1277,6 +1277,14 @@ class Profile < ApplicationRecord
     person.kind_of?(Profile) && person.has_permission?("post_content", self)
   end
 
+  def allow_post_scrap?(person = nil)
+    if self.kind_of?(Person) && person.kind_of?(Person)
+      self.is_a_friend?(person)
+    else 
+      person.kind_of?(Profile) && person.has_permission?("post_content", self)
+    end
+  end
+
   def allow_edit?(person = nil)
     person.kind_of?(Profile) && person.has_permission?("edit_profile", self)
   end


=====================================
test/models/profile_test.rb
=====================================
@@ -2563,4 +2563,50 @@ class ProfileTest < ActiveSupport::TestCase
       assert_includes Profile.accessible_to(with_permission), profile
       assert_not_includes Profile.accessible_to(without_permission), profile
     end
+
+
+    should "return true to allow_post_scrap? when user is a friend of person profile" do
+      person = fast_create(Person)
+      friend = fast_create(Person)
+      person.add_friend(friend)
+
+      assert person.send("allow_post_scrap?", friend)
+    end
+
+    should "return false to allow_post_scrap? when user is not a friend of person profile" do
+      person = fast_create(Person)
+      friend = fast_create(Person)
+
+      assert !person.send("allow_post_scrap?", friend)
+    end
+
+    should "return false to allow_post_scrap? when user is null in person profile" do
+      person = fast_create(Person)
+      friend = fast_create(Person)
+
+      assert !person.send("allow_post_scrap?", nil)
+    end
+
+    should "return true to allow_post_scrap? when user has permission to post content" do
+      profile = fast_create(Profile)
+      person = fast_create(Person)
+
+      give_permission(person, 'post_content', profile)
+
+      assert profile.send("allow_post_scrap?", person)
+    end
+
+    should "return false to allow_post_scrap? when user has no permission to post content in profile" do
+      profile = fast_create(Person)
+      person = fast_create(Person)
+
+      assert !profile.send("allow_post_scrap?", person)
+    end
+
+    should "return false to allow_post_scrap? when user is null to access profile" do
+      profile = fast_create(Person)
+
+      assert !profile.send("allow_post_scrap?", nil)
+    end
+
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/29cc62c630297c6547659c3a6ab58bffea745b9f...24359f0414cd4b78cba4517ecc16c51372de491c

-- 
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/29cc62c630297c6547659c3a6ab58bffea745b9f...24359f0414cd4b78cba4517ecc16c51372de491c
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20190926/62109bae/attachment-0001.html>


More information about the Noosfero-dev mailing list