docbrown/app/services/notification_subscriptions/unsubscribe.rb
Joshua Wehner 20c282df80
Still trying to fix notification caching when un/subscribing (#19754)
* Revert "Uncache subscribe to comments (#19739)"

This reverts commit 30500805ae.

* Tweak cache key for faster cache change on un/subscribe
2023-07-17 11:23:55 +02:00

40 lines
980 B
Ruby

module NotificationSubscriptions
class Unsubscribe
attr_reader :current_user, :subscription_id
def self.call(...)
new(...).call
end
def initialize(current_user, subscription_id)
@current_user = current_user
@subscription_id = subscription_id
end
def call
unsubscribe_subscription
end
def unsubscribe_subscription
return { errors: "Subscription ID is missing" } if subscription_id.nil?
notification = NotificationSubscription.find_by(user_id: current_user.id,
id: subscription_id)
return { errors: "Notification subscription not found" } if notification.nil?
destroy_notification(notification)
end
private
def destroy_notification(notification)
notification.destroy
if notification.destroyed?
{ destroyed: true }
else
{ errors: notification.errors_as_sentence }
end
end
end
end