Skip to content

Commit

Permalink
Add job to set profile info for Signal
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Oct 24, 2024
1 parent bbe1688 commit bd8f027
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/controllers/organizations/signal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions app/dashboards/organization_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand All @@ -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[
Expand All @@ -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[
Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions app/jobs/signal_adapter/set_profile_info_job.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bd8f027

Please sign in to comment.