Commit 24359f04 authored by Leandro Nunes's avatar Leandro Nunes

Merge branch 'add-scrap-api' into 'master'

adding scrap to api

See merge request !1747
parents 29cc62c6 d201e40f
Pipeline #84706433 failed with stages
in 24 minutes and 14 seconds
......@@ -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)
......
......@@ -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
......
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
......@@ -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
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment