Module: ActivityNotification::NotificationApi

Extended by:
ActiveSupport::Concern
Included in:
ORM::ActiveRecord::Notification, ORM::Dynamoid::Notification, ORM::Mongoid::Notification
Defined in:
lib/activity_notification/apis/notification_api.rb

Overview

Defines API for notification included in Notification model.

Instance Method Summary collapse

Instance Method Details

#after_storeObject

Call after store action with stored notification



511
512
# File 'lib/activity_notification/apis/notification_api.rb', line 511

def after_store
end

#email_subscribed?Boolean

Returns if the target subscribes this notification email.

Returns:

  • (Boolean)

    If the target subscribes the notification



704
705
706
# File 'lib/activity_notification/apis/notification_api.rb', line 704

def email_subscribed?
  target.subscribes_to_notification_email?(key)
end

#group_member?Boolean

Returns if the notification is group member belonging to owner.

Returns:

  • (Boolean)

    If the notification is group member



597
598
599
# File 'lib/activity_notification/apis/notification_api.rb', line 597

def group_member?
  group_owner_id.present?
end

#group_member_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer

Returns count of group members of the notification. This method is designed to cache group by query result to avoid N+1 call.

Parameters:

  • limit (Integer) (defaults to: ActivityNotification.config.opened_index_limit)

    Limit to query for opened notifications

Returns:

  • (Integer)

    Count of group members of the notification



626
627
628
# File 'lib/activity_notification/apis/notification_api.rb', line 626

def group_member_count(limit = ActivityNotification.config.opened_index_limit)
  meta_group_member_count(:opened_group_member_count, :unopened_group_member_count, limit)
end

#group_member_exists?(limit = ActivityNotification.config.opened_index_limit) ⇒ Boolean

Returns if group member of the notification exists. This method is designed to cache group by query result to avoid N+1 call.

Parameters:

  • limit (Integer) (defaults to: ActivityNotification.config.opened_index_limit)

    Limit to query for opened notifications

Returns:

  • (Boolean)

    If group member of the notification exists



606
607
608
# File 'lib/activity_notification/apis/notification_api.rb', line 606

def group_member_exists?(limit = ActivityNotification.config.opened_index_limit)
  group_member_count(limit) > 0
end

#group_member_notifier_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer

Returns count of group member notifiers of the notification not including group owner notifier. It always returns 0 if group owner notifier is blank. It counts only the member notifier of the same type with group owner notifier. This method is designed to cache group by query result to avoid N+1 call.

Parameters:

  • limit (Integer) (defaults to: ActivityNotification.config.opened_index_limit)

    Limit to query for opened notifications

Returns:

  • (Integer)

    Count of group member notifiers of the notification



646
647
648
# File 'lib/activity_notification/apis/notification_api.rb', line 646

def group_member_notifier_count(limit = ActivityNotification.config.opened_index_limit)
  meta_group_member_count(:opened_group_member_notifier_count, :unopened_group_member_notifier_count, limit)
end

#group_member_notifier_exists?(limit = ActivityNotification.config.opened_index_limit) ⇒ Boolean

Returns if group member notifier except group owner notifier exists. It always returns false if group owner notifier is blank. It counts only the member notifier of the same type with group owner notifier. This method is designed to cache group by query result to avoid N+1 call.

Parameters:

  • limit (Integer) (defaults to: ActivityNotification.config.opened_index_limit)

    Limit to query for opened notifications

Returns:

  • (Boolean)

    If group member of the notification exists



617
618
619
# File 'lib/activity_notification/apis/notification_api.rb', line 617

def group_member_notifier_exists?(limit = ActivityNotification.config.opened_index_limit)
  group_member_notifier_count(limit) > 0
end

#group_notification_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer

Returns count of group notifications including owner and members. This method is designed to cache group by query result to avoid N+1 call.

Parameters:

  • limit (Integer) (defaults to: ActivityNotification.config.opened_index_limit)

    Limit to query for opened notifications

