-
- 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['location'] = self::processElement($object, 'location', 'name', 'Place');
- $item['attachments'] = defaults($object, 'attachment', null);
- $item['tags'] = defaults($object, 'tag', null);
- $item['service'] = self::processElement($object, 'instrument', 'name', '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)
- {
- if (empty($array)) {
- return false;
- }
-
- if (empty($array[$element])) {
- return false;
- }
-
- if (is_string($array[$element])) {
- return $array[$element];
- }
-
- if (is_null($type)) {
- 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)) {
- 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]';
- }
- }
-
- /// @todo add nsfw for $sensitive
-
- return $tag_text;
- }
-
- private static function constructAttachList($attachments, $item)
- {
- if (empty($attachments)) {
- return $item;
- }
-
- foreach ($attachments as $attach) {
- $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
- if ($filetype == 'image') {
- $item['body'] .= "\n[img]".$attach['url'].'[/img]';
- } else {
- if (!empty($item["attach"])) {
- $item["attach"] .= ',';
- } else {
- $item["attach"] = '';
- }
- if (!isset($attach['length'])) {
- $attach['length'] = "0";
- }
- $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
- }
- }
-
- return $item;
- }
-
- private static function createItem($activity, $body)
- {
- /// @todo What to do with $activity['context']?
-
- $item = [];
- $item['network'] = Protocol::ACTIVITYPUB;
- $item['private'] = !in_array(0, $activity['receiver']);
- $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
- $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
- $item['uri'] = $activity['uri'];
- $item['parent-uri'] = $activity['reply-to-uri'];
- $item['verb'] = ACTIVITY_POST;
- $item['object-type'] = ACTIVITY_OBJ_NOTE; /// Todo?
- $item['created'] = $activity['published'];
- $item['edited'] = $activity['updated'];
- $item['guid'] = $activity['uuid'];
- $item['title'] = HTML::toBBCode($activity['name']);
- $item['content-warning'] = HTML::toBBCode($activity['summary']);
- $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
- $item['location'] = $activity['location'];
- $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
- $item['app'] = $activity['service'];
- $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
-
- $item = self::constructAttachList($activity['attachments'], $item);
-
- $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
- $item['source'] = $body;
- $item['conversation-uri'] = $activity['conversation'];
-
- foreach ($activity['receiver'] as $receiver) {
- $item['uid'] = $receiver;
- $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
-
- if (($receiver != 0) && empty($item['contact-id'])) {
- $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
- }
-
- $item_id = Item::insert($item);
- logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
- if (!empty($item_id) && ($item['uid'] == 0)) {
- Item::distribute($item_id);
- }
- }
- }
-
- private static function activityItem($data)
- {
- logger('Activity "' . $data['type'] . '" for ' . $data['object']);
- $items = Item::select(['id'], ['uri' => $data['object']]);
- while ($item = Item::fetch($items)) {
- logger('Activity ' . $data['type'] . ' for item ' . $item['id'], LOGGER_DEBUG);
- Item::performLike($item['id'], strtolower($data['type']));
- }
- DBA::close($item);
- logger('Activity done');
- }
-