Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Chatwoot #726

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.development.sample
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ NGROK_AUTHTOKEN=
NGROK_REGION=in
RAILS_MASTER_KEY=
ACTIVE_STORAGE_LOCATION=local
CHATWOOT_WEBSITE_TOKEN=
CHATWOOT_IDENTITY_VERIFICATION_TOKEN=
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ gem "pagy", "~> 6.0"
gem "view_component", "~> 3.0"
gem "activerecord_json_validator", "~> 2.1"
gem "sidekiq-cron", "~> 1.9"
gem "intercom-rails", "~> 0.4.2"
gem "google-apis-firebaseappdistribution_v1", "~> 0.1"
gem "google-apis-firebase_v1beta1", "~> 0.35.0"
gem "initials", "~> 0.4.3"
Expand Down
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,6 @@ GEM
activesupport (>= 6.0.0)
railties (>= 6.0.0)
initials (0.4.3)
intercom-rails (0.4.2)
activesupport (> 3.0)
invisible_captcha (2.3.0)
rails (>= 5.2)
io-console (0.7.2)
Expand Down Expand Up @@ -806,7 +804,6 @@ DEPENDENCIES
http (~> 5.0)
importmap-rails (~> 1.0)
initials (~> 0.4.3)
intercom-rails (~> 0.4.2)
invisible_captcha (~> 2.3)
jbuilder (~> 2.11)
json-schema (~> 4.1)
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/authentication/email/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class Authentication::Email::SessionsController < Devise::SessionsController

before_action :skip_authentication, only: [:new, :create]
before_action :set_confirmed_email, only: [:new]
after_action :prepare_intercom_shutdown, only: [:destroy]
after_action :intercom_shutdown, only: [:new]
after_action :prepare_support_chat_shutdown, only: [:destroy]
after_action :support_chat_shutdown, only: [:new]
after_action :track_login, only: [:create]

def new
Expand All @@ -21,19 +21,19 @@ def destroy

protected

def prepare_intercom_shutdown
IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
end

def intercom_shutdown
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, request.domain)
end

def set_confirmed_email
@confirmed_email = params[:confirmed_email].presence || nil
end

def track_login
SiteAnalytics.track(current_user, current_organization, device, "Login", {email: true})
end

def prepare_support_chat_shutdown
Chatwoot::ShutdownHelper.prepare_chatwoot_shutdown(session)
end

def support_chat_shutdown
Chatwoot::ShutdownHelper.chatwoot_shutdown(session, cookies, request.domain)
end
end
16 changes: 8 additions & 8 deletions app/controllers/authentication/sso/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class Authentication::Sso::SessionsController < ApplicationController
include Authenticatable

before_action :skip_authentication, only: [:new, :create]
after_action :prepare_intercom_shutdown, only: [:destroy]
after_action :intercom_shutdown, only: [:new]
after_action :prepare_support_chat_shutdown, only: [:destroy]
after_action :support_chat_shutdown, only: [:new]

def new
set_invite
Expand Down Expand Up @@ -63,15 +63,15 @@ def set_invite
@invite = Accounts::Invite.find_by(token: invite_token) if invite_token.present?
end

def prepare_intercom_shutdown
IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
def track_login
SiteAnalytics.track(current_user, current_organization, device, "Login", {sso: true})
end

def intercom_shutdown
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, request.domain)
def prepare_support_chat_shutdown
Chatwoot::ShutdownHelper.prepare_chatwoot_shutdown(session)
end

def track_login
SiteAnalytics.track(current_user, current_organization, device, "Login", {sso: true})
def support_chat_shutdown
Chatwoot::ShutdownHelper.chatwoot_shutdown(session, cookies, request.domain)
end
end
4 changes: 4 additions & 0 deletions app/models/accounts/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ def pending_profile?(organization)
github_login.blank? || team_for(organization).blank?
end

def support_chat_identifier
OpenSSL::HMAC.hexdigest("sha256", ENV["CHATWOOT_IDENTITY_VERIFICATION_TOKEN"], id)
end

protected

def confirmation_required?
Expand Down
29 changes: 29 additions & 0 deletions app/views/layouts/signed_in_application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<%= render partial: "shared/favicon" %>
<title><%= page_title(@page_name, current_organization, @app, @release) %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="environment" content="<%= Rails.env %>"/>
<meta name="sentryDSNUrl" content="<%= ENV["FRONTEND_SENTRY_DSN"] %>"/>
<!-- current deploy: <%= Site.git_ref %> at <%= Site.git_ref_at %> -->
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

Expand All @@ -14,6 +17,32 @@

<%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<script nonce="<%= content_security_policy_nonce %>">
(function (d, t) {
let BASE_URL = "https://app.chatwoot.com";
let g = d.createElement(t), s = d.getElementsByTagName(t)[0];

g.src = BASE_URL + "/packs/js/sdk.js";
g.defer = true;
g.async = true;
s.parentNode.insertBefore(g, s);
g.onload = function () {
window.chatwootSDK.run({
websiteToken: "<%= ENV["CHATWOOT_WEBSITE_TOKEN"] %>",
baseUrl: BASE_URL
})
}
})(document, "script");

window.addEventListener("chatwoot:ready", function () {
window.$chatwoot.setUser("<%= current_user&.id %>", {
email: "<%= current_user&.email %>",
name: "<%= current_user&.full_name %>",
company_name: "<%= current_organization&.name %>",
identifier_hash: "<%= current_user&.support_chat_identifier %>",
});
});
</script>
<meta name="environment" content="<%= Rails.env %>"/>
<meta name="sentryDSNUrl" content="<%= ENV["FRONTEND_SENTRY_DSN"] %>"/>
<meta name="currentAppId" content="<%= @app&.slug if @app.present? %>"/>
Expand Down
1 change: 1 addition & 0 deletions config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def report_only
policy.connect_src(:self, *connect_src_uris)
policy.child_src(:self, *connect_src_uris)
policy.report_uri(csp_reporting_uri.to_s) if csp_reporting_uri.present?
policy.frame_src(:self, *connect_src_uris)
end

Rails.application.config.content_security_policy_nonce_generator = ->(request) { Base64.strict_encode64(request.session.id.to_s) }
Expand Down
118 changes: 0 additions & 118 deletions config/initializers/intercom.rb

This file was deleted.

38 changes: 38 additions & 0 deletions lib/chatwoot/shutdown_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Chatwoot
module ShutdownHelper
# This helper allows to erase cookies when a user logs out of an application
# Do not use before a redirect_to because it will not clear the cookies on a redirection
def self.chatwoot_shutdown_helper(cookies, domain = nil)
nil_session = {value: nil, expires: 1.day.ago}
nil_session = nil_session.merge(domain: domain) unless domain.nil? || domain == "localhost"
chatwoot_token = ENV["CHATWOOT_WEBSITE_TOKEN"]
session_cookie = "cw_user_#{chatwoot_token}"
conversation_cookie = "cw_conversation"

if cookies.is_a?(ActionDispatch::Cookies::CookieJar)
cookies[session_cookie] = nil_session
cookies[conversation_cookie] = nil_session
else
controller = cookies
controller.response.delete_cookie(session_cookie, nil_session)
controller.response.delete_cookie(conversation_cookie, nil_session)
end

Rails.logger.debug { "Chatwoot: session cleared" }
rescue
Rails.logger.debug { "Chatwoot: shutdown failed" }
nil
end

def self.prepare_chatwoot_shutdown(session)
session[:perform_chatwoot_shutdown] = true
end

def self.chatwoot_shutdown(session, cookies, domain = nil)
if session[:perform_chatwoot_shutdown]
session.delete(:perform_chatwoot_shutdown)
chatwoot_shutdown_helper(cookies, domain)
end
end
end
end
9 changes: 0 additions & 9 deletions lib/core_extensions/intercom_rails/auto_include.rb

This file was deleted.

Loading