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.

168 lines
3.8 KiB

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