decidim-chatbot 0.0.1
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.
- checksums.yaml +7 -0
- data/.erb_lint.yml +2134 -0
- data/.github/workflows/website.yml +57 -0
- data/.gitignore +13 -0
- data/.gitlab-ci.yml +204 -0
- data/.node-version +1 -0
- data/.nvmrc +1 -0
- data/.rspec +2 -0
- data/.rubocop.yml +17 -0
- data/.ruby-version +1 -0
- data/.simplecov +36 -0
- data/CONTRIBUTING.md +54 -0
- data/Gemfile +50 -0
- data/Gemfile.lock +1013 -0
- data/LICENSE.md +661 -0
- data/README.md +62 -0
- data/Rakefile +54 -0
- data/app/commands/decidim/chatbot/admin/destroy_item.rb +25 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_exit.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_login.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_propose_proposals.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_read_blogs.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_read_proposals.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_vote_on_proposal.rb +12 -0
- data/app/commands/decidim/chatbot/admin/notifications/update_manual.rb +12 -0
- data/app/commands/decidim/chatbot/admin/notifications/update_phase.rb +12 -0
- data/app/commands/decidim/chatbot/admin/persist_item.rb +47 -0
- data/app/commands/decidim/chatbot/admin/reorder_items.rb +51 -0
- data/app/commands/decidim/chatbot/admin/send_test_notification.rb +74 -0
- data/app/commands/decidim/chatbot/admin/unsubscribe_user.rb +35 -0
- data/app/commands/decidim/chatbot/admin/update_chatbot.rb +17 -0
- data/app/commands/decidim/chatbot/notifications/deliver.rb +30 -0
- data/app/commands/decidim/chatbot/notifications/enqueue_phase.rb +67 -0
- data/app/commands/decidim/chatbot/notifications/send.rb +115 -0
- data/app/commands/decidim/chatbot/whatsapp/create_from_phone.rb +95 -0
- data/app/commands/decidim/chatbot/whatsapp/sync_phone.rb +56 -0
- data/app/controllers/decidim/api/rest_full/chatbot/base_controller.rb +52 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbot_menu_items_controller.rb +49 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbot_users_controller.rb +46 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbots_controller.rb +40 -0
- data/app/controllers/decidim/chatbot/admin/chatbot_edit.rb +85 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/generals_controller.rb +43 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/menu_items_controller.rb +32 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/notifications_controller.rb +92 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/users/filterable.rb +33 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/users_controller.rb +53 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/whatsapps_controller.rb +59 -0
- data/app/controllers/decidim/chatbot/admin/chatbots_controller.rb +20 -0
- data/app/controllers/decidim/chatbot/admin/items_crud.rb +183 -0
- data/app/forms/decidim/chatbot/admin/chatbot_form.rb +20 -0
- data/app/forms/decidim/chatbot/admin/config_form.rb +20 -0
- data/app/forms/decidim/chatbot/admin/menu_item_form.rb +80 -0
- data/app/forms/decidim/chatbot/admin/menu_items/component_id_form.rb +60 -0
- data/app/forms/decidim/chatbot/admin/menu_items/exit_form.rb +24 -0
- data/app/forms/decidim/chatbot/admin/menu_items/login_form.rb +35 -0
- data/app/forms/decidim/chatbot/admin/menu_items/propose_proposals_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/propose_proposals_labels.rb +53 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_blogs_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_content_labels.rb +51 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_proposals_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/vote_on_proposal_form.rb +123 -0
- data/app/forms/decidim/chatbot/admin/notification_item_form.rb +111 -0
- data/app/forms/decidim/chatbot/admin/notifications/manual_form.rb +13 -0
- data/app/forms/decidim/chatbot/admin/notifications/phase_form.rb +79 -0
- data/app/forms/decidim/chatbot/admin/notifications/template_fields.rb +60 -0
- data/app/forms/decidim/chatbot/admin/notifications/test_form.rb +179 -0
- data/app/helpers/decidim/chatbot/admin/items_helper.rb +44 -0
- data/app/jobs/decidim/chatbot/chatbot_webhook_job.rb +59 -0
- data/app/jobs/decidim/chatbot/send_notification_job.rb +33 -0
- data/app/models/decidim/chatbot/chatbot.rb +47 -0
- data/app/models/decidim/chatbot/item.rb +99 -0
- data/app/packs/.keep +0 -0
- data/app/presenters/decidim/chatbot/admin_log/chatbot_presenter.rb +42 -0
- data/app/presenters/decidim/chatbot/admin_log/item_presenter.rb +32 -0
- data/app/queries/decidim/chatbot/admin/proposal_vote_mode.rb +41 -0
- data/app/queries/decidim/chatbot/whatsapp_users.rb +58 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_menu_item_serializer.rb +113 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_notification_serializer.rb +19 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_serializer.rb +66 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_user_identity_serializer.rb +23 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_user_serializer.rb +28 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_webhook_serializer.rb +21 -0
- data/app/services/decidim/chatbot/chatbot_webhook_handler.rb +40 -0
- data/app/services/decidim/chatbot/notifications/bindings.rb +87 -0
- data/app/services/decidim/chatbot/whatsapp/body_parameters.rb +29 -0
- data/app/services/decidim/chatbot/whatsapp/graph_client.rb +136 -0
- data/app/services/decidim/chatbot/whatsapp/status_mapper.rb +93 -0
- data/app/services/decidim/chatbot/whatsapp/template_manager_url.rb +35 -0
- data/app/views/decidim/chatbot/admin/chatbots/generals/_menu_preview.html.erb +49 -0
- data/app/views/decidim/chatbot/admin/chatbots/generals/edit.html.erb +79 -0
- data/app/views/decidim/chatbot/admin/chatbots/index.html.erb +50 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/_form.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/edit.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/index.html.erb +43 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/new.html.erb +12 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_form.html.erb +4 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_manual_fields.html.erb +8 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_phase_fields.html.erb +6 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/edit.html.erb +3 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/index.html.erb +40 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/new.html.erb +11 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/test.html.erb +35 -0
- data/app/views/decidim/chatbot/admin/chatbots/tabs/_whatsapp.html.erb +47 -0
- data/app/views/decidim/chatbot/admin/chatbots/users/_filters.html.erb +23 -0
- data/app/views/decidim/chatbot/admin/chatbots/users/index.html.erb +56 -0
- data/app/views/decidim/chatbot/admin/chatbots/whatsapps/show.html.erb +5 -0
- data/app/views/decidim/chatbot/admin/shared/_item_form.html.erb +64 -0
- data/app/views/decidim/chatbot/admin/shared/_row_actions.html.erb +28 -0
- data/app/views/decidim/chatbot/admin/shared/_tabs_menu.html.erb +13 -0
- data/app/views/decidim/chatbot/admin/shared/_type_fields.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/shared/_type_picker.html.erb +15 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_component_id.html.erb +9 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_exit.html.erb +5 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_login.html.erb +8 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_manual.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_phase.html.erb +46 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_propose_proposals.html.erb +10 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_read_blogs.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_read_proposals.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_vote_on_proposal.html.erb +26 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_abstention_label.html.erb +7 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_vote_label.html.erb +7 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_votes_closed_warning.html.erb +6 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_weight_labels.html.erb +15 -0
- data/bin/check +13 -0
- data/bin/ci-setup +38 -0
- data/bin/gen-node-client +112 -0
- data/bin/gen-openapi +248 -0
- data/bin/i18n-tasks +27 -0
- data/bin/postversion +17 -0
- data/config/i18n-tasks.yml +38 -0
- data/config/locales/decidim_chatbot.en.yml +323 -0
- data/config/locales/decidim_chatbot.fr.yml +29 -0
- data/contrib/decidim-node-client/.gitignore +4 -0
- data/contrib/decidim-node-client/.npmignore +1 -0
- data/contrib/decidim-node-client/.openapi-generator/FILES +8 -0
- data/contrib/decidim-node-client/.openapi-generator/VERSION +1 -0
- data/contrib/decidim-node-client/.openapi-generator-ignore +13 -0
- data/contrib/decidim-node-client/.yarnrc.yml +5 -0
- data/contrib/decidim-node-client/README.md +37 -0
- data/contrib/decidim-node-client/api.ts +14193 -0
- data/contrib/decidim-node-client/base.ts +70 -0
- data/contrib/decidim-node-client/common.ts +185 -0
- data/contrib/decidim-node-client/configuration.ts +138 -0
- data/contrib/decidim-node-client/git_push.sh +57 -0
- data/contrib/decidim-node-client/index.ts +16 -0
- data/contrib/decidim-node-client/package.json +49 -0
- data/contrib/decidim-node-client/packup.config.ts +7 -0
- data/contrib/decidim-node-client/tsconfig.json +25 -0
- data/contrib/decidim-node-client/yarn.lock +2367 -0
- data/crowdin.yml +15 -0
- data/db/migrate/20260725120000_create_decidim_chatbot_chatbots.rb +20 -0
- data/db/migrate/20260822120000_create_decidim_chatbot_items.rb +20 -0
- data/db/migrate/20260824120000_add_menu_and_error_copy_to_decidim_chatbot_chatbots.rb +13 -0
- data/db/migrate/20260824140000_add_enabled_to_decidim_chatbot_items.rb +7 -0
- data/decidim-chatbot.gemspec +40 -0
- data/docker-compose.ci.yml +59 -0
- data/docker-compose.yml +47 -0
- data/lib/decidim/chatbot/admin_engine.rb +67 -0
- data/lib/decidim/chatbot/admin_menu.rb +41 -0
- data/lib/decidim/chatbot/default_manifests.rb +91 -0
- data/lib/decidim/chatbot/engine.rb +107 -0
- data/lib/decidim/chatbot/manifest.rb +31 -0
- data/lib/decidim/chatbot/menu_item_label_keys.rb +26 -0
- data/lib/decidim/chatbot/registry.rb +42 -0
- data/lib/decidim/chatbot/rest_full_ability.rb +24 -0
- data/lib/decidim/chatbot/settings_tab.rb +18 -0
- data/lib/decidim/chatbot/test_definitions.rb +352 -0
- data/lib/decidim/chatbot/variable_interpolator.rb +30 -0
- data/lib/decidim/chatbot/version.rb +21 -0
- data/lib/decidim/chatbot/webhook_publisher.rb +29 -0
- data/lib/decidim/chatbot.rb +41 -0
- data/lib/decidim-chatbot.rb +3 -0
- data/lib/tasks/chatbot.rake +64 -0
- data/openapi/openapi.json +37801 -0
- data/openapitools.json +7 -0
- data/package.json +22 -0
- data/prettier.config.js +15 -0
- data/spec/commands/decidim/chatbot/admin/destroy_item_spec.rb +24 -0
- data/spec/commands/decidim/chatbot/admin/persist_item_spec.rb +80 -0
- data/spec/commands/decidim/chatbot/admin/reorder_items_spec.rb +53 -0
- data/spec/commands/decidim/chatbot/admin/send_test_notification_spec.rb +105 -0
- data/spec/commands/decidim/chatbot/admin/unsubscribe_user_spec.rb +46 -0
- data/spec/commands/decidim/chatbot/admin/update_chatbot_spec.rb +56 -0
- data/spec/commands/decidim/chatbot/notifications/enqueue_phase_spec.rb +59 -0
- data/spec/commands/decidim/chatbot/notifications/send_spec.rb +101 -0
- data/spec/commands/decidim/chatbot/whatsapp/create_from_phone_spec.rb +122 -0
- data/spec/commands/decidim/chatbot/whatsapp/sync_phone_spec.rb +110 -0
- data/spec/controllers/decidim/chatbot/admin/chatbots_controller_spec.rb +25 -0
- data/spec/factories/chatbot.rb +44 -0
- data/spec/forms/decidim/chatbot/admin/chatbot_form_spec.rb +41 -0
- data/spec/forms/decidim/chatbot/admin/config_form_spec.rb +30 -0
- data/spec/forms/decidim/chatbot/admin/menu_item_form_spec.rb +49 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/component_id_form_spec.rb +54 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/component_id_forms_spec.rb +512 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/exit_form_spec.rb +39 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/login_form_spec.rb +101 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/propose_proposals_form_prefill_spec.rb +26 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/read_blogs_form_prefill_spec.rb +22 -0
- data/spec/forms/decidim/chatbot/admin/notifications/manual_form_spec.rb +130 -0
- data/spec/forms/decidim/chatbot/admin/notifications/phase_form_spec.rb +79 -0
- data/spec/forms/decidim/chatbot/admin/notifications/test_form_spec.rb +172 -0
- data/spec/jobs/decidim/chatbot/chatbot_webhook_job_spec.rb +47 -0
- data/spec/jobs/decidim/chatbot/send_notification_job_spec.rb +53 -0
- data/spec/lib/decidim/chatbot/engine_spec.rb +44 -0
- data/spec/lib/decidim/chatbot/registry_spec.rb +30 -0
- data/spec/lib/decidim/chatbot/settings_tab_spec.rb +21 -0
- data/spec/lib/decidim/chatbot/variable_interpolator_spec.rb +34 -0
- data/spec/lib/decidim/chatbot/whatsapp/status_mapper_spec.rb +64 -0
- data/spec/lib/decidim/chatbot_spec.rb +59 -0
- data/spec/models/decidim/chatbot/chatbot_spec.rb +23 -0
- data/spec/models/decidim/chatbot/item_spec.rb +88 -0
- data/spec/presenters/decidim/chatbot/admin_log/chatbot_presenter_spec.rb +64 -0
- data/spec/queries/decidim/chatbot/admin/proposal_vote_mode_spec.rb +74 -0
- data/spec/queries/decidim/chatbot/whatsapp_users_spec.rb +57 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbot_menu_items_controller_spec.rb +344 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbot_users_controller_spec.rb +140 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbots_controller_spec.rb +195 -0
- data/spec/requests/decidim/chatbot/admin/chatbots/notifications_controller_spec.rb +44 -0
- data/spec/requests/decidim/chatbot/admin/tenant_isolation_spec.rb +133 -0
- data/spec/requests/decidim/chatbot/admin/unsubscribe_user_spec.rb +28 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_menu_item_serializer_spec.rb +321 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_serializer_spec.rb +49 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_user_serializer_spec.rb +40 -0
- data/spec/services/decidim/chatbot/chatbot_webhook_handler_spec.rb +43 -0
- data/spec/services/decidim/chatbot/notifications/bindings_spec.rb +86 -0
- data/spec/services/decidim/chatbot/whatsapp/body_parameters_spec.rb +22 -0
- data/spec/services/decidim/chatbot/whatsapp/graph_client_spec.rb +205 -0
- data/spec/services/decidim/chatbot/whatsapp/template_manager_url_spec.rb +33 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/support/allow_all_hosts.rb +9 -0
- data/spec/support/chatbot_admin.rb +16 -0
- data/spec/support/decidim_factories.rb +7 -0
- data/spec/support/decidim_system_factories.rb +3 -0
- data/spec/support/devise.rb +5 -0
- data/spec/support/rack_test_system_driver.rb +43 -0
- data/spec/support/whatsapp_graph.rb +15 -0
- data/spec/swagger_helper.rb +47 -0
- data/spec/system/decidim/chatbot/admin/admin_can_send_manual_when_ready_spec.rb +35 -0
- data/spec/system/decidim/chatbot/admin/admin_configures_vote_on_proposal_menu_item_spec.rb +84 -0
- data/spec/system/decidim/chatbot/admin/admin_confirms_send_now_recipient_count_spec.rb +40 -0
- data/spec/system/decidim/chatbot/admin/admin_creates_menu_item_with_demo_field_spec.rb +65 -0
- data/spec/system/decidim/chatbot/admin/admin_creates_phase_and_manual_notifications_spec.rb +52 -0
- data/spec/system/decidim/chatbot/admin/admin_does_not_see_other_org_processes_in_phase_select_spec.rb +27 -0
- data/spec/system/decidim/chatbot/admin/admin_does_not_see_other_organization_chatbots_spec.rb +25 -0
- data/spec/system/decidim/chatbot/admin/admin_is_redirected_when_chatbot_is_disabled_spec.rb +28 -0
- data/spec/system/decidim/chatbot/admin/admin_lists_organization_chatbots_spec.rb +36 -0
- data/spec/system/decidim/chatbot/admin/admin_manual_notification_sends_with_nickname_spec.rb +68 -0
- data/spec/system/decidim/chatbot/admin/admin_navigates_chatbot_main_tabs_spec.rb +52 -0
- data/spec/system/decidim/chatbot/admin/admin_phase_notification_sends_on_step_change_spec.rb +79 -0
- data/spec/system/decidim/chatbot/admin/admin_picks_published_blog_component_spec.rb +205 -0
- data/spec/system/decidim/chatbot/admin/admin_picks_published_participatory_space_spec.rb +45 -0
- data/spec/system/decidim/chatbot/admin/admin_reorders_chatbot_menu_items_spec.rb +29 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_disabled_menu_item_faded_on_the_table_spec.rb +23 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_disabled_notification_item_faded_on_the_table_spec.rb +40 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_notification_facts_and_frozen_template_name_spec.rb +71 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_propose_proposals_defaults_prefilled_spec.rb +119 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_type_specific_fields_spec.rb +89 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_whatsapp_facts_and_can_sync_spec.rb +48 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_whatsapp_template_edit_link_spec.rb +37 -0
- data/spec/system/decidim/chatbot/admin/admin_tests_notification_spec.rb +125 -0
- data/spec/system/decidim/chatbot/admin/admin_unchecks_enabled_on_a_menu_item_spec.rb +30 -0
- data/spec/system/decidim/chatbot/admin/admin_unsubscribes_whatsapp_user_from_chatbot_spec.rb +35 -0
- data/spec/system/decidim/chatbot/admin/admin_updates_welcome_message_from_general_tab_spec.rb +84 -0
- data/spec/system/decidim/chatbot/system/admin_updates_organization_chatbot_settings_spec.rb +33 -0
- data/spec/tasks/chatbot_create_spec.rb +99 -0
- data/spec/tasks/chatbot_sync_spec.rb +90 -0
- data/website/.node-version +1 -0
- data/website/.nvmrc +1 -0
- data/website/docs/developer/_category_.json +4 -0
- data/website/docs/developer/contribute.md +87 -0
- data/website/docs/index.md +42 -0
- data/website/docs/integrate/_category_.json +4 -0
- data/website/docs/integrate/index.md +214 -0
- data/website/docusaurus.config.ts +93 -0
- data/website/package.json +37 -0
- data/website/sidebars.ts +7 -0
- data/website/src/css/custom.css +37 -0
- data/website/static/api-docs.html +2218 -0
- data/website/static/img/logo.svg +1 -0
- data/website/static/openapi.json +37801 -0
- data/website/tsconfig.json +6 -0
- data/website/yarn.lock +9782 -0
- data/yarn.lock +1003 -0
- metadata +424 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module MenuItems
|
|
7
|
+
class VoteOnProposalForm < ComponentIdForm
|
|
8
|
+
translatable_attribute :action_prompt_label, String
|
|
9
|
+
translatable_attribute :menu_button_label, String
|
|
10
|
+
translatable_attribute :menu_connector_label, String
|
|
11
|
+
translatable_attribute :skip_menu_button, String
|
|
12
|
+
translatable_attribute :vote_label, String
|
|
13
|
+
translatable_attribute :weight_1_label, String
|
|
14
|
+
translatable_attribute :weight_2_label, String
|
|
15
|
+
translatable_attribute :weight_3_label, String
|
|
16
|
+
translatable_attribute :abstention_label, String
|
|
17
|
+
|
|
18
|
+
validates :action_prompt_label, :menu_button_label, :menu_connector_label, :skip_menu_button, translatable_presence: true
|
|
19
|
+
validates :vote_label, translatable_presence: true, if: :requires_vote_label?
|
|
20
|
+
validates :weight_1_label, :weight_2_label, :weight_3_label,
|
|
21
|
+
translatable_presence: true, if: :requires_weight_labels?
|
|
22
|
+
validates :abstention_label, translatable_presence: true, if: :requires_abstention_label?
|
|
23
|
+
|
|
24
|
+
def map_model(model)
|
|
25
|
+
super
|
|
26
|
+
@mapped_organization = model.chatbot.organization
|
|
27
|
+
map_label_settings(model.settings)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def settings_hash
|
|
31
|
+
super.merge(label_settings)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def shows_vote_behavior_fields?
|
|
35
|
+
persisted?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def shows_votes_closed_warning?
|
|
39
|
+
selected_component.present? && !vote_mode.votes_open?
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def shows_vote_label?
|
|
43
|
+
vote_mode.simple_vote?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def shows_weight_labels?
|
|
47
|
+
vote_mode.voting_cards?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def shows_abstention_label?
|
|
51
|
+
vote_mode.abstention?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def requires_vote_label?
|
|
57
|
+
shows_vote_behavior_fields? && shows_vote_label?
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def requires_weight_labels?
|
|
61
|
+
shows_vote_behavior_fields? && shows_weight_labels?
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def requires_abstention_label?
|
|
65
|
+
shows_vote_behavior_fields? && shows_abstention_label?
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def vote_mode
|
|
69
|
+
@vote_mode ||= ProposalVoteMode.new(selected_component)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def selected_component
|
|
73
|
+
return if component_id.blank?
|
|
74
|
+
|
|
75
|
+
@selected_component ||= eligible_components.find { |item| item.id == component_id }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def map_label_settings(settings)
|
|
79
|
+
self.action_prompt_label = translations_from(settings["action_prompt_label"])
|
|
80
|
+
self.menu_button_label = translations_from(settings["menu_button_label"])
|
|
81
|
+
self.menu_connector_label = translations_from(settings["menu_connector_label"])
|
|
82
|
+
self.skip_menu_button = translations_from(settings["skip_menu_button"])
|
|
83
|
+
self.vote_label = translations_from(settings["vote_label"])
|
|
84
|
+
self.weight_1_label = translations_from(settings["weight_1_label"])
|
|
85
|
+
self.weight_2_label = translations_from(settings["weight_2_label"])
|
|
86
|
+
self.weight_3_label = translations_from(settings["weight_3_label"])
|
|
87
|
+
self.abstention_label = translations_from(settings["abstention_label"])
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def label_settings
|
|
91
|
+
{
|
|
92
|
+
"action_prompt_label" => translations_from(action_prompt_label),
|
|
93
|
+
"menu_button_label" => translations_from(menu_button_label),
|
|
94
|
+
"menu_connector_label" => translations_from(menu_connector_label),
|
|
95
|
+
"skip_menu_button" => translations_from(skip_menu_button)
|
|
96
|
+
}.merge(mode_label_settings)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def mode_label_settings
|
|
100
|
+
return simple_label_settings if shows_vote_label?
|
|
101
|
+
return weight_label_settings if shows_weight_labels?
|
|
102
|
+
|
|
103
|
+
{}
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def simple_label_settings
|
|
107
|
+
{ "vote_label" => translations_from(vote_label) }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def weight_label_settings
|
|
111
|
+
labels = {
|
|
112
|
+
"weight_1_label" => translations_from(weight_1_label),
|
|
113
|
+
"weight_2_label" => translations_from(weight_2_label),
|
|
114
|
+
"weight_3_label" => translations_from(weight_3_label)
|
|
115
|
+
}
|
|
116
|
+
labels["abstention_label"] = translations_from(abstention_label) if shows_abstention_label?
|
|
117
|
+
labels
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
class NotificationItemForm < MenuItemForm
|
|
7
|
+
include Notifications::TemplateFields
|
|
8
|
+
|
|
9
|
+
TEMPLATE_ID = /\A\d+\z/
|
|
10
|
+
|
|
11
|
+
translatable_attribute :template_id, String
|
|
12
|
+
translatable_attribute :variables_json, String
|
|
13
|
+
|
|
14
|
+
validates :template_id, translatable_presence: true
|
|
15
|
+
validate :template_ids_must_be_numeric
|
|
16
|
+
validate :variables_must_be_json_objects, if: :shows_variables_json?
|
|
17
|
+
validate :variable_keys_must_follow_facebook_pattern, if: :shows_variables_json?
|
|
18
|
+
validate :interpolation_tokens_must_be_allowed, if: :shows_variables_json?
|
|
19
|
+
|
|
20
|
+
def scope_name
|
|
21
|
+
"notification"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def validates_title?
|
|
25
|
+
false
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def shows_title?
|
|
29
|
+
false
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def shows_introduction?
|
|
33
|
+
false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def map_model(model)
|
|
37
|
+
@mapped_organization = model.chatbot.organization
|
|
38
|
+
self.template_id = translations_from(model.settings["template_id"])
|
|
39
|
+
self.variables_json = encode_stored_variables(model.settings["variables"])
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def settings_hash
|
|
43
|
+
{ "template_id" => stringify_hash(template_id), "variables" => parse_form_variables }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def variables_json
|
|
47
|
+
return stringify_hash(super).with_indifferent_access if locales_organization.blank?
|
|
48
|
+
|
|
49
|
+
locale_keys.index_with { |locale| stringify_hash(super)[locale].presence || "{}" }.with_indifferent_access
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def template_ids_must_be_numeric
|
|
55
|
+
locale_keys.each do |locale|
|
|
56
|
+
value = stringify_hash(template_id)[locale].to_s
|
|
57
|
+
next if value.blank?
|
|
58
|
+
next if value.match?(TEMPLATE_ID)
|
|
59
|
+
|
|
60
|
+
errors.add(:template_id, :invalid)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def variables_must_be_json_objects
|
|
65
|
+
locale_keys.each { |locale| validate_json_object(locale) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def validate_json_object(locale)
|
|
69
|
+
parsed_object(form_json(locale))
|
|
70
|
+
rescue JSON::ParserError
|
|
71
|
+
errors.add(:variables_json, :invalid)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def parse_form_variables
|
|
75
|
+
locale_keys.index_with { |locale| parsed_object(form_json(locale)) }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def form_json(locale)
|
|
79
|
+
stringify_hash(variables_json)[locale].presence || "{}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def parsed_object(raw)
|
|
83
|
+
value = JSON.parse(raw.presence || "{}")
|
|
84
|
+
raise JSON::ParserError, "not an object" unless value.is_a?(Hash)
|
|
85
|
+
|
|
86
|
+
value
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def encode_stored_variables(value)
|
|
90
|
+
locale_keys.index_with { |locale| JSON.generate(stored_object(value, locale)) }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def stored_object(value, locale)
|
|
94
|
+
return locale_object(value, locale) if locale_keyed?(value)
|
|
95
|
+
return stringify_hash(value) if locale == default_locale && value.is_a?(Hash)
|
|
96
|
+
|
|
97
|
+
{}
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def locale_keyed?(value)
|
|
101
|
+
value.is_a?(Hash) && stringify_hash(value).keys.intersect?(locale_keys)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def locale_object(value, locale)
|
|
105
|
+
item = stringify_hash(value)[locale]
|
|
106
|
+
item.is_a?(Hash) ? item : {}
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module Notifications
|
|
7
|
+
class PhaseForm < NotificationItemForm
|
|
8
|
+
VARIABLES = %w(space_name phase_name phase_start phase_end).freeze
|
|
9
|
+
|
|
10
|
+
attribute :participatory_space_gid, String
|
|
11
|
+
attribute :use_space_name, :boolean, default: false
|
|
12
|
+
attribute :use_phase_name, :boolean, default: false
|
|
13
|
+
attribute :use_phase_start, :boolean, default: false
|
|
14
|
+
attribute :use_phase_end, :boolean, default: false
|
|
15
|
+
|
|
16
|
+
validates :participatory_space_gid, presence: true
|
|
17
|
+
validate :space_must_be_allowed
|
|
18
|
+
|
|
19
|
+
def shows_variables_json?
|
|
20
|
+
false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def map_model(model)
|
|
24
|
+
super
|
|
25
|
+
self.participatory_space_gid = model.settings["participatory_space_gid"]
|
|
26
|
+
assign_variable_flags(model)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def settings_hash
|
|
30
|
+
super.merge(
|
|
31
|
+
"participatory_space_gid" => participatory_space_gid,
|
|
32
|
+
"variables" => selected_variables
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def space_choices
|
|
37
|
+
allowed_spaces.map { |space| [space_label(space), space.to_gid.to_s] }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def assign_variable_flags(model)
|
|
43
|
+
keys = model.variables_hash.keys.map(&:to_s)
|
|
44
|
+
self.use_space_name = keys.include?("space_name")
|
|
45
|
+
self.use_phase_name = keys.include?("phase_name")
|
|
46
|
+
self.use_phase_start = keys.include?("phase_start")
|
|
47
|
+
self.use_phase_end = keys.include?("phase_end")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def selected_variables
|
|
51
|
+
selected_variable_keys.index_with { |key| "%{#{key}}" }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def selected_variable_keys
|
|
55
|
+
VARIABLES.select { |key| public_send(:"use_#{key}") }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def space_must_be_allowed
|
|
59
|
+
return if allowed_space_gids.include?(participatory_space_gid.to_s)
|
|
60
|
+
|
|
61
|
+
errors.add(:participatory_space_gid, :inclusion)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def allowed_space_gids
|
|
65
|
+
allowed_spaces.map { |space| space.to_gid.to_s }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def allowed_spaces
|
|
69
|
+
@allowed_spaces ||= Array(current_organization.public_participatory_spaces)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def space_label(space)
|
|
73
|
+
translated_attribute(space.title)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module Notifications
|
|
7
|
+
# Shared template name / variables JSON rules for notification forms.
|
|
8
|
+
module TemplateFields
|
|
9
|
+
SHARED_KEYS = %w(organization_name chatbot_name user_name nickname).freeze
|
|
10
|
+
TOKEN = /%\{(\w+)\}/
|
|
11
|
+
FACEBOOK_KEY = /\A[a-z_]+\z/
|
|
12
|
+
|
|
13
|
+
def interpolation_keys
|
|
14
|
+
SHARED_KEYS
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def shows_variables_json?
|
|
18
|
+
true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def interpolation_tokens_must_be_allowed
|
|
24
|
+
unknown_tokens.each { errors.add(:variables_json, :invalid) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def variable_keys_must_follow_facebook_pattern
|
|
28
|
+
invalid_facebook_keys.each { errors.add(:variables_json, :invalid) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def invalid_facebook_keys
|
|
32
|
+
collected_keys.grep_v(FACEBOOK_KEY)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def collected_keys
|
|
36
|
+
parsed_variable_maps.flat_map { |_locale, hash| hash.keys.map(&:to_s) }.uniq
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def unknown_tokens
|
|
40
|
+
collected_tokens - interpolation_keys
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def collected_tokens
|
|
44
|
+
parsed_variable_maps.flat_map { |_locale, hash| tokens_in(hash) }.uniq
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def tokens_in(hash)
|
|
48
|
+
hash.values.flat_map { |value| value.to_s.scan(TOKEN).flatten }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def parsed_variable_maps
|
|
52
|
+
parse_form_variables
|
|
53
|
+
rescue JSON::ParserError
|
|
54
|
+
{}
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module Notifications
|
|
7
|
+
class TestForm < Decidim::Form
|
|
8
|
+
include Decidim::TranslatableAttributes
|
|
9
|
+
|
|
10
|
+
mimic :test
|
|
11
|
+
|
|
12
|
+
attribute :locale, String
|
|
13
|
+
attribute :phone_number, String
|
|
14
|
+
attribute :step_id, Integer
|
|
15
|
+
attribute :values, Hash, default: -> { {} }
|
|
16
|
+
|
|
17
|
+
validates :locale, inclusion: { in: :available_locales }
|
|
18
|
+
validates :phone_number, presence: true
|
|
19
|
+
validates :step_id, presence: true, if: :phase?
|
|
20
|
+
validate :phone_must_be_digits
|
|
21
|
+
validate :step_must_belong, if: :phase?
|
|
22
|
+
|
|
23
|
+
def hydrate
|
|
24
|
+
self.locale = default_locale if locale.blank?
|
|
25
|
+
self.values = stringify_hash(locale_variables)
|
|
26
|
+
self
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def valid?(*)
|
|
30
|
+
self.locale = default_locale if locale.blank?
|
|
31
|
+
super
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def values
|
|
35
|
+
stringify_hash(super)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def phase?
|
|
39
|
+
item.manifest_name == "phase"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def item
|
|
43
|
+
context.item
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def variable_keys
|
|
47
|
+
locale_variables.keys
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def step_choices
|
|
51
|
+
steps.map { |step| [translated_attribute(step.title), step.id] }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def normalized_phone
|
|
55
|
+
phone_number.to_s.gsub(/\D/, "")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def template_id
|
|
59
|
+
hash = stringify_hash(item.settings["template_id"])
|
|
60
|
+
hash[locale].presence || hash[default_locale].presence || hash.values.find(&:present?).to_s
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def body_parameters
|
|
64
|
+
Decidim::Chatbot::Whatsapp::BodyParameters.from(payload_values)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def locale_variables
|
|
68
|
+
pick_locale_hash(stringify_hash(item.settings["variables"]))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def phone_must_be_digits
|
|
74
|
+
return if phone_number.blank?
|
|
75
|
+
return if normalized_phone.match?(/\A\d{8,15}\z/)
|
|
76
|
+
|
|
77
|
+
errors.add(:phone_number, :invalid)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def step_must_belong
|
|
81
|
+
return if step_id.blank? || selected_step
|
|
82
|
+
|
|
83
|
+
errors.add(:step_id, :inclusion)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def selected_step
|
|
87
|
+
steps.find { |step| step.id == step_id.to_i }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def steps
|
|
91
|
+
return [] unless space.respond_to?(:steps)
|
|
92
|
+
|
|
93
|
+
space.steps.order(:position)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def space
|
|
97
|
+
@space ||= locate_space
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def locate_space
|
|
101
|
+
gid = item.settings["participatory_space_gid"].presence
|
|
102
|
+
return if gid.blank?
|
|
103
|
+
|
|
104
|
+
located = GlobalID::Locator.locate(gid)
|
|
105
|
+
located if located.try(:organization) == current_organization
|
|
106
|
+
rescue ActiveRecord::RecordNotFound
|
|
107
|
+
nil
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def payload_values
|
|
111
|
+
return interpolator.expand(locale_variables) if phase?
|
|
112
|
+
|
|
113
|
+
locale_variables.keys.index_with { |key| stringify_hash(values)[key].to_s }
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def interpolator
|
|
117
|
+
VariableInterpolator.new(phase_bindings)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def phase_bindings
|
|
121
|
+
I18n.with_locale(locale) do
|
|
122
|
+
{
|
|
123
|
+
"organization_name" => current_organization.name,
|
|
124
|
+
"chatbot_name" => translated_attribute(item.chatbot.name),
|
|
125
|
+
"user_name" => "",
|
|
126
|
+
"space_name" => space_title,
|
|
127
|
+
"phase_name" => step_title,
|
|
128
|
+
"phase_start" => Decidim::Chatbot::Notifications::Bindings.format_date(selected_step&.start_date),
|
|
129
|
+
"phase_end" => Decidim::Chatbot::Notifications::Bindings.format_date(selected_step&.end_date)
|
|
130
|
+
}
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def space_title
|
|
135
|
+
return "" unless space
|
|
136
|
+
|
|
137
|
+
I18n.with_locale(locale) { translated_attribute(space.title) }
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def step_title
|
|
141
|
+
return "" unless selected_step
|
|
142
|
+
|
|
143
|
+
I18n.with_locale(locale) { translated_attribute(selected_step.title) }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def pick_locale_hash(keyed)
|
|
147
|
+
picked = keyed[locale]
|
|
148
|
+
return stringify_hash(picked) if picked.is_a?(Hash)
|
|
149
|
+
|
|
150
|
+
picked = keyed[default_locale]
|
|
151
|
+
return stringify_hash(picked) if picked.is_a?(Hash)
|
|
152
|
+
return keyed unless locale_keyed?(keyed)
|
|
153
|
+
|
|
154
|
+
{}
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def locale_keyed?(keyed)
|
|
158
|
+
keyed.keys.intersect?(available_locales)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def available_locales
|
|
162
|
+
current_organization.available_locales.map(&:to_s)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def default_locale
|
|
166
|
+
current_organization.default_locale.to_s
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def stringify_hash(value)
|
|
170
|
+
return {} if value.blank?
|
|
171
|
+
|
|
172
|
+
raw = value.respond_to?(:to_unsafe_h) ? value.to_unsafe_h : value.to_h
|
|
173
|
+
raw.stringify_keys
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module ItemsHelper
|
|
7
|
+
def item_row_style(item)
|
|
8
|
+
"opacity: 0.6" unless item.enabled?
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def notification_template_id_help
|
|
12
|
+
url = Decidim::Chatbot::Whatsapp::TemplateManagerUrl.message_templates_for(current_organization)
|
|
13
|
+
if url.present?
|
|
14
|
+
link = link_to(
|
|
15
|
+
t("decidim.chatbot.admin.items.message_templates"),
|
|
16
|
+
url,
|
|
17
|
+
target: "_blank",
|
|
18
|
+
rel: "noopener noreferrer"
|
|
19
|
+
)
|
|
20
|
+
t("decidim.chatbot.admin.items.template_id_help_html", link:).html_safe
|
|
21
|
+
else
|
|
22
|
+
t("decidim.chatbot.admin.items.template_id_help")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def notification_template_edit_link(form)
|
|
27
|
+
return unless form.object.persisted?
|
|
28
|
+
|
|
29
|
+
template_id = form.object.template_id[I18n.locale.to_s].presence ||
|
|
30
|
+
form.object.template_id.values.find(&:present?)
|
|
31
|
+
url = Decidim::Chatbot::Whatsapp::TemplateManagerUrl.for(current_organization, template_id)
|
|
32
|
+
return if url.blank?
|
|
33
|
+
|
|
34
|
+
link_to(
|
|
35
|
+
t("decidim.chatbot.admin.items.edit_template"),
|
|
36
|
+
url,
|
|
37
|
+
target: "_blank",
|
|
38
|
+
rel: "noopener noreferrer"
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
class ChatbotWebhookJob < ::Decidim::RestFull::ApplicationJob
|
|
6
|
+
def perform(event_name, chatbot_id, organization_id)
|
|
7
|
+
organization = Decidim::Organization.find(organization_id)
|
|
8
|
+
return unless Decidim::RestFull::Core::ModuleAvailability.module_enabled?(organization, :chatbot)
|
|
9
|
+
|
|
10
|
+
chatbot = Decidim::Chatbot::Chatbot.find(chatbot_id)
|
|
11
|
+
return unless chatbot.decidim_organization_id == organization.id
|
|
12
|
+
|
|
13
|
+
permissions_for(event_name, organization).each do |permission|
|
|
14
|
+
dispatch_for_permission(permission, event_name, chatbot, organization)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def permissions_for(event_name, organization)
|
|
21
|
+
Decidim::RestFull::Core::Permission.where(permission: event_name, api_client: organization.api_clients)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def dispatch_for_permission(permission, event_name, chatbot, organization)
|
|
25
|
+
serializer = Decidim::Api::RestFull::Chatbot::ChatbotWebhookSerializer.new(
|
|
26
|
+
chatbot,
|
|
27
|
+
event_name:,
|
|
28
|
+
organization:
|
|
29
|
+
)
|
|
30
|
+
payload = build_payload(permission.api_client, serializer, organization)
|
|
31
|
+
return log_invalid_event(event_name, payload) unless payload.valid?
|
|
32
|
+
|
|
33
|
+
webhook_registrations_for(permission.api_client, event_name).each do |registration|
|
|
34
|
+
enqueue_webhook(registration, payload)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def build_payload(api_client, serializer, organization)
|
|
39
|
+
Decidim::RestFull::Core::WebhookEventForm.new(
|
|
40
|
+
**serializer.event_attributes
|
|
41
|
+
).with_context(organization:, api_client:)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def log_invalid_event(event_name, payload)
|
|
45
|
+
Rails.logger.warn("Invalid chatbot webhook event #{event_name}: #{payload.errors.full_messages.join(", ")}")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def webhook_registrations_for(api_client, event_name)
|
|
49
|
+
Decidim::RestFull::Core::WebhookRegistration.where(api_client_id: api_client.id).where(
|
|
50
|
+
"subscriptions @> ?", [event_name].to_json
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def enqueue_webhook(webhook_registration, payload)
|
|
55
|
+
Decidim::RestFull::Core::WebhookJob.perform_later(webhook_registration, payload.as_json, payload.timestamp.to_s)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
class SendNotificationJob < ApplicationJob
|
|
6
|
+
queue_as :default
|
|
7
|
+
|
|
8
|
+
def perform(item_id, actor_id)
|
|
9
|
+
item = Item.find_by(id: item_id)
|
|
10
|
+
return if skip?(item)
|
|
11
|
+
|
|
12
|
+
Notifications::Send.call(item, actor_for(item, actor_id))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def skip?(item)
|
|
18
|
+
item.blank? || !item.enabled? || item.template_id.blank?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def actor_for(item, actor_id)
|
|
22
|
+
user = Decidim::User.find_by(id: actor_id)
|
|
23
|
+
return user if user&.decidim_organization_id == item.organization.id
|
|
24
|
+
|
|
25
|
+
fallback_admin(item)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def fallback_admin(item)
|
|
29
|
+
Decidim::User.where(organization: item.organization, admin: true).order(:id).first
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|