online_notifications_count.rb 897 B

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Subscriptions
  3. class OnlineNotificationsCount < BaseSubscription
  4. description 'Updates unseen notifications count'
  5. argument :user_id, GraphQL::Types::ID, 'ID of the user to receive updates for', loads: Gql::Types::UserType
  6. field :unseen_count, Integer, null: false, description: 'Count of unseen notifications for the user'
  7. # Allow subscriptions only for users where the current user has read permission for.
  8. def authorized?(user:)
  9. context.current_user == user
  10. end
  11. def subscribe(user:)
  12. response(user)
  13. end
  14. def update(user:)
  15. response(user)
  16. end
  17. private
  18. def scope(user)
  19. OnlineNotification.where(user: user)
  20. end
  21. def response(user)
  22. {
  23. unseen_count: scope(user).where(seen: false).count,
  24. }
  25. end
  26. end
  27. end