Welcome to mirror list, hosted at ThFree Co, Russian Federation.

notification.rb « models « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aaa56d53d827d7e281f5526f00bd4ce76027d765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.
#
class Notification < ActiveRecord::Base
  belongs_to :recipient, class_name: "User"
  has_many :notification_actors, dependent: :destroy
  has_many :actors, class_name: "Person", through: :notification_actors, source: :person
  belongs_to :target, polymorphic: true

  def self.for(recipient, opts={})
    where(opts.merge!(recipient_id: recipient.id)).order("updated_at DESC")
  end

  def email_the_user(target, actor)
    recipient.mail(mail_job, recipient_id, actor.id, target.id)
  end

  def set_read_state( read_state )
    update_column(:unread, !read_state)
  end

  def mail_job
    raise NotImplementedError.new("Subclass this.")
  end

  def linked_object
    target
  end

  def self.concatenate_or_create(recipient, target, actor)
    return nil if suppress_notification?(recipient, actor)

    find_or_initialize_by(recipient: recipient, target: target, unread: true).tap do |notification|
      notification.actors |= [actor]
      # Explicitly touch the notification to update updated_at whenever new actor is inserted in notification.
      if notification.new_record? || notification.changed?
        notification.save!
      else
        notification.touch
      end
    end
  end

  def self.create_notification(recipient, target, actor)
    return nil if suppress_notification?(recipient, actor)

    create(recipient: recipient, target: target, actors: [actor])
  end

  private_class_method def self.suppress_notification?(recipient, actor)
    recipient.blocks.where(person: actor).exists?
  end

  def self.types
    {
      "also_commented" => "Notifications::AlsoCommented",
      "comment_on_post" => "Notifications::CommentOnPost",
      "liked" => "Notifications::Liked",
      "mentioned" => "Notifications::Mentioned",
      "reshared" => "Notifications::Reshared",
      "started_sharing" => "Notifications::StartedSharing"
    }
  end
end