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],
  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",
  "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.


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

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

Instance Method Details

#get_by_guid(guid) ⇒ Object


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

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

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


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

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


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

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)

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

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