1
0

notification_serializer.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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