1
1
Fork 0

Entities, Source and api functions rearranged

This commit is contained in:
Michael 2021-11-24 06:44:25 +00:00
parent 452cd57437
commit a3aab4a75a
5 changed files with 710 additions and 749 deletions

File diff suppressed because it is too large Load diff

View file

@ -74,7 +74,7 @@ class Status extends BaseFactory
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws ImagickException|HTTPException\NotFoundException * @throws ImagickException|HTTPException\NotFoundException
*/ */
public function createFromItemId(int $id): \Friendica\Object\Api\Twitter\Status public function createFromItemId(int $id, $include_entities = false): \Friendica\Object\Api\Twitter\Status
{ {
$fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network', $fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord']; 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord'];
@ -82,7 +82,7 @@ class Status extends BaseFactory
if (!$item) { if (!$item) {
throw new HTTPException\NotFoundException('Item with ID ' . $id . ' not found.'); throw new HTTPException\NotFoundException('Item with ID ' . $id . ' not found.');
} }
return $this->createFromArray($item); return $this->createFromArray($item, $include_entities);
} }
/** /**
@ -93,7 +93,7 @@ class Status extends BaseFactory
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws ImagickException|HTTPException\NotFoundException * @throws ImagickException|HTTPException\NotFoundException
*/ */
public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Twitter\Status public function createFromUriId(int $uriId, $uid = 0, $include_entities = false): \Friendica\Object\Api\Twitter\Status
{ {
$fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network', $fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord']; 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord'];
@ -101,7 +101,7 @@ class Status extends BaseFactory
if (!$item) { if (!$item) {
throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.')); throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
} }
return $this->createFromArray($item); return $this->createFromArray($item, $include_entities);
} }
/** /**
@ -112,13 +112,17 @@ class Status extends BaseFactory
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws ImagickException|HTTPException\NotFoundException * @throws ImagickException|HTTPException\NotFoundException
*/ */
private function createFromArray(array $item, $uid = 0): \Friendica\Object\Api\Twitter\Status private function createFromArray(array $item, $include_entities): \Friendica\Object\Api\Twitter\Status
{ {
$author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid']); $author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid']);
$owner = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid']); $owner = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid']);
$friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]); $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]);
if (!$include_entities) {
$item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
}
$text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API), 0)); $text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API), 0));
$geo = []; $geo = [];
@ -133,13 +137,16 @@ class Status extends BaseFactory
} }
} }
$hashtags = $this->hashtag->createFromUriId($item['uri-id'], $text); if ($include_entities) {
$medias = $this->media->createFromUriId($item['uri-id'], $text); $hashtags = $this->hashtag->createFromUriId($item['uri-id'], $text);
$urls = $this->url->createFromUriId($item['uri-id'], $text); $medias = $this->media->createFromUriId($item['uri-id'], $text);
$mentions = $this->mention->createFromUriId($item['uri-id'], $text); $urls = $this->url->createFromUriId($item['uri-id'], $text);
$mentions = $this->mention->createFromUriId($item['uri-id'], $text);
} else {
$attachments = $this->attachment->createFromUriId($item['uri-id'], $text);
}
$friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid); $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $item['uid']);
$attachments = $this->attachment->createFromUriId($item['uri-id'], $text);
$shared = BBCode::fetchShareAttributes($item['body']); $shared = BBCode::fetchShareAttributes($item['body']);
if (!empty($shared['guid'])) { if (!empty($shared['guid'])) {
@ -147,16 +154,19 @@ class Status extends BaseFactory
$shared_uri_id = $shared_item['uri-id'] ?? 0; $shared_uri_id = $shared_item['uri-id'] ?? 0;
$hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text)); if ($include_entities) {
$medias = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text)); $hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text));
$urls = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text)); $medias = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text));
$mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text)); $urls = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text));
$attachments = array_merge($attachments, $this->attachment->createFromUriId($item['uri-id'], $text)); $mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text));
} else {
$attachments = array_merge($attachments, $this->attachment->createFromUriId($item['uri-id'], $text));
}
} }
if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) { if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
$retweeted = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray(); $retweeted = $this->createFromUriId($item['thr-parent-id'], $item['uid'])->toArray();
$retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]); $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $item['uid']]]);
$item['title'] = $retweeted_item['title'] ?? $item['title']; $item['title'] = $retweeted_item['title'] ?? $item['title'];
$item['body'] = $retweeted_item['body'] ?? $item['body']; $item['body'] = $retweeted_item['body'] ?? $item['body'];
$author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid']); $author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid']);
@ -166,10 +176,12 @@ class Status extends BaseFactory
$quoted = []; // @todo $quoted = []; // @todo
$entities = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions]; if ($include_entities) {
$entities = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions];
// Attachments are currently deactivated for testing purposes $attachments = [];
$attachments = []; } else {
$entities = [];
}
return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments); return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments);
} }

View file

@ -186,7 +186,7 @@ class BaseApi extends BaseModule
* *
* @return array token * @return array token
*/ */
protected static function getCurrentApplication() public static function getCurrentApplication()
{ {
$token = OAuth::getCurrentApplicationToken(); $token = OAuth::getCurrentApplicationToken();

View file

@ -129,17 +129,19 @@ class Status extends BaseDataTransferObject
$this->external_url = $item['plink']; $this->external_url = $item['plink'];
$this->favorited = (bool)$item['starred']; $this->favorited = (bool)$item['starred'];
$this->friendica_comments = $friendica_comments; $this->friendica_comments = $friendica_comments;
$this->source = $item['app'] ?: 'web'; $this->source = $item['app'];
$this->geo = $geo; $this->geo = $geo;
$this->friendica_activities = $friendica_activities; $this->friendica_activities = $friendica_activities;
$this->attachments = $attachments; $this->attachments = $attachments;
$this->entities = $entities; $this->entities = $entities;
$this->extended_entities = $entities; $this->extended_entities = $entities;
if ($this->source == 'web') { $origin = ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']);
$this->source = ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']);
} elseif (ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']) != $this->source) { if (empty($this->source)) {
$this->source = trim($this->source. ' (' . ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']) . ')'); $this->source = $origin;
} elseif ($origin != $this->source) {
$this->source = trim($this->source. ' (' . $origin . ')');
} }
} }
@ -164,6 +166,19 @@ class Status extends BaseDataTransferObject
$status['geo'] = null; $status['geo'] = null;
} }
if (empty($status['entities'])) {
$status['entities'] = null;
}
if (empty($status['extended_entities'])) {
$status['extended_entities'] = null;
}
if (empty($status['attachments'])) {
$status['attachments'] = null;
}
return $status; return $status;
} }
} }

View file

@ -243,7 +243,7 @@ class ApiTest extends FixtureTest
*/ */
public function testApiSource() public function testApiSource()
{ {
self::assertEquals('api', api_source()); self::assertEquals('API', BaseApi::getCurrentApplication()['name']);
} }
/** /**
@ -254,7 +254,7 @@ class ApiTest extends FixtureTest
public function testApiSourceWithTwidere() public function testApiSourceWithTwidere()
{ {
$_SERVER['HTTP_USER_AGENT'] = 'Twidere'; $_SERVER['HTTP_USER_AGENT'] = 'Twidere';
self::assertEquals('Twidere', api_source()); self::assertEquals('Twidere', BaseApi::getCurrentApplication()['name']);
} }
/** /**
@ -265,7 +265,7 @@ class ApiTest extends FixtureTest
public function testApiSourceWithGet() public function testApiSourceWithGet()
{ {
$_GET['source'] = 'source_name'; $_GET['source'] = 'source_name';
self::assertEquals('source_name', api_source()); self::assertEquals('source_name', BaseApi::getCurrentApplication()['name']);
} }
/** /**