Class: ActivityNotification::OptionalTarget::ActionCableChannel

Inherits:
Base
  • Object
show all
Defined in:
lib/activity_notification/optional_targets/action_cable_channel.rb

Overview

Optional target implementation to broadcast to Action Cable channel

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_optional_target_name

Constructor Details

This class inherits a constructor from ActivityNotification::OptionalTarget::Base

Instance Method Details

#format_message(notification, options = {}) ⇒ Hash

Format message to broadcast

Parameters:

  • notification (Notification)

    Notification instance

  • options (Hash) (defaults to: {})

    Options for publishing

Returns:

  • (Hash)

    Formatted message to broadcast



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/activity_notification/optional_targets/action_cable_channel.rb', line 54

def format_message(notification, options = {})
  index_options = options.slice(:filter, :limit, :without_grouping, :with_group_members, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key, :later_than, :earlier_than)
  {
    id:                          notification.id,
    view:                        render_notification_message(notification, options),
    text:                        notification.text(options),
    notifiable_path:             notification.notifiable_path,
    group_owner_id:              notification.group_owner_id,
    group_owner_view:            notification.group_owner? ? nil : render_notification_message(notification.group_owner, options),
    unopened_notification_count: notification.target.unopened_notification_count(index_options)
  }
end

#initialize_target(options = {}) ⇒ Object

Initialize method to prepare Action Cable channel

Parameters:

  • options (Hash) (defaults to: {})

    Options for initializing

Options Hash (options):

  • :channel_prefix (String) — default: ActivityNotification.config.notification_channel_prefix

    Channel name prefix to broadcast notifications

  • :composite_key_delimiter (String) — default: ActivityNotification.config.composite_key_delimiter

    Composite key delimiter for channel name



9
10
11
12
# File 'lib/activity_notification/optional_targets/action_cable_channel.rb', line 9

def initialize_target(options = {})
  @channel_prefix          = options.delete(:channel_prefix)          || ActivityNotification.config.notification_channel_prefix
  @composite_key_delimiter = options.delete(:composite_key_delimiter) || ActivityNotification.config.composite_key_delimiter
end

#notification_action_cable_allowed?(notification) ⇒ Boolean

Check if Action Cable notification is allowed

Parameters:

Returns:

  • (Boolean)

    Whether Action Cable notification is allowed



45
46
47
48
# File 'lib/activity_notification/optional_targets/action_cable_channel.rb', line 45

def notification_action_cable_allowed?(notification)
  notification.target.notification_action_cable_allowed?(notification.notifiable, notification.key) &&
    notification.notifiable.notifiable_action_cable_allowed?(notification.target, notification.key)
end

#notify(notification, options = {}) ⇒ Object

Broadcast to ActionCable subscribers

Parameters:

  • notification (Notification)

    Notification instance

  • options (Hash) (defaults to: {})

    Options for publishing

Options Hash (options):

  • :target (String, Symbol) — default: nil

    Target type name to find template or i18n text

  • :partial_root (String) — default: "activity_notification/notifications/#{target}", controller.target_view_path, 'activity_notification/notifications/default'

    Partial template name

  • :partial (String) — default: self.key.tr('.', '/')

    Root path of partial template

  • :layout (String) — default: nil

    Layout template name

  • :layout_root (String) — default: 'layouts'

    Root path of layout template

  • :fallback (String, Symbol) — default: :default

    Fallback template to use when MissingTemplate is raised. Set :text to use i18n text as fallback.

  • :filter (String) — default: nil

    Filter option to load notification index (Nothing as auto, ‘opened’ or ‘unopened’)

  • :limit (String) — default: nil

    Limit to query for notifications

  • :without_grouping (String) — default: 'false'

    If notification index will include group members

  • :with_group_members (String) — default: 'false'

    If notification index will include group members

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notification index later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notification index earlier than specified time

  • others (Hash)

    Parameters to be set as locals



34
35
36
37
38
39
40
# File 'lib/activity_notification/optional_targets/action_cable_channel.rb', line 34

def notify(notification, options = {})
  if notification_action_cable_allowed?(notification)
    target_channel_name = "#{@channel_prefix}_#{notification.target_type}#{@composite_key_delimiter}#{notification.target_id}"
    index_options = options.slice(:filter, :limit, :without_grouping, :with_group_members, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key, :later_than, :earlier_than)
    ActionCable.server.broadcast(target_channel_name, format_message(notification, options))
  end
end