[deploy] Remove All Notification Count Helper Classes (#8221)

This commit is contained in:
Molly Struve 2020-06-02 09:48:02 -05:00 committed by GitHub
parent c837e7968a
commit f2632a3f6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 82 deletions

View file

@ -1,12 +1,15 @@
class Notifications::ReadsController < ApplicationController
def create
result = ""
result = ReadNotificationsService.new(current_user).mark_as_read if current_user
render plain: "" && return unless current_user
current_user.notifications.unread.update_all(read: true)
current_user.touch(:last_notification_activity)
if params[:org_id] && current_user.org_member?(params[:org_id])
org = Organization.find_by(id: params[:org_id])
ReadNotificationsService.new(org).mark_as_read
org.notifications.unread.update_all(read: true)
end
current_user&.touch(:last_notification_activity)
render plain: result
render plain: "read"
end
end

View file

@ -1,15 +0,0 @@
class NotificationCounter
def initialize(receiver)
@receiver = receiver
end
def unread_notification_count
return 0 if Rails.env.test?
@receiver.notifications.where(read: false).count
end
def set_to_zero
@receiver.notifications.where(read: false).update_all(read: true)
end
end

View file

@ -38,7 +38,7 @@ class NotifyMailer < ApplicationMailer
@user = user
return if RateLimitChecker.new.limit_by_email_recipient_address(@user.email)
@unread_notifications_count = NotificationCounter.new(@user).unread_notification_count
@unread_notifications_count = @user.notifications.unread.count
@unsubscribe = generate_unsubscribe_token(@user.id, :email_unread_notifications)
subject = "🔥 You have #{@unread_notifications_count} unread notifications on #{ApplicationConfig['COMMUNITY_NAME']}"
mail(to: @user.email, subject: subject)

View file

@ -1,30 +0,0 @@
class ReadNotificationsService
def initialize(receiver)
@receiver = receiver
end
def mark_as_read
NotificationCounter.new(@receiver).set_to_zero
# remove_notifications(@recipient.id)
"read"
end
# This was not working as expected. Go back to drawing board.
# def remove_notifications(user_id)
# return unless ApplicationConfig["PUSHER_BEAMS_KEY"] && ApplicationConfig["PUSHER_BEAMS_KEY"].size == 64
# payload = {
# apns: {
# aps: {
# alert: {
# title: "DEV Notifications",
# body: "Marking as read 🙂"
# }
# },
# data: {
# url: "REMOVE_NOTIFICATIONS"
# }
# }
# }
# Pusher::PushNotifications.publish(interests: ["user-notifications-#{user_id}"], payload: payload)
# end
end

View file

@ -1,29 +1,52 @@
require "rails_helper"
RSpec.describe "Notifications::Reads", type: :request do
def create_follow_notifications(user, following_user)
follow_instance = following_user.follow(user)
Notification.send_new_follower_notification_without_delay(follow_instance)
end
describe "POST /notifications/reads" do
let(:stubbed_service_object) { double }
let(:user) { create(:user) }
let(:following_user) { create(:user) }
before do
sign_in user
allow(ReadNotificationsService).to receive(:new).and_return(stubbed_service_object)
allow(stubbed_service_object).to receive(:mark_as_read)
context "when use is signed in" do
before { sign_in user }
it "marks notifications as read" do
create_follow_notifications(user, following_user)
expect(user.notifications.unread.count).to eq(1)
post "/notifications/reads/"
expect(response).to have_http_status(:ok)
expect(response.body).to eq("read")
expect(user.notifications.unread.count).to eq(0)
end
it "marks personal and org notifications as read" do
org_admin = create(:user, :org_admin)
org = org_admin.organizations.first
create_follow_notifications(org, following_user)
create_follow_notifications(org_admin, following_user)
expect(org_admin.notifications.unread.count).to eq(1)
expect(org.notifications.unread.count).to eq(1)
sign_in org_admin
post "/notifications/reads/", params: { org_id: org.id }
expect(response).to have_http_status(:ok)
expect(response.body).to eq("read")
expect(org_admin.notifications.unread.count).to eq(0)
expect(org.notifications.unread.count).to eq(0)
end
end
it "marks notifications as read" do
it "returns an empty response without a current_user" do
post "/notifications/reads/"
expect(response).to have_http_status(:ok)
expect(stubbed_service_object).to have_received(:mark_as_read).once
end
it "marks personal and org notifications as read" do
org_admin = create(:user, :org_admin)
org_id = org_admin.organizations.first.id
sign_in org_admin
post "/notifications/reads/", params: { org_id: org_id }
expect(response).to have_http_status(:ok)
expect(stubbed_service_object).to have_received(:mark_as_read).twice
expect(response).to have_http_status(:no_content)
expect(response.body).to eq("")
end
end
end

View file

@ -1,15 +0,0 @@
require "rails_helper"
RSpec.describe ReadNotificationsService, type: :service do
let(:user) { create(:user) }
let(:mock) { instance_double(NotificationCounter) }
before do
allow(NotificationCounter).to receive(:new) { mock }
allow(mock).to receive(:set_to_zero).and_return(true)
end
it "returns read when #mark_as_read is called" do
expect(described_class.new(user).mark_as_read).to eq("read")
end
end