- self::processActivity($activity);
- }
- }
-
- function processActivity($activity, $body = '', $uid = null)
- {
- 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;
- }
-
- // ----------------------------------
-/*
- // 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']);
-*/
- // Fetch all receivers from to, cc, bto and bcc
- $receivers = self::getReceivers($activity);
-
- // When it is a delivery to a personal inbox we add that user to the receivers
- if (!empty($uid)) {
- $owner = User::getOwnerDataById($uid);
- $additional = [$owner['url'] => $uid];
- $receivers = array_merge($receivers, $additional);
- }
-
- logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
-
- logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
-
- // Fetch the content only on activities where this matters
- if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
- $item = self::fetchObject($object_url, $activity['object']);
- if (empty($item)) {
- logger("Object data couldn't be processed", LOGGER_DEBUG);
- return;
- }
- } else {
- if (in_array($activity['type'], ['Accept'])) {
- $item['object'] = self::processElement($activity, 'object', 'actor', 'type', 'Follow');
- } elseif (in_array($activity['type'], ['Undo'])) {
- $item['object'] = self::processElement($activity, 'object', 'object', 'type', 'Follow');
- } else {
- $item['uri'] = $activity['id'];
- $item['author'] = $activity['actor'];
- $item['updated'] = $item['published'] = $activity['published'];
- $item['uuid'] = '';
- $item['name'] = $activity['type'];
- $item['summary'] = '';
- $item['content'] = '';
- $item['location'] = '';
- $item['tags'] = [];
- $item['sensitive'] = false;
- $item['service'] = '';
- $item['attachments'] = [];
- $item['conversation'] = '';
- $item['object'] = $object_url;
- }
- $item['id'] = $activity['id'];
- $item['receiver'] = [];
- $item['type'] = $activity['type'];
- }
-
- $item = self::addActivityFields($item, $activity);
-
- $item['owner'] = $actor;
-
- $item['receiver'] = array_merge($item['receiver'], $receivers);
-
- switch ($activity['type']) {
- case 'Create':
- case 'Update':
- case 'Announce':
- self::createItem($item, $body);
- break;
-
- case 'Like':
- self::likeItem($item, $body);
- break;
-
- case 'Dislike':
- break;
-
- case 'Delete':
- break;
-
- case 'Follow':
- self::followUser($item);
- break;
-
- case 'Accept':
- self::acceptFollowUser($item);
- break;
-
- case 'Undo':
- self::undoFollowUser($item);
- 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', 'type', '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', LOGGER_DEBUG);
- return false;
- } elseif (is_string($data)) {
- logger('No object array provided.', LOGGER_DEBUG);
- $item = Item::selectFirst([], ['uri' => $data]);
- if (!DBA::isResult($item)) {
- logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
- return false;
- }
- logger('Using already stored item', LOGGER_DEBUG);
- $data = self::createNote($item);
- } else {
- logger('Using provided object', LOGGER_DEBUG);
- }
- }
-
- if (empty($data['type'])) {
- logger('Empty type', LOGGER_DEBUG);
- return false;
- } else {
- $type = $data['type'];
- logger('Type ' . $type, LOGGER_DEBUG);
- }
-
- 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'];
- }
-
- $item['uuid'] = defaults($object, 'uuid', null);
- $item['owner'] = $item['author'] = self::processElement($object, 'attributedTo', 'id');
- $item['context'] = defaults($object, 'context', null);
- $item['conversation'] = defaults($object, 'conversation', null);
- $item['sensitive'] = defaults($object, 'sensitive', null);
- $item['name'] = defaults($object, 'title', null);
- $item['name'] = defaults($object, 'name', $item['name']);
- $item['summary'] = defaults($object, 'summary', null);
- $item['content'] = defaults($object, 'content', null);
- $item['source'] = defaults($object, 'source', null);
- $item['location'] = self::processElement($object, 'location', 'name', 'type', 'Place');
- $item['attachments'] = defaults($object, 'attachment', null);
- $item['tags'] = defaults($object, 'tag', null);
- $item['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service');
- $item['alternate-url'] = self::processElement($object, 'url', 'href');
- $item['receiver'] = self::getReceivers($object);
-
-/*
- // To-Do
- unset($object['source']);
-
- // Unhandled
- unset($object['@context']);
- unset($object['type']);
- unset($object['actor']);
- unset($object['signature']);
- unset($object['mediaType']);
- unset($object['duration']);
- unset($object['replies']);
- unset($object['icon']);
-
- // Also missing:
- audience, preview, endTime, startTime, generator, image
-*/
- return $item;
- }
-
- private static function processNote($object)
- {
- $item = [];
-
-/*
- // To-Do?
- unset($object['emoji']);
- unset($object['atomUri']);
- unset($object['inReplyToAtomUri']);
-
- // Unhandled
- unset($object['contentMap']);
- unset($object['announcement_count']);
- unset($object['announcements']);
- unset($object['context_id']);
- unset($object['likes']);
- unset($object['like_count']);
- unset($object['inReplyToStatusId']);
- unset($object['shares']);
- unset($object['quoteUrl']);
- unset($object['statusnetConversationId']);
-*/
- return $item;
- }
-
- private static function processArticle($object)
- {
- $item = [];
-
- return $item;
- }
-
- private static function processVideo($object)
- {
- $item = [];
-/*
- // To-Do?
- unset($object['category']);
- unset($object['licence']);
- unset($object['language']);
- unset($object['commentsEnabled']);
-
- // Unhandled
- unset($object['views']);
- unset($object['waitTranscoding']);
- unset($object['state']);
- unset($object['support']);
- unset($object['subtitleLanguage']);
- unset($object['likes']);
- unset($object['dislikes']);
- unset($object['shares']);
- unset($object['comments']);
-*/
- return $item;
- }
-
- private static function processElement($array, $element, $key, $type = null, $type_value = null)
- {
- if (empty($array)) {
- return false;
- }
-
- if (empty($array[$element])) {
- return false;
- }
-
- if (is_string($array[$element])) {
- return $array[$element];
- }
-
- if (is_null($type_value)) {
- if (!empty($array[$element][$key])) {
- return $array[$element][$key];
- }
-
- if (!empty($array[$element][0][$key])) {
- return $array[$element][0][$key];
- }
-
- return false;
- }
-
- if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
- return $array[$element][$key];
- }
-
- /// @todo Add array search
-
- return false;
- }
-
- private static function convertMentions($body)
- {
- $URLSearchString = "^\[\]";
- $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
-
- return $body;
- }
-
- private static function constructTagList($tags, $sensitive)
- {
- if (empty($tags)) {
- return '';
- }
-
- $tag_text = '';
- foreach ($tags as $tag) {
- if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
- if (!empty($tag_text)) {
- $tag_text .= ',';
- }
-
- $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
- }