Class: NotificationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/notification_service.rb

Constant Summary collapse

NOTIFICATION_TYPES =
{
  Comment       => [Notifications::MentionedInComment, Notifications::CommentOnPost, Notifications::AlsoCommented],
  Like          => [Notifications::Liked, Notifications::LikedComment],
  StatusMessage => [Notifications::MentionedInPost],
  Conversation  => [Notifications::PrivateMessage],
  Message       => [Notifications::PrivateMessage],
  Reshare       => [Notifications::Reshared],
  Contact       => [Notifications::StartedSharing]
}.freeze
NOTIFICATIONS_JSON_TYPES =
{
  "also_commented"       => "Notifications::AlsoCommented",
  "comment_on_post"      => "Notifications::CommentOnPost",
  "liked"                => "Notifications::Liked",
  "liked_comment"        => "Notifications::LikedComment",
  "mentioned"            => "Notifications::MentionedInPost",
  "mentioned_in_comment" => "Notifications::MentionedInComment",
  "reshared"             => "Notifications::Reshared",
  "started_sharing"      => "Notifications::StartedSharing",
  "contacts_birthday"    => "Notifications::ContactsBirthday"
}.freeze
NOTIFICATIONS_REVERSE_JSON_TYPES =
NOTIFICATIONS_JSON_TYPES.invert.freeze

Instance Method Summary collapse

Constructor Details

#initialize(user = nil) ⇒ NotificationService

Returns a new instance of NotificationService.



28
29
30
# File 'app/services/notification_service.rb', line 28

def initialize(user=nil)
  @user = user
end

Instance Method Details

#get_by_guid(guid) ⇒ Object



43
44
45
# File 'app/services/notification_service.rb', line 43

def get_by_guid(guid)
  Notification.where(recipient_id: @user.id, guid: guid).first
end

#index(unread_only = nil, only_after = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'app/services/notification_service.rb', line 32

def index(unread_only=nil, only_after=nil)
  query_string = "recipient_id = ? "
  query_string += "AND unread = true " if unread_only
  where_clause = [query_string, @user.id]
  if only_after
    query_string += " AND created_at >= ?"
    where_clause = [query_string, @user.id, only_after]
  end
  Notification.where(where_clause).includes(:target, actors: :profile)
end

#notify(object, recipient_user_ids) ⇒ Object



55
56
57
# File 'app/services/notification_service.rb', line 55

def notify(object, recipient_user_ids)
  notification_types(object).each {|type| type.notify(object, recipient_user_ids) }
end

#update_status_by_guid(guid, is_read_status) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


47
48
49
50
51
52
53
# File 'app/services/notification_service.rb', line 47

def update_status_by_guid(guid, is_read_status)
  notification = get_by_guid(guid)
  raise ActiveRecord::RecordNotFound unless notification

  notification.set_read_state(is_read_status)
  true
end