-
-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport 'Implement push notifications for conversations' messages' t…
…o v0.27 (#12511) * Implement push notifications for conversations' messages * Refactor variables definitions in SendPushNotification spec * Add spec for ConversationMailer It also adds the factories to ease-up the specs * Extract ConversationMailer methods to HasConversation concern * Add PushNotificationMessage model * Implement push notifications for conversations' messages * Fix spec * Simplify PushNotificationMessage model by removing useless attributes * Fix url and icon methods in PushNotificationMessage As a bonus, there are more examples in the spec. * Add missing class in yardoc * Clarify signature * Use more idiomatic way of checking for presence Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro> * Move module inclusion to the top for consistency Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro> * Add skip_injection on factory definition Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro> * Use alias_method for :user * Fix rubocop offenses --------- Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro> * Running linters * Add missing methods * Fix failing specs * Run Linter * Fix failing specs * Apply suggestions from code review Co-authored-by: Andrés Pereira de Lucena <andreslucena@users.noreply.github.com> * Update decidim-core/lib/decidim/core/test/factories.rb Co-authored-by: Andrés Pereira de Lucena <andreslucena@users.noreply.github.com> --------- Co-authored-by: Andrés Pereira de Lucena <andreslucena@users.noreply.github.com>
- Loading branch information
1 parent
013bfd3
commit c0fac7e
Showing
13 changed files
with
652 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
decidim-core/app/models/decidim/push_notification_message.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
# A messsage from a conversation that will be sent as a push notification | ||
class PushNotificationMessage | ||
class InvalidActionError < StandardError; end | ||
|
||
include SanitizeHelper | ||
include Decidim::TranslatableAttributes | ||
|
||
def initialize(recipient:, conversation:, message:) | ||
@recipient = recipient | ||
@conversation = conversation | ||
@message = message | ||
end | ||
|
||
attr_reader :recipient, :conversation, :message | ||
|
||
alias user recipient | ||
|
||
def body | ||
decidim_html_escape(translated_attribute(message)) | ||
end | ||
|
||
def icon | ||
organization.attached_uploader(:favicon).variant_url(:big, host: organization.host) | ||
end | ||
|
||
def url | ||
EngineRouter.new("decidim", {}).public_send(:conversation_path, host: organization.host, id: @conversation) | ||
end | ||
|
||
private | ||
|
||
def organization | ||
@organization ||= recipient.organization | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
decidim-core/app/services/decidim/push_notification_message_sender.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
# A wrapper for preparing push notifications messages from conversations | ||
# It respects the same contract as the Decidim::Messaging::ConversationMailer | ||
class PushNotificationMessageSender | ||
include HasConversations | ||
|
||
def deliver | ||
SendPushNotification.new.perform(@notification, title) | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Metrics/ParameterLists | ||
# rubocop:disable Lint/UnusedMethodArgument | ||
# | ||
# There are some parameters thar are not used in the method, but they are needed to | ||
# keep the same contract as the Decidim::Messaging::ConversationMailer | ||
def send_notification(from:, to:, conversation:, action:, message: nil, third_party: nil) | ||
@action = action | ||
@sender = to | ||
@third_party = third_party | ||
|
||
@notification = PushNotificationMessage.new(recipient: to, conversation: conversation, message: message) | ||
|
||
self | ||
end | ||
# rubocop:enable Lint/UnusedMethodArgument | ||
# rubocop:enable Metrics/ParameterLists | ||
|
||
def title | ||
get_subject(action: @action, sender: @sender, third_party: @third_party) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.