-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add job to set profile info for Signal
- Loading branch information
Showing
6 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
db/migrate/20241022083441_add_messengers_about_text_to_organizations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters