From 57cf384fae928d88a019abfcb2b72768d2a6d205 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 24 Nov 2021 07:06:28 +0000 Subject: [PATCH] Moved acitivites --- include/api.php | 80 +----------------------- src/DI.php | 8 +++ src/Factory/Api/Friendica/Activities.php | 16 ++++- tests/legacy/ApiTest.php | 8 +-- 4 files changed, 28 insertions(+), 84 deletions(-) diff --git a/include/api.php b/include/api.php index 8db59070b..b93fa39d5 100644 --- a/include/api.php +++ b/include/api.php @@ -49,7 +49,6 @@ use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\TooManyRequestsException; use Friendica\Network\HTTPException\UnauthorizedException; use Friendica\Object\Image; -use Friendica\Protocol\Activity; use Friendica\Security\BasicAuth; use Friendica\Util\DateTimeFormat; use Friendica\Util\Images; @@ -245,83 +244,6 @@ function api_format_messages($item, $recipient, $sender) return $ret; } -/** - * return likes, dislikes and attend status for item - * - * @param array $item array - * @param string $type Return type (atom, rss, xml, json) - * - * @return array - * likes => int count, - * dislikes => int count - * @throws BadRequestException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - */ -function api_format_items_activities($item, $type = "json") -{ - $activities = [ - 'like' => [], - 'dislike' => [], - 'attendyes' => [], - 'attendno' => [], - 'attendmaybe' => [], - 'announce' => [], - ]; - - $condition = ['uid' => $item['uid'], 'thr-parent' => $item['uri'], 'gravity' => GRAVITY_ACTIVITY]; - $ret = Post::selectForUser($item['uid'], ['author-id', 'verb'], $condition); - - while ($parent_item = Post::fetch($ret)) { - // not used as result should be structured like other user data - //builtin_activity_puller($i, $activities); - - // get user data and add it to the array of the activity - $user = DI::twitterUser()->createFromContactId($parent_item['author-id'], $item['uid'])->toArray(); - switch ($parent_item['verb']) { - case Activity::LIKE: - $activities['like'][] = $user; - break; - case Activity::DISLIKE: - $activities['dislike'][] = $user; - break; - case Activity::ATTEND: - $activities['attendyes'][] = $user; - break; - case Activity::ATTENDNO: - $activities['attendno'][] = $user; - break; - case Activity::ATTENDMAYBE: - $activities['attendmaybe'][] = $user; - break; - case Activity::ANNOUNCE: - $activities['announce'][] = $user; - break; - default: - break; - } - } - - DBA::close($ret); - - if ($type == "xml") { - $xml_activities = []; - foreach ($activities as $k => $v) { - // change xml element from "like" to "friendica:like" - $xml_activities["friendica:".$k] = $v; - // add user data into xml output - $k_user = 0; - foreach ($v as $user) { - $xml_activities["friendica:".$k][$k_user++.":user"] = $user; - } - } - $activities = $xml_activities; - } - - return $activities; -} - /** * * @param string $acl_string @@ -643,7 +565,7 @@ function prepare_photo_data($type, $scale, $photo_id, $uid) throw new NotFoundException('Photo-related item not found.'); } - $data['photo']['friendica_activities'] = api_format_items_activities($item, $type); + $data['photo']['friendica_activities'] = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], $type); // retrieve comments on photo $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)", diff --git a/src/DI.php b/src/DI.php index 1531988aa..b40222237 100644 --- a/src/DI.php +++ b/src/DI.php @@ -270,6 +270,14 @@ abstract class DI // "Factory" namespace instances // + /** + * @return Factory\Api\Friendica\Activities + */ + public static function friendicaActivities() + { + return self::$dice->create(Factory\Api\Friendica\Activities::class); + } + /** * @return Factory\Api\Mastodon\Account */ diff --git a/src/Factory/Api/Friendica/Activities.php b/src/Factory/Api/Friendica/Activities.php index c55649e9b..06d9cfd6d 100644 --- a/src/Factory/Api/Friendica/Activities.php +++ b/src/Factory/Api/Friendica/Activities.php @@ -50,7 +50,7 @@ class Activities extends BaseFactory * @return Array * @throws HTTPException\InternalServerErrorException */ - public function createFromUriId(int $uriId, int $uid): array + public function createFromUriId(int $uriId, int $uid, $type = 'json'): array { $activities = [ 'like' => [], @@ -94,6 +94,20 @@ class Activities extends BaseFactory DBA::close($ret); + if ($type == 'xml') { + $xml_activities = []; + foreach ($activities as $k => $v) { + // change xml element from "like" to "friendica:like" + $xml_activities["friendica:".$k] = $v; + // add user data into xml output + $k_user = 0; + foreach ($v as $user) { + $xml_activities['friendica:' . $k][$k_user++ . ':user'] = $user; + } + } + $activities = $xml_activities; + } + return $activities; } } diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index e8d1a928d..6657644f8 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -2162,8 +2162,8 @@ class ApiTest extends FixtureTest */ public function testApiFormatItemsActivities() { - $item = ['uid' => 0, 'uri' => '']; - $result = api_format_items_activities($item); + $item = ['uid' => 0, 'uri-id' => 1]; + $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid']); self::assertArrayHasKey('like', $result); self::assertArrayHasKey('dislike', $result); self::assertArrayHasKey('attendyes', $result); @@ -2178,8 +2178,8 @@ class ApiTest extends FixtureTest */ public function testApiFormatItemsActivitiesWithXml() { - $item = ['uid' => 0, 'uri' => '']; - $result = api_format_items_activities($item, 'xml'); + $item = ['uid' => 0, 'uri-id' => 1]; + $result = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], 'xml'); self::assertArrayHasKey('friendica:like', $result); self::assertArrayHasKey('friendica:dislike', $result); self::assertArrayHasKey('friendica:attendyes', $result);