Skip to content
Browse files

Done with news feed

  • Loading branch information...
1 parent 5128830 commit e2e014c91125f41df6a51b7603c8e00b8d853207 Ankit Bansal committed May 19, 2012
View
11 app/models/event_entity.rb
@@ -3,17 +3,22 @@ class EventEntity < ActiveRecord::Base
belongs_to :entity, :polymorphic => true # person, community, ... anyone and anything related to the event
scope :involves, lambda { |entity| where("(entity_id = ? AND entity_type = ?)", entity.id, entity.class.to_s) }
+ scope :human_networks, joins("human_networks on event_entities.entity_id = human_networks.entity_id and event_entities.entity_type = human_networks.entity_type")
+ scope :event_logs, joins("event_logs on event_entities.event_log_id = event_logs.id")
def self.recent_events_for person
event_entities = Arel::Table.new(EventEntity.table_name.to_sym)
event_logs = Arel::Table.new(EventLog.table_name.to_sym)
+ human_networks = Arel::Table.new(HumanNetwork.table_name.to_sym)
+
query = event_entities.join(event_logs).on(event_entities[:event_log_id].eq(event_logs[:id]))
- .where(event_entities[:entity_id].eq(person.id))
- .where(event_entities[:entity_type].eq('Person'))
+ .join(human_networks).on(event_entities[:entity_id].eq(human_networks[:entity_id]))
+ .where(human_networks[:person_id].eq(person.id))
.where(event_entities[:user_only].eq(false))
- .project(event_entities[:event_log_id], event_logs[:event_type_id])
+ .project(event_entities[:event_log_id], event_logs[:event_type_id], human_networks[:network_type])
.order("#{event_entities.name}.created_at desc")
.take(25)
+
EventEntity.find_by_sql(query.to_sql)
end
View
13 app/models/event_type.rb
@@ -7,6 +7,14 @@ class EventType < ActiveRecord::Base
GROUP_NEWS_FEED => 'news feed'
}
+ EVENT_WEIGHTAGE = {
+ 18 => 1,
+ 19 => 3,
+ 20 => 3,
+ 21 => 1,
+ 85 => 1
+ }
+
has_many :activity_logs
has_many :event_logs
@@ -43,6 +51,11 @@ def status_name
# "REQUESTER CANCEL GIFTER", # 16
# "REQUESTER CANCEL REQUESTER" # 17
+
+ def self.weightage event_type_id
+ EVENT_WEIGHTAGE[event_type_id].present? ? EVENT_WEIGHTAGE[event_type_id] : 0
+ end
+
def self.new_item_request_gifter(return_only_id = true)
return_only_id ? 2 : EventType.find(2)
end
View
13 app/models/human_network.rb
@@ -26,6 +26,15 @@ class HumanNetwork < ActiveRecord::Base
validates_presence_of :entity_id, :entity_type, :person_id
+
+
+ NETWORK_WEIGHTAGE = {
+ 'TrustedNetwork' => 3,
+ 'FacebookFriend' => 2,
+ 'MutualNetwork' => 1,
+ 'GroupAdmin' => 2,
+ 'Member' => 2
+ }
# def requester?(person)
# self.person == person
@@ -61,5 +70,7 @@ def update_entity_ids
self.entity_id = self.entity.entity.id
end
-
+ def self.weightage network_type
+ NETWORK_WEIGHTAGE[network_type].present? ? NETWORK_WEIGHTAGE[network_type] : 0
+ end
end
View
52 app/models/person.rb
@@ -52,13 +52,6 @@ class Person < ActiveRecord::Base
scope :notification_candidate, where("(email_notification_count = 0) OR ((email_notification_count in (?)) AND last_notification_email < ?) OR ((email_notification_count in (?)) AND last_notification_email < ?) AND authorised_account = ?", [1,2], Time.now - 78.hours, [3,4], Time.now - 168.hours, true)
scope :exclude_users, lambda { |entity| where("id not in (?)", entity)}
scope :include_users, lambda { |entity| where("id in (?)", entity)}
-
- WEIGHTAGE = {
- :trusted => 3,
- :fb => 2,
- :mutual => 1,
- :group => 1
- }
def recent_activity_logs(min_count = 10)
logs = activity_logs.where(:read => false).order("#{ActivityLog.table_name}.created_at DESC")
@@ -279,46 +272,27 @@ def first_name
end
def news_feed
- user_network = HumanNetwork.involves_as_person(self) + HumanNetwork.part_of_village(self)
recent_events = EventEntity.recent_events_for self
- weighted_connection = {}
- user_network.each do |network|
- if weighted_connection.key(network.person_id).present?
- weighted_connection[network.person_id] += WEIGHTAGE[network.network_type]
- else
- weighted_connection[network.person_id] = WEIGHTAGE[network.network_type]
- end
- end
-
- weighted_events = []
+ weighted_events = {}
recent_events.each do |event|
-
+ weightage = HumanNetwork.weightage(event.network_type) + EventType.weightage(event.event_type_id)
+ weighted_events[event.event_log_id] ||= 0
+ weighted_events[event.event_log_id] += weightage
end
- ee = Arel::Table.new(EventEntity.table_name.to_sym)
- pn = Arel::Table.new(HumanNetwork.table_name.to_sym)
-
- pn_network = pn.project(pn[:person_id], Arel.sql("4 as trusted_relationship_value")).where(pn[:entity_id].eq(self_id)).where(pn[:entity_type].eq("Person"))
-
- query = ee.project(Arel.sql("#{ee.name}.event_log_id as event_log_id"), Arel.sql("SUM(trusted_relationship_value) as total_relationship_value"))
- query = query.join(Arel.sql("LEFT JOIN (#{pn_network.to_sql}) AS network ON #{ee.name}.entity_id = network.person_id AND #{ee.name}.entity_type = 'Person' AND user_only = 'false'"))
- query = query.group(ee[:event_log_id], ee[:created_at]).order("#{ee.name}.created_at DESC").take(25)
- query = query.where(Arel.sql("person_id IS NOT NULL or (#{ee.name}.entity_type = 'Person' and #{ee.name}.entity_id = #{self_id})"))
-
- event_log_ids = EventEntity.find_by_sql(query.to_sql)
-
- # CASHE PREVIOUSLY SHOWN NEWS FEED IF NOT ALREADY CASHED
- event_log_ids = event_log_ids.reverse
-
- event_log_ids.each do |e|
- conditions = { :type_id => EventDisplay::DASHBOARD_FEED,
- :person_id => self_id,
- :event_log_id => e.event_log_id }
+ sorted_events = weighted_events.sort_by { |k,v| v }
+
+ top_events = sorted_events.reverse[0..9]
+ top_events.each do |e|
+ conditions = { :type_id => EventDisplay::DASHBOARD_FEED,
+ :person_id => self.id,
+ :event_log_id => e[0] }
EventDisplay.find(:first, :conditions => conditions) || EventDisplay.create(conditions)
- end
+ end
end
+ # Will remove this once story is approve
# def news_feed
# # Updated SQL to get all events relating to anyone in a user's trusted
# # network or to themselves
View
8 spec/models/event_entity_spec.rb
@@ -4,9 +4,13 @@
describe "recent_events_for" do
it "should provide recent activity of person" do
person = Factory :person
- Factory :event_entity
+ user = Factory :person
+ some_entity = Factory :event_entity
event_entity = Factory :event_entity, :entity_id => person.id
- recent_events = EventEntity.recent_events_for person
+
+ Factory :human_network, :person => user, :entity_id => event_entity.entity_id , :entity_type => event_entity.entity_type
+
+ recent_events = EventEntity.recent_events_for user
recent_events.count.should == 1
recent_events.first.event_log_id.should == event_entity.event_log_id
end
View
11 spec/models/event_type_spec.rb
@@ -1,5 +1,14 @@
require 'spec_helper'
describe EventType do
- pending "add some examples to (or delete) #{__FILE__}"
+ describe "self.weightage" do
+ it "should return weightage of an event type" do
+ EventType.weightage(18).should == 1
+ EventType.weightage(19).should == 3
+ EventType.weightage(20).should == 3
+ EventType.weightage(21).should == 1
+ EventType.weightage(85).should == 1
+ EventType.weightage(86).should == 0
+ end
+ end
end
View
11 spec/models/human_network_spec.rb
@@ -87,3 +87,14 @@ def basic_environment
end
end
+describe "self.weightage" do
+ it "should return weightage of a network type" do
+ HumanNetwork.weightage('TrustedNetwork').should == 3
+ HumanNetwork.weightage('FacebookFriend').should == 2
+ HumanNetwork.weightage('MutualNetwork').should == 1
+ HumanNetwork.weightage('GroupAdmin').should == 2
+ HumanNetwork.weightage('Member').should == 2
+ HumanNetwork.weightage('AnythingElse').should == 0
+ end
+end
+
View
23 spec/models/person_spec.rb
@@ -370,7 +370,28 @@
end
describe "news_feed" do
-
+ it "should find top weighted events and create event cache" do
+ person_1 = Factory :person
+ friend_1 = Factory :person
+ friend_2 = Factory :person
+ village_1 = Factory :village
+ event_log_1 = Factory :event_log
+ event_log_2 = Factory :event_log
+
+ entity = Factory :event_entity
+
+ Factory :event_entity, :entity_id => friend_1.id, :entity_type => "Person", :event_log_id => event_log_1.id
+ Factory :event_entity, :entity_id => village_1.id, :entity_type => "Village", :event_log_id => event_log_1.id
+ Factory :event_entity, :entity_id => friend_2.id, :entity_type => "Person", :event_log_id => event_log_2.id
+
+ Factory :human_network, :person => person_1, :entity_id => friend_1.id , :entity_type => "Person", :network_type => "TrustedNetwork"
+ Factory :human_network, :person => person_1, :entity_id => village_1.id , :entity_type => "Village", :network_type => "Member"
+ Factory :human_network, :person => person_1, :entity_id => friend_2.id , :entity_type => "Person", :network_type => "MutualNetwork"
+
+ person_1.news_feed
+
+ EventDisplay.all.count.should == 2
+ end
end
end

0 comments on commit e2e014c

Please sign in to comment.
Something went wrong with that request. Please try again.