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 Dec 3, 2024
1 parent a9a9701 commit 508cd06
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
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
6 changes: 5 additions & 1 deletion app/dashboards/organization_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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 @@ -58,6 +59,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 @@ -81,6 +83,7 @@ class OrganizationDashboard < Administrate::BaseDashboard
threemarb_api_secret
threemarb_private
signal_server_phone_number
messengers_about_text
].freeze

FORM_ATTRIBUTES_EDIT = %i[
Expand All @@ -100,6 +103,7 @@ class OrganizationDashboard < Administrate::BaseDashboard
channel_image
signal_complete_onboarding_link
whats_app_quick_reply_button_text
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
4 changes: 4 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
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 @@ -236,6 +239,7 @@
t.string "signal_complete_onboarding_link"
t.jsonb "whats_app_quick_reply_button_text", default: {"more_info"=>"Mehr Infos", "answer_request"=>"Antworten"}
t.string "whats_app_more_info_message", default: ""
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 508cd06

Please sign in to comment.