decidim-restfull-proposals 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/app/controllers/decidim/api/rest_full/components/proposal_components_controller.rb +91 -0
  3. data/app/controllers/decidim/api/rest_full/draft_proposals/draft_proposals_controller.rb +116 -0
  4. data/app/controllers/decidim/api/rest_full/proposals/proposal_extended_data_controller.rb +31 -0
  5. data/app/controllers/decidim/api/rest_full/proposals/proposals_controller.rb +172 -0
  6. data/app/controllers/decidim/api/rest_full/vote_proposals/vote_proposals_controller.rb +90 -0
  7. data/app/jobs/decidim/rest_full/proposals/proposal_webhook_job.rb +67 -0
  8. data/app/models/decidim/rest_full/proposals/proposal_application_id.rb +14 -0
  9. data/app/serializers/decidim/api/rest_full/proposals/draft_proposal_serializer.rb +44 -0
  10. data/app/serializers/decidim/api/rest_full/proposals/proposal_component_serializer.rb +232 -0
  11. data/app/serializers/decidim/api/rest_full/proposals/proposal_serializer.rb +113 -0
  12. data/app/serializers/decidim/api/rest_full/proposals/proposal_state_serializer.rb +39 -0
  13. data/app/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer.rb +54 -0
  14. data/app/serializers/decidim/api/rest_full/proposals/vote_proposal_serializer.rb +27 -0
  15. data/app/services/decidim/rest_full/proposals/draft_proposals_operations.rb +248 -0
  16. data/app/services/decidim/rest_full/proposals/proposal_vote_eligibility.rb +106 -0
  17. data/app/services/decidim/rest_full/proposals/proposal_votes_operations.rb +173 -0
  18. data/app/services/decidim/rest_full/proposals/vote_proposals_operations.rb +114 -0
  19. data/config/locales/decidim_rest_full_proposals.en.yml +26 -0
  20. data/lib/decidim/rest_full/http_cache/proposal_show_fingerprint.rb +77 -0
  21. data/lib/decidim/rest_full/proposals/engine.rb +129 -0
  22. data/lib/decidim/rest_full/proposals/proposal_client_id_override.rb +18 -0
  23. data/lib/decidim/rest_full/proposals/proposals_controller_override.rb +28 -0
  24. data/lib/decidim/rest_full/proposals/ransackers.rb +84 -0
  25. data/lib/decidim/rest_full/proposals/test_definitions.rb +7 -0
  26. data/lib/decidim/rest_full/test/definitions/draft_proposal.rb +101 -0
  27. data/lib/decidim/rest_full/test/definitions/proposal.rb +115 -0
  28. data/lib/decidim/rest_full/test/definitions/proposal_component_schema.rb +110 -0
  29. data/lib/decidim/rest_full/test/definitions/proposals.rb +4 -0
  30. data/lib/decidim/rest_full/test/definitions/tags/draft_proposals_tag.rb +26 -0
  31. data/lib/decidim/rest_full/test/definitions/tags/proposal_tag.rb +32 -0
  32. data/lib/decidim/rest_full/test/definitions/vote_proposal.rb +87 -0
  33. data/lib/decidim-restfull-proposals.rb +17 -0
  34. data/spec/jobs/decidim/rest_full/proposals/proposal_webhook_job_spec.rb +66 -0
  35. data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_index_spec.rb +149 -0
  36. data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_show_spec.rb +140 -0
  37. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_create_spec.rb +116 -0
  38. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_delete_spec.rb +64 -0
  39. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_publish_spec.rb +132 -0
  40. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_show_spec.rb +72 -0
  41. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_update_spec.rb +307 -0
  42. data/spec/requests/decidim/api/rest_full/proposals/proposal_extended_data_controller_spec.rb +80 -0
  43. data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_index_spec.rb +275 -0
  44. data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_show_spec.rb +187 -0
  45. data/spec/requests/decidim/api/rest_full/scenarios/draft_publish_async_increments_count_and_lists_newest_spec.rb +144 -0
  46. data/spec/requests/decidim/api/rest_full/scenarios/participatory_process_phase_change_webhook_and_component_meta_spec.rb +130 -0
  47. data/spec/requests/decidim/api/rest_full/scenarios/proposals_published_at_desc_prev_next_chain_spec.rb +72 -0
  48. data/spec/requests/decidim/api/rest_full/scenarios/search_vote_enabled_impersonate_and_vote_spec.rb +88 -0
  49. data/spec/requests/decidim/api/rest_full/vote_proposals/vote_proposals_controller_spec.rb +231 -0
  50. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_chatbot_menu_spec.rb +147 -0
  51. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_phases_spec.rb +88 -0
  52. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_votes_spec.rb +103 -0
  53. data/spec/serializers/decidim/api/rest_full/proposals/proposal_serializer_can_vote_spec.rb +86 -0
  54. data/spec/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer_spec.rb +63 -0
  55. data/spec/services/decidim/rest_full/proposals/proposal_vote_eligibility_spec.rb +120 -0
  56. metadata +132 -0
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+
5
+ module Decidim
6
+ module RestFull
7
+ module HttpCache
8
+ # Cheap validators for conditional GET on proposal show (avoids heavy window pagination SQL).
9
+ class ProposalShowFingerprint
10
+ def self.for_request(controller, proposal)
11
+ new(
12
+ organization: controller.send(:current_organization),
13
+ proposal:,
14
+ client_id: controller.send(:client_id),
15
+ act_as: controller.send(:act_as),
16
+ locales: controller.send(:available_locales),
17
+ populate: controller.send(:populate_params),
18
+ filter: controller.params[:filter]
19
+ )
20
+ end
21
+
22
+ def initialize(organization:, proposal:, client_id:, act_as:, locales:, populate:, filter:) # rubocop:disable Metrics/ParameterLists
23
+ @organization = organization
24
+ @proposal = proposal
25
+ @client_id = client_id
26
+ @act_as = act_as
27
+ @locales = Array(locales).map(&:to_s).sort.join(",")
28
+ @populate = Array(populate).compact.map(&:to_s).sort.join(",")
29
+ @filter = filter.respond_to?(:to_unsafe_h) ? filter.to_unsafe_h.to_json : filter.to_s
30
+ end
31
+
32
+ def last_modified
33
+ @last_modified ||= fingerprint_last_modified
34
+ end
35
+
36
+ def etag
37
+ segments = Decidim::RestFull::Core::HttpCache::FingerprintContributorRegistry.etag_segments_for(
38
+ :proposal_show,
39
+ proposal: @proposal,
40
+ organization: @organization
41
+ )
42
+ input = [
43
+ @organization.id,
44
+ @proposal.id,
45
+ last_modified.to_f,
46
+ @client_id,
47
+ @act_as&.id,
48
+ @locales,
49
+ @populate.to_s,
50
+ @filter,
51
+ *segments
52
+ ].join("/")
53
+ %("#{Digest::SHA256.hexdigest(input)}")
54
+ end
55
+
56
+ private
57
+
58
+ def fingerprint_last_modified
59
+ pid = @proposal.id
60
+ row = Decidim::Proposals::Proposal.where(id: pid).pick(:updated_at, :decidim_component_id)
61
+ return Time.zone.at(0) unless row
62
+
63
+ proposal_updated, component_id = row
64
+ component_updated = Decidim::Component.where(id: component_id).pick(:updated_at)
65
+ votes_max = Decidim::Proposals::ProposalVote.where(decidim_proposal_id: pid).maximum(:updated_at)
66
+ contributor_max = Decidim::RestFull::Core::HttpCache::FingerprintContributorRegistry.max_updated_at_for(
67
+ :proposal_show,
68
+ proposal: @proposal,
69
+ organization: @organization
70
+ )
71
+ ts = [proposal_updated, component_updated, votes_max, contributor_max].compact.max
72
+ ts || proposal_updated
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ class Engine < ::Rails::Engine
7
+ config.root = Proposals::ENGINE_ROOT
8
+
9
+ config.to_prepare do
10
+ next unless defined?(Decidim::Proposals)
11
+
12
+ Decidim::Proposals::Proposal.include(Decidim::RestFull::Proposals::ProposalClientIdOverride)
13
+ Decidim::Proposals::ProposalsController.include(Decidim::RestFull::Proposals::ProposalsControllerOverride)
14
+ Decidim::RestFull::Proposals::Ransackers.register!
15
+ Decidim::Proposals::Proposal.include(Decidim::RestFull::Core::HasExtendedData)
16
+ end
17
+
18
+ initializer "rest_full.proposals.extension" do
19
+ Decidim::RestFull::Extension.register(:proposals) do |ext|
20
+ ext.toggle_feature gem: "decidim-restfull-proposals"
21
+ ext.controller_paths "proposals", "proposal_components", "draft_proposals", "vote_proposals"
22
+ ext.oauth_scopes :proposals
23
+ ext.permissions(:proposals, "proposals.read", group: :proposals)
24
+ ext.permissions(:proposals, "proposals.draft", group: :proposals)
25
+ ext.permissions(:proposals, "proposals.vote", group: :proposals)
26
+ ext.permissions(:proposals, "proposals.extended_data.read", group: :proposals)
27
+ ext.permissions(:proposals, "proposals.extended_data.update", group: :proposals)
28
+
29
+ ext.api_job "draft_proposals#create", ->(ctx, p) { Proposals::DraftProposalsOperations.new(ctx, p).create! }
30
+ ext.api_job "draft_proposals#update", ->(ctx, p) { Proposals::DraftProposalsOperations.new(ctx, p).update! }
31
+ ext.api_job "draft_proposals#destroy", ->(ctx, p) { Proposals::DraftProposalsOperations.new(ctx, p).destroy! }
32
+ ext.api_job "draft_proposals#publish", ->(ctx, p) { Proposals::DraftProposalsOperations.new(ctx, p).publish! }
33
+ ext.api_job "vote_proposals#create", ->(ctx, p) { Proposals::VoteProposalsOperations.new(ctx, p).create! }
34
+
35
+ {
36
+ "draft_proposal_creation.succeeded" => :draft_proposal,
37
+ "draft_proposal_update.succeeded" => :draft_proposal,
38
+ "proposal_creation.succeeded" => :proposal,
39
+ "proposal_update.succeeded" => :proposal,
40
+ "proposal_state_change.succeeded" => :proposal
41
+ }.each do |event_name, schema_ref|
42
+ name = event_name
43
+ ext.webhook_event(
44
+ name,
45
+ scope: :proposals,
46
+ payload_schema_ref: schema_ref,
47
+ schema_key: "wh_#{name.delete_suffix(".succeeded").tr(".", "_")}",
48
+ trigger: "Proposal lifecycle notification",
49
+ example: lambda { |organization|
50
+ ::Decidim::Api::RestFull::Proposals::ProposalWebhookSerializer.example_envelope(
51
+ organization,
52
+ event_name: name
53
+ )
54
+ }
55
+ )
56
+ end
57
+
58
+ ext.webhooks(
59
+ /decidim\.events\./,
60
+ /decidim\.proposals\./,
61
+ handler: Decidim::RestFull::Core::WebhookDispatcher.instance.method(:handle_proposals)
62
+ )
63
+
64
+ ext.open_api_definitions(
65
+ File.join(Proposals::ENGINE_ROOT, "lib/decidim/rest_full/proposals/test_definitions.rb")
66
+ )
67
+
68
+ ext.rswag_specs(
69
+ File.join(Proposals::ENGINE_ROOT, "spec/requests/decidim/api/rest_full/proposals/**/*_spec.rb"),
70
+ File.join(Proposals::ENGINE_ROOT, "spec/requests/decidim/api/rest_full/draft_proposals/**/*_spec.rb"),
71
+ File.join(Proposals::ENGINE_ROOT, "spec/requests/decidim/api/rest_full/vote_proposals/**/*_spec.rb"),
72
+ File.join(Proposals::ENGINE_ROOT, "spec/requests/decidim/api/rest_full/components/proposal_components*_spec.rb")
73
+ )
74
+
75
+ ext.routes do
76
+ resources :components, only: [] do
77
+ collection do
78
+ Decidim::RestFull::Routing.read_resources(
79
+ self,
80
+ :proposal_components,
81
+ controller: "components/proposal_components",
82
+ only: [:index, :show]
83
+ )
84
+ end
85
+ end
86
+
87
+ Decidim::RestFull::Routing.read_resources(
88
+ self,
89
+ :proposals,
90
+ controller: "proposals/proposals",
91
+ only: [:index, :show]
92
+ ) do
93
+ member do
94
+ resources :extended_data, only: [], controller: "/decidim/api/rest_full/proposals/proposal_extended_data" do
95
+ collection do
96
+ get "/", action: :index
97
+ put "/", action: :update
98
+ put "/sync", action: :update_sync
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ Decidim::RestFull::Routing.async_resources(
105
+ self,
106
+ :draft_proposals,
107
+ controller: "draft_proposals/draft_proposals",
108
+ only: [:index, :show, :update, :create, :destroy],
109
+ member: { post: { publish: :publish, "publish/sync": :publish_sync } }
110
+ )
111
+
112
+ Decidim::RestFull::Routing.async_resources(
113
+ self,
114
+ :vote_proposals,
115
+ controller: "vote_proposals/vote_proposals",
116
+ only: [:index, :show, :create, :destroy]
117
+ )
118
+ end
119
+ end
120
+
121
+ Decidim::RestFull::Core::Configuration.events_for_proposals = %w(
122
+ draft_proposal_creation.succeeded draft_proposal_update.succeeded
123
+ proposal_creation.succeeded proposal_update.succeeded proposal_state_change.succeeded
124
+ )
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ module ProposalClientIdOverride
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ has_one :rest_full_application,
11
+ foreign_key: "proposal_id",
12
+ class_name: "Decidim::RestFull::Proposals::ProposalApplicationId",
13
+ dependent: :destroy
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ module ProposalsControllerOverride
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ # Alias the original proposal_draft method
11
+ alias_method :original_proposal_draft, :proposal_draft
12
+
13
+ def proposal_draft_without_external_client_ids
14
+ Decidim::Proposals::Proposal
15
+ .from_all_author_identities(current_user)
16
+ .not_hidden
17
+ .only_amendables
18
+ .joins("LEFT OUTER JOIN proposal_application_ids ON proposal_application_ids.proposal_id = decidim_proposals_proposals.id")
19
+ .where(component: current_component, proposal_application_ids: { id: nil })
20
+ .find_by(published_at: nil)
21
+ end
22
+
23
+ alias_method :proposal_draft, :proposal_draft_without_external_client_ids
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ # Proposal-specific Ransackers (only loaded with decidim-restfull-proposals).
7
+ module Ransackers
8
+ module_function
9
+
10
+ def register!
11
+ return unless defined?(Decidim::Proposals::Proposal)
12
+
13
+ register_state_ransacker!
14
+ register_weight_proposal_ransacker!
15
+ register_proposal_ransackables!
16
+ end
17
+
18
+ def register_proposal_ransackables!
19
+ Decidim::RestFull::Core::Ransackers.extend_ransackable_attributes!(
20
+ Decidim::Proposals::Proposal,
21
+ %w(state voted_weight)
22
+ )
23
+ end
24
+
25
+ def register_state_ransacker!
26
+ Decidim::Proposals::Proposal.ransacker :state do |_r|
27
+ Arel.sql(<<~SQL.squish
28
+ COALESCE(
29
+ (SELECT
30
+ tstate.token
31
+ FROM #{Decidim::Proposals::ProposalState.table_name} AS tstate
32
+ WHERE
33
+ decidim_proposals_proposals.decidim_proposals_proposal_state_id = tstate.id
34
+ LIMIT 1), ''
35
+ )
36
+ SQL
37
+ )
38
+ end
39
+ end
40
+
41
+ def register_weight_proposal_ransacker!
42
+ Decidim::Proposals::Proposal.ransacker :voted_weight, args: [:parent, :ransacker_args] do |_parent, _ransacker_args|
43
+ current_user = ::Decidim::Api::RestFull::Proposals::ProposalsController::CurrentUser.user
44
+ raise "current_user is nil" if current_user.nil?
45
+
46
+ awesome_support = Decidim::Toggle.gem_present?("decidim-decidim_awesome") && Decidim::DecidimAwesome.enabled?(:weighted_proposal_voting)
47
+ unless awesome_support
48
+ return Arel.sql(<<~SQL.squish
49
+ (
50
+ SELECT
51
+ '1' AS weight
52
+ FROM #{Decidim::Proposals::ProposalVote.table_name} AS tvote
53
+ WHERE
54
+ tvote.decidim_proposal_id = #{Decidim::Proposals::Proposal.table_name}.id
55
+ AND tvote.decidim_author_id = #{current_user.id.to_i}
56
+ LIMIT 1
57
+ )
58
+ SQL
59
+ )
60
+ end
61
+
62
+ Arel.sql(<<~SQL.squish
63
+ (
64
+ SELECT
65
+ CASE
66
+ WHEN tweight.id IS NULL THEN '1'
67
+ ELSE CAST(tweight.weight AS VARCHAR)
68
+ END AS weight
69
+ FROM #{Decidim::Proposals::ProposalVote.table_name} AS tvote
70
+ LEFT JOIN #{Decidim::DecidimAwesome::VoteWeight.table_name} AS tweight
71
+ ON tvote.id = tweight.proposal_vote_id
72
+ WHERE
73
+ tvote.decidim_proposal_id = #{Decidim::Proposals::Proposal.table_name}.id
74
+ AND tvote.decidim_author_id = #{current_user.id.to_i}
75
+ LIMIT 1
76
+ )
77
+ SQL
78
+ )
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../test/definitions/proposal_component_schema"
4
+ require_relative "../test/definitions/proposals"
5
+ require_relative "../test/definitions/vote_proposal"
6
+ require_relative "../test/definitions/tags/proposal_tag"
7
+ require_relative "../test/definitions/tags/draft_proposals_tag"
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ Decidim::RestFull::Core::DefinitionRegistry.register_resource(:draft_proposal) do
4
+ {
5
+ type: :object,
6
+ title: "Draft Proposal",
7
+ properties: {
8
+ id: { type: :string, description: "Draft Proposal Id" },
9
+ type: { type: :string, enum: ["draft_proposal"] },
10
+ attributes: {
11
+ title: "Draft Proposal Attributes",
12
+ type: :object,
13
+ properties: {
14
+ title: {
15
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop),
16
+ :description => "Draft Proposal title"
17
+ },
18
+ body: {
19
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop),
20
+ :description => "Draft Proposal content"
21
+ },
22
+ errors: {
23
+ type: :object,
24
+ title: "Draft Proposal Validation Errors",
25
+ properties: {
26
+ title: {
27
+ type: :array,
28
+ items: { type: :string }
29
+ },
30
+ body: {
31
+ type: :array,
32
+ items: { type: :string }
33
+ }
34
+ },
35
+ required: [:title, :body],
36
+ description: "Draft current errors"
37
+ },
38
+ created_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:creation_date) },
39
+ updated_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:edition_date) }
40
+ },
41
+ required: [:created_at, :updated_at, :title, :body],
42
+ additionalProperties: false
43
+ },
44
+ meta: {
45
+ type: :object,
46
+ title: "Draft Proposition Metadata",
47
+ properties: {
48
+ publishable: { type: :boolean, description: "Draft is published as it is now?" },
49
+ client_id: { type: :string, description: "Attached client_id" },
50
+ scope: { type: :integer, description: "Scope Id" },
51
+ fields: { type: :array, description: "Editable field names", items: { type: :string } }
52
+ },
53
+ additionalProperties: {
54
+ oneOf: [
55
+ {
56
+ type: :boolean
57
+ },
58
+ {
59
+ type: :integer
60
+ },
61
+ {
62
+ type: :string
63
+ },
64
+ {
65
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop)
66
+ }
67
+ ]
68
+ },
69
+ required: [:publishable, :client_id, :fields]
70
+ },
71
+ links: {
72
+ type: :object,
73
+ title: "Proposal Links",
74
+ properties: {
75
+ self: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
76
+ collection: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
77
+ related: Decidim::RestFull::Core::DefinitionRegistry.resource_link
78
+ },
79
+ additionalProperties: false,
80
+ required: [:self, :related, :collection]
81
+ },
82
+ relationships: {
83
+ type: :object,
84
+ title: "Draft Proposal Relationships",
85
+ properties: {
86
+ space: Decidim::RestFull::Core::DefinitionRegistry.belongs_to_relation({
87
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:space_type)
88
+ }, title: "Draft Proposal Related Space"),
89
+ component: Decidim::RestFull::Core::DefinitionRegistry.belongs_to_relation({
90
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:component_type)
91
+ }, title: "Draft Proposal Related Component"),
92
+ author: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("user", "user_group", title: "Draft Proposal Author"),
93
+ coauthors: Decidim::RestFull::Core::DefinitionRegistry.has_many("user", "user_group", title: "Draft Proposal Coauthors")
94
+ },
95
+ required: [:component, :space],
96
+ additionalProperties: false
97
+ }
98
+ },
99
+ required: [:id, :type, :attributes, :meta, :links]
100
+ }.freeze
101
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ Decidim::RestFull::Core::DefinitionRegistry.register_resource(:proposal) do
4
+ {
5
+ type: :object,
6
+ title: "Proposal",
7
+ properties: {
8
+ id: { type: :string, description: "Proposal Id" },
9
+ type: { type: :string, enum: ["proposal"] },
10
+ attributes: {
11
+ type: :object,
12
+ title: "Proposal Attributes",
13
+ properties: {
14
+ title: {
15
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop),
16
+ :description => "Proposal title"
17
+ },
18
+ body: {
19
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop),
20
+ :description => "Proposal content"
21
+ },
22
+ created_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:creation_date) },
23
+ updated_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:edition_date) },
24
+ extended_data: {
25
+ type: :object,
26
+ additionalProperties: true,
27
+ description: "Present when the client has proposals.extended_data.read"
28
+ }
29
+ },
30
+ required: [:created_at, :updated_at, :title, :body],
31
+ additionalProperties: false
32
+ },
33
+ meta: {
34
+ type: :object,
35
+ title: "Proposal Metadata",
36
+ properties: {
37
+ published: { type: :boolean, description: "Whether the proposal is published" },
38
+ can_vote: {
39
+ type: :boolean,
40
+ description: "Whether this proposal accepts votes given component voting settings and Awesome status restriction (or not rejected when restriction is off)"
41
+ },
42
+ scope: { type: :integer, description: "Scope Id" },
43
+ voted: {
44
+ type: :object,
45
+ title: "Current User Proposal Vote Metadata",
46
+ properties: {
47
+ weight: { type: :integer, description: "Vote weight" }
48
+ },
49
+ required: [:weight],
50
+ additionalProperties: false,
51
+ nullable: true
52
+ }
53
+ },
54
+ additionalProperties: {
55
+ oneOf: [
56
+ {
57
+ type: :boolean
58
+ },
59
+ {
60
+ type: :integer
61
+ },
62
+ {
63
+ type: :string
64
+ },
65
+ {
66
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop)
67
+ }
68
+ ]
69
+ },
70
+ required: [:published, :can_vote]
71
+ },
72
+ links: {
73
+ type: :object,
74
+ title: "Proposal Links",
75
+ properties: {
76
+ self: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
77
+ collection: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
78
+ related: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
79
+ prev: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
80
+ next: Decidim::RestFull::Core::DefinitionRegistry.resource_link
81
+ },
82
+ additionalProperties: false,
83
+ required: [:self, :collection, :related]
84
+ },
85
+ relationships: {
86
+ type: :object,
87
+ title: "Proposal Relationships",
88
+ properties: {
89
+ state: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("proposal_state", title: "Proposal State Relationship") do |state_schema|
90
+ state_schema[:properties][:meta] = {
91
+ type: :object,
92
+ title: "Proposal State Relationship Metadata",
93
+ properties: {
94
+ token: { type: :string, description: "Proposal State token" }
95
+ },
96
+ required: [:token]
97
+ }
98
+
99
+ state_schema[:required].push(:meta)
100
+ state_schema
101
+ end,
102
+ space: Decidim::RestFull::Core::DefinitionRegistry.belongs_to_relation({
103
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:space_type)
104
+ }, title: "Linked Space"),
105
+ component: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("proposal_component", title: "Linked Proposal Component"),
106
+ author: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("user", "user_group", title: "Proposal's Author"),
107
+ coauthors: Decidim::RestFull::Core::DefinitionRegistry.has_many("user", "user_group", title: "Proposal's Coauthors")
108
+ },
109
+ required: [:component, :space],
110
+ additionalProperties: false
111
+ }
112
+ },
113
+ required: [:id, :type, :attributes, :meta, :links]
114
+ }.freeze
115
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ Decidim::RestFull::Core::DefinitionRegistry.register_component_manifest_schema(manifest: :proposals, schema_name: :proposal_component)
4
+
5
+ Decidim::RestFull::Core::DefinitionRegistry.extends_object(:proposal_component, :generic_component) do |proposal_component|
6
+ proposal_component[:title] = "Proposal Component"
7
+ proposal_component[:description] = <<~README
8
+ A proposal component can host proposals from participants, and official proposals (proposals from the organization).
9
+ This component have many metadatas that explain what are the restrictions regarding proposing, voting, commenting, amending or endorsing.#{" "}
10
+
11
+ Features toggles:#{" "}
12
+ - `can_create_proposals`: If participants can create proposals
13
+ - `can_vote`: If voting is enabled for the component (and, when impersonating, if unvoted votable proposals remain)
14
+ - `awesome_votes_enabled_by_status`: Decidim Awesome step setting — restrict voting by proposal status
15
+ - `awesome_votes_enabled_states`: Allowed proposal state tokens when status restriction is on
16
+ - `can_comment`: If participants can comments
17
+ - .... and some more
18
+
19
+
20
+ README
21
+ proposal_component[:properties][:type] = { type: :string, enum: ["proposal_component"] }
22
+ proposal_component[:properties][:attributes][:properties][:manifest_name] = { type: :string, enum: ["proposals"] }
23
+ proposal_component[:properties][:links][:properties][:draft] = Decidim::RestFull::Core::DefinitionRegistry.resource_link
24
+ additional_properties = {
25
+ can_create_proposals: { type: :boolean, description: "If the current user can create proposal (component allows, and user did not reach publication limit)" },
26
+ can_vote: {
27
+ type: :boolean,
28
+ description: "If voting is enabled for the component (votes_enabled and not votes_blocked; " \
29
+ "when impersonating, also requires unvoted votable proposals)"
30
+ },
31
+ awesome_votes_enabled_by_status: {
32
+ type: :boolean,
33
+ description: "Decidim Awesome step setting: when true, voting is restricted to proposals whose status is listed in awesome_votes_enabled_states"
34
+ },
35
+ awesome_votes_enabled_states: {
36
+ type: :array,
37
+ items: { type: :string },
38
+ description: "Allowed proposal state tokens when awesome_votes_enabled_by_status is true (e.g. accepted, evaluating, not_answered)"
39
+ },
40
+ can_comment: { type: :boolean, description: "If the current user can comment on the component" },
41
+ geocoding_enabled: { type: :boolean, description: "If the component needs a map to display its resources" },
42
+ attachments_allowed: { type: :boolean, description: "If the component allows to attach files to resources" },
43
+ collaborative_drafts_enabled: { type: :boolean, description: "If you can create collaborative draft for the proposal" },
44
+ comments_enabled: { type: :boolean, description: "If you can comment on proposals" },
45
+ comments_max_length: { type: :integer, description: "Characters limit for comment" },
46
+ default_sort_order: { type: :string, enum: %w(
47
+ random recent most_voted most_endorsed most_commented most_followed with_more_authors automatic default
48
+ ), description: "Default order of proposals" },
49
+ official_proposals_enabled: { type: :boolean, description: "If proposals can be official" },
50
+ participatory_texts_enabled: { type: :boolean, description: "If proposals are based on a text modification" },
51
+ proposal_edit_before_minutes: { type: :integer, description: "Time in minute participant can edit the proposal" },
52
+ proposal_edit_time: { type: :string, enum: %w(infinite limited), description: "Type of restriction for proposal edition" },
53
+ proposal_limit: { type: :integer, description: "Max proposal per participant. No maximum if value is 0" },
54
+ resources_permissions_enabled: { type: :boolean, description: "If authorizations can be defined per proposal" },
55
+ threshold_per_proposal: { type: :integer, description: "Threshold to compare similar proposals" },
56
+ vote_limit: { type: :integer, description: "Max Number of vote per participant. 0 if no limit" },
57
+ endorsements_enabled: { type: :boolean, description: "If endorsements are enabled" },
58
+ votes_enabled: { type: :boolean, description: "If votes on proposal are enabled" },
59
+ creation_enabled: { type: :boolean, description: "If participant can create proposal are enabled" },
60
+ proposal_answering_enabled: { type: :boolean, description: "If officials can answer proposals" },
61
+ amendment_creation_enabled: { type: :boolean, description: "If participant can propose an amendment to a proposal" },
62
+ amendment_reaction_enabled: { type: :boolean, description: "If participant can react to an amendment of a proposal" },
63
+ amendment_promotion_enabled: { type: :boolean, description: "If participant choose an amendment to replace their initial proposal" },
64
+ votes: {
65
+ title: "Proposal Vote Weights Options",
66
+ description: <<~DESC.squish,
67
+ Always present (never null). Empty array when votes are not available (`votes_enabled` false).
68
+ When votes are enabled: simple vote ([1] or [0,1] with abstention), or voting cards ([1,2,3] or [0,1,2,3]).
69
+ Independent of `can_vote` (per-actor flag).
70
+ DESC
71
+ type: :array,
72
+ items: {
73
+ type: :object,
74
+ title: "Proposal Vote Weight",
75
+ properties: {
76
+ label: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop), :description => "Label to voting button" },
77
+ weight: { type: :integer, description: "Value to add to the vote. 0 for abstention" }
78
+ },
79
+ required: [:label, :weight]
80
+ }
81
+ },
82
+ awesome_voting_manifest: {
83
+ type: :string,
84
+ description: "Decidim Awesome weighted voting manifest (e.g. empty/default or voting_cards). Present when Awesome weighted voting is enabled."
85
+ },
86
+ voting_cards_show_abstain: {
87
+ type: :boolean,
88
+ description: "Decidim Awesome: include abstention (weight 0) in vote options."
89
+ },
90
+ voting_cards_show_modal_help: {
91
+ type: :boolean,
92
+ description: "Decidim Awesome: show voting help modal."
93
+ },
94
+ voting_cards_box_title: {
95
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop),
96
+ :description => "Decidim Awesome: title for the voting box."
97
+ },
98
+ voting_cards_instructions: {
99
+ "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop),
100
+ :description => "Decidim Awesome: voting instructions (help modal)."
101
+ }
102
+ }
103
+ proposal_component[:properties][:meta][:properties].merge!(additional_properties)
104
+ proposal_component[:properties][:meta][:required].push(
105
+ :can_create_proposals, :can_vote, :can_comment, :geocoding_enabled, :attachments_allowed, :vote_limit, :votes,
106
+ :awesome_votes_enabled_by_status, :awesome_votes_enabled_states
107
+ )
108
+ proposal_component
109
+ end
110
+ Decidim::RestFull::Core::DefinitionRegistry.register_response_for(:proposal_component)