Remove webpush gem and code (#4883)

This commit is contained in:
Molly Struve 2019-11-22 15:46:11 -06:00 committed by Ben Halpern
parent 36479f09c3
commit 091392cf1f
10 changed files with 5 additions and 173 deletions

View file

@ -171,11 +171,6 @@ variable :STRIPE_PUBLISHABLE_KEY, :String, default: "Optional"
variable :STRIPE_SECRET_KEY, :String, default: "Optional"
variable :STRIPE_CANCELLATION_SECRET, :String, default: "Optional"
# For browser webpush notifications (webpush gem) (totally random base64 nums)
# (https://github.com/zaru/webpush)
variable :VAPID_PUBLIC_KEY, :String, default: "dGhpcyBpcyBkZXYudG8gdGVzdCBkYXRh"
variable :VAPID_PRIVATE_KEY, :String, default: "VGhpcyBpcyBtb3JlIHRlc3QgZGF0YQ=="
# For video calling in DEV Connect
# (https://www.twilio.com/docs/video)
variable :TWILIO_ACCOUNT_SID, :String, default: "Optional"

View file

@ -100,7 +100,6 @@ gem "uglifier", "~> 4.2" # Uglifier minifies JavaScript files
gem "ulid", "~> 1.1" # Universally Unique Lexicographically Sortable Identifier implementation for Ruby
gem "validate_url", "~> 1.0" # Library for validating urls in Rails
gem "webpacker", "~> 3.5" # Use webpack to manage app-like JavaScript modules in Rails
gem "webpush", "~> 1.0" # Encryption Utilities for Web Push payload
group :development do
gem "better_errors", "~> 2.5" # Provides a better error page for Rails and other Rack apps

View file

@ -400,7 +400,6 @@ GEM
hashdiff (1.0.0)
hashie (3.6.0)
heapy (0.1.4)
hkdf (0.3.0)
honeycomb-beeline (1.2.0)
libhoney (~> 1.8)
html_tokenizer (0.0.7)
@ -845,9 +844,6 @@ GEM
activesupport (>= 4.2)
rack-proxy (>= 0.6.1)
railties (>= 4.2)
webpush (1.0.0)
hkdf (~> 0.2)
jwt (~> 2.0)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
@ -1005,7 +1001,6 @@ DEPENDENCIES
webdrivers (~> 4.1)
webmock (~> 3.7)
webpacker (~> 3.5)
webpush (~> 1.0)
yard (~> 0.9.20)
yard-activerecord (~> 0.0.16)
yard-activesupport-concern (~> 0.0.1)

View file

@ -5,27 +5,18 @@ class MessagesController < ApplicationController
@message = Message.new(message_params)
@message.user_id = session_current_user_id
authorize @message
success = false
if @message.valid?
message_json = create_pusher_payload(@message)
Pusher.trigger(@message.chat_channel.pusher_channels, "message-created", message_json)
end
if @message.save
begin
@message.send_push
success = true
message_json = create_pusher_payload(@message)
Pusher.trigger(@message.chat_channel.pusher_channels, "message-created", message_json)
rescue Pusher::Error => e
logger.info "PUSHER ERROR: #{e.message}"
end
end
if success
render json: { status: "success", message: "Message created" }, status: :created
else
error_message = "Message created but could not trigger Pusher"
render json: { status: "error", message: error_message }, status: :created
end
if @message.save
render json: { status: "success", message: "Message created" }, status: :created
else
render json: {
status: "error",

View file

@ -1,14 +0,0 @@
module Messages
class SendPushJob < ApplicationJob
queue_as :messages_send_push
def perform(user_id, chat_channel_id, message_html, service = Messages::SendPush)
user = User.find_by(id: user_id)
chat_channel = ChatChannel.find_by(id: chat_channel_id)
return unless user && chat_channel
service.call(user, chat_channel, message_html)
end
end
end

View file

@ -17,10 +17,6 @@ class Message < ApplicationRecord
HexComparer.new(color_options).brightness(0.9)
end
def send_push
Messages::SendPushJob.perform_later(user.id, chat_channel.id, message_html)
end
def direct_receiver
return if chat_channel.channel_type != "direct"

View file

@ -1,42 +0,0 @@
module Messages
class SendPush
def initialize(user, chat_channel, message_html)
@user = user
@chat_channel = chat_channel
@message_html = message_html
end
def self.call(*args)
new(*args).call
end
def call
receivers = chat_channel.chat_channel_memberships.where.not(user_id: user.id).select(:user_id)
PushNotificationSubscription.where(user_id: receivers).find_each do |sub|
break if no_push_necessary?(sub)
Webpush.payload_send(
endpoint: sub.endpoint,
message: ActionView::Base.full_sanitizer.sanitize(message_html),
p256dh: sub.p256dh_key,
auth: sub.auth_key,
ttl: 24 * 60 * 60,
vapid: {
subject: "https://dev.to",
public_key: ApplicationConfig["VAPID_PUBLIC_KEY"],
private_key: ApplicationConfig["VAPID_PRIVATE_KEY"]
},
)
end
end
private
attr_reader :user, :chat_channel, :message_html
def no_push_necessary?(sub)
membership = sub.user.chat_channel_memberships.order("last_opened_at DESC").first
membership.last_opened_at > 40.seconds.ago
end
end
end

View file

@ -48,5 +48,4 @@
if (navigator.userAgent.match(/iPhone/i) && !navigator.userAgent.match('CriOS')) {
document.getElementById("chat").classList.add("live-chat--iossafari")
}
window.vapidPublicKey = new Uint8Array(<%= Base64.urlsafe_decode64(ApplicationConfig["VAPID_PUBLIC_KEY"]).bytes %>);
</script>

View file

@ -1,49 +0,0 @@
require "rails_helper"
RSpec.describe Messages::SendPushJob, type: :job do
include_examples "#enqueues_job", "messages_send_push", 456, 789, "<h1>Hello</h1"
describe "#perform_now" do
let(:messages_send_push_service) { double }
context "when no user found" do
before do
allow(User).to receive(:find_by)
allow(messages_send_push_service).to receive(:call)
end
it "does not call the service" do
described_class.perform_now(456, 789, "<html>", messages_send_push_service)
expect(messages_send_push_service).not_to have_received(:call)
end
end
context "when no chat channel found" do
before do
allow(ChatChannel).to receive(:find_by)
allow(messages_send_push_service).to receive(:call)
end
it "does not call the service" do
described_class.perform_now(456, 789, "<html>", messages_send_push_service)
expect(messages_send_push_service).not_to have_received(:call)
end
end
context "when user + chat channel" do
let(:user) { double }
let(:chat_channel) { double }
before do
allow(User).to receive(:find_by).and_return(user)
allow(ChatChannel).to receive(:find_by).and_return(chat_channel)
allow(messages_send_push_service).to receive(:call).with(user, chat_channel, "<html>")
end
it "does call the service" do
described_class.perform_now(456, 789, "<html>", messages_send_push_service)
expect(messages_send_push_service).to have_received(:call).with(user, chat_channel, "<html>")
end
end
end
end

View file

@ -1,38 +0,0 @@
require "rails_helper"
RSpec.describe Messages::SendPush, type: :service do
let!(:user1) { create(:user) }
let!(:user2) { create(:user) }
let!(:chat_channel) { create(:chat_channel) }
let!(:message) { build(:message, chat_channel_id: chat_channel.id, user_id: user2.id) }
before do
create(:chat_channel_membership, user_id: user2.id, chat_channel_id: chat_channel.id)
PushNotificationSubscription.create(
user_id: user2.id,
endpoint: "http://nowhere.togo", p256dh_key: "BBoN_OkTfE_0uObue",
auth_key: "aW1hcm thcmF",
notification_type: "browser"
)
allow(Webpush).to receive(:payload_send).and_return(true)
end
context "when push is needed" do
it "pushes notification subscription messages" do
described_class.call(user1, chat_channel, message.message_html)
expect(Webpush).to have_received(:payload_send)
end
end
context "when push is not necessary" do
before do
membership = PushNotificationSubscription.last.user.chat_channel_memberships.order("last_opened_at DESC").first
membership.update(last_opened_at: 3.seconds.ago)
end
it "does not push subscription message" do
described_class.call(user1, chat_channel, message.message_html)
expect(Webpush).not_to have_received(:payload_send)
end
end
end