Module: ActivityNotification::NotificationApi

Extended by:
ActiveSupport::Concern
Included in:
Notification
Defined in:
lib/activity_notification/apis/notification_api.rb

Overview

Defines API for notification included in Notification model.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_optionsArray<Notificaion>

Returns available options for kinds of notify methods.

Returns:

  • (Array<Notificaion>)

    Available options for kinds of notify methods



136
137
138
# File 'lib/activity_notification/apis/notification_api.rb', line 136

def available_options
  [:key, :group, :parameters, :notifier, :send_email, :send_later].freeze
end

.group_member_exists?(notifications) ⇒ Boolean

Returns if group member of the notifications exists. This method is designed to be called from controllers or views to avoid N+1.

Parameters:

  • notifications (Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of the notifications to test member exists

Returns:

  • (Boolean)

    If group member of the notifications exists



108
109
110
# File 'lib/activity_notification/apis/notification_api.rb', line 108

def group_member_exists?(notifications)
  notifications.present? and where(group_owner_id: notifications.map(&:id)).exists?
end

.notify(target_type, notifiable, options = {}) ⇒ Array<Notificaion>

Generates notifications to configured targets with notifiable model.

Examples:

Use with target_type as Symbol

ActivityNotification::Notification.notify :users, @comment

Use with target_type as String

ActivityNotification::Notification.notify 'User', @comment

Use with target_type as Class

ActivityNotification::Notification.notify User, @comment

Use with options

ActivityNotification::Notification.notify :users, @comment, key: 'custom.comment', group: @comment.article
ActivityNotification::Notification.notify :users, @comment, parameters: { reply_to: @comment.reply_to }, send_later: false

Parameters:

  • target_type (Symbol, String, Class)

    Type of target

  • notifiable (Object)

    Notifiable instance

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

    Options for notifications

Options Hash (options):

  • :key (String) — default: notifiable.default_notification_key

    Key of the notification

  • :group (Object) — default: nil

    Group unit of the notifications

  • :notifier (Object) — default: nil

    Notifier of the notifications

  • :parameters (Hash) — default: {}

    Additional parameters of the notifications

  • :send_email (Boolean) — default: true

    If it sends notification email

  • :send_later (Boolean) — default: true

    If it sends notification email asynchronously

Returns:

  • (Array<Notificaion>)

    Array of generated notifications



34
35
36
37
38
39
# File 'lib/activity_notification/apis/notification_api.rb', line 34

def notify(target_type, notifiable, options = {})
  targets = notifiable.notification_targets(target_type, options[:key])
  unless targets.blank?
    notify_all(targets, notifiable, options)
  end
end

.notify_all(targets, notifiable, options = {}) ⇒ Array<Notificaion>

Generates notifications to specified targets.

Examples:

Notify to all users

ActivityNotification::Notification.notify_all User.all, @comment

Parameters:

  • targets (Array<Object>)

    Targets to send notifications

  • notifiable (Object)

    Notifiable instance

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

    Options for notifications

Options Hash (options):

  • :key (String) — default: notifiable.default_notification_key

    Key of the notification

  • :group (Object) — default: nil

    Group unit of the notifications

  • :notifier (Object) — default: nil

    Notifier of the notifications

  • :parameters (Hash) — default: {}

    Additional parameters of the notifications

  • :send_email (Boolean) — default: true

    Whether it sends notification email

  • :send_later (Boolean) — default: true

    Whether it sends notification email asynchronously

Returns:

  • (Array<Notificaion>)

    Array of generated notifications



56
57
58
# File 'lib/activity_notification/apis/notification_api.rb', line 56

def notify_all(targets, notifiable, options = {})
  targets.map { |target| target.notify_to(notifiable, options) }
end

.notify_to(target, notifiable, options = {}) ⇒ Notification

Generates notifications to one target.

Examples:

Notify to one user

ActivityNotification::Notification.notify_to @comment.auther, @comment

Parameters:

  • target (Object)

    Target to send notifications

  • notifiable (Object)

    Notifiable instance

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

    Options for notifications

Options Hash (options):

  • :key (String) — default: notifiable.default_notification_key

    Key of the notification

  • :group (Object) — default: nil

    Group unit of the notifications

  • :notifier (Object) — default: nil

    Notifier of the notifications

  • :parameters (Hash) — default: {}

    Additional parameters of the notifications

  • :send_email (Boolean) — default: true

    Whether it sends notification email

  • :send_later (Boolean) — default: true

    Whether it sends notification email asynchronously

Returns:



75
76
77
78
79
80
81
82
83
84
# File 'lib/activity_notification/apis/notification_api.rb', line 75

def notify_to(target, notifiable, options = {})
  send_email = options.has_key?(:send_email) ? options[:send_email] : true
  send_later = options.has_key?(:send_later) ? options[:send_later] : true
  # Store notification
  notification = store_notification(target, notifiable, options)
  # Send notification email
  notification.send_notification_email({ send_later: send_later }) if send_email
  # Return created notification
  notification
end

.open_all_of(target, options = {}) ⇒ Integer

TODO:

Add filter option

Opens all notifications of the target.

Parameters:

  • target (Object)

    Target of the notifications to open

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

    Options for opening notifications

Options Hash (options):

  • :opened_at (DateTime) — default: DateTime.now

    Time to set to opened_at of the notification record

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance 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

Returns:

  • (Integer)

    Number of opened notification records



98
99
100
101
# File 'lib/activity_notification/apis/notification_api.rb', line 98

def open_all_of(target, options = {})
  opened_at = options[:opened_at] || DateTime.now
  target.notifications.unopened_only.filtered_by_options(options).update_all(opened_at: opened_at)
end

.send_batch_notification_email(target, notifications, options = {}) ⇒ Mail::Message|ActionMailer::DeliveryJob|NilClass

Sends batch notification email to the target.

Parameters:

  • target (Object)

    Target of batch notification email

  • notifications (Array<Notification>)

    Target notifications to send batch notification email

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

    Options for notification email

Options Hash (options):

  • :send_later (Boolean) — default: false

    If it sends notification email asynchronously

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

    Fallback template to use when MissingTemplate is raised

  • :batch_key (String) — default: nil

    Key of the batch notification email, a key of the first notification will be used if not specified

Returns:

  • (Mail::Message|ActionMailer::DeliveryJob|NilClass)

    Email message or its delivery job, return NilClass for wrong target



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/activity_notification/apis/notification_api.rb', line 121

def send_batch_notification_email(target, notifications, options = {})
  return if notifications.blank?
  if target.batch_notification_email_allowed?(notifications.first.notifiable_type, notifications.first.key)
    send_later = options.has_key?(:send_later) ? options[:send_later] : true
    if send_later
      Mailer.send_batch_notification_email(target, notifications, options).deliver_later
    else
      Mailer.send_batch_notification_email(target, notifications, options).deliver_now
    end
  end
end

Instance Method Details

#group_member?Boolean

Returns if the notification is group member belonging to owner.

Returns:

  • (Boolean)

    If the notification is group member



219
220
221
# File 'lib/activity_notification/apis/notification_api.rb', line 219

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



248
249
250
# File 'lib/activity_notification/apis/notification_api.rb', line 248

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



228
229
230
# File 'lib/activity_notification/apis/notification_api.rb', line 228

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



268
269
270
# File 'lib/activity_notification/apis/notification_api.rb', line 268

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



239
240
241
# File 'lib/activity_notification/apis/notification_api.rb', line 239

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



257
258
259
# File 'lib/activity_notification/apis/notification_api.rb', line 257

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



278
279
280
281
# File 'lib/activity_notification/apis/notification_api.rb', line 278

def group_notifier_count(limit = ActivityNotification.config.opened_index_limit)
  notification = group_member? ? 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



212
213
214
# File 'lib/activity_notification/apis/notification_api.rb', line 212

def group_owner?
  group_owner_id.blank?
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



287
288
289
290
# File 'lib/activity_notification/apis/notification_api.rb', line 287

def latest_group_member
  notification = group_member? ? 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



295
296
297
298
# File 'lib/activity_notification/apis/notification_api.rb', line 295

def notifiable_path
  notifiable.present? or raise ActiveRecord::RecordNotFound.new("Couldn't find notifiable #{notifiable_type}")
  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: DateTime.now

    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



188
189
190
191
192
193
# File 'lib/activity_notification/apis/notification_api.rb', line 188

def open!(options = {})
  opened_at = options[:opened_at] || DateTime.now
  with_members = options.has_key?(:with_members) ? options[:with_members] : true
  update(opened_at: opened_at)
  with_members ? group_members.update_all(opened_at: opened_at) + 1 : 1
end

#opened?Boolean

Returns if the notification is opened.

Returns:

  • (Boolean)

    If the notification is opened



205
206
207
# File 'lib/activity_notification/apis/notification_api.rb', line 205

def opened?
  opened_at.present?
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



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/activity_notification/apis/notification_api.rb', line 170

def send_notification_email(options = {})
  if target.notification_email_allowed?(notifiable, key) and 
     notifiable.notification_email_allowed?(target, key)
    send_later = options.has_key?(:send_later) ? options[:send_later] : true
    if send_later
      Mailer.send_notification_email(self, options).deliver_later
    else
      Mailer.send_notification_email(self, options).deliver_now
    end
  end
end

#unopened?Boolean

Returns if the notification is unopened.

Returns:

  • (Boolean)

    If the notification is unopened



198
199
200
# File 'lib/activity_notification/apis/notification_api.rb', line 198

def unopened?
  !opened?
end