seen.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class OnlineNotification::Seen < BaseMutation
  4. description 'Mark an online notification as seen'
  5. argument :object_id, GraphQL::Types::ID, description: 'ID of the object the notification is about.'
  6. field :success, Boolean, null: false, description: 'Did we successfully set the online notification to seen?'
  7. def authorized?(object_id:)
  8. relevant_notifications(object_id).all? do |notification|
  9. Pundit.authorize(context.current_user, notification, :update?)
  10. end
  11. end
  12. def resolve(object_id:)
  13. relevant_notifications(object_id).each do |notification|
  14. notification.update!(seen: true)
  15. end
  16. { success: true }
  17. end
  18. private
  19. def relevant_notifications(object_id)
  20. ::OnlineNotification.list_by_object(
  21. object_name(object_id),
  22. online_notification_object(object_id).id
  23. )
  24. .reject(&:seen?)
  25. .select { |notification| notification.user_id == context.current_user.id }
  26. end
  27. def object_name(object_id)
  28. @object_name ||= GlobalID::Locator.locate(object_id)&.class&.name
  29. end
  30. def online_notification_object(object_id)
  31. @online_notification_object ||= Gql::ZammadSchema.authorized_object_from_id(
  32. object_id,
  33. type: object_name(object_id).constantize,
  34. user: context.current_user,
  35. )
  36. end
  37. end
  38. end