Last active
August 29, 2015 14:07
-
-
Save tbarbugli/eb5ccb50431ff3c6704a to your computer and use it in GitHub Desktop.
activity_enrich
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_users($ids){ | |
// queries the db and returns an assoc array eg. array(1=>user1,...) | |
return array( | |
'1'=> 'tommaso', | |
'2'=> 'mike', | |
'3'=> 'thierry', | |
); | |
} | |
// same as get_users but on different tables | |
function get_posts($ids){ | |
return array( | |
'23'=> 'building a newsfeed with stream', | |
); | |
} | |
function get_follows($ids){ | |
return array( | |
'24'=> 'mike followed tommaso', | |
); | |
} | |
function get_images($ids){ | |
return array( | |
'24'=> 'http://33.media.tumblr.com/avatar_be8b5eef9ae6_128.png', | |
); | |
} | |
function enrich_activities($activities){ | |
$ids = array(); | |
$objects = array(); | |
// collect object ids to retrieve from database | |
foreach ($activities as $activity) { | |
$ids['user'][$activity['actor']] = 1; | |
$content_data = explode(":", $activity['object']); | |
$content_type = $content_data[0]; | |
$content_id = $content_data[1]; | |
$ids[$content_type][$content_id] = 1; | |
} | |
// gets data from db | |
$objects['user'] = get_users(array_keys($ids['user'])); | |
$objects['post'] = get_posts(array_keys($ids['post'])); | |
$objects['image'] = get_follows(array_keys($ids['image'])); | |
$objects['follow'] = get_images(array_keys($ids['follow'])); | |
// at this point $objects looks like this | |
// array( | |
// 'user'=>array(1=>user1, 2=>user2), | |
// 'post'=>array(23=>post23), | |
// ) | |
foreach ($activities as $key => $activity) { | |
$activities[$key]['actor'] = $objects['user'][$activity['actor']]; | |
$content_data = explode(":", $activity['object']); | |
$content_type = $content_data[0]; | |
$content_id = $content_data[1]; | |
$activities[$key]['object'] = $objects[$content_type][$content_id]; | |
} | |
return $activities; | |
} | |
$activities = array( | |
array('actor'=> '1', 'verb'=> 'tweet', 'object'=> 'post:23'), | |
array('actor'=> '2', 'verb'=> 'tweet', 'object'=> 'image:24'), | |
array('actor'=> '3', 'verb'=> 'tweet', 'object'=> 'follow:24') | |
); | |
print_r(enrich_activities($activities)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment