From bd8f0271c3b9e0cabdd75c02fda4530ce10b4e2d Mon Sep 17 00:00:00 2001 From: Matthew Rider Date: Tue, 22 Oct 2024 11:17:05 +0200 Subject: [PATCH] Add job to set profile info for Signal --- .../organizations/signal_controller.rb | 1 + app/dashboards/organization_dashboard.rb | 7 ++- .../signal_adapter/set_profile_info_job.rb | 53 +++++++++++++++++++ app/models/organization.rb | 1 + ..._messengers_about_text_to_organizations.rb | 7 +++ db/schema.rb | 6 ++- 6 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 app/jobs/signal_adapter/set_profile_info_job.rb create mode 100644 db/migrate/20241022083441_add_messengers_about_text_to_organizations.rb diff --git a/app/controllers/organizations/signal_controller.rb b/app/controllers/organizations/signal_controller.rb index 9ece2eac3..25a04bd7f 100644 --- a/app/controllers/organizations/signal_controller.rb +++ b/app/controllers/organizations/signal_controller.rb @@ -24,6 +24,7 @@ def verify when Net::HTTPSuccess SignalAdapter::SetTrustModeJob.perform_later(signal_server_phone_number: @organization.signal_server_phone_number) SignalAdapter::SetUsernameJob.perform_later(organization_id: @organization.id) + SignalAdapter::SetProfileInfoJob.perform_later(organization_id: @organization.id) else handle_error_response(response) render :verify_form, status: :unprocessable_entity diff --git a/app/dashboards/organization_dashboard.rb b/app/dashboards/organization_dashboard.rb index 5ab54421f..12d3763b4 100644 --- a/app/dashboards/organization_dashboard.rb +++ b/app/dashboards/organization_dashboard.rb @@ -30,7 +30,8 @@ class OrganizationDashboard < Administrate::BaseDashboard threemarb_api_identity: Field::String, threemarb_api_secret: Field::String, threemarb_private: Field::String, - signal_server_phone_number: SetupSignalLinkField + signal_server_phone_number: SetupSignalLinkField, + messengers_about_text: Field::String }.freeze COLLECTION_ATTRIBUTES = %i[ @@ -55,6 +56,7 @@ class OrganizationDashboard < Administrate::BaseDashboard telegram_bot_username threemarb_api_identity signal_server_phone_number + messengers_about_text ].freeze FORM_ATTRIBUTES_NEW = %i[ @@ -78,6 +80,7 @@ class OrganizationDashboard < Administrate::BaseDashboard threemarb_api_secret threemarb_private signal_server_phone_number + messengers_about_text ].freeze FORM_ATTRIBUTES_EDIT = %i[ @@ -94,7 +97,7 @@ class OrganizationDashboard < Administrate::BaseDashboard onboarding_additional_consent_text onboarding_allowed channel_image - signal_complete_onboarding_link + messengers_about_text ].freeze COLLECTION_FILTERS = {}.freeze diff --git a/app/jobs/signal_adapter/set_profile_info_job.rb b/app/jobs/signal_adapter/set_profile_info_job.rb new file mode 100644 index 000000000..31c5c95cf --- /dev/null +++ b/app/jobs/signal_adapter/set_profile_info_job.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require 'net/http' + +module SignalAdapter + class SetProfileInfoJob < ApplicationJob + attr_reader :organization + + def perform(organization_id:) + @organization = Organization.find(organization_id) + uri = URI.parse("#{ENV.fetch('SIGNAL_CLI_REST_API_ENDPOINT', 'http://localhost:8080')}/v1/profiles/#{organization.signal_server_phone_number}") + request = Net::HTTP::Put.new(uri, { + Accept: 'application/json', + 'Content-Type': 'application/json' + }) + + request.body = update_profile_payload.to_json + response = Net::HTTP.start(uri.host, uri.port) do |http| + http.request(request) + end + case response + when Net::HTTPSuccess + Rails.logger.debug 'Great!' + else + handle_error(response) + end + end + + private + + def update_profile_payload + { + base64_avatar: Base64.encode64( + File.open(ActiveStorage::Blob.service.path_for(organization.channel_image.attachment.blob.key), 'rb').read + ), + name: organization.name, + about: organization.messengers_about_text + } + end + + def handle_error(response) + error_message = JSON.parse(response.body)['error'] + exception = SignalAdapter::BadRequestError.new(error_code: response.code, message: error_message) + context = { + code: response.code, + message: response.message, + headers: response.to_hash, + body: error_message + } + ErrorNotifier.report(exception, context: context) + end + end +end diff --git a/app/models/organization.rb b/app/models/organization.rb index bfbafaf84..489b829f2 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -28,6 +28,7 @@ class Organization < ApplicationRecord validates :telegram_bot_username, uniqueness: true, allow_nil: true validates :signal_server_phone_number, phony_plausible: true + validates :messengers_about_text, length: { maximum: 139 }, allow_blank: true def channels_onboarding_allowed { diff --git a/db/migrate/20241022083441_add_messengers_about_text_to_organizations.rb b/db/migrate/20241022083441_add_messengers_about_text_to_organizations.rb new file mode 100644 index 000000000..5de566b43 --- /dev/null +++ b/db/migrate/20241022083441_add_messengers_about_text_to_organizations.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddMessengersAboutTextToOrganizations < ActiveRecord::Migration[6.1] + def change + add_column :organizations, :messengers_about_text, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e2206882e..0f3ca5ada 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2024_10_10_111754) do +ActiveRecord::Schema.define(version: 2024_10_22_083441) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -127,6 +127,9 @@ t.index ["organization_id"], name: "index_contributors_on_organization_id" end + create_table "data_migrations", primary_key: "version", id: :string, force: :cascade do |t| + end + create_table "delayed_jobs", force: :cascade do |t| t.integer "priority", default: 0, null: false t.integer "attempts", default: 0, null: false @@ -234,6 +237,7 @@ t.jsonb "onboarding_allowed", default: {"email"=>true, "signal"=>true, "threema"=>true, "telegram"=>true, "whats_app"=>true} t.jsonb "twilio_content_sids", default: {"new_request_day1"=>"", "new_request_day2"=>"", "new_request_day3"=>"", "new_request_night1"=>"", "new_request_night2"=>"", "new_request_night3"=>"", "new_request_evening1"=>"", "new_request_evening2"=>"", "new_request_evening3"=>"", "new_request_morning1"=>"", "new_request_morning2"=>"", "new_request_morning3"=>""} t.string "signal_complete_onboarding_link" + t.string "messengers_about_text" t.index ["business_plan_id"], name: "index_organizations_on_business_plan_id" t.index ["contact_person_id"], name: "index_organizations_on_contact_person_id" t.index ["telegram_bot_username"], name: "index_organizations_on_telegram_bot_username", unique: true