diff --git a/app/models/proposal_notification.rb b/app/models/proposal_notification.rb index 60912d887..686f3ce90 100644 --- a/app/models/proposal_notification.rb +++ b/app/models/proposal_notification.rb @@ -7,6 +7,8 @@ class ProposalNotification < ActiveRecord::Base validates :proposal, presence: true validate :minimum_interval + scope :public_for_api, -> { joins(:proposal).where("proposals.hidden_at IS NULL") } + def minimum_interval return true if proposal.try(:notifications).blank? if proposal.notifications.last.created_at > (Time.current - Setting[:proposal_notification_minimum_interval_in_days].to_i.days).to_datetime diff --git a/config/initializers/graphql.rb b/config/initializers/graphql.rb index b6d98562b..b899476f7 100644 --- a/config/initializers/graphql.rb +++ b/config/initializers/graphql.rb @@ -1,9 +1,10 @@ API_TYPE_DEFINITIONS = { User => %I[ id username proposals ], Debate => %I[ id title description created_at cached_votes_total cached_votes_up cached_votes_down comments_count hot_score confidence_score geozone_id geozone comments public_author ], - Proposal => %I[ id title description external_url cached_votes_up comments_count hot_score confidence_score created_at summary video_url geozone_id retired_at retired_reason retired_explanation geozone comments public_author ], + Proposal => %I[ id title description external_url cached_votes_up comments_count hot_score confidence_score created_at summary video_url geozone_id retired_at retired_reason retired_explanation geozone comments proposal_notifications public_author ], Comment => %I[ id commentable_id commentable_type body created_at cached_votes_total cached_votes_up cached_votes_down ancestry confidence_score public_author ], Geozone => %I[ id name ] + ProposalNotification => %I[ title body proposal_id created_at proposal ], } type_creator = GraphQL::TypeCreator.new diff --git a/spec/models/proposal_notification_spec.rb b/spec/models/proposal_notification_spec.rb index f66b254b6..cd8bf346a 100644 --- a/spec/models/proposal_notification_spec.rb +++ b/spec/models/proposal_notification_spec.rb @@ -22,6 +22,22 @@ describe ProposalNotification do expect(notification).to_not be_valid end + describe "public_for_api scope" do + it "returns proposal notifications" do + proposal = create(:proposal) + notification = create(:proposal_notification, proposal: proposal) + + expect(ProposalNotification.public_for_api).to include(notification) + end + + it "blocks notifications whose proposal is hidden" do + proposal = create(:proposal, :hidden) + notification = create(:proposal_notification, proposal: proposal) + + expect(ProposalNotification.public_for_api).not_to include(notification) + end + end + describe "minimum interval between notifications" do before(:each) do