diff --git a/Envfile b/Envfile index 0fd4a4173..7b12399c1 100644 --- a/Envfile +++ b/Envfile @@ -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" diff --git a/Gemfile b/Gemfile index 86071eb73..cd5fc41c3 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index e60725006..998a64d86 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index bc4ee77b9..821a33894 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -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", diff --git a/app/jobs/messages/send_push_job.rb b/app/jobs/messages/send_push_job.rb deleted file mode 100644 index 8926057f1..000000000 --- a/app/jobs/messages/send_push_job.rb +++ /dev/null @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index c72238069..797fdca4e 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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" diff --git a/app/services/messages/send_push.rb b/app/services/messages/send_push.rb deleted file mode 100644 index 7a0a47940..000000000 --- a/app/services/messages/send_push.rb +++ /dev/null @@ -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 diff --git a/app/views/chat_channels/index.html.erb b/app/views/chat_channels/index.html.erb index 26e483d4f..604e6e669 100644 --- a/app/views/chat_channels/index.html.erb +++ b/app/views/chat_channels/index.html.erb @@ -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 %>); diff --git a/spec/jobs/messages/send_push_job_spec.rb b/spec/jobs/messages/send_push_job_spec.rb deleted file mode 100644 index b4ec9ce0e..000000000 --- a/spec/jobs/messages/send_push_job_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require "rails_helper" - -RSpec.describe Messages::SendPushJob, type: :job do - include_examples "#enqueues_job", "messages_send_push", 456, 789, "

Hello", 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, "", 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, "") - end - - it "does call the service" do - described_class.perform_now(456, 789, "", messages_send_push_service) - expect(messages_send_push_service).to have_received(:call).with(user, chat_channel, "") - end - end - end -end diff --git a/spec/services/messages/send_push_spec.rb b/spec/services/messages/send_push_spec.rb deleted file mode 100644 index 67d44624c..000000000 --- a/spec/services/messages/send_push_spec.rb +++ /dev/null @@ -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