Skip to content

Commit

Permalink
Merge pull request #181 from MartinGoulet/notifications
Browse files Browse the repository at this point in the history
Create a notification when creating a note
  • Loading branch information
acmetech committed Feb 6, 2014
2 parents 382c2bc + ea8f0ea commit 5709674
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/socializer/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def create
@note.activity_verb = 'post'
@note.save!
@activity = Activity.find_by(activity_object_id: @note.guid)
Notification.create_for_activity(@activity)
respond_to do |format|
format.js
end
Expand Down
38 changes: 38 additions & 0 deletions app/models/socializer/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Notification < ActiveRecord::Base
# Increment the number of unread notifications
after_save { activity_object.increment_unread_notifications_count }

default_scope { order(created_at: :desc) }

attr_accessible :read

# Relationships
Expand All @@ -12,5 +14,41 @@ class Notification < ActiveRecord::Base
# Validations
validates :activity_id, presence: true
validates :activity_object_id, presence: true

def self.create_for_activity(activity)
# Get all ties related to the audience of the activity
potential_contact_id = get_potential_contact_id(activity.id)
potential_contact_id.each do |t|
# If the contact has the author of the activity in one of his circle.
if has_person_into_circle(t.contact_id, activity.activitable_actor.id)
create_notification(activity, t.contact_id)
end
end
end

private

def self.create_notification(activity, contact_id)
n = Notification.new
n.activity = activity
n.activity_object = ActivityObject.find_by(id: contact_id)
n.save!
end

def self.get_potential_contact_id(activity_id)
# Activity -> Audience -> ActivityObject -> Circle -> Tie -> contact_id
Tie.select { contact_id
}.joins { circle.activity_object.audiences
}.where { circle.activity_object.audiences.activity_id.eq( activity_id ) }.flatten.uniq
end

def self.has_person_into_circle(parent_contact_id, child_contact_id)
# ActivityObject.id = parent_contact_id
# ActivityObject -> Circle -> Tie -> contact_id = child_contact_id
ActivityObject.select { id
}.joins { circles.ties
}.where { id.eq(parent_contact_id) & circles.ties.contact_id.eq(child_contact_id)
}.first.present?
end
end
end
3 changes: 2 additions & 1 deletion app/views/socializer/notifications/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
<div class="notification-body">
<div>
<span class="person-name"><%= n.activity.actor.display_name %></span>
<span><%= n.activity.verb.name %></span>
<span><%= 'has %s ' % n.activity.verb.name %></span>
<span><%= 'a note' if n.activity.activitable_object.note? %></span>
</div>
<div class="time-elapsed"><%= time_ago(n.created_at) %></div>
</div>
Expand Down

0 comments on commit 5709674

Please sign in to comment.