You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

  1. # frozen_string_literal: true
  2. class Web::NotificationSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. include ActionView::Helpers::TextHelper
  5. include ActionView::Helpers::SanitizeHelper
  6. attributes :access_token, :preferred_locale, :notification_id,
  7. :notification_type, :icon, :title, :body
  8. def access_token
  9. current_push_subscription.associated_access_token
  10. end
  11. def preferred_locale
  12. current_push_subscription.associated_user&.locale || I18n.default_locale
  13. end
  14. def notification_id
  15. object.id
  16. end
  17. def notification_type
  18. object.type
  19. end
  20. def icon
  21. full_asset_url(object.from_account.avatar_static_url)
  22. end
  23. def title
  24. I18n.t("notification_mailer.#{object.type}.subject", name: object.from_account.display_name.presence || object.from_account.username)
  25. end
  26. def body
  27. str = strip_tags(object.target_status&.spoiler_text&.presence || object.target_status&.text || object.from_account.note)
  28. truncate(HTMLEntities.new.decode(str.to_str), length: 140) # Do not encode entities, since this value will not be used in HTML
  29. end
  30. end