Returns:

  • (Integer)

    Count of group notifications including owner and members



635
636
637
# File 'lib/activity_notification/apis/notification_api.rb', line 635

def group_notification_count(limit = ActivityNotification.config.opened_index_limit)
  group_member_count(limit) + 1
end

#group_notifier_count(limit = ActivityNotification.config.opened_index_limit) ⇒ Integer

Returns count of group member notifiers including group owner notifier. It always returns 0 if group owner notifier is blank. This method is designed to cache group by query result to avoid N+1 call.

Parameters:

  • limit (Integer) (defaults to: ActivityNotification.config.opened_index_limit)

    Limit to query for opened notifications

Returns:

  • (Integer)

    Count of group notifications including owner and members



656
657
658
659
# File 'lib/activity_notification/apis/notification_api.rb', line 656

def group_notifier_count(limit = ActivityNotification.config.opened_index_limit)
  notification = group_member? && group_owner.present? ? group_owner : self
  notification.notifier.present? ? group_member_notifier_count(limit) + 1 : 0
end

#group_owner?Boolean

Returns if the notification is group owner.

Returns:

  • (Boolean)

    If the notification is group owner



590
591
592
# File 'lib/activity_notification/apis/notification_api.rb', line 590

def group_owner?
  !group_member?
end

#latest_group_memberNotificaion

Returns the latest group member notification instance of this notification. If this group owner has no group members, group owner instance self will be returned.

Returns:

  • (Notificaion)

    Notification instance of the latest group member notification



665
666
667
668
# File 'lib/activity_notification/apis/notification_api.rb', line 665

def latest_group_member
  notification = group_member? && group_owner.present? ? group_owner : self
  notification.group_member_exists? ? notification.group_members.latest : self
end

#notifiable_pathString

Returns notifiable_path to move after opening notification with notifiable.notifiable_path.

Returns:

  • (String)

    Notifiable path URL to move after opening notification



685
686
687
688
# File 'lib/activity_notification/apis/notification_api.rb', line 685

def notifiable_path
  notifiable.blank? and raise ActivityNotification::NotifiableNotFoundError.new("Couldn't find associated notifiable (#{notifiable_type}) of #{self.class.name} with 'id'=#{id}")
  notifiable.notifiable_path(target_type, key)
end

#open!(options = {}) ⇒ Integer

Opens the notification.

Parameters:

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

    Options for opening notifications

Options Hash (options):

  • :opened_at (DateTime) — default: Time.current

    Time to set to opened_at of the notification record

  • :with_members (Boolean) — default: true

    If it opens notifications including group members

Returns:

  • (Integer)

    Number of opened notification records



563
564
565
566
567
568
569
570
571
# File 'lib/activity_notification/apis/notification_api.rb', line 563

def open!(options = {})
  opened? and return 0
  opened_at    = options[:opened_at] || Time.current
  with_members = options.has_key?(:with_members) ? options[:with_members] : true
  unopened_member_count = with_members ? group_members.unopened_only.count : 0
  group_members.update_all(opened_at: opened_at) if with_members
  update(opened_at: opened_at)
  unopened_member_count + 1
end

#opened?Boolean

Returns if the notification is opened.

Returns:

  • (Boolean)

    If the notification is opened



583
584
585
# File 'lib/activity_notification/apis/notification_api.rb', line 583

def opened?
  opened_at.present?
end

#optional_target_namesArray<Symbol>

Returns optional_target names of the notification from configured field or overridden method.

Returns:

  • (Array<Symbol>)

    Array of optional target names



723
724
725
# File 'lib/activity_notification/apis/notification_api.rb', line 723

def optional_target_names
  notifiable.optional_target_names(target.to_resources_name, key)
end

#optional_target_subscribed?(optional_target_name) ⇒ Boolean

Returns if the target subscribes this notification email.

Parameters:

  • optional_target_name (String, Symbol)

    Class name of the optional target implementation (e.g. :amazon_sns, :slack)

Returns:

  • (Boolean)

    If the target subscribes the specified optional target of the notification



711
712
713
# File 'lib/activity_notification/apis/notification_api.rb', line 711

def optional_target_subscribed?(optional_target_name)
  target.subscribes_to_optional_target?(key, optional_target_name)
end

#optional_targetsArray<ActivityNotification::OptionalTarget::Base>

Returns optional_targets of the notification from configured field or overridden method.

Returns:



717
718
719
# File 'lib/activity_notification/apis/notification_api.rb', line 717

def optional_targets
  notifiable.optional_targets(target.to_resources_name, key)
end

#prepare_to_storeObject

:nocov: Returns prepared notification object to store

Returns:

  • (Object)

    prepared notification object to store



506
507
508
# File 'lib/activity_notification/apis/notification_api.rb', line 506

def prepare_to_store
  self
end

#printable_notifiable_nameString

Returns printable notifiable model name to show in view or email.

Returns:

  • (String)

    Printable notifiable model name



692
693
694
# File 'lib/activity_notification/apis/notification_api.rb', line 692

def printable_notifiable_name
  notifiable.printable_notifiable_name(target, key)
end

#publish_to_optional_targets(options = {}) ⇒ Hash

Publishes notification to the optional targets.

Parameters:

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

    Options for optional targets

Returns:

  • (Hash)

    Result of publishing to optional target



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/activity_notification/apis/notification_api.rb', line 536

def publish_to_optional_targets(options = {})
  notifiable.optional_targets(target.to_resources_name, key).map { |optional_target|
    optional_target_name = optional_target.to_optional_target_name
    if optional_target_subscribed?(optional_target_name)
      begin
        optional_target.notify(self, options[optional_target_name] || {})
        [optional_target_name, true]
      rescue => e
        Rails.logger.error(e)
        if ActivityNotification.config.rescue_optional_target_errors
          [optional_target_name, e]
        else
          raise e
        end
      end
    else
      [optional_target_name, false]
    end
  }.to_h
end

#remove_from_groupNotificaion

Remove from notification group and make a new group owner.

Returns:

  • (Notificaion)

    New group owner instance of the notification group



673
674
675
676
677
678
679
680
# File 'lib/activity_notification/apis/notification_api.rb', line 673

def remove_from_group
  new_group_owner = group_members.earliest
  if new_group_owner.present?
    new_group_owner.update(group_owner_id: nil)
    group_members.update_all(group_owner_id: new_group_owner.id)
  end
  new_group_owner
end

#send_notification_email(options = {}) ⇒ Mail::Message, ActionMailer::DeliveryJob

Sends notification email to the target.

Parameters:

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

    Options for notification email

Options Hash (options):

  • :send_later (Boolean)

    If it sends notification email asynchronously

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

    Fallback template to use when MissingTemplate is raised

Returns:

  • (Mail::Message, ActionMailer::DeliveryJob)

    Email message or its delivery job



521
522
523
524
525
526
527
528
529
530
# File 'lib/activity_notification/apis/notification_api.rb', line 521

def send_notification_email(options = {})
  if target.notification_email_allowed?(notifiable, key) &&
     notifiable.notification_email_allowed?(target, key) &&
     email_subscribed?
    send_later = options.has_key?(:send_later) ? options[:send_later] : true
    send_later ?
      @@notification_mailer.send_notification_email(self, options).deliver_later :
      @@notification_mailer.send_notification_email(self, options).deliver_now
  end
end

#subscribed?Boolean

Returns if the target subscribes this notification.

Returns:

  • (Boolean)

    If the target subscribes the notification



698
699
700
# File 'lib/activity_notification/apis/notification_api.rb', line 698

def subscribed?
  target.subscribes_to_notification?(key)
end

#unopened?Boolean

Returns if the notification is unopened.

Returns:

  • (Boolean)

    If the notification is unopened



576
577
578
# File 'lib/activity_notification/apis/notification_api.rb', line 576

def unopened?
  !opened?
end