X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FActivityPub%2FProcessor.php;h=f723d287b9d232b07679767feb841a2063cfc697;hb=b1e4c0931af6a1c937ea713cf70a5dbc742f30db;hp=be17ea469c55c9959e2eaa2868f522aeaa4a0861;hpb=834844573bd9ad0ddd9b1caa59252f0468b36d5c;p=friendica.git diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index be17ea469c..f723d287b9 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -27,6 +27,7 @@ use Friendica\Content\Text\Markdown; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Core\System; +use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\APContact; @@ -46,6 +47,7 @@ use Friendica\Protocol\Relay; use Friendica\Util\DateTimeFormat; use Friendica\Util\JsonLD; use Friendica\Util\Strings; +use Friendica\Worker\Delivery; /** * ActivityPub Processor Protocol class @@ -69,7 +71,7 @@ class Processor * @param array $languages * @return string language JSON */ - private static function processLanguages(array $languages) + private static function processLanguages(array $languages): string { $codes = array_keys($languages); $lang = []; @@ -86,12 +88,13 @@ class Processor /** * Replaces emojis in the body * - * @param array $emojis + * @param int $uri_id * @param string $body + * @param array $emojis * * @return string with replaced emojis */ - private static function replaceEmojis(int $uri_id, $body, array $emojis) + private static function replaceEmojis(int $uri_id, string $body, array $emojis) { $body = strtr($body, array_combine( @@ -141,7 +144,7 @@ class Processor * @param array $activity * @param array $item */ - private static function storeAttachments($activity, $item) + private static function storeAttachments(array $activity, array $item) { if (empty($activity['attachments'])) { return; @@ -158,7 +161,7 @@ class Processor * @param array $activity * @param array $item */ - private static function storeQuestion($activity, $item) + private static function storeQuestion(array $activity, array $item) { if (empty($activity['question'])) { return; @@ -170,7 +173,7 @@ class Processor } if (!empty($activity['question']['end-time'])) { - $question['end-time'] = $activity['question']['end-time']; + $question['end-time'] = DateTimeFormat::utc($activity['question']['end-time']); } Post\Question::update($item['uri-id'], $question); @@ -189,7 +192,7 @@ class Processor * @param array $activity Activity array * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function updateItem($activity) + public static function updateItem(array $activity) { $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity', 'post-type'], ['uri' => $activity['id']]); if (!DBA::isResult($item)) { @@ -215,6 +218,7 @@ class Processor return; } + Post\History::add($item['uri-id'], $item); Item::update($item, ['uri' => $activity['id']]); if ($activity['object_type'] == 'as:Event') { @@ -238,8 +242,12 @@ class Processor $event['edited'] = DateTimeFormat::utc($activity['updated']); $event['summary'] = HTML::toBBCode($activity['name']); $event['desc'] = HTML::toBBCode($activity['content']); - $event['start'] = $activity['start-time']; - $event['finish'] = $activity['end-time']; + if (!empty($activity['start-time'])) { + $event['start'] = DateTimeFormat::utc($activity['start-time']); + } + if (!empty($activity['end-time'])) { + $event['finish'] = DateTimeFormat::utc($activity['end-time']); + } $event['nofinish'] = empty($event['finish']); $event['location'] = $activity['location']; @@ -255,7 +263,7 @@ class Processor * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function createItem($activity) + public static function createItem(array $activity): array { $item = []; $item['verb'] = Activity::POST; @@ -404,7 +412,7 @@ class Processor * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function deleteItem($activity) + public static function deleteItem(array $activity) { $owner = Contact::getIdForURL($activity['actor']); @@ -419,7 +427,7 @@ class Processor * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function addTag($activity) + public static function addTag(array $activity) { if (empty($activity['object_content']) || empty($activity['object_id'])) { return; @@ -450,7 +458,7 @@ class Processor * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function createActivity($activity, $verb) + public static function createActivity(array $activity, string $verb) { $item = self::createItem($activity); if (empty($item)) { @@ -475,8 +483,8 @@ class Processor /** * Fetch the Uri-Id of a post for the "featured" collection * - * @param array $activity - * @return null|int + * @param array $activity + * @return null|int */ private static function getUriIdForFeaturedCollection(array $activity) { @@ -514,7 +522,7 @@ class Processor /** * Add a post to the "Featured" collection * - * @param array $activity + * @param array $activity */ public static function addToFeaturedCollection(array $activity) { @@ -531,7 +539,7 @@ class Processor /** * Remove a post to the "Featured" collection * - * @param array $activity + * @param array $activity */ public static function removeFromFeaturedCollection(array $activity) { @@ -554,12 +562,16 @@ class Processor * @return int event id * @throws \Exception */ - public static function createEvent($activity, $item) + public static function createEvent(array $activity, array $item): int { $event['summary'] = HTML::toBBCode($activity['name'] ?: $activity['summary']); $event['desc'] = HTML::toBBCode($activity['content']); - $event['start'] = $activity['start-time']; - $event['finish'] = $activity['end-time']; + if (!empty($activity['start-time'])) { + $event['start'] = DateTimeFormat::utc($activity['start-time']); + } + if (!empty($activity['end-time'])) { + $event['finish'] = DateTimeFormat::utc($activity['end-time']); + } $event['nofinish'] = empty($event['finish']); $event['location'] = $activity['location']; $event['cid'] = $item['contact-id']; @@ -594,20 +606,22 @@ class Processor * @return array|bool Returns the item array or false if there was an unexpected occurrence * @throws \Exception */ - private static function processContent($activity, $item) + private static function processContent(array $activity, array $item) { if (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/markdown')) { - $item['title'] = Markdown::toBBCode($activity['name']); + $item['title'] = strip_tags($activity['name']); $content = Markdown::toBBCode($activity['content']); } elseif (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/bbcode')) { $item['title'] = $activity['name']; $content = $activity['content']; } else { // By default assume "text/html" - $item['title'] = HTML::toBBCode($activity['name']); + $item['title'] = (empty($activity['name']) ? HTML::toBBCode($activity['name']) : ''); $content = HTML::toBBCode($activity['content']); } + $item['title'] = trim(BBCode::toPlaintext($item['title'])); + if (!empty($activity['languages'])) { $item['language'] = self::processLanguages($activity['languages']); } @@ -941,7 +955,10 @@ class Processor } else { $name = trim(parse_url($receiver, PHP_URL_PATH), '/'); } - Tag::store($uriid, $type, $name, $receiver); + + $target = Tag::getTargetType($receiver); + Logger::debug('Got target type', ['type' => $target, 'url' => $receiver]); + Tag::store($uriid, $type, $name, $receiver, $target); } } } @@ -1013,8 +1030,8 @@ class Processor /** * Fetch featured posts from a contact with the given url * - * @param string $url - * @return void + * @param string $url + * @return void */ public static function fetchFeaturedPosts(string $url) { @@ -1265,6 +1282,10 @@ class Processor return; } + if ($result && DI::config()->get('system', 'transmit_pending_events') && ($owner['contact-type'] == Contact::TYPE_COMMUNITY)) { + self::transmitPendingEvents($cid, $owner['uid']); + } + if (empty($contact)) { Contact::update(['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]); } @@ -1272,6 +1293,33 @@ class Processor Logger::notice('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']); } + /** + * Transmit pending events to the new follower + * + * @param integer $cid + * @param integer $uid + * @return void + */ + private static function transmitPendingEvents(int $cid, int $uid) + { + $account = DBA::selectFirst('account-user-view', ['ap-inbox', 'ap-sharedinbox'], ['id' => $cid]); + $inbox = $account['ap-sharedinbox'] ?: $account['ap-inbox']; + + $events = DBA::select('event', ['id'], ["`uid` = ? AND `start` > ? AND `type` != ?", $uid, DateTimeFormat::utcNow(), 'birthday']); + while ($event = DBA::fetch($events)) { + $post = Post::selectFirst(['id', 'uri-id', 'created'], ['event-id' => $event['id']]); + if (empty($post)) { + continue; + } + if (DI::config()->get('system', 'bulk_delivery')) { + Post\Delivery::add($post['uri-id'], $uid, $inbox, $post['created'], Delivery::POST, [$cid]); + Worker::add(PRIORITY_HIGH, 'APDelivery', '', 0, $inbox, 0); + } else { + Worker::add(PRIORITY_HIGH, 'APDelivery', Delivery::POST, $post['id'], $inbox, $uid, [$cid], $post['uri-id']); + } + } + } + /** * Update the given profile * @@ -1330,7 +1378,7 @@ class Processor $uid = User::getIdForURL($activity['object_id']); if (empty($uid)) { - return; + return; } Contact\User::setIsBlocked($cid, $uid, true); @@ -1353,7 +1401,7 @@ class Processor $uid = User::getIdForURL($activity['object_object']); if (empty($uid)) { - return; + return; } Contact\User::setIsBlocked($cid, $uid, false);