The code powering m.abunchtell.com https://m.abunchtell.com
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

192 líneas
6.3 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: web_push_subscriptions
  5. #
  6. # id :integer not null, primary key
  7. # endpoint :string not null
  8. # key_p256dh :string not null
  9. # key_auth :string not null
  10. # data :json
  11. # created_at :datetime not null
  12. # updated_at :datetime not null
  13. #
  14. require 'webpush'
  15. require_relative '../../models/setting'
  16. class Web::PushSubscription < ApplicationRecord
  17. include RoutingHelper
  18. include StreamEntriesHelper
  19. include ActionView::Helpers::TranslationHelper
  20. include ActionView::Helpers::SanitizeHelper
  21. has_one :session_activation
  22. before_create :send_welcome_notification
  23. def push(notification)
  24. name = display_name notification.from_account
  25. title = title_str(name, notification)
  26. body = body_str notification
  27. dir = dir_str body
  28. url = url_str notification
  29. image = image_str notification
  30. actions = actions_arr notification
  31. access_token = actions.empty? ? nil : find_or_create_access_token(notification).token
  32. nsfw = notification.target_status.nil? || notification.target_status.spoiler_text.empty? ? nil : notification.target_status.spoiler_text
  33. # TODO: Make sure that the payload does not exceed 4KB - Webpush::PayloadTooLarge
  34. Webpush.payload_send(
  35. message: JSON.generate(
  36. title: title,
  37. dir: dir,
  38. image: image,
  39. badge: full_asset_url('badge.png', skip_pipeline: true),
  40. tag: notification.id,
  41. timestamp: notification.created_at,
  42. icon: notification.from_account.avatar_static_url,
  43. data: {
  44. content: decoder.decode(strip_tags(body)),
  45. nsfw: nsfw.nil? ? nil : decoder.decode(strip_tags(nsfw)),
  46. url: url,
  47. actions: actions,
  48. access_token: access_token,
  49. message: translate('push_notifications.group.title'), # Do not pass count, will be formatted in the ServiceWorker
  50. }
  51. ),
  52. endpoint: endpoint,
  53. p256dh: key_p256dh,
  54. auth: key_auth,
  55. vapid: {
  56. subject: "mailto:#{Setting.site_contact_email}",
  57. private_key: Rails.configuration.x.vapid_private_key,
  58. public_key: Rails.configuration.x.vapid_public_key,
  59. },
  60. ttl: 40 * 60 * 60 # 48 hours
  61. )
  62. end
  63. def pushable?(notification)
  64. data && data.key?('alerts') && data['alerts'][notification.type.to_s]
  65. end
  66. def as_payload
  67. payload = {
  68. id: id,
  69. endpoint: endpoint,
  70. }
  71. payload[:alerts] = data['alerts'] if data && data.key?('alerts')
  72. payload
  73. end
  74. private
  75. def title_str(name, notification)
  76. case notification.type
  77. when :mention then translate('push_notifications.mention.title', name: name)
  78. when :follow then translate('push_notifications.follow.title', name: name)
  79. when :favourite then translate('push_notifications.favourite.title', name: name)
  80. when :reblog then translate('push_notifications.reblog.title', name: name)
  81. end
  82. end
  83. def body_str(notification)
  84. case notification.type
  85. when :mention then notification.target_status.text
  86. when :follow then notification.from_account.note
  87. when :favourite then notification.target_status.text
  88. when :reblog then notification.target_status.text
  89. end
  90. end
  91. def url_str(notification)
  92. case notification.type
  93. when :mention then web_url("statuses/#{notification.target_status.id}")
  94. when :follow then web_url("accounts/#{notification.from_account.id}")
  95. when :favourite then web_url("statuses/#{notification.target_status.id}")
  96. when :reblog then web_url("statuses/#{notification.target_status.id}")
  97. end
  98. end
  99. def actions_arr(notification)
  100. actions =
  101. case notification.type
  102. when :mention then [
  103. {
  104. title: translate('push_notifications.mention.action_favourite'),
  105. icon: full_asset_url('web-push-icon_favourite.png', skip_pipeline: true),
  106. todo: 'request',
  107. method: 'POST',
  108. action: "/api/v1/statuses/#{notification.target_status.id}/favourite",
  109. },
  110. ]
  111. else []
  112. end
  113. should_hide = notification.type.equal?(:mention) && !notification.target_status.nil? && (notification.target_status.sensitive || !notification.target_status.spoiler_text.empty?)
  114. can_boost = notification.type.equal?(:mention) && !notification.target_status.nil? && !notification.target_status.hidden?
  115. if should_hide
  116. actions.insert(0, title: translate('push_notifications.mention.action_expand'), icon: full_asset_url('web-push-icon_expand.png', skip_pipeline: true), todo: 'expand', action: 'expand')
  117. end
  118. if can_boost
  119. actions << { title: translate('push_notifications.mention.action_boost'), icon: full_asset_url('web-push-icon_reblog.png', skip_pipeline: true), todo: 'request', method: 'POST', action: "/api/v1/statuses/#{notification.target_status.id}/reblog" }
  120. end
  121. actions
  122. end
  123. def image_str(notification)
  124. return nil if notification.target_status.nil? || notification.target_status.media_attachments.empty?
  125. full_asset_url(notification.target_status.media_attachments.first.file.url(:small))
  126. end
  127. def dir_str(body)
  128. rtl?(body) ? 'rtl' : 'ltr'
  129. end
  130. def send_welcome_notification
  131. Webpush.payload_send(
  132. message: JSON.generate(
  133. title: translate('push_notifications.subscribed.title'),
  134. icon: full_asset_url('android-chrome-192x192.png', skip_pipeline: true),
  135. badge: full_asset_url('badge.png', skip_pipeline: true),
  136. data: {
  137. content: translate('push_notifications.subscribed.body'),
  138. actions: [],
  139. url: web_url('notifications'),
  140. }
  141. ),
  142. endpoint: endpoint,
  143. p256dh: key_p256dh,
  144. auth: key_auth,
  145. vapid: {
  146. subject: "mailto:#{Setting.site_contact_email}",
  147. private_key: Rails.configuration.x.vapid_private_key,
  148. public_key: Rails.configuration.x.vapid_public_key,
  149. },
  150. ttl: 5 * 60 # 5 minutes
  151. )
  152. end
  153. def find_or_create_access_token(notification)
  154. Doorkeeper::AccessToken.find_or_create_for(
  155. Doorkeeper::Application.find_by(superapp: true),
  156. notification.account.user.id,
  157. Doorkeeper::OAuth::Scopes.from_string('read write follow'),
  158. Doorkeeper.configuration.access_token_expires_in,
  159. Doorkeeper.configuration.refresh_token_enabled?
  160. )
  161. end
  162. def decoder
  163. @decoder ||= HTMLEntities.new
  164. end
  165. end