recently_viewed.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class Tickets::RecentlyViewed < BaseQuery
  4. description 'Fetch tickets recently viewed by the current user'
  5. argument :except_ticket_internal_id, Integer, required: false, description: 'Optional ticket ID to be filtered out from results'
  6. argument :limit, Integer, required: false, description: 'Limit for the amount of entries'
  7. type [Gql::Types::TicketType], null: false
  8. # Disable field scoping, as no collection but an array is returned and scoping is performed
  9. # right here in the resolver.
  10. def self.register_in_schema(schema)
  11. schema.field graphql_field_name, resolver: self, scope: false
  12. end
  13. def self.authorize(_obj, ctx)
  14. ctx.current_user.permissions?(['ticket.agent'])
  15. end
  16. def resolve(except_ticket_internal_id: nil, limit: 8)
  17. ::RecentView.list(context.current_user, limit + 1, 'Ticket').select do |recent_view|
  18. !except_ticket_internal_id || recent_view.o_id != except_ticket_internal_id
  19. end.map do |recent_view|
  20. ::Ticket.lookup(id: recent_view.o_id)
  21. end.select do |recent_ticket|
  22. ::TicketPolicy.new(context.current_user, recent_ticket).agent_read_access?
  23. end.first(limit)
  24. end
  25. end
  26. end