- self::processActivity($activity, $url);
- }
- }
-
- function processActivity($activity, $url)
- {
- if (empty($activity['type'])) {
- return;
- }
-
- if (empty($activity['object'])) {
- return;
- }
-
- if (empty($activity['actor'])) {
- return;
- }
-
- $actor = self::processElement($activity, 'actor', 'id');
- if (empty($actor)) {
- return;
- }
-
- if (is_string($activity['object'])) {
- $object_url = $activity['object'];
- } elseif (!empty($activity['object']['id'])) {
- $object_url = $activity['object']['id'];
- } else {
- return;
- }
-
- $receivers = self::getReceivers($activity);
- if (empty($receivers)) {
- return;
- }
-
- // ----------------------------------
- // unhandled
- unset($activity['@context']);
- unset($activity['id']);
-
- // Non standard
- unset($activity['title']);
- unset($activity['atomUri']);
- unset($activity['context_id']);
- unset($activity['statusnetConversationId']);
-
- $structure = $activity;
-
- // To-Do?
- unset($activity['context']);
- unset($activity['location']);
-
- // handled
- unset($activity['to']);
- unset($activity['cc']);
- unset($activity['bto']);
- unset($activity['bcc']);
- unset($activity['type']);
- unset($activity['actor']);
- unset($activity['object']);
- unset($activity['published']);
- unset($activity['updated']);
- unset($activity['instrument']);
- unset($activity['inReplyTo']);
-
- if (!empty($activity)) {
- echo "Activity\n";
- print_r($activity);
- die($url."\n");
- }
-
- $activity = $structure;
- // ----------------------------------
-
- $item = self::fetchObject($object_url, $url);
- if (empty($item)) {
- return;
- }
-
- $item = self::addActivityFields($item, $activity);
-
- $item['owner'] = $actor;
-
- $item['receiver'] = array_merge($item['receiver'], $receivers);
-
- switch ($activity['type']) {
- case 'Create':
- case 'Update':
- self::createItem($item);
- break;
-
- case 'Announce':
- self::announceItem($item);
- break;
-
- case 'Like':
- case 'Dislike':
- self::activityItem($item);
- break;
-
- case 'Follow':
- break;
-
- default:
- echo "Unknown activity: ".$activity['type']."\n";
- print_r($item);
- die();
- break;
- }
- }
-
- private static function getReceivers($activity)
- {
- $receivers = [];
-
- $elements = ['to', 'cc', 'bto', 'bcc'];
- foreach ($elements as $element) {
- if (empty($activity[$element])) {
- continue;
- }
-
- // The receiver can be an arror or a string
- if (is_string($activity[$element])) {
- $activity[$element] = [$activity[$element]];
- }
-
- foreach ($activity[$element] as $receiver) {
- if ($receiver == self::PUBLIC) {
- $receivers[$receiver] = 0;
- }
-
- $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
- $contact = DBA::selectFirst('contact', ['id'], $condition);
- if (!DBA::isResult($contact)) {
- continue;
- }
- $receivers[$receiver] = $contact['id'];
- }
- }
- return $receivers;
- }
-
- private static function addActivityFields($item, $activity)
- {
- if (!empty($activity['published']) && empty($item['published'])) {
- $item['published'] = $activity['published'];
- }
-
- if (!empty($activity['updated']) && empty($item['updated'])) {
- $item['updated'] = $activity['updated'];
- }
-
- if (!empty($activity['inReplyTo']) && empty($item['parent-uri'])) {
- $item['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
- }
-
- if (!empty($activity['instrument'])) {
- $item['service'] = self::processElement($activity, 'instrument', 'name', 'Service');
- }
-
- // Remove all "null" fields
- foreach ($item as $field => $content) {
- if (is_null($content)) {
- unset($item[$field]);
- }
- }
-
- return $item;
- }
-
- private static function fetchObject($object_url, $url)
- {
- $data = self::fetchContent($object_url);
- if (empty($data)) {
- return false;
- }
-
- if (empty($data['type'])) {
- return false;
- } else {
- $type = $data['type'];
- }
-
- if (in_array($type, ['Note', 'Article', 'Video'])) {
- $common = self::processCommonData($data, $url);
- }
-
- switch ($type) {
- case 'Note':
- return array_merge($common, self::processNote($data, $url));
- case 'Article':
- return array_merge($common, self::processArticle($data, $url));
- case 'Video':
- return array_merge($common, self::processVideo($data, $url));
-
- case 'Announce':
- if (empty($data['object'])) {
- return false;
- }
- return self::fetchObject($data['object'], $url);
-
- case 'Person':
- case 'Tombstone':
- break;
-
- default:
- echo "Unknown object type: ".$data['type']."\n";
- print_r($data);
- die($url."\n");
- break;
- }
- }
-
- private static function processCommonData(&$object, $url)
- {
- if (empty($object['id']) || empty($object['attributedTo'])) {
- return false;
- }
-
- $item = [];
- $item['uri'] = $object['id'];
-
- if (!empty($object['inReplyTo'])) {
- $item['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
- } else {
- $item['reply-to-uri'] = $item['uri'];
- }
-
- $item['published'] = defaults($object, 'published', null);
- $item['updated'] = defaults($object, 'updated', $item['published']);
-
- if (empty($item['published']) && !empty($item['updated'])) {
- $item['published'] = $item['updated'];