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.

169 lines
3.8 KiB

  1. # frozen_string_literal: true
  2. class Web::NotificationSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. include StreamEntriesHelper
  5. class DataSerializer < ActiveModel::Serializer
  6. include RoutingHelper
  7. include StreamEntriesHelper
  8. include ActionView::Helpers::SanitizeHelper
  9. attributes :content, :nsfw, :url, :actions,
  10. :access_token, :message, :dir
  11. def content
  12. decoder.decode(strip_tags(body))
  13. end
  14. def dir
  15. rtl?(body) ? 'rtl' : 'ltr'
  16. end
  17. def nsfw
  18. return if object.target_status.nil?
  19. object.target_status.spoiler_text.presence
  20. end
  21. def url
  22. case object.type
  23. when :mention
  24. web_url("statuses/#{object.target_status.id}")
  25. when :follow
  26. web_url("accounts/#{object.from_account.id}")
  27. when :favourite
  28. web_url("statuses/#{object.target_status.id}")
  29. when :reblog
  30. web_url("statuses/#{object.target_status.id}")
  31. end
  32. end
  33. def actions
  34. return @actions if defined?(@actions)
  35. @actions = []
  36. if object.type == :mention
  37. @actions << expand_action if collapsed?
  38. @actions << favourite_action
  39. @actions << reblog_action if rebloggable?
  40. end
  41. @actions
  42. end
  43. def access_token
  44. return if actions.empty?
  45. current_push_subscription.access_token
  46. end
  47. def message
  48. I18n.t('push_notifications.group.title')
  49. end
  50. private
  51. def body
  52. case object.type
  53. when :mention
  54. object.target_status.text
  55. when :follow
  56. object.from_account.note
  57. when :favourite
  58. object.target_status.text
  59. when :reblog
  60. object.target_status.text
  61. end
  62. end
  63. def decoder
  64. @decoder ||= HTMLEntities.new
  65. end
  66. def expand_action
  67. {
  68. title: I18n.t('push_notifications.mention.action_expand'),
  69. icon: full_asset_url('web-push-icon_expand.png', skip_pipeline: true),
  70. todo: 'expand',
  71. action: 'expand',
  72. }
  73. end
  74. def favourite_action
  75. {
  76. title: I18n.t('push_notifications.mention.action_favourite'),
  77. icon: full_asset_url('web-push-icon_favourite.png', skip_pipeline: true),
  78. todo: 'request',
  79. method: 'POST',
  80. action: "/api/v1/statuses/#{object.target_status.id}/favourite",
  81. }
  82. end
  83. def reblog_action
  84. {
  85. title: I18n.t('push_notifications.mention.action_boost'),
  86. icon: full_asset_url('web-push-icon_reblog.png', skip_pipeline: true),
  87. todo: 'request',
  88. method: 'POST',
  89. action: "/api/v1/statuses/#{object.target_status.id}/reblog",
  90. }
  91. end
  92. def collapsed?
  93. !object.target_status.nil? && (object.target_status.sensitive? || object.target_status.spoiler_text.present?)
  94. end
  95. def rebloggable?
  96. !object.target_status.nil? && !object.target_status.hidden?
  97. end
  98. end
  99. attributes :title, :image, :badge, :tag,
  100. :timestamp, :icon
  101. has_one :data, serializer: DataSerializer
  102. def title
  103. case object.type
  104. when :mention
  105. I18n.t('push_notifications.mention.title', name: name)
  106. when :follow
  107. I18n.t('push_notifications.follow.title', name: name)
  108. when :favourite
  109. I18n.t('push_notifications.favourite.title', name: name)
  110. when :reblog
  111. I18n.t('push_notifications.reblog.title', name: name)
  112. end
  113. end
  114. def image
  115. return if object.target_status.nil? || object.target_status.media_attachments.empty?
  116. full_asset_url(object.target_status.media_attachments.first.file.url(:small))
  117. end
  118. def badge
  119. full_asset_url('badge.png', skip_pipeline: true)
  120. end
  121. def tag
  122. object.id
  123. end
  124. def timestamp
  125. object.created_at
  126. end
  127. def icon
  128. object.from_account.avatar_static_url
  129. end
  130. def data
  131. object
  132. end
  133. private
  134. def name
  135. display_name(object.from_account)
  136. end
  137. end