Class: ActivityNotification::OptionalTarget::ActionCableApiChannel

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

Overview

Optional target implementation to broadcast to Action Cable API 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



36
37
38
39
40
41
# File 'lib/activity_notification/optional_targets/action_cable_api_channel.rb', line 36

def format_message(notification, options = {})
  {
    notification: notification.as_json(notification_json_options.merge(options)),
    group_owner:  notification.group_owner? ? nil : notification.group_owner.as_json(notification_json_options.merge(options))
  }
end

#initialize_target(options = {}) ⇒ Object

Initialize method to prepare Action Cable API channel

Parameters:

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

    Options for initializing

Options Hash (options):

  • :channel_prefix (String) — default: ActivityNotification.config.notification_api_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_api_channel.rb', line 9

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

#notification_action_cable_api_allowed?(notification) ⇒ Boolean

Check if Action Cable notification API is allowed

Parameters:

Returns:

  • (Boolean)

    Whether Action Cable notification API is allowed



27
28
29
30
# File 'lib/activity_notification/optional_targets/action_cable_api_channel.rb', line 27

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

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

Broadcast to ActionCable API subscribers

Parameters:

  • notification (Notification)

    Notification instance

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

    Options for publishing



17
18
19
20
21
22
# File 'lib/activity_notification/optional_targets/action_cable_api_channel.rb', line 17

def notify(notification, options = {})
  if notification_action_cable_api_allowed?(notification)
    target_channel_name = "#{@channel_prefix}_#{notification.target_type}#{@composite_key_delimiter}#{notification.target_id}"
    ActionCable.server.broadcast(target_channel_name, format_message(notification, options))
  end
end