Skip to content

Commit

Permalink
Feedback widget implementation, part I
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Feb 22, 2021
1 parent 3742dc1 commit 5b7d4b7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/controllers/api/feedbacks_controller.rb
@@ -0,0 +1,12 @@
module Api
class FeedbacksController < AbstractConfigController
def create
message = raw_json[:message]
slug = raw_json[:slug]
if message
current_device.delay.provide_feedback(message, slug)
end
render json: {}
end
end
end
20 changes: 20 additions & 0 deletions app/models/device.rb
Expand Up @@ -235,4 +235,24 @@ def perform_gradual_upgrade
def send_upgrade_request
Transport.current.amqp_send(UPGRADE_RPC, id, "from_clients")
end

def provide_feedback(message, slug = "Not provided")
webhook_url = ENV["FEEDBACK_WEBHOOK_URL"]
if webhook_url
email = self.users.pluck(:email).join(" ")
firmware_kind = fbos_config.firmware_hardware
payload = {
"text": [
"`Device ID`: #{id}",
"`Email`: #{email}",
"`Model`: #{firmware_kind}",
"`Slug`: #{slug}",
"`Message`: #{message}"
].join("\n")
}.to_json
Faraday.post(webhook_url,
payload,
"Content-Type" => "application/json")
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -29,6 +29,7 @@

# Singular API Resources:
{
feedback: [:create],
demo_account: [:create],
device: [:create, :destroy, :show, :update],
fbos_config: [:destroy, :show, :update],
Expand Down
2 changes: 2 additions & 0 deletions example.env
Expand Up @@ -152,3 +152,5 @@ DATADOG_CLIENT_TOKEN=??
# report of new FarmBot installations (not new users, but
# actual FarmBot installations).
CUSTOMER_SUPPORT_SUBSCRIBERS=alice@protonmail.com,bob@yahoo.com
# URL to send user-generated feedback to.
FEEDBACK_WEBHOOK_URL=http://localhost:3000/change_this
18 changes: 18 additions & 0 deletions spec/controllers/api/folders/feedbacks_spec.rb
@@ -0,0 +1,18 @@
require "spec_helper"

describe Api::FeedbacksController do
let(:user) { FactoryBot.create(:user) }
include Devise::Test::ControllerHelpers

it "submits feedback" do
sign_in user
input = { message: "Example message", slug: "Example slug" }
expect(Faraday).to receive(:post).and_return("THIS IS A STUB")
with_modified_env FEEDBACK_WEBHOOK_URL: 'https://localhost:3000/' do
run_jobs_now do
post :create, body: input.to_json
end
end
expect(response.status).to eq(200)
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -116,6 +116,10 @@ def run_jobs_now
Delayed::Worker.delay_jobs = delay_jobs
end

def with_modified_env(options, &block)
ClimateControl.modify(options, &block)
end

# Reassign constants without getting a bunch of warnings to STDOUT.
# This is just for testing purposes, so NBD.
def const_reassign(target, const, value)
Expand Down

0 comments on commit 5b7d4b7

Please sign in to comment.