docbrown/app/services/user_subscriptions/is_subscribed_cache_checker.rb
dependabot[bot] 816855ce3b
Bump rubocop from 1.17.0 to 1.18.0 (#14107)
* Bump rubocop from 1.17.0 to 1.18.0

Bumps [rubocop](https://github.com/rubocop/rubocop) from 1.17.0 to 1.18.0.
- [Release notes](https://github.com/rubocop/rubocop/releases)
- [Changelog](https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop/compare/v1.17.0...v1.18.0)

---
updated-dependencies:
- dependency-name: rubocop
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* yarn install

* Fix violations

* Restore the default aligned setting

* Trigger Travis CI

* Escape HTML

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: rhymes <github@rhymes.dev>
2021-07-01 13:51:49 +02:00

29 lines
868 B
Ruby

module UserSubscriptions
# This checks if the provided user is subscribed to the provided source
# (returns boolean).
class IsSubscribedCacheChecker
attr_accessor :user, :source_type, :source_id
def self.call(...)
new(...).call
end
def initialize(user, params)
@user = user
@source_type = params[:source_type]
@source_id = params[:source_id]
end
def call
cache_key = "user-#{user.id}-#{user.updated_at.rfc3339}-#{user.subscribed_to_user_subscriptions_count}/" \
"is_subscribed_#{source_type}_#{source_id}"
Rails.cache.fetch(cache_key, expires_in: 24.hours) do
UserSubscription.where(
subscriber_id: user.id,
user_subscription_sourceable_type: source_type,
user_subscription_sourceable_id: source_id,
).any?
end
end
end
end