- self::processActivity($activity);
- }
- }
-
- function processActivity($activity, $body = '')
- {
- if (empty($activity['type'])) {
- logger('Empty type', LOGGER_DEBUG);
- return;
- }
-
- if (empty($activity['object'])) {
- logger('Empty object', LOGGER_DEBUG);
- return;
- }
-
- if (empty($activity['actor'])) {
- logger('Empty actor', LOGGER_DEBUG);
- return;
-
- }
-
- $actor = self::processElement($activity, 'actor', 'id');
- if (empty($actor)) {
- logger('Empty actor - 2', LOGGER_DEBUG);
- return;
- }
-
- if (is_string($activity['object'])) {
- $object_url = $activity['object'];
- } elseif (!empty($activity['object']['id'])) {
- $object_url = $activity['object']['id'];
- } else {
- logger('No object found', LOGGER_DEBUG);
- return;
- }
-
- $receivers = self::getReceivers($activity);
- if (empty($receivers)) {
- logger('No receivers found', LOGGER_DEBUG);
- return;
- }
-
- // ----------------------------------
-/*
- // unhandled
- unset($activity['@context']);
- unset($activity['id']);
-
- // Non standard
- unset($activity['title']);
- unset($activity['atomUri']);
- unset($activity['context_id']);
- unset($activity['statusnetConversationId']);
-
- // To-Do?
- unset($activity['context']);
- unset($activity['location']);
- unset($activity['signature']);
-*/
-
- if (!in_array($activity['type'], ['Like', 'Dislike'])) {
- $item = self::fetchObject($object_url, $activity['object']);
- if (empty($item)) {
- logger("Object data couldn't be processed", LOGGER_DEBUG);
- return;
- }
- } else {
- $item['object'] = $object_url;
- $item['receiver'] = [];
- $item['type'] = $activity['type'];
- }
-
- $item = self::addActivityFields($item, $activity);
-
- $item['owner'] = $actor;
-
- $item['receiver'] = array_merge($item['receiver'], $receivers);
-
- logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
-
- switch ($activity['type']) {
- case 'Create':
- case 'Update':
- case 'Announce':
- self::createItem($item, $body);
- break;
-
- case 'Like':
- case 'Dislike':
- self::activityItem($item, $body);
- break;
-
- case 'Follow':
- break;
-
- default:
- logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
- 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', ['uid'], $condition);
- if (!DBA::isResult($contact)) {
- continue;
- }
- $receivers[$receiver] = $contact['uid'];
- }
- }
- 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');
- }
- return $item;
- }
-
- private static function fetchObject($object_url, $object = [])
- {
- $data = self::fetchContent($object_url);
- if (empty($data)) {
- $data = $object;
- if (empty($data)) {
- logger('Empty content');
- return false;
- } else {
- logger('Using provided object');
- }
- }
-
- if (empty($data['type'])) {
- logger('Empty type');
- return false;
- } else {
- $type = $data['type'];
- logger('Type ' . $type);
- }
-
- if (in_array($type, ['Note', 'Article', 'Video'])) {
- $common = self::processCommonData($data);
- }
-
- switch ($type) {
- case 'Note':
- return array_merge($common, self::processNote($data));
- case 'Article':
- return array_merge($common, self::processArticle($data));
- case 'Video':
- return array_merge($common, self::processVideo($data));
-
- case 'Announce':
- if (empty($data['object'])) {
- return false;
- }
- return self::fetchObject($data['object']);
-
- case 'Person':
- case 'Tombstone':
- break;
-
- default:
- logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
- break;
- }
- }
-
- private static function processCommonData(&$object)
- {
- if (empty($object['id']) || empty($object['attributedTo'])) {
- return false;
- }
-
- $item = [];
- $item['type'] = $object['type'];
- $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'